Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for TechnoVE switches."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable, Coroutine
6 from dataclasses import dataclass
7 from typing import Any
8 
9 from technove import Station as TechnoVEStation
10 
11 from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
12 from homeassistant.const import EntityCategory
13 from homeassistant.core import HomeAssistant
14 from homeassistant.exceptions import ServiceValidationError
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from . import TechnoVEConfigEntry
18 from .const import DOMAIN
19 from .coordinator import TechnoVEDataUpdateCoordinator
20 from .entity import TechnoVEEntity
21 from .helpers import technove_exception_handler
22 
23 
25  coordinator: TechnoVEDataUpdateCoordinator, enabled: bool
26 ) -> None:
27  if coordinator.data.info.auto_charge:
29  translation_domain=DOMAIN,
30  translation_key="set_charging_enabled_on_auto_charge",
31  )
32  await coordinator.technove.set_charging_enabled(enabled=enabled)
33  coordinator.data.info.is_session_active = enabled
34  coordinator.async_set_updated_data(coordinator.data)
35 
36 
37 async def _enable_charging(coordinator: TechnoVEDataUpdateCoordinator) -> None:
38  await _set_charging_enabled(coordinator, True)
39 
40 
41 async def _disable_charging(coordinator: TechnoVEDataUpdateCoordinator) -> None:
42  await _set_charging_enabled(coordinator, False)
43 
44 
45 async def _set_auto_charge(
46  coordinator: TechnoVEDataUpdateCoordinator, enabled: bool
47 ) -> None:
48  await coordinator.technove.set_auto_charge(enabled=enabled)
49 
50 
51 @dataclass(frozen=True, kw_only=True)
53  """Describes TechnoVE binary sensor entity."""
54 
55  is_on_fn: Callable[[TechnoVEStation], bool]
56  turn_on_fn: Callable[[TechnoVEDataUpdateCoordinator], Coroutine[Any, Any, None]]
57  turn_off_fn: Callable[[TechnoVEDataUpdateCoordinator], Coroutine[Any, Any, None]]
58 
59 
60 SWITCHES = [
62  key="auto_charge",
63  translation_key="auto_charge",
64  entity_category=EntityCategory.CONFIG,
65  is_on_fn=lambda station: station.info.auto_charge,
66  turn_on_fn=lambda coordinator: _set_auto_charge(coordinator, True),
67  turn_off_fn=lambda coordinator: _set_auto_charge(coordinator, False),
68  ),
70  key="session_active",
71  translation_key="session_active",
72  entity_category=EntityCategory.CONFIG,
73  is_on_fn=lambda station: station.info.is_session_active,
74  turn_on_fn=_enable_charging,
75  turn_off_fn=_disable_charging,
76  ),
77 ]
78 
79 
81  hass: HomeAssistant,
82  entry: TechnoVEConfigEntry,
83  async_add_entities: AddEntitiesCallback,
84 ) -> None:
85  """Set up TechnoVE switch based on a config entry."""
86 
88  TechnoVESwitchEntity(entry.runtime_data, description)
89  for description in SWITCHES
90  )
91 
92 
94  """Defines a TechnoVE switch entity."""
95 
96  entity_description: TechnoVESwitchDescription
97 
98  def __init__(
99  self,
100  coordinator: TechnoVEDataUpdateCoordinator,
101  description: TechnoVESwitchDescription,
102  ) -> None:
103  """Initialize a TechnoVE switch entity."""
104  self.entity_descriptionentity_description = description
105  super().__init__(coordinator, description.key)
106 
107  @property
108  def is_on(self) -> bool:
109  """Return the state of the TechnoVE switch."""
110 
111  return self.entity_descriptionentity_description.is_on_fn(self.coordinator.data)
112 
113  @technove_exception_handler
114  async def async_turn_on(self, **kwargs: Any) -> None:
115  """Turn on the TechnoVE switch."""
116  await self.entity_descriptionentity_description.turn_on_fn(self.coordinator)
117 
118  @technove_exception_handler
119  async def async_turn_off(self, **kwargs: Any) -> None:
120  """Turn off the TechnoVE switch."""
121  await self.entity_descriptionentity_description.turn_off_fn(self.coordinator)
None __init__(self, TechnoVEDataUpdateCoordinator coordinator, TechnoVESwitchDescription description)
Definition: switch.py:102
None _disable_charging(TechnoVEDataUpdateCoordinator coordinator)
Definition: switch.py:41
None _set_auto_charge(TechnoVEDataUpdateCoordinator coordinator, bool enabled)
Definition: switch.py:47
None _enable_charging(TechnoVEDataUpdateCoordinator coordinator)
Definition: switch.py:37
None async_setup_entry(HomeAssistant hass, TechnoVEConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:84
None _set_charging_enabled(TechnoVEDataUpdateCoordinator coordinator, bool enabled)
Definition: switch.py:26