Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Support for Tuya buttons."""
2 
3 from __future__ import annotations
4 
5 from tuya_sharing import CustomerDevice, Manager
6 
7 from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
8 from homeassistant.const import EntityCategory
9 from homeassistant.core import HomeAssistant, callback
10 from homeassistant.helpers.dispatcher import async_dispatcher_connect
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 
13 from . import TuyaConfigEntry
14 from .const import TUYA_DISCOVERY_NEW, DPCode
15 from .entity import TuyaEntity
16 
17 # All descriptions can be found here.
18 # https://developer.tuya.com/en/docs/iot/standarddescription?id=K9i5ql6waswzq
19 BUTTONS: dict[str, tuple[ButtonEntityDescription, ...]] = {
20  # Robot Vacuum
21  # https://developer.tuya.com/en/docs/iot/fsd?id=K9gf487ck1tlo
22  "sd": (
24  key=DPCode.RESET_DUSTER_CLOTH,
25  translation_key="reset_duster_cloth",
26  entity_category=EntityCategory.CONFIG,
27  ),
29  key=DPCode.RESET_EDGE_BRUSH,
30  translation_key="reset_edge_brush",
31  entity_category=EntityCategory.CONFIG,
32  ),
34  key=DPCode.RESET_FILTER,
35  translation_key="reset_filter",
36  entity_category=EntityCategory.CONFIG,
37  ),
39  key=DPCode.RESET_MAP,
40  translation_key="reset_map",
41  entity_category=EntityCategory.CONFIG,
42  ),
44  key=DPCode.RESET_ROLL_BRUSH,
45  translation_key="reset_roll_brush",
46  entity_category=EntityCategory.CONFIG,
47  ),
48  ),
49  # Wake Up Light II
50  # Not documented
51  "hxd": (
53  key=DPCode.SWITCH_USB6,
54  translation_key="snooze",
55  ),
56  ),
57 }
58 
59 
61  hass: HomeAssistant, entry: TuyaConfigEntry, async_add_entities: AddEntitiesCallback
62 ) -> None:
63  """Set up Tuya buttons dynamically through Tuya discovery."""
64  hass_data = entry.runtime_data
65 
66  @callback
67  def async_discover_device(device_ids: list[str]) -> None:
68  """Discover and add a discovered Tuya buttons."""
69  entities: list[TuyaButtonEntity] = []
70  for device_id in device_ids:
71  device = hass_data.manager.device_map[device_id]
72  if descriptions := BUTTONS.get(device.category):
73  entities.extend(
74  TuyaButtonEntity(device, hass_data.manager, description)
75  for description in descriptions
76  if description.key in device.status
77  )
78 
79  async_add_entities(entities)
80 
81  async_discover_device([*hass_data.manager.device_map])
82 
83  entry.async_on_unload(
84  async_dispatcher_connect(hass, TUYA_DISCOVERY_NEW, async_discover_device)
85  )
86 
87 
89  """Tuya Button Device."""
90 
91  def __init__(
92  self,
93  device: CustomerDevice,
94  device_manager: Manager,
95  description: ButtonEntityDescription,
96  ) -> None:
97  """Init Tuya button."""
98  super().__init__(device, device_manager)
99  self.entity_descriptionentity_description = description
100  self._attr_unique_id_attr_unique_id_attr_unique_id = f"{super().unique_id}{description.key}"
101 
102  def press(self) -> None:
103  """Press the button."""
104  self._send_command_send_command([{"code": self.entity_descriptionentity_description.key, "value": True}])
None __init__(self, CustomerDevice device, Manager device_manager, ButtonEntityDescription description)
Definition: button.py:96
None _send_command(self, list[dict[str, Any]] commands)
Definition: entity.py:295
ElkSystem|None async_discover_device(HomeAssistant hass, str host)
Definition: discovery.py:78
None async_setup_entry(HomeAssistant hass, TuyaConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: button.py:62
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103