Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Support for deCONZ buttons."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 
7 from pydeconz.models.event import EventType
8 from pydeconz.models.scene import Scene as PydeconzScene
9 from pydeconz.models.sensor.presence import Presence
10 
12  DOMAIN as BUTTON_DOMAIN,
13  ButtonDeviceClass,
14  ButtonEntity,
15  ButtonEntityDescription,
16 )
17 from homeassistant.config_entries import ConfigEntry
18 from homeassistant.const import EntityCategory
19 from homeassistant.core import HomeAssistant, callback
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 
22 from .entity import DeconzDevice, DeconzSceneMixin
23 from .hub import DeconzHub
24 
25 
26 @dataclass(frozen=True, kw_only=True)
28  """Class describing deCONZ button entities."""
29 
30  button_fn: str
31  suffix: str
32 
33 
34 ENTITY_DESCRIPTIONS = {
35  PydeconzScene: [
37  key="store",
38  button_fn="store",
39  suffix="Store Current Scene",
40  icon="mdi:inbox-arrow-down",
41  entity_category=EntityCategory.CONFIG,
42  )
43  ]
44 }
45 
46 
48  hass: HomeAssistant,
49  config_entry: ConfigEntry,
50  async_add_entities: AddEntitiesCallback,
51 ) -> None:
52  """Set up the deCONZ button entity."""
53  hub = DeconzHub.get_hub(hass, config_entry)
54  hub.entities[BUTTON_DOMAIN] = set()
55 
56  @callback
57  def async_add_scene(_: EventType, scene_id: str) -> None:
58  """Add scene button from deCONZ."""
59  scene = hub.api.scenes[scene_id]
61  DeconzSceneButton(scene, hub, description)
62  for description in ENTITY_DESCRIPTIONS.get(PydeconzScene, [])
63  )
64 
65  hub.register_platform_add_device_callback(
66  async_add_scene,
67  hub.api.scenes,
68  )
69 
70  @callback
71  def async_add_presence_sensor(_: EventType, sensor_id: str) -> None:
72  """Add presence sensor reset button from deCONZ."""
73  sensor = hub.api.sensors.presence[sensor_id]
74  if sensor.presence_event is not None:
76 
77  hub.register_platform_add_device_callback(
78  async_add_presence_sensor,
79  hub.api.sensors.presence,
80  )
81 
82 
84  """Representation of a deCONZ button entity."""
85 
86  TYPE = BUTTON_DOMAIN
87 
88  def __init__(
89  self,
90  device: PydeconzScene,
91  hub: DeconzHub,
92  description: DeconzButtonDescription,
93  ) -> None:
94  """Initialize deCONZ number entity."""
95  self.entity_description: DeconzButtonDescription = description
96  super().__init__(device, hub)
97 
98  self._attr_name_attr_name_attr_name = f"{self._attr_name} {description.suffix}"
99 
100  async def async_press(self) -> None:
101  """Store light states into scene."""
102  async_button_fn = getattr(
103  self.hub.api.scenes,
104  self.entity_description.button_fn,
105  )
106  await async_button_fn(self._device.group_id, self._device.id)
107 
108  def get_device_identifier(self) -> str:
109  """Return a unique identifier for this scene."""
110  return f"{super().get_device_identifier()}-{self.entity_description.key}"
111 
112 
113 class DeconzPresenceResetButton(DeconzDevice[Presence], ButtonEntity):
114  """Representation of a deCONZ presence reset button entity."""
115 
116  _name_suffix = "Reset Presence"
117  unique_id_suffix = "reset_presence"
118 
119  _attr_entity_category = EntityCategory.CONFIG
120  _attr_device_class = ButtonDeviceClass.RESTART
121 
122  TYPE = BUTTON_DOMAIN
123 
124  async def async_press(self) -> None:
125  """Store reset presence state."""
126  await self.hub.api.sensors.presence.set_config(
127  id=self._device.resource_id,
128  reset_presence=True,
129  )
None __init__(self, PydeconzScene device, DeconzHub hub, DeconzButtonDescription description)
Definition: button.py:93
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: button.py:51