Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Platform to control a Salda Smarty XP/XV ventilation unit."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 import logging
8 from typing import Any
9 
10 from pysmarty2 import Smarty
11 
12 from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from .coordinator import SmartyConfigEntry, SmartyCoordinator
17 from .entity import SmartyEntity
18 
19 _LOGGER = logging.getLogger(__name__)
20 
21 
22 @dataclass(frozen=True, kw_only=True)
24  """Class describing Smarty switch."""
25 
26  is_on_fn: Callable[[Smarty], bool]
27  turn_on_fn: Callable[[Smarty], bool | None]
28  turn_off_fn: Callable[[Smarty], bool | None]
29 
30 
31 ENTITIES: tuple[SmartySwitchDescription, ...] = (
33  key="boost",
34  translation_key="boost",
35  is_on_fn=lambda smarty: smarty.boost,
36  turn_on_fn=lambda smarty: smarty.enable_boost(),
37  turn_off_fn=lambda smarty: smarty.disable_boost(),
38  ),
39 )
40 
41 
43  hass: HomeAssistant,
44  entry: SmartyConfigEntry,
45  async_add_entities: AddEntitiesCallback,
46 ) -> None:
47  """Set up the Smarty Switch Platform."""
48 
49  coordinator = entry.runtime_data
50 
52  SmartySwitch(coordinator, description) for description in ENTITIES
53  )
54 
55 
57  """Representation of a Smarty Switch."""
58 
59  entity_description: SmartySwitchDescription
60 
61  def __init__(
62  self,
63  coordinator: SmartyCoordinator,
64  entity_description: SmartySwitchDescription,
65  ) -> None:
66  """Initialize the entity."""
67  super().__init__(coordinator)
68  self.entity_descriptionentity_description = entity_description
69  self._attr_unique_id_attr_unique_id = (
70  f"{coordinator.config_entry.entry_id}_{entity_description.key}"
71  )
72 
73  @property
74  def is_on(self) -> bool:
75  """Return the state of the switch."""
76  return self.entity_descriptionentity_description.is_on_fn(self.coordinator.client)
77 
78  async def async_turn_on(self, **kwargs: Any) -> None:
79  """Turn the switch on."""
80  await self.hasshasshass.async_add_executor_job(
81  self.entity_descriptionentity_description.turn_on_fn, self.coordinator.client
82  )
83  await self.coordinator.async_refresh()
84 
85  async def async_turn_off(self, **kwargs: Any) -> None:
86  """Turn the switch off."""
87  await self.hasshasshass.async_add_executor_job(
88  self.entity_descriptionentity_description.turn_off_fn, self.coordinator.client
89  )
90  await self.coordinator.async_refresh()
None __init__(self, SmartyCoordinator coordinator, SmartySwitchDescription entity_description)
Definition: switch.py:65
None async_setup_entry(HomeAssistant hass, SmartyConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:46