diff --git a/custom_components/tahoma/binary_sensor.py b/custom_components/tahoma/binary_sensor.py index 10c4454f7..a7c686947 100644 --- a/custom_components/tahoma/binary_sensor.py +++ b/custom_components/tahoma/binary_sensor.py @@ -60,6 +60,10 @@ def device_class(self): def update(self): """Update the state.""" + if self.should_wait(): + self.schedule_update_ha_state(True) + return + self.controller.get_states([self.tahoma_device]) if CORE_CONTACT_STATE in self.tahoma_device.active_states: diff --git a/custom_components/tahoma/climate.py b/custom_components/tahoma/climate.py index 34f81c925..28b435223 100644 --- a/custom_components/tahoma/climate.py +++ b/custom_components/tahoma/climate.py @@ -299,6 +299,10 @@ def update_temp(self, state=None): def update(self): """Update the state.""" + if self.should_wait(): + self.schedule_update_ha_state(True) + return + self.controller.get_states([self.tahoma_device]) self.update_temp(None) if self._widget == W_ST: diff --git a/custom_components/tahoma/cover.py b/custom_components/tahoma/cover.py index 0fc4040a3..231fb86ee 100644 --- a/custom_components/tahoma/cover.py +++ b/custom_components/tahoma/cover.py @@ -89,6 +89,10 @@ def __init__(self, tahoma_device, controller): def update(self): """Update method.""" + if self.should_wait(): + self.schedule_update_ha_state(True) + return + self.controller.get_states([self.tahoma_device]) # Set current position. diff --git a/custom_components/tahoma/light.py b/custom_components/tahoma/light.py index e37b7e975..032630379 100644 --- a/custom_components/tahoma/light.py +++ b/custom_components/tahoma/light.py @@ -130,10 +130,11 @@ def effect(self) -> str: return self._effect def update(self): - """Fetch new state data for this light. + """Fetch new state data for this light.""" - This is the only method that should fetch new data for Home Assistant. - """ + if self.should_wait(): + self.schedule_update_ha_state(True) + return self.controller.get_states([self.tahoma_device]) diff --git a/custom_components/tahoma/lock.py b/custom_components/tahoma/lock.py index a164c712c..989b91720 100644 --- a/custom_components/tahoma/lock.py +++ b/custom_components/tahoma/lock.py @@ -42,6 +42,10 @@ def __init__(self, tahoma_device, controller): def update(self): """Update method.""" + if self.should_wait(): + self.schedule_update_ha_state(True) + return + self.controller.get_states([self.tahoma_device]) self._battery_level = self.tahoma_device.active_states["core:BatteryState"] self._name = self.tahoma_device.active_states["core:NameState"] diff --git a/custom_components/tahoma/sensor.py b/custom_components/tahoma/sensor.py index 7e35b7e64..c1010f7d5 100644 --- a/custom_components/tahoma/sensor.py +++ b/custom_components/tahoma/sensor.py @@ -105,6 +105,10 @@ def device_class(self) -> Optional[str]: def update(self): """Update the state.""" + if self.should_wait(): + self.schedule_update_ha_state(True) + return + self.controller.get_states([self.tahoma_device]) if CORE_LUMINANCE_STATE in self.tahoma_device.active_states: diff --git a/custom_components/tahoma/switch.py b/custom_components/tahoma/switch.py index 6575cb10d..92dd143a1 100644 --- a/custom_components/tahoma/switch.py +++ b/custom_components/tahoma/switch.py @@ -37,6 +37,10 @@ def __init__(self, tahoma_device, controller): def update(self): """Update method.""" # Postpone the immediate state check for changes that take time. + if self.should_wait(): + self.schedule_update_ha_state(True) + return + if self._skip_update: self._skip_update = False return diff --git a/custom_components/tahoma/tahoma_device.py b/custom_components/tahoma/tahoma_device.py index 7471345f9..fed2d2d70 100644 --- a/custom_components/tahoma/tahoma_device.py +++ b/custom_components/tahoma/tahoma_device.py @@ -21,6 +21,7 @@ def __init__(self, tahoma_device, controller): self.tahoma_device = tahoma_device self._name = self.tahoma_device.label self.controller = controller + self._exec_queue = [] async def async_added_to_hass(self): """Entity created.""" @@ -113,6 +114,12 @@ def device_info(self): "sw_version": self.tahoma_device.type, } + def should_wait(self): + """Wait for actions to finish.""" + exec_queue = self.controller.get_current_executions() + self._exec_queue = [e for e in self._exec_queue if e in exec_queue] + return True if self._exec_queue else False + async def async_apply_action(self, cmd_name, *args): """Apply Action to Device in async context.""" await self.hass.async_add_executor_job(self.apply_action, cmd_name, *args) @@ -123,5 +130,5 @@ def apply_action(self, cmd_name, *args): action = Action(self.tahoma_device.url) action.add_command(cmd_name, *args) exec_id = self.controller.apply_actions("HomeAssistant", [action]) - while exec_id in self.controller.get_current_executions(): - continue + self._exec_queue.append(exec_id) + return exec_id