Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Support for bond buttons."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 
7 from bond_async import Action
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 . import BondConfigEntry
14 from .entity import BondEntity
15 from .models import BondData
16 from .utils import BondDevice
17 
18 # The api requires a step size even though it does not
19 # seem to matter what is is as the underlying device is likely
20 # getting an increase/decrease signal only
21 STEP_SIZE = 10
22 
23 
24 @dataclass(frozen=True, kw_only=True)
26  """Class to describe a Bond Button entity."""
27 
28  # BondEntity does not support UNDEFINED,
29  # restrict the type to str | None
30  name: str | None = None
31  mutually_exclusive: Action | None
32  argument: int | None
33 
34 
36  key=Action.STOP,
37  name="Stop Actions",
38  translation_key="stop_actions",
39  mutually_exclusive=None,
40  argument=None,
41 )
42 
43 
44 BUTTONS: tuple[BondButtonEntityDescription, ...] = (
46  key=Action.TOGGLE_POWER,
47  name="Toggle Power",
48  translation_key="toggle_power",
49  mutually_exclusive=Action.TURN_ON,
50  argument=None,
51  ),
53  key=Action.TOGGLE_LIGHT,
54  name="Toggle Light",
55  translation_key="toggle_light",
56  mutually_exclusive=Action.TURN_LIGHT_ON,
57  argument=None,
58  ),
60  key=Action.INCREASE_BRIGHTNESS,
61  name="Increase Brightness",
62  translation_key="increase_brightness",
63  mutually_exclusive=Action.SET_BRIGHTNESS,
64  argument=STEP_SIZE,
65  ),
67  key=Action.DECREASE_BRIGHTNESS,
68  name="Decrease Brightness",
69  translation_key="decrease_brightness",
70  mutually_exclusive=Action.SET_BRIGHTNESS,
71  argument=STEP_SIZE,
72  ),
74  key=Action.TOGGLE_UP_LIGHT,
75  name="Toggle Up Light",
76  translation_key="toggle_up_light",
77  mutually_exclusive=Action.TURN_UP_LIGHT_ON,
78  argument=None,
79  ),
81  key=Action.TOGGLE_DOWN_LIGHT,
82  name="Toggle Down Light",
83  translation_key="toggle_down_light",
84  mutually_exclusive=Action.TURN_DOWN_LIGHT_ON,
85  argument=None,
86  ),
88  key=Action.START_DIMMER,
89  name="Start Dimmer",
90  translation_key="start_dimmer",
91  mutually_exclusive=Action.SET_BRIGHTNESS,
92  argument=None,
93  ),
95  key=Action.START_UP_LIGHT_DIMMER,
96  name="Start Up Light Dimmer",
97  translation_key="start_up_light_dimmer",
98  mutually_exclusive=Action.SET_UP_LIGHT_BRIGHTNESS,
99  argument=None,
100  ),
102  key=Action.START_DOWN_LIGHT_DIMMER,
103  name="Start Down Light Dimmer",
104  translation_key="start_down_light_dimmer",
105  mutually_exclusive=Action.SET_DOWN_LIGHT_BRIGHTNESS,
106  argument=None,
107  ),
109  key=Action.START_INCREASING_BRIGHTNESS,
110  name="Start Increasing Brightness",
111  translation_key="start_increasing_brightness",
112  mutually_exclusive=Action.SET_BRIGHTNESS,
113  argument=None,
114  ),
116  key=Action.START_DECREASING_BRIGHTNESS,
117  name="Start Decreasing Brightness",
118  translation_key="start_decreasing_brightness",
119  mutually_exclusive=Action.SET_BRIGHTNESS,
120  argument=None,
121  ),
123  key=Action.INCREASE_UP_LIGHT_BRIGHTNESS,
124  name="Increase Up Light Brightness",
125  translation_key="increase_up_light_brightness",
126  mutually_exclusive=Action.SET_UP_LIGHT_BRIGHTNESS,
127  argument=STEP_SIZE,
128  ),
130  key=Action.DECREASE_UP_LIGHT_BRIGHTNESS,
131  name="Decrease Up Light Brightness",
132  translation_key="decrease_up_light_brightness",
133  mutually_exclusive=Action.SET_UP_LIGHT_BRIGHTNESS,
134  argument=STEP_SIZE,
135  ),
137  key=Action.INCREASE_DOWN_LIGHT_BRIGHTNESS,
138  name="Increase Down Light Brightness",
139  translation_key="increase_down_light_brightness",
140  mutually_exclusive=Action.SET_DOWN_LIGHT_BRIGHTNESS,
141  argument=STEP_SIZE,
142  ),
144  key=Action.DECREASE_DOWN_LIGHT_BRIGHTNESS,
145  name="Decrease Down Light Brightness",
146  translation_key="decrease_down_light_brightness",
147  mutually_exclusive=Action.SET_DOWN_LIGHT_BRIGHTNESS,
148  argument=STEP_SIZE,
149  ),
151  key=Action.CYCLE_UP_LIGHT_BRIGHTNESS,
152  name="Cycle Up Light Brightness",
153  translation_key="cycle_up_light_brightness",
154  mutually_exclusive=Action.SET_UP_LIGHT_BRIGHTNESS,
155  argument=STEP_SIZE,
156  ),
158  key=Action.CYCLE_DOWN_LIGHT_BRIGHTNESS,
159  name="Cycle Down Light Brightness",
160  translation_key="cycle_down_light_brightness",
161  mutually_exclusive=Action.SET_DOWN_LIGHT_BRIGHTNESS,
162  argument=STEP_SIZE,
163  ),
165  key=Action.CYCLE_BRIGHTNESS,
166  name="Cycle Brightness",
167  translation_key="cycle_brightness",
168  mutually_exclusive=Action.SET_BRIGHTNESS,
169  argument=STEP_SIZE,
170  ),
172  key=Action.INCREASE_SPEED,
173  name="Increase Speed",
174  translation_key="increase_speed",
175  mutually_exclusive=Action.SET_SPEED,
176  argument=1,
177  ),
179  key=Action.DECREASE_SPEED,
180  name="Decrease Speed",
181  translation_key="decrease_speed",
182  mutually_exclusive=Action.SET_SPEED,
183  argument=1,
184  ),
186  key=Action.TOGGLE_DIRECTION,
187  name="Toggle Direction",
188  translation_key="toggle_direction",
189  mutually_exclusive=Action.SET_DIRECTION,
190  argument=None,
191  ),
193  key=Action.INCREASE_TEMPERATURE,
194  name="Increase Temperature",
195  translation_key="increase_temperature",
196  mutually_exclusive=None,
197  argument=1,
198  ),
200  key=Action.DECREASE_TEMPERATURE,
201  name="Decrease Temperature",
202  translation_key="decrease_temperature",
203  mutually_exclusive=None,
204  argument=1,
205  ),
207  key=Action.INCREASE_FLAME,
208  name="Increase Flame",
209  translation_key="increase_flame",
210  mutually_exclusive=None,
211  argument=STEP_SIZE,
212  ),
214  key=Action.DECREASE_FLAME,
215  name="Decrease Flame",
216  translation_key="decrease_flame",
217  mutually_exclusive=None,
218  argument=STEP_SIZE,
219  ),
221  key=Action.TOGGLE_OPEN,
222  name="Toggle Open",
223  mutually_exclusive=Action.OPEN,
224  argument=None,
225  ),
227  key=Action.INCREASE_POSITION,
228  name="Increase Position",
229  translation_key="increase_position",
230  mutually_exclusive=Action.SET_POSITION,
231  argument=STEP_SIZE,
232  ),
234  key=Action.DECREASE_POSITION,
235  name="Decrease Position",
236  translation_key="decrease_position",
237  mutually_exclusive=Action.SET_POSITION,
238  argument=STEP_SIZE,
239  ),
241  key=Action.OPEN_NEXT,
242  name="Open Next",
243  translation_key="open_next",
244  mutually_exclusive=None,
245  argument=None,
246  ),
248  key=Action.CLOSE_NEXT,
249  name="Close Next",
250  translation_key="close_next",
251  mutually_exclusive=None,
252  argument=None,
253  ),
254 )
255 
256 
258  hass: HomeAssistant,
259  entry: BondConfigEntry,
260  async_add_entities: AddEntitiesCallback,
261 ) -> None:
262  """Set up Bond button devices."""
263  data = entry.runtime_data
264  entities: list[BondButtonEntity] = []
265 
266  for device in data.hub.devices:
267  device_entities = [
268  BondButtonEntity(data, device, description)
269  for description in BUTTONS
270  if device.has_action(description.key)
271  and (
272  description.mutually_exclusive is None
273  or not device.has_action(description.mutually_exclusive)
274  )
275  ]
276  if device_entities and device.has_action(STOP_BUTTON.key):
277  # Most devices have the stop action available, but
278  # we only add the stop action button if we add actions
279  # since its not so useful if there are no actions to stop
280  device_entities.append(BondButtonEntity(data, device, STOP_BUTTON))
281  entities.extend(device_entities)
282 
283  async_add_entities(entities)
284 
285 
287  """Bond Button Device."""
288 
289  entity_description: BondButtonEntityDescription
290 
291  def __init__(
292  self,
293  data: BondData,
294  device: BondDevice,
295  description: BondButtonEntityDescription,
296  ) -> None:
297  """Init Bond button."""
298  self.entity_descriptionentity_description = description
299  super().__init__(data, device, description.name, description.key.lower())
300 
301  async def async_press(self) -> None:
302  """Press the button."""
303  description = self.entity_descriptionentity_description
304  key = description.key
305  if argument := description.argument:
306  action = Action(key, argument)
307  else:
308  action = Action(key)
309  await self._bond_bond.action(self._device_id_device_id, action)
310 
311  def _apply_state(self) -> None:
312  """Apply the state."""
None __init__(self, BondData data, BondDevice device, BondButtonEntityDescription description)
Definition: button.py:296
None async_setup_entry(HomeAssistant hass, BondConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: button.py:261