From 9b8f0e1ee973e564e0d416227fe37eb6e0e06256 Mon Sep 17 00:00:00 2001 From: Michael <35783820+mib1185@users.noreply.github.com> Date: Thu, 4 Jan 2024 23:36:36 +0100 Subject: [PATCH] Fix switch states in AVM FRITZ!Box Tools (#107183) --- homeassistant/components/fritz/common.py | 1 + homeassistant/components/fritz/switch.py | 21 ++++++++++----------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/fritz/common.py b/homeassistant/components/fritz/common.py index 63f9f593ea8879..bad73d913206bf 100644 --- a/homeassistant/components/fritz/common.py +++ b/homeassistant/components/fritz/common.py @@ -1063,6 +1063,7 @@ class SwitchInfo(TypedDict): type: str callback_update: Callable callback_switch: Callable + init_state: bool class FritzBoxBaseEntity: diff --git a/homeassistant/components/fritz/switch.py b/homeassistant/components/fritz/switch.py index 026c0f3d6fbfe8..c3da6b5af0b8b8 100644 --- a/homeassistant/components/fritz/switch.py +++ b/homeassistant/components/fritz/switch.py @@ -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() ] @@ -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"] @@ -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)}" @@ -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__( @@ -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) @@ -553,7 +551,7 @@ 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__( @@ -561,7 +559,7 @@ def __init__( 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 @@ -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)