Skip to content

Commit

Permalink
Bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Mamontov committed Apr 9, 2022
1 parent 976354e commit 34dbe21
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 19 deletions.
4 changes: 2 additions & 2 deletions custom_components/miwifi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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()
Expand Down
30 changes: 22 additions & 8 deletions custom_components/miwifi/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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]
2 changes: 1 addition & 1 deletion custom_components/miwifi/manifest.json
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 2 additions & 0 deletions custom_components/miwifi/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 22 additions & 8 deletions custom_components/miwifi/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand All @@ -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]

0 comments on commit 34dbe21

Please sign in to comment.