From 34cde218763a34fc98aea8cf255726663ddd33ca Mon Sep 17 00:00:00 2001 From: Jc2k Date: Tue, 13 Aug 2019 09:09:55 +0100 Subject: [PATCH] Fix for HomeKit controller state not updating after put (#25903) --- .../homekit_controller/connection.py | 30 +++++++++++++++++-- tests/components/homekit_controller/common.py | 1 + 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/homekit_controller/connection.py b/homeassistant/components/homekit_controller/connection.py index c4c00cb384b..5e703682f93 100644 --- a/homeassistant/components/homekit_controller/connection.py +++ b/homeassistant/components/homekit_controller/connection.py @@ -273,6 +273,12 @@ class HKDevice: # connection was dropped. return + self.process_new_events(new_values_dict) + + _LOGGER.debug("Finished HomeKit controller update") + + def process_new_events(self, new_values_dict): + """Process events from accessory into HA state.""" self.available = True for (aid, cid), value in new_values_dict.items(): @@ -281,8 +287,6 @@ class HKDevice: self.hass.helpers.dispatcher.async_dispatcher_send(self.signal_state_updated) - _LOGGER.debug("Finished HomeKit controller update") - async def get_characteristics(self, *args, **kwargs): """Read latest state from homekit accessory.""" async with self.pairing_lock: @@ -298,10 +302,30 @@ class HKDevice: chars.append((row["aid"], row["iid"], row["value"])) async with self.pairing_lock: - await self.hass.async_add_executor_job( + results = await self.hass.async_add_executor_job( self.pairing.put_characteristics, chars ) + # Feed characteristics back into HA and update the current state + # results will only contain failures, so anythin in characteristics + # but not in results was applied successfully - we can just have HA + # reflect the change immediately. + + new_entity_state = {} + for row in characteristics: + key = (row["aid"], row["iid"]) + + # If the key was returned by put_characteristics() then the + # change didnt work + if key in results: + continue + + # Otherwise it was accepted and we can apply the change to + # our state + new_entity_state[key] = {"value": row["value"]} + + self.process_new_events(new_entity_state) + @property def unique_id(self): """ diff --git a/tests/components/homekit_controller/common.py b/tests/components/homekit_controller/common.py index 526884f86d4..2e1cfd7a77d 100644 --- a/tests/components/homekit_controller/common.py +++ b/tests/components/homekit_controller/common.py @@ -74,6 +74,7 @@ class FakePairing: if char.iid != cid: continue char.set_value(new_val) + return {} class FakeController: