1 """Support for Lektrico switch entities."""
3 from collections.abc
import Callable, Coroutine
4 from dataclasses
import dataclass
7 from lektricowifi
import Device
14 from .
import LektricoConfigEntry, LektricoDeviceDataUpdateCoordinator
15 from .entity
import LektricoEntity
18 @dataclass(frozen=True, kw_only=True)
20 """Describes Lektrico switch entity."""
22 value_fn: Callable[[dict[str, Any]], bool]
23 set_value_fn: Callable[[Device, dict[Any, Any], bool], Coroutine[Any, Any, Any]]
26 SWITCHS_FOR_ALL_CHARGERS: tuple[LektricoSwitchEntityDescription, ...] = (
29 translation_key=
"authentication",
30 entity_category=EntityCategory.CONFIG,
31 value_fn=
lambda data:
bool(data[
"require_auth"]),
32 set_value_fn=
lambda device, data, value: device.set_auth(
not value),
36 translation_key=
"lock",
37 entity_category=EntityCategory.CONFIG,
38 value_fn=
lambda data:
str(data[
"charger_state"]) ==
"locked",
39 set_value_fn=
lambda device, data, value: device.set_charger_locked(value),
44 SWITCHS_FOR_3_PHASE_CHARGERS: tuple[LektricoSwitchEntityDescription, ...] = (
46 key=
"force_single_phase",
47 translation_key=
"force_single_phase",
48 entity_category=EntityCategory.CONFIG,
49 value_fn=
lambda data: data[
"relay_mode"] == 1,
50 set_value_fn=
lambda device, data, value: (
51 device.set_relay_mode(data[
"dynamic_current"], 1)
53 else device.set_relay_mode(data[
"dynamic_current"], 3)
61 entry: LektricoConfigEntry,
62 async_add_entities: AddEntitiesCallback,
64 """Set up Lektrico switch entities based on a config entry."""
65 coordinator = entry.runtime_data
67 switchs_to_be_used: tuple[LektricoSwitchEntityDescription, ...]
68 if coordinator.device_type == Device.TYPE_3P22K:
69 switchs_to_be_used = SWITCHS_FOR_ALL_CHARGERS + SWITCHS_FOR_3_PHASE_CHARGERS
71 switchs_to_be_used = SWITCHS_FOR_ALL_CHARGERS
77 f
"{entry.data[CONF_TYPE]}_{entry.data[ATTR_SERIAL_NUMBER]}",
79 for description
in switchs_to_be_used
84 """Defines a Lektrico switch entity."""
86 entity_description: LektricoSwitchEntityDescription
90 description: LektricoSwitchEntityDescription,
91 coordinator: LektricoDeviceDataUpdateCoordinator,
94 """Initialize Lektrico switch."""
95 super().
__init__(coordinator, device_name)
97 self.
_attr_unique_id_attr_unique_id = f
"{coordinator.serial_number}_{description.key}"
101 """Return the state of the switch."""
105 """Turn the switch on."""
107 self.coordinator.device, self.coordinator.data,
True
109 await self.coordinator.async_request_refresh()
112 """Turn the switch off."""
114 self.coordinator.device, self.coordinator.data,
False
116 await self.coordinator.async_request_refresh()
None async_turn_off(self, **Any kwargs)
None __init__(self, LektricoSwitchEntityDescription description, LektricoDeviceDataUpdateCoordinator coordinator, str device_name)
None async_turn_on(self, **Any kwargs)
None async_setup_entry(HomeAssistant hass, LektricoConfigEntry entry, AddEntitiesCallback async_add_entities)