From 13538155ef2f8ee6e80b088226804037c8c03c56 Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Tue, 12 Nov 2024 22:17:38 +0200 Subject: [PATCH] feat: fire an event when operation mode changes * so that the entities shown are in sync --- custom_components/ouman_eh800/climate.py | 15 ++++++++++++++- custom_components/ouman_eh800/const.py | 2 ++ custom_components/ouman_eh800/select.py | 15 ++++++++++++++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/custom_components/ouman_eh800/climate.py b/custom_components/ouman_eh800/climate.py index 669e712..2b5f838 100644 --- a/custom_components/ouman_eh800/climate.py +++ b/custom_components/ouman_eh800/climate.py @@ -12,7 +12,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from . import OumanEH800Device -from .const import DOMAIN +from .const import DOMAIN, EVENT_CHANGE_L1_OPERATION_MODE from .eh800 import OPERATION_MODES _LOGGER = logging.getLogger(__name__) @@ -67,6 +67,18 @@ class OumanEH800DeviceClimate(ClimateEntity): self._attr_unique_id = f"ouman_eh800_{description.key}" self._attr_device_info = device.device_info + async def async_added_to_hass(self): + self.hass.bus.async_listen( + EVENT_CHANGE_L1_OPERATION_MODE, self.async_update_event_handler + ) + + async def async_update_event_handler( + self, + event, # pylint: disable=unused-argument + ): + await self.async_update() + self.async_write_ha_state() + @property def extra_state_attributes(self) -> dict: return self._device.device.data @@ -131,6 +143,7 @@ class OumanEH800DeviceClimate(ClimateEntity): self.entity_description.operation_mode_key, operation_mode.value, ) + self.hass.bus.async_fire(EVENT_CHANGE_L1_OPERATION_MODE) self.async_write_ha_state() async def async_update(self) -> None: diff --git a/custom_components/ouman_eh800/const.py b/custom_components/ouman_eh800/const.py index e1f0f04..858552f 100644 --- a/custom_components/ouman_eh800/const.py +++ b/custom_components/ouman_eh800/const.py @@ -8,3 +8,5 @@ CONF_HOST = "host" CONF_PORT = "port" CONF_USERNAME = "username" CONF_PASSWORD = "password" + +EVENT_CHANGE_L1_OPERATION_MODE = "ouman_eh800_change_l1_operation_mode" diff --git a/custom_components/ouman_eh800/select.py b/custom_components/ouman_eh800/select.py index 64d2ee1..43b963f 100644 --- a/custom_components/ouman_eh800/select.py +++ b/custom_components/ouman_eh800/select.py @@ -9,7 +9,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from . import OumanEH800Device -from .const import DOMAIN +from .const import DOMAIN, EVENT_CHANGE_L1_OPERATION_MODE from .eh800 import OPERATION_MODES _LOGGER = logging.getLogger(__name__) @@ -48,6 +48,18 @@ class OumanEH800DeviceSelect(SelectEntity): self._attr_unique_id = f"ouman_eh800_{description.key}" self._attr_device_info = device.device_info + async def async_added_to_hass(self): + self.hass.bus.async_listen( + EVENT_CHANGE_L1_OPERATION_MODE, self.async_update_event_handler + ) + + async def async_update_event_handler( + self, + event, # pylint: disable=unused-argument + ): + await self.async_update() + self.async_write_ha_state() + @property def current_option(self) -> str: operation_mode = int( @@ -70,6 +82,7 @@ class OumanEH800DeviceSelect(SelectEntity): self.entity_description.key, operation_mode.value, ) + self.hass.bus.async_fire(EVENT_CHANGE_L1_OPERATION_MODE) self.async_write_ha_state() async def async_update(self) -> None: -- 2.44.1