3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
9 from yolink.client_request
import ClientRequest
10 from yolink.const
import (
11 ATTR_DEVICE_MANIPULATOR,
12 ATTR_DEVICE_MULTI_OUTLET,
16 from yolink.device
import YoLinkDevice
17 from yolink.outlet_request_builder
import OutletRequestBuilder
22 SwitchEntityDescription,
28 from .const
import DEV_MODEL_MULTI_OUTLET_YS6801, DOMAIN
29 from .coordinator
import YoLinkCoordinator
30 from .entity
import YoLinkEntity
33 @dataclass(frozen=True)
35 """YoLink SwitchEntityDescription."""
37 exists_fn: Callable[[YoLinkDevice], bool] =
lambda _:
True
38 plug_index_fn: Callable[[YoLinkDevice], int |
None] =
lambda _:
None
41 DEVICE_TYPES: tuple[YoLinkSwitchEntityDescription, ...] = (
44 device_class=SwitchDeviceClass.OUTLET,
46 exists_fn=
lambda device: device.device_type == ATTR_DEVICE_OUTLET,
49 key=
"manipulator_state",
50 translation_key=
"manipulator_state",
52 exists_fn=
lambda device: device.device_type == ATTR_DEVICE_MANIPULATOR,
57 device_class=SwitchDeviceClass.SWITCH,
58 exists_fn=
lambda device: device.device_type == ATTR_DEVICE_SWITCH,
61 key=
"multi_outlet_usb_ports",
62 translation_key=
"usb_ports",
63 device_class=SwitchDeviceClass.OUTLET,
64 exists_fn=
lambda device: device.device_type == ATTR_DEVICE_MULTI_OUTLET
65 and device.device_model_name.startswith(DEV_MODEL_MULTI_OUTLET_YS6801),
66 plug_index_fn=
lambda _: 0,
69 key=
"multi_outlet_plug_1",
70 translation_key=
"plug_1",
71 device_class=SwitchDeviceClass.OUTLET,
72 exists_fn=
lambda device: device.device_type == ATTR_DEVICE_MULTI_OUTLET,
73 plug_index_fn=
lambda device: (
75 if device.device_model_name.startswith(DEV_MODEL_MULTI_OUTLET_YS6801)
80 key=
"multi_outlet_plug_2",
81 translation_key=
"plug_2",
82 device_class=SwitchDeviceClass.OUTLET,
83 exists_fn=
lambda device: device.device_type == ATTR_DEVICE_MULTI_OUTLET,
84 plug_index_fn=
lambda device: (
86 if device.device_model_name.startswith(DEV_MODEL_MULTI_OUTLET_YS6801)
91 key=
"multi_outlet_plug_3",
92 translation_key=
"plug_3",
93 device_class=SwitchDeviceClass.OUTLET,
94 exists_fn=
lambda device: device.device_type == ATTR_DEVICE_MULTI_OUTLET
95 and device.device_model_name.startswith(DEV_MODEL_MULTI_OUTLET_YS6801),
96 plug_index_fn=
lambda _: 3,
99 key=
"multi_outlet_plug_4",
100 translation_key=
"plug_4",
101 device_class=SwitchDeviceClass.OUTLET,
102 exists_fn=
lambda device: device.device_type == ATTR_DEVICE_MULTI_OUTLET
103 and device.device_model_name.startswith(DEV_MODEL_MULTI_OUTLET_YS6801),
104 plug_index_fn=
lambda _: 4,
109 ATTR_DEVICE_MANIPULATOR,
110 ATTR_DEVICE_MULTI_OUTLET,
118 config_entry: ConfigEntry,
119 async_add_entities: AddEntitiesCallback,
121 """Set up YoLink switch from a config entry."""
122 device_coordinators = hass.data[DOMAIN][config_entry.entry_id].device_coordinators
123 switch_device_coordinators = [
125 for device_coordinator
in device_coordinators.values()
126 if device_coordinator.device.device_type
in DEVICE_TYPE
130 for switch_device_coordinator
in switch_device_coordinators
131 for description
in DEVICE_TYPES
132 if description.exists_fn(switch_device_coordinator.device)
137 """YoLink Switch Entity."""
139 entity_description: YoLinkSwitchEntityDescription
143 config_entry: ConfigEntry,
144 coordinator: YoLinkCoordinator,
145 description: YoLinkSwitchEntityDescription,
147 """Init YoLink switch."""
148 super().
__init__(config_entry, coordinator)
151 f
"{coordinator.device.device_id} {self.entity_description.key}"
155 self, state_value: str | list[str] |
None, plug_index: int |
None
157 """Parse state value."""
158 if isinstance(state_value, list)
and plug_index
is not None:
159 return state_value[plug_index] ==
"open"
160 return state_value ==
"open" if state_value
is not None else None
164 """Update HA Entity State."""
172 """Call setState api to change switch state."""
173 client_request: ClientRequest =
None
174 if self.coordinator.device.device_type
in [
176 ATTR_DEVICE_MULTI_OUTLET,
178 client_request = OutletRequestBuilder.set_state_request(
179 state, self.
entity_descriptionentity_description.plug_index_fn(self.coordinator.device)
182 client_request = ClientRequest(
"setState", {
"state": state})
185 state, self.
entity_descriptionentity_description.plug_index_fn(self.coordinator.device)
190 """Turn the entity on."""
194 """Turn the entity off."""
None call_device(self, ClientRequest request)
bool|None _get_state(self, str|list[str]|None state_value, int|None plug_index)
None call_state_change(self, str state)
None update_entity_state(self, dict[str, str|list[str]] state)
None async_turn_off(self, **Any kwargs)
None __init__(self, ConfigEntry config_entry, YoLinkCoordinator coordinator, YoLinkSwitchEntityDescription description)
None async_turn_on(self, **Any kwargs)
None async_write_ha_state(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)