Add entity attribute to Home Assistant state subscribtions (#30)

This commit is contained in:
Franck Nijhof 2021-05-12 10:57:01 +02:00 committed by GitHub
parent c7836302d0
commit fa6d8de974
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 87 additions and 62 deletions

View File

@ -561,6 +561,7 @@ message SubscribeHomeAssistantStateResponse {
option (id) = 39;
option (source) = SOURCE_SERVER;
string entity_id = 1;
string attribute = 2;
}
message HomeAssistantStateResponse {
@ -570,6 +571,7 @@ message HomeAssistantStateResponse {
string entity_id = 1;
string state = 2;
string attribute = 3;
}
// ==================== IMPORT TIME ====================

File diff suppressed because one or more lines are too long

View File

@ -199,26 +199,35 @@ class APIClient:
kwargs[key] = getattr(msg, key)
on_service_call(HomeassistantServiceCall(**kwargs))
await self._connection.send_message_callback_response(pb.SubscribeHomeassistantServicesRequest(),
on_msg)
await self._connection.send_message_callback_response(
pb.SubscribeHomeassistantServicesRequest(), on_msg
)
async def subscribe_home_assistant_states(self, on_state_sub: Callable[[str], None]) -> None:
async def subscribe_home_assistant_states(
self, on_state_sub: Callable[[str, Optional[str]], None]
) -> None:
self._check_authenticated()
def on_msg(msg):
if isinstance(msg, pb.SubscribeHomeAssistantStateResponse):
on_state_sub(msg.entity_id)
on_state_sub(msg.entity_id, msg.attribute)
await self._connection.send_message_callback_response(
pb.SubscribeHomeAssistantStatesRequest(), on_msg)
pb.SubscribeHomeAssistantStatesRequest(), on_msg
)
async def send_home_assistant_state(self, entity_id: str, state: str) -> None:
async def send_home_assistant_state(
self, entity_id: str, attribute: Optional[str], state: str
) -> None:
self._check_authenticated()
await self._connection.send_message(pb.HomeAssistantStateResponse(
entity_id=entity_id,
state=state,
))
await self._connection.send_message(
pb.HomeAssistantStateResponse(
entity_id=entity_id,
state=state,
attribute=attribute,
)
)
async def cover_command(self,
key: int,