Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Platform for eq3 switch entities."""
2 
3 from collections.abc import Awaitable, Callable
4 from dataclasses import dataclass
5 from typing import TYPE_CHECKING, Any
6 
7 from eq3btsmart import Thermostat
8 from eq3btsmart.models import Status
9 
10 from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from . import Eq3ConfigEntry
15 from .const import ENTITY_KEY_AWAY, ENTITY_KEY_BOOST, ENTITY_KEY_LOCK
16 from .entity import Eq3Entity
17 
18 
19 @dataclass(frozen=True, kw_only=True)
21  """Entity description for eq3 switch entities."""
22 
23  toggle_func: Callable[[Thermostat], Callable[[bool], Awaitable[None]]]
24  value_func: Callable[[Status], bool]
25 
26 
27 SWITCH_ENTITY_DESCRIPTIONS = [
29  key=ENTITY_KEY_LOCK,
30  translation_key=ENTITY_KEY_LOCK,
31  toggle_func=lambda thermostat: thermostat.async_set_locked,
32  value_func=lambda status: status.is_locked,
33  ),
35  key=ENTITY_KEY_BOOST,
36  translation_key=ENTITY_KEY_BOOST,
37  toggle_func=lambda thermostat: thermostat.async_set_boost,
38  value_func=lambda status: status.is_boost,
39  ),
41  key=ENTITY_KEY_AWAY,
42  translation_key=ENTITY_KEY_AWAY,
43  toggle_func=lambda thermostat: thermostat.async_set_away,
44  value_func=lambda status: status.is_away,
45  ),
46 ]
47 
48 
50  hass: HomeAssistant,
51  entry: Eq3ConfigEntry,
52  async_add_entities: AddEntitiesCallback,
53 ) -> None:
54  """Set up the entry."""
55 
57  Eq3SwitchEntity(entry, entity_description)
58  for entity_description in SWITCH_ENTITY_DESCRIPTIONS
59  )
60 
61 
63  """Base class for eq3 switch entities."""
64 
65  entity_description: Eq3SwitchEntityDescription
66 
67  def __init__(
68  self,
69  entry: Eq3ConfigEntry,
70  entity_description: Eq3SwitchEntityDescription,
71  ) -> None:
72  """Initialize the entity."""
73 
74  super().__init__(entry, entity_description.key)
75  self.entity_descriptionentity_description = entity_description
76 
77  async def async_turn_on(self, **kwargs: Any) -> None:
78  """Turn on the switch."""
79 
80  await self.entity_descriptionentity_description.toggle_func(self._thermostat_thermostat)(True)
81 
82  async def async_turn_off(self, **kwargs: Any) -> None:
83  """Turn off the switch."""
84 
85  await self.entity_descriptionentity_description.toggle_func(self._thermostat_thermostat)(False)
86 
87  @property
88  def is_on(self) -> bool:
89  """Return the state of the switch."""
90 
91  if TYPE_CHECKING:
92  assert self._thermostat_thermostat.status is not None
93 
94  return self.entity_descriptionentity_description.value_func(self._thermostat_thermostat.status)
None __init__(self, Eq3ConfigEntry entry, Eq3SwitchEntityDescription entity_description)
Definition: switch.py:71
None async_setup_entry(HomeAssistant hass, Eq3ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:53