diff --git a/custom_components/miwifi/__init__.py b/custom_components/miwifi/__init__.py index 841eeb6..4d9d8ca 100644 --- a/custom_components/miwifi/__init__.py +++ b/custom_components/miwifi/__init__.py @@ -13,7 +13,7 @@ CONF_TIMEOUT, EVENT_HOMEASSISTANT_STOP, ) -from homeassistant.core import HomeAssistant, CALLBACK_TYPE +from homeassistant.core import HomeAssistant, Event, CALLBACK_TYPE from homeassistant.helpers.storage import Store from .const import ( @@ -91,7 +91,7 @@ async def async_start(with_sleep: bool = False) -> None: hass.config_entries.async_setup_platforms(entry, PLATFORMS) - async def async_stop() -> None: + async def async_stop(event: Event) -> None: """Async stop""" await _updater.async_stop() diff --git a/custom_components/miwifi/light.py b/custom_components/miwifi/light.py index 643a7e9..5ef7015 100644 --- a/custom_components/miwifi/light.py +++ b/custom_components/miwifi/light.py @@ -118,6 +118,8 @@ def __init__( self._attr_is_on = updater.data.get(description.key, False) + self._change_icon(self._attr_is_on) + async def async_added_to_hass(self) -> None: """When entity is added to hass.""" @@ -145,12 +147,7 @@ def _handle_coordinator_update(self) -> None: self._attr_available = is_available self._attr_is_on = is_on - # fmt: off - icon_name: str = f"{self.entity_description.key}_{STATE_ON if is_on else STATE_OFF}" - # fmt: on - - if icon_name in ICONS: - self._attr_icon = ICONS[icon_name] + self._change_icon(is_on) self.async_write_ha_state() @@ -203,5 +200,22 @@ async def _async_call(self, method: str, state: str, **kwargs: Any) -> None: if action: await action() - self._updater.data[self.entity_description.key] = state == STATE_ON - self._attr_is_on = state == STATE_ON + is_on: bool = state == STATE_ON + + self._updater.data[self.entity_description.key] = is_on + self._attr_is_on = is_on + self._change_icon(is_on) + + self.async_write_ha_state() + + def _change_icon(self, is_on: bool) -> None: + """Change icon + + :param is_on: bool + """ + + # fmt: off + icon_name: str = f"{self.entity_description.key}_{STATE_ON if is_on else STATE_OFF}" + # fmt: on + if icon_name in ICONS: + self._attr_icon = ICONS[icon_name] diff --git a/custom_components/miwifi/manifest.json b/custom_components/miwifi/manifest.json index 55cae71..838e620 100644 --- a/custom_components/miwifi/manifest.json +++ b/custom_components/miwifi/manifest.json @@ -1,7 +1,7 @@ { "domain": "miwifi", "name": "MiWiFi", - "version": "2.2.4", + "version": "2.2.5", "documentation": "https://github.com/dmamontov/hass-miwifi/blob/main/README.md", "issue_tracker": "https://github.com/dmamontov/hass-miwifi/issues", "config_flow": true, diff --git a/custom_components/miwifi/select.py b/custom_components/miwifi/select.py index d01d462..026b748 100644 --- a/custom_components/miwifi/select.py +++ b/custom_components/miwifi/select.py @@ -360,6 +360,8 @@ async def async_select_option(self, option: str) -> None: self._updater.data[self.entity_description.key] = option self._change_icon(option) + self.async_write_ha_state() + def _change_icon(self, option: str) -> None: """Change icon diff --git a/custom_components/miwifi/switch.py b/custom_components/miwifi/switch.py index 6689d49..8b0b1ea 100644 --- a/custom_components/miwifi/switch.py +++ b/custom_components/miwifi/switch.py @@ -152,6 +152,8 @@ def __init__( self._attr_is_on = updater.data.get(description.key, False) + self._change_icon(self._attr_is_on) + self._attr_available = self._additional_prepare() async def async_added_to_hass(self) -> None: @@ -181,12 +183,7 @@ def _handle_coordinator_update(self) -> None: self._attr_available = is_available self._attr_is_on = is_on - # fmt: off - icon_name: str = f"{self.entity_description.key}_{STATE_ON if is_on else STATE_OFF}" - # fmt: on - - if icon_name in ICONS: - self._attr_icon = ICONS[icon_name] + self._change_icon(is_on) self.async_write_ha_state() @@ -275,8 +272,13 @@ async def _async_call(self, method: str, state: str, **kwargs: Any) -> None: if action: await action() - self._updater.data[self.entity_description.key] = state == STATE_ON - self._attr_is_on = state == STATE_ON + is_on: bool = state == STATE_ON + + self._updater.data[self.entity_description.key] = is_on + self._attr_is_on = is_on + self._change_icon(is_on) + + self.async_write_ha_state() def _additional_prepare(self) -> bool: """Prepare wifi switch @@ -298,3 +300,15 @@ def _additional_prepare(self) -> bool: self._attr_name = ATTR_WIFI_NAME return is_available + + def _change_icon(self, is_on: bool) -> None: + """Change icon + + :param is_on: bool + """ + + # fmt: off + icon_name: str = f"{self.entity_description.key}_{STATE_ON if is_on else STATE_OFF}" + # fmt: on + if icon_name in ICONS: + self._attr_icon = ICONS[icon_name]