mirror of
https://github.com/home-assistant/core
synced 2024-08-28 03:36:46 +02:00
Fix switch states in AVM FRITZ!Box Tools (#107183)
This commit is contained in:
parent
f5e7631e84
commit
9b8f0e1ee9
@ -1063,6 +1063,7 @@ class SwitchInfo(TypedDict):
|
|||||||
type: str
|
type: str
|
||||||
callback_update: Callable
|
callback_update: Callable
|
||||||
callback_switch: Callable
|
callback_switch: Callable
|
||||||
|
init_state: bool
|
||||||
|
|
||||||
|
|
||||||
class FritzBoxBaseEntity:
|
class FritzBoxBaseEntity:
|
||||||
|
@ -166,9 +166,7 @@ async def _async_wifi_entities_list(
|
|||||||
|
|
||||||
_LOGGER.debug("WiFi networks list: %s", networks)
|
_LOGGER.debug("WiFi networks list: %s", networks)
|
||||||
return [
|
return [
|
||||||
FritzBoxWifiSwitch(
|
FritzBoxWifiSwitch(avm_wrapper, device_friendly_name, index, data)
|
||||||
avm_wrapper, device_friendly_name, index, data["switch_name"]
|
|
||||||
)
|
|
||||||
for index, data in networks.items()
|
for index, data in networks.items()
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -310,18 +308,16 @@ class FritzBoxBaseCoordinatorSwitch(CoordinatorEntity[AvmWrapper], SwitchEntity)
|
|||||||
await self._async_handle_turn_on_off(turn_on=False)
|
await self._async_handle_turn_on_off(turn_on=False)
|
||||||
|
|
||||||
|
|
||||||
class FritzBoxBaseSwitch(FritzBoxBaseEntity):
|
class FritzBoxBaseSwitch(FritzBoxBaseEntity, SwitchEntity):
|
||||||
"""Fritz switch base class."""
|
"""Fritz switch base class."""
|
||||||
|
|
||||||
_attr_is_on: bool | None = False
|
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
avm_wrapper: AvmWrapper,
|
avm_wrapper: AvmWrapper,
|
||||||
device_friendly_name: str,
|
device_friendly_name: str,
|
||||||
switch_info: SwitchInfo,
|
switch_info: SwitchInfo,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Init Fritzbox port switch."""
|
"""Init Fritzbox base switch."""
|
||||||
super().__init__(avm_wrapper, device_friendly_name)
|
super().__init__(avm_wrapper, device_friendly_name)
|
||||||
|
|
||||||
self._description = switch_info["description"]
|
self._description = switch_info["description"]
|
||||||
@ -330,6 +326,7 @@ class FritzBoxBaseSwitch(FritzBoxBaseEntity):
|
|||||||
self._type = switch_info["type"]
|
self._type = switch_info["type"]
|
||||||
self._update = switch_info["callback_update"]
|
self._update = switch_info["callback_update"]
|
||||||
self._switch = switch_info["callback_switch"]
|
self._switch = switch_info["callback_switch"]
|
||||||
|
self._attr_is_on = switch_info["init_state"]
|
||||||
|
|
||||||
self._name = f"{self._friendly_name} {self._description}"
|
self._name = f"{self._friendly_name} {self._description}"
|
||||||
self._unique_id = f"{self._avm_wrapper.unique_id}-{slugify(self._description)}"
|
self._unique_id = f"{self._avm_wrapper.unique_id}-{slugify(self._description)}"
|
||||||
@ -381,7 +378,7 @@ class FritzBoxBaseSwitch(FritzBoxBaseEntity):
|
|||||||
self._attr_is_on = turn_on
|
self._attr_is_on = turn_on
|
||||||
|
|
||||||
|
|
||||||
class FritzBoxPortSwitch(FritzBoxBaseSwitch, SwitchEntity):
|
class FritzBoxPortSwitch(FritzBoxBaseSwitch):
|
||||||
"""Defines a FRITZ!Box Tools PortForward switch."""
|
"""Defines a FRITZ!Box Tools PortForward switch."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
@ -412,6 +409,7 @@ class FritzBoxPortSwitch(FritzBoxBaseSwitch, SwitchEntity):
|
|||||||
type=SWITCH_TYPE_PORTFORWARD,
|
type=SWITCH_TYPE_PORTFORWARD,
|
||||||
callback_update=self._async_fetch_update,
|
callback_update=self._async_fetch_update,
|
||||||
callback_switch=self._async_switch_on_off_executor,
|
callback_switch=self._async_switch_on_off_executor,
|
||||||
|
init_state=port_mapping["NewEnabled"],
|
||||||
)
|
)
|
||||||
super().__init__(avm_wrapper, device_friendly_name, switch_info)
|
super().__init__(avm_wrapper, device_friendly_name, switch_info)
|
||||||
|
|
||||||
@ -553,7 +551,7 @@ class FritzBoxProfileSwitch(FritzDeviceBase, SwitchEntity):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
class FritzBoxWifiSwitch(FritzBoxBaseSwitch, SwitchEntity):
|
class FritzBoxWifiSwitch(FritzBoxBaseSwitch):
|
||||||
"""Defines a FRITZ!Box Tools Wifi switch."""
|
"""Defines a FRITZ!Box Tools Wifi switch."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
@ -561,7 +559,7 @@ class FritzBoxWifiSwitch(FritzBoxBaseSwitch, SwitchEntity):
|
|||||||
avm_wrapper: AvmWrapper,
|
avm_wrapper: AvmWrapper,
|
||||||
device_friendly_name: str,
|
device_friendly_name: str,
|
||||||
network_num: int,
|
network_num: int,
|
||||||
network_name: str,
|
network_data: dict,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Init Fritz Wifi switch."""
|
"""Init Fritz Wifi switch."""
|
||||||
self._avm_wrapper = avm_wrapper
|
self._avm_wrapper = avm_wrapper
|
||||||
@ -571,12 +569,13 @@ class FritzBoxWifiSwitch(FritzBoxBaseSwitch, SwitchEntity):
|
|||||||
self._network_num = network_num
|
self._network_num = network_num
|
||||||
|
|
||||||
switch_info = SwitchInfo(
|
switch_info = SwitchInfo(
|
||||||
description=f"Wi-Fi {network_name}",
|
description=f"Wi-Fi {network_data['switch_name']}",
|
||||||
friendly_name=device_friendly_name,
|
friendly_name=device_friendly_name,
|
||||||
icon="mdi:wifi",
|
icon="mdi:wifi",
|
||||||
type=SWITCH_TYPE_WIFINETWORK,
|
type=SWITCH_TYPE_WIFINETWORK,
|
||||||
callback_update=self._async_fetch_update,
|
callback_update=self._async_fetch_update,
|
||||||
callback_switch=self._async_switch_on_off_executor,
|
callback_switch=self._async_switch_on_off_executor,
|
||||||
|
init_state=network_data["enabled"],
|
||||||
)
|
)
|
||||||
super().__init__(self._avm_wrapper, device_friendly_name, switch_info)
|
super().__init__(self._avm_wrapper, device_friendly_name, switch_info)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user