Skip to content

Commit

Permalink
Fix switch states in AVM FRITZ!Box Tools (#107183)
Browse files Browse the repository at this point in the history
  • Loading branch information
mib1185 committed Jan 4, 2024
1 parent f5e7631 commit 9b8f0e1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
1 change: 1 addition & 0 deletions homeassistant/components/fritz/common.py
Expand Up @@ -1063,6 +1063,7 @@ class SwitchInfo(TypedDict):
type: str
callback_update: Callable
callback_switch: Callable
init_state: bool


class FritzBoxBaseEntity:
Expand Down
21 changes: 10 additions & 11 deletions homeassistant/components/fritz/switch.py
Expand Up @@ -166,9 +166,7 @@ async def _async_wifi_entities_list(

_LOGGER.debug("WiFi networks list: %s", networks)
return [
FritzBoxWifiSwitch(
avm_wrapper, device_friendly_name, index, data["switch_name"]
)
FritzBoxWifiSwitch(avm_wrapper, device_friendly_name, index, data)
for index, data in networks.items()
]

Expand Down Expand Up @@ -310,18 +308,16 @@ async def async_turn_off(self, **kwargs: Any) -> None:
await self._async_handle_turn_on_off(turn_on=False)


class FritzBoxBaseSwitch(FritzBoxBaseEntity):
class FritzBoxBaseSwitch(FritzBoxBaseEntity, SwitchEntity):
"""Fritz switch base class."""

_attr_is_on: bool | None = False

def __init__(
self,
avm_wrapper: AvmWrapper,
device_friendly_name: str,
switch_info: SwitchInfo,
) -> None:
"""Init Fritzbox port switch."""
"""Init Fritzbox base switch."""
super().__init__(avm_wrapper, device_friendly_name)

self._description = switch_info["description"]
Expand All @@ -330,6 +326,7 @@ def __init__(
self._type = switch_info["type"]
self._update = switch_info["callback_update"]
self._switch = switch_info["callback_switch"]
self._attr_is_on = switch_info["init_state"]

self._name = f"{self._friendly_name} {self._description}"
self._unique_id = f"{self._avm_wrapper.unique_id}-{slugify(self._description)}"
Expand Down Expand Up @@ -381,7 +378,7 @@ async def _async_handle_turn_on_off(self, turn_on: bool) -> None:
self._attr_is_on = turn_on


class FritzBoxPortSwitch(FritzBoxBaseSwitch, SwitchEntity):
class FritzBoxPortSwitch(FritzBoxBaseSwitch):
"""Defines a FRITZ!Box Tools PortForward switch."""

def __init__(
Expand Down Expand Up @@ -412,6 +409,7 @@ def __init__(
type=SWITCH_TYPE_PORTFORWARD,
callback_update=self._async_fetch_update,
callback_switch=self._async_switch_on_off_executor,
init_state=port_mapping["NewEnabled"],
)
super().__init__(avm_wrapper, device_friendly_name, switch_info)

Expand Down Expand Up @@ -553,15 +551,15 @@ async def _async_handle_turn_on_off(self, turn_on: bool) -> bool:
return True


class FritzBoxWifiSwitch(FritzBoxBaseSwitch, SwitchEntity):
class FritzBoxWifiSwitch(FritzBoxBaseSwitch):
"""Defines a FRITZ!Box Tools Wifi switch."""

def __init__(
self,
avm_wrapper: AvmWrapper,
device_friendly_name: str,
network_num: int,
network_name: str,
network_data: dict,
) -> None:
"""Init Fritz Wifi switch."""
self._avm_wrapper = avm_wrapper
Expand All @@ -571,12 +569,13 @@ def __init__(
self._network_num = network_num

switch_info = SwitchInfo(
description=f"Wi-Fi {network_name}",
description=f"Wi-Fi {network_data['switch_name']}",
friendly_name=device_friendly_name,
icon="mdi:wifi",
type=SWITCH_TYPE_WIFINETWORK,
callback_update=self._async_fetch_update,
callback_switch=self._async_switch_on_off_executor,
init_state=network_data["enabled"],
)
super().__init__(self._avm_wrapper, device_friendly_name, switch_info)

Expand Down

0 comments on commit 9b8f0e1

Please sign in to comment.