Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Creates button entities for the Husqvarna Automower integration."""
2 
3 from collections.abc import Awaitable, Callable
4 from dataclasses import dataclass
5 import logging
6 from typing import Any
7 
8 from aioautomower.model import MowerAttributes
9 from aioautomower.session import AutomowerSession
10 
11 from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from . import AutomowerConfigEntry
16 from .coordinator import AutomowerDataUpdateCoordinator
17 from .entity import (
18  AutomowerAvailableEntity,
19  _check_error_free,
20  handle_sending_exception,
21 )
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 PARALLEL_UPDATES = 1
26 
27 
28 @dataclass(frozen=True, kw_only=True)
30  """Describes Automower button entities."""
31 
32  available_fn: Callable[[MowerAttributes], bool] = lambda _: True
33  exists_fn: Callable[[MowerAttributes], bool] = lambda _: True
34  press_fn: Callable[[AutomowerSession, str], Awaitable[Any]]
35 
36 
37 MOWER_BUTTON_TYPES: tuple[AutomowerButtonEntityDescription, ...] = (
39  key="confirm_error",
40  translation_key="confirm_error",
41  available_fn=lambda data: data.mower.is_error_confirmable,
42  exists_fn=lambda data: data.capabilities.can_confirm_error,
43  press_fn=lambda session, mower_id: session.commands.error_confirm(mower_id),
44  ),
46  key="sync_clock",
47  translation_key="sync_clock",
48  available_fn=_check_error_free,
49  press_fn=lambda session, mower_id: session.commands.set_datetime(mower_id),
50  ),
51 )
52 
53 
55  hass: HomeAssistant,
56  entry: AutomowerConfigEntry,
57  async_add_entities: AddEntitiesCallback,
58 ) -> None:
59  """Set up button platform."""
60  coordinator = entry.runtime_data
62  AutomowerButtonEntity(mower_id, coordinator, description)
63  for mower_id in coordinator.data
64  for description in MOWER_BUTTON_TYPES
65  if description.exists_fn(coordinator.data[mower_id])
66  )
67 
68 
70  """Defining the AutomowerButtonEntity."""
71 
72  entity_description: AutomowerButtonEntityDescription
73 
74  def __init__(
75  self,
76  mower_id: str,
77  coordinator: AutomowerDataUpdateCoordinator,
78  description: AutomowerButtonEntityDescription,
79  ) -> None:
80  """Set up AutomowerButtonEntity."""
81  super().__init__(mower_id, coordinator)
82  self.entity_descriptionentity_description = description
83  self._attr_unique_id_attr_unique_id = f"{mower_id}_{description.key}"
84 
85  @property
86  def available(self) -> bool:
87  """Return the available attribute of the entity."""
88  return self.entity_descriptionentity_description.available_fn(self.mower_attributesmower_attributes)
89 
90  @handle_sending_exception()
91  async def async_press(self) -> None:
92  """Send a command to the mower."""
93  await self.entity_descriptionentity_description.press_fn(self.coordinator.api, self.mower_idmower_id)
None __init__(self, str mower_id, AutomowerDataUpdateCoordinator coordinator, AutomowerButtonEntityDescription description)
Definition: button.py:79
None async_setup_entry(HomeAssistant hass, AutomowerConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: button.py:58