Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Lektrico switch entities."""
2 
3 from collections.abc import Callable, Coroutine
4 from dataclasses import dataclass
5 from typing import Any
6 
7 from lektricowifi import Device
8 
9 from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
10 from homeassistant.const import ATTR_SERIAL_NUMBER, CONF_TYPE, EntityCategory
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from . import LektricoConfigEntry, LektricoDeviceDataUpdateCoordinator
15 from .entity import LektricoEntity
16 
17 
18 @dataclass(frozen=True, kw_only=True)
20  """Describes Lektrico switch entity."""
21 
22  value_fn: Callable[[dict[str, Any]], bool]
23  set_value_fn: Callable[[Device, dict[Any, Any], bool], Coroutine[Any, Any, Any]]
24 
25 
26 SWITCHS_FOR_ALL_CHARGERS: tuple[LektricoSwitchEntityDescription, ...] = (
28  key="authentication",
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),
33  ),
35  key="lock",
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),
40  ),
41 )
42 
43 
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)
52  if value
53  else device.set_relay_mode(data["dynamic_current"], 3)
54  ),
55  ),
56 )
57 
58 
60  hass: HomeAssistant,
61  entry: LektricoConfigEntry,
62  async_add_entities: AddEntitiesCallback,
63 ) -> None:
64  """Set up Lektrico switch entities based on a config entry."""
65  coordinator = entry.runtime_data
66 
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
70  else:
71  switchs_to_be_used = SWITCHS_FOR_ALL_CHARGERS
72 
75  description,
76  coordinator,
77  f"{entry.data[CONF_TYPE]}_{entry.data[ATTR_SERIAL_NUMBER]}",
78  )
79  for description in switchs_to_be_used
80  )
81 
82 
84  """Defines a Lektrico switch entity."""
85 
86  entity_description: LektricoSwitchEntityDescription
87 
88  def __init__(
89  self,
90  description: LektricoSwitchEntityDescription,
91  coordinator: LektricoDeviceDataUpdateCoordinator,
92  device_name: str,
93  ) -> None:
94  """Initialize Lektrico switch."""
95  super().__init__(coordinator, device_name)
96  self.entity_descriptionentity_description = description
97  self._attr_unique_id_attr_unique_id = f"{coordinator.serial_number}_{description.key}"
98 
99  @property
100  def is_on(self) -> bool:
101  """Return the state of the switch."""
102  return self.entity_descriptionentity_description.value_fn(self.coordinator.data)
103 
104  async def async_turn_on(self, **kwargs: Any) -> None:
105  """Turn the switch on."""
106  await self.entity_descriptionentity_description.set_value_fn(
107  self.coordinator.device, self.coordinator.data, True
108  )
109  await self.coordinator.async_request_refresh()
110 
111  async def async_turn_off(self, **kwargs: Any) -> None:
112  """Turn the switch off."""
113  await self.entity_descriptionentity_description.set_value_fn(
114  self.coordinator.device, self.coordinator.data, False
115  )
116  await self.coordinator.async_request_refresh()
None __init__(self, LektricoSwitchEntityDescription description, LektricoDeviceDataUpdateCoordinator coordinator, str device_name)
Definition: switch.py:93
None async_setup_entry(HomeAssistant hass, LektricoConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:63