Home Assistant Unofficial Reference 2024.12.1
siren.py
Go to the documentation of this file.
1 """Support for Tuya siren."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from tuya_sharing import CustomerDevice, Manager
8 
10  SirenEntity,
11  SirenEntityDescription,
12  SirenEntityFeature,
13 )
14 from homeassistant.const import EntityCategory
15 from homeassistant.core import HomeAssistant, callback
16 from homeassistant.helpers.dispatcher import async_dispatcher_connect
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 
19 from . import TuyaConfigEntry
20 from .const import TUYA_DISCOVERY_NEW, DPCode
21 from .entity import TuyaEntity
22 
23 # All descriptions can be found here:
24 # https://developer.tuya.com/en/docs/iot/standarddescription?id=K9i5ql6waswzq
25 SIRENS: dict[str, tuple[SirenEntityDescription, ...]] = {
26  # Multi-functional Sensor
27  # https://developer.tuya.com/en/docs/iot/categorydgnbj?id=Kaiuz3yorvzg3
28  "dgnbj": (
30  key=DPCode.ALARM_SWITCH,
31  ),
32  ),
33  # Siren Alarm
34  # https://developer.tuya.com/en/docs/iot/categorysgbj?id=Kaiuz37tlpbnu
35  "sgbj": (
37  key=DPCode.ALARM_SWITCH,
38  ),
39  ),
40  # Smart Camera
41  # https://developer.tuya.com/en/docs/iot/categorysp?id=Kaiuz35leyo12
42  "sp": (
44  key=DPCode.SIREN_SWITCH,
45  ),
46  ),
47  # CO2 Detector
48  # https://developer.tuya.com/en/docs/iot/categoryco2bj?id=Kaiuz3wes7yuy
49  "co2bj": (
51  key=DPCode.ALARM_SWITCH,
52  entity_category=EntityCategory.CONFIG,
53  ),
54  ),
55 }
56 
57 
59  hass: HomeAssistant, entry: TuyaConfigEntry, async_add_entities: AddEntitiesCallback
60 ) -> None:
61  """Set up Tuya siren dynamically through Tuya discovery."""
62  hass_data = entry.runtime_data
63 
64  @callback
65  def async_discover_device(device_ids: list[str]) -> None:
66  """Discover and add a discovered Tuya siren."""
67  entities: list[TuyaSirenEntity] = []
68  for device_id in device_ids:
69  device = hass_data.manager.device_map[device_id]
70  if descriptions := SIRENS.get(device.category):
71  entities.extend(
72  TuyaSirenEntity(device, hass_data.manager, description)
73  for description in descriptions
74  if description.key in device.status
75  )
76 
77  async_add_entities(entities)
78 
79  async_discover_device([*hass_data.manager.device_map])
80 
81  entry.async_on_unload(
82  async_dispatcher_connect(hass, TUYA_DISCOVERY_NEW, async_discover_device)
83  )
84 
85 
87  """Tuya Siren Entity."""
88 
89  _attr_supported_features = SirenEntityFeature.TURN_ON | SirenEntityFeature.TURN_OFF
90  _attr_name = None
91 
92  def __init__(
93  self,
94  device: CustomerDevice,
95  device_manager: Manager,
96  description: SirenEntityDescription,
97  ) -> None:
98  """Init Tuya Siren."""
99  super().__init__(device, device_manager)
100  self.entity_descriptionentity_description = description
101  self._attr_unique_id_attr_unique_id_attr_unique_id = f"{super().unique_id}{description.key}"
102 
103  @property
104  def is_on(self) -> bool:
105  """Return true if siren is on."""
106  return self.devicedevice.status.get(self.entity_descriptionentity_description.key, False)
107 
108  def turn_on(self, **kwargs: Any) -> None:
109  """Turn the siren on."""
110  self._send_command_send_command([{"code": self.entity_descriptionentity_description.key, "value": True}])
111 
112  def turn_off(self, **kwargs: Any) -> None:
113  """Turn the siren off."""
114  self._send_command_send_command([{"code": self.entity_descriptionentity_description.key, "value": False}])
None _send_command(self, list[dict[str, Any]] commands)
Definition: entity.py:295
None __init__(self, CustomerDevice device, Manager device_manager, SirenEntityDescription description)
Definition: siren.py:97
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: siren.py:60
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103