Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Button entities for Acaia scales."""
2 
3 from collections.abc import Callable, Coroutine
4 from dataclasses import dataclass
5 from typing import Any
6 
7 from aioacaia.acaiascale import AcaiaScale
8 
9 from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 
13 from .coordinator import AcaiaConfigEntry
14 from .entity import AcaiaEntity
15 
16 PARALLEL_UPDATES = 0
17 
18 
19 @dataclass(kw_only=True, frozen=True)
21  """Description for acaia button entities."""
22 
23  press_fn: Callable[[AcaiaScale], Coroutine[Any, Any, None]]
24 
25 
26 BUTTONS: tuple[AcaiaButtonEntityDescription, ...] = (
28  key="tare",
29  translation_key="tare",
30  press_fn=lambda scale: scale.tare(),
31  ),
33  key="reset_timer",
34  translation_key="reset_timer",
35  press_fn=lambda scale: scale.reset_timer(),
36  ),
38  key="start_stop",
39  translation_key="start_stop",
40  press_fn=lambda scale: scale.start_stop_timer(),
41  ),
42 )
43 
44 
46  hass: HomeAssistant,
47  entry: AcaiaConfigEntry,
48  async_add_entities: AddEntitiesCallback,
49 ) -> None:
50  """Set up button entities and services."""
51 
52  coordinator = entry.runtime_data
53  async_add_entities(AcaiaButton(coordinator, description) for description in BUTTONS)
54 
55 
57  """Representation of an Acaia button."""
58 
59  entity_description: AcaiaButtonEntityDescription
60 
61  async def async_press(self) -> None:
62  """Handle the button press."""
63  await self.entity_descriptionentity_description.press_fn(self._scale_scale_scale)
None async_setup_entry(HomeAssistant hass, AcaiaConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: button.py:49