1 """Platform for switch integration."""
3 from __future__
import annotations
5 from collections.abc
import Awaitable, Callable
6 from dataclasses
import dataclass
9 from devolo_plc_api.device
import Device
10 from devolo_plc_api.device_api
import WifiGuestAccessGet
11 from devolo_plc_api.exceptions.device
import DevicePasswordProtected, DeviceUnavailable
19 from .
import DevoloHomeNetworkConfigEntry
20 from .const
import DOMAIN, SWITCH_GUEST_WIFI, SWITCH_LEDS
21 from .coordinator
import DevoloDataUpdateCoordinator
22 from .entity
import DevoloCoordinatorEntity
26 type _DataType = WifiGuestAccessGet | bool
29 @dataclass(frozen=True, kw_only=True)
31 """Describes devolo switch entity."""
33 is_on_func: Callable[[_DataT], bool]
34 turn_on_func: Callable[[Device], Awaitable[bool]]
35 turn_off_func: Callable[[Device], Awaitable[bool]]
38 SWITCH_TYPES: dict[str, DevoloSwitchEntityDescription[Any]] = {
39 SWITCH_GUEST_WIFI: DevoloSwitchEntityDescription[WifiGuestAccessGet](
40 key=SWITCH_GUEST_WIFI,
41 is_on_func=
lambda data: data.enabled
is True,
42 turn_on_func=
lambda device: device.device.async_set_wifi_guest_access(
True),
43 turn_off_func=
lambda device: device.device.async_set_wifi_guest_access(
False),
45 SWITCH_LEDS: DevoloSwitchEntityDescription[bool](
47 entity_category=EntityCategory.CONFIG,
49 turn_on_func=
lambda device: device.device.async_set_led_setting(
True),
50 turn_off_func=
lambda device: device.device.async_set_led_setting(
False),
57 entry: DevoloHomeNetworkConfigEntry,
58 async_add_entities: AddEntitiesCallback,
60 """Get all devices and sensors and setup them via config entry."""
61 device = entry.runtime_data.device
62 coordinators = entry.runtime_data.coordinators
64 entities: list[DevoloSwitchEntity[Any]] = []
65 if device.device
and "led" in device.device.features:
69 coordinators[SWITCH_LEDS],
70 SWITCH_TYPES[SWITCH_LEDS],
73 if device.device
and "wifi1" in device.device.features:
77 coordinators[SWITCH_GUEST_WIFI],
78 SWITCH_TYPES[SWITCH_GUEST_WIFI],
85 DevoloCoordinatorEntity[_DataT], SwitchEntity
87 """Representation of a devolo switch."""
89 entity_description: DevoloSwitchEntityDescription[_DataT]
93 entry: DevoloHomeNetworkConfigEntry,
94 coordinator: DevoloDataUpdateCoordinator[_DataT],
95 description: DevoloSwitchEntityDescription[_DataT],
97 """Initialize entity."""
98 self.entity_description = description
103 """State of the switch."""
104 return self.entity_description.
is_on_func(self.coordinator.data)
107 """Turn the entity on."""
110 except DevicePasswordProtected
as ex:
111 self.entry.async_start_reauth(self.hass)
113 translation_domain=DOMAIN,
114 translation_key=
"password_protected",
115 translation_placeholders={
"title": self.entry.title},
117 except DeviceUnavailable:
119 await self.coordinator.async_request_refresh()
122 """Turn the entity off."""
125 except DevicePasswordProtected
as ex:
126 self.entry.async_start_reauth(self.hass)
128 translation_domain=DOMAIN,
129 translation_key=
"password_protected",
130 translation_placeholders={
"title": self.entry.title},
132 except DeviceUnavailable:
134 await self.coordinator.async_request_refresh()
None async_setup_entry(HomeAssistant hass, DevoloHomeNetworkConfigEntry entry, AddEntitiesCallback async_add_entities)
None async_turn_off(self, **Any kwargs)
None async_turn_on(self, **Any kwargs)
None __init__(self, DevoloHomeNetworkConfigEntry entry, DevoloDataUpdateCoordinator[_DataT] coordinator, DevoloSwitchEntityDescription[_DataT] description)