Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Support for button entities."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass, field
6 
7 from gardena_bluetooth.const import Reset
8 from gardena_bluetooth.parse import CharacteristicBool
9 
10 from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
11 from homeassistant.const import EntityCategory
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from . import GardenaBluetoothConfigEntry
16 from .entity import GardenaBluetoothDescriptorEntity
17 
18 
19 @dataclass(frozen=True)
21  """Description of entity."""
22 
23  char: CharacteristicBool = field(default_factory=lambda: CharacteristicBool(""))
24 
25  @property
26  def context(self) -> set[str]:
27  """Context needed for update coordinator."""
28  return {self.char.uuid}
29 
30 
31 DESCRIPTIONS = (
33  key=Reset.factory_reset.uuid,
34  translation_key="factory_reset",
35  entity_category=EntityCategory.DIAGNOSTIC,
36  entity_registry_enabled_default=False,
37  char=Reset.factory_reset,
38  ),
39 )
40 
41 
43  hass: HomeAssistant,
44  entry: GardenaBluetoothConfigEntry,
45  async_add_entities: AddEntitiesCallback,
46 ) -> None:
47  """Set up button based on a config entry."""
48  coordinator = entry.runtime_data
49  entities = [
50  GardenaBluetoothButton(coordinator, description, description.context)
51  for description in DESCRIPTIONS
52  if description.key in coordinator.characteristics
53  ]
54  async_add_entities(entities)
55 
56 
58  """Representation of a button."""
59 
60  entity_description: GardenaBluetoothButtonEntityDescription
61 
62  async def async_press(self) -> None:
63  """Trigger button action."""
64  await self.coordinator.write(self.entity_descriptionentity_description.char, True)
None write(self, Characteristic[CharacteristicType] char, CharacteristicType value)
Definition: coordinator.py:88
None async_setup_entry(HomeAssistant hass, GardenaBluetoothConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: button.py:46