1 """Switches for AVM Fritz!Box buttons."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
8 from typing
import Any, Final
13 ButtonEntityDescription,
22 from .const
import BUTTON_TYPE_WOL, CONNECTION_TYPE_LAN, DATA_FRITZ, DOMAIN, MeshRoles
23 from .coordinator
import AvmWrapper, FritzData, FritzDevice, _is_tracked
24 from .entity
import FritzDeviceBase
26 _LOGGER = logging.getLogger(__name__)
29 @dataclass(frozen=True, kw_only=True)
31 """Class to describe a Button entity."""
33 press_action: Callable[[AvmWrapper], Any]
38 key=
"firmware_update",
39 translation_key=
"firmware_update",
40 device_class=ButtonDeviceClass.UPDATE,
41 entity_category=EntityCategory.CONFIG,
42 press_action=
lambda avm_wrapper: avm_wrapper.async_trigger_firmware_update(),
46 device_class=ButtonDeviceClass.RESTART,
47 entity_category=EntityCategory.CONFIG,
48 press_action=
lambda avm_wrapper: avm_wrapper.async_trigger_reboot(),
52 translation_key=
"reconnect",
53 device_class=ButtonDeviceClass.RESTART,
54 entity_category=EntityCategory.CONFIG,
55 press_action=
lambda avm_wrapper: avm_wrapper.async_trigger_reconnect(),
59 translation_key=
"cleanup",
60 entity_category=EntityCategory.CONFIG,
61 press_action=
lambda avm_wrapper: avm_wrapper.async_trigger_cleanup(),
69 async_add_entities: AddEntitiesCallback,
71 """Set buttons for device."""
72 _LOGGER.debug(
"Setting up buttons")
73 avm_wrapper: AvmWrapper = hass.data[DOMAIN][entry.entry_id]
75 entities_list: list[ButtonEntity] = [
76 FritzButton(avm_wrapper, entry.title, button)
for button
in BUTTONS
79 if avm_wrapper.mesh_role == MeshRoles.SLAVE:
83 data_fritz: FritzData = hass.data[DATA_FRITZ]
89 def async_update_avm_device() -> None:
90 """Update the values of the AVM device."""
93 entry.async_on_unload(
95 hass, avm_wrapper.signal_device_new, async_update_avm_device
101 """Defines a Fritz!Box base button."""
103 entity_description: FritzButtonDescription
104 _attr_has_entity_name =
True
108 avm_wrapper: AvmWrapper,
109 device_friendly_name: str,
110 description: FritzButtonDescription,
112 """Initialize Fritz!Box button."""
116 self.
_attr_unique_id_attr_unique_id = f
"{self.avm_wrapper.unique_id}-{description.key}"
119 connections={(CONNECTION_NETWORK_MAC, avm_wrapper.mac)},
120 name=device_friendly_name,
124 """Triggers Fritz!Box service."""
130 avm_wrapper: AvmWrapper,
131 data_fritz: FritzData,
132 ) -> list[FritzBoxWOLButton]:
133 """Add new WOL button entities from the AVM device."""
134 _LOGGER.debug(
"Setting up %s buttons", BUTTON_TYPE_WOL)
136 new_wols: list[FritzBoxWOLButton] = []
138 if avm_wrapper.unique_id
not in data_fritz.wol_buttons:
139 data_fritz.wol_buttons[avm_wrapper.unique_id] = set()
141 for mac, device
in avm_wrapper.devices.items():
142 if _is_tracked(mac, data_fritz.wol_buttons.values()):
143 _LOGGER.debug(
"Skipping wol button creation for device %s", device.hostname)
146 if device.connection_type != CONNECTION_TYPE_LAN:
148 "Skipping wol button creation for device %s, not connected via LAN",
154 data_fritz.wol_buttons[avm_wrapper.unique_id].
add(mac)
156 _LOGGER.debug(
"Creating %s wol buttons", len(new_wols))
161 """Defines a FRITZ!Box Tools Wake On LAN button."""
163 _attr_icon =
"mdi:lan-pending"
164 _attr_entity_registry_enabled_default =
False
166 def __init__(self, avm_wrapper: AvmWrapper, device: FritzDevice) ->
None:
167 """Initialize Fritz!Box WOL button."""
168 super().
__init__(avm_wrapper, device)
169 self.
_name_name = f
"{self.hostname} Wake on LAN"
173 connections={(CONNECTION_NETWORK_MAC, self._mac)},
174 default_manufacturer=
"AVM",
175 default_model=
"FRITZ!Box Tracked device",
176 default_name=device.hostname,
179 avm_wrapper.unique_id,
184 """Press the button."""
dict[str, Any] async_wake_on_lan(self, str mac_address)
bool add(self, _T matcher)
bool _is_tracked(str mac, ValuesView current_devices)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)