Home Assistant Unofficial Reference 2024.12.1
button.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.button import ButtonEntity, ButtonEntityDescription
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 button."""
25 
26  press_fn: Callable[[Smarty], bool | None]
27 
28 
29 ENTITIES: tuple[SmartyButtonDescription, ...] = (
31  key="reset_filters_timer",
32  translation_key="reset_filters_timer",
33  press_fn=lambda smarty: smarty.reset_filters_timer(),
34  ),
35 )
36 
37 
39  hass: HomeAssistant,
40  entry: SmartyConfigEntry,
41  async_add_entities: AddEntitiesCallback,
42 ) -> None:
43  """Set up the Smarty Button Platform."""
44 
45  coordinator = entry.runtime_data
46 
48  SmartyButton(coordinator, description) for description in ENTITIES
49  )
50 
51 
53  """Representation of a Smarty Button."""
54 
55  entity_description: SmartyButtonDescription
56 
57  def __init__(
58  self,
59  coordinator: SmartyCoordinator,
60  entity_description: SmartyButtonDescription,
61  ) -> None:
62  """Initialize the entity."""
63  super().__init__(coordinator)
64  self.entity_descriptionentity_description = entity_description
65  self._attr_unique_id_attr_unique_id = (
66  f"{coordinator.config_entry.entry_id}_{entity_description.key}"
67  )
68 
69  async def async_press(self, **kwargs: Any) -> None:
70  """Press the button."""
71  await self.hasshasshass.async_add_executor_job(
72  self.entity_descriptionentity_description.press_fn, self.coordinator.client
73  )
74  await self.coordinator.async_refresh()
None __init__(self, SmartyCoordinator coordinator, SmartyButtonDescription entity_description)
Definition: button.py:61
None async_setup_entry(HomeAssistant hass, SmartyConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: button.py:42