Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Button entities for the Motionblinds Bluetooth integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable, Coroutine
6 from dataclasses import dataclass
7 import logging
8 from typing import Any
9 
10 from motionblindsble.device import MotionDevice
11 
12 from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
13 from homeassistant.config_entries import ConfigEntry
14 from homeassistant.const import EntityCategory
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from .const import ATTR_CONNECT, ATTR_DISCONNECT, ATTR_FAVORITE, CONF_MAC_CODE, DOMAIN
19 from .entity import MotionblindsBLEEntity
20 
21 _LOGGER = logging.getLogger(__name__)
22 
23 PARALLEL_UPDATES = 0
24 
25 
26 @dataclass(frozen=True, kw_only=True)
28  """Entity description of a button entity with command attribute."""
29 
30  command: Callable[[MotionDevice], Coroutine[Any, Any, None]]
31 
32 
33 BUTTON_TYPES: list[MotionblindsBLEButtonEntityDescription] = [
35  key=ATTR_CONNECT,
36  translation_key=ATTR_CONNECT,
37  entity_category=EntityCategory.CONFIG,
38  command=lambda device: device.connect(),
39  ),
41  key=ATTR_DISCONNECT,
42  translation_key=ATTR_DISCONNECT,
43  entity_category=EntityCategory.CONFIG,
44  command=lambda device: device.disconnect(),
45  ),
47  key=ATTR_FAVORITE,
48  translation_key=ATTR_FAVORITE,
49  entity_category=EntityCategory.CONFIG,
50  command=lambda device: device.favorite(),
51  ),
52 ]
53 
54 
56  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
57 ) -> None:
58  """Set up button entities based on a config entry."""
59 
60  device: MotionDevice = hass.data[DOMAIN][entry.entry_id]
61 
64  device,
65  entry,
66  entity_description,
67  unique_id_suffix=entity_description.key,
68  )
69  for entity_description in BUTTON_TYPES
70  )
71 
72 
74  """Representation of a button entity."""
75 
76  entity_description: MotionblindsBLEButtonEntityDescription
77 
78  async def async_added_to_hass(self) -> None:
79  """Log button entity information."""
80  _LOGGER.debug(
81  "(%s) Setting up %s button entity",
82  self.entryentry.data[CONF_MAC_CODE],
83  self.entity_descriptionentity_description.key,
84  )
85 
86  async def async_press(self) -> None:
87  """Handle the button press."""
88  await self.entity_descriptionentity_description.command(self.devicedevice)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: button.py:57