Home Assistant Unofficial Reference 2024.12.1
siren.py
Go to the documentation of this file.
1 """YoLink Siren."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import Any
8 
9 from yolink.client_request import ClientRequest
10 from yolink.const import ATTR_DEVICE_SIREN
11 from yolink.device import YoLinkDevice
12 
14  SirenEntity,
15  SirenEntityDescription,
16  SirenEntityFeature,
17 )
18 from homeassistant.config_entries import ConfigEntry
19 from homeassistant.core import HomeAssistant, callback
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 
22 from .const import DOMAIN
23 from .coordinator import YoLinkCoordinator
24 from .entity import YoLinkEntity
25 
26 
27 @dataclass(frozen=True)
29  """YoLink SirenEntityDescription."""
30 
31  exists_fn: Callable[[YoLinkDevice], bool] = lambda _: True
32  value: Callable[[Any], bool | None] = lambda _: None
33 
34 
35 DEVICE_TYPES: tuple[YoLinkSirenEntityDescription, ...] = (
37  key="state",
38  value=lambda value: value == "alert" if value is not None else None,
39  exists_fn=lambda device: device.device_type in [ATTR_DEVICE_SIREN],
40  ),
41 )
42 
43 DEVICE_TYPE = [ATTR_DEVICE_SIREN]
44 
45 
47  hass: HomeAssistant,
48  config_entry: ConfigEntry,
49  async_add_entities: AddEntitiesCallback,
50 ) -> None:
51  """Set up YoLink siren from a config entry."""
52  device_coordinators = hass.data[DOMAIN][config_entry.entry_id].device_coordinators
53  siren_device_coordinators = [
54  device_coordinator
55  for device_coordinator in device_coordinators.values()
56  if device_coordinator.device.device_type in DEVICE_TYPE
57  ]
59  YoLinkSirenEntity(config_entry, siren_device_coordinator, description)
60  for siren_device_coordinator in siren_device_coordinators
61  for description in DEVICE_TYPES
62  if description.exists_fn(siren_device_coordinator.device)
63  )
64 
65 
67  """YoLink Siren Entity."""
68 
69  _attr_name = None
70 
71  entity_description: YoLinkSirenEntityDescription
72 
73  def __init__(
74  self,
75  config_entry: ConfigEntry,
76  coordinator: YoLinkCoordinator,
77  description: YoLinkSirenEntityDescription,
78  ) -> None:
79  """Init YoLink Siren."""
80  super().__init__(config_entry, coordinator)
81  self.entity_descriptionentity_description = description
82  self._attr_unique_id_attr_unique_id = (
83  f"{coordinator.device.device_id} {self.entity_description.key}"
84  )
85  self._attr_supported_features_attr_supported_features = (
86  SirenEntityFeature.TURN_ON | SirenEntityFeature.TURN_OFF
87  )
88 
89  @callback
90  def update_entity_state(self, state: dict[str, Any]) -> None:
91  """Update HA Entity State."""
92  self._attr_is_on_attr_is_on = self.entity_descriptionentity_description.value(
93  state.get(self.entity_descriptionentity_description.key)
94  )
95  self.async_write_ha_stateasync_write_ha_state()
96 
97  async def call_state_change(self, state: bool) -> None:
98  """Call setState api to change siren state."""
99  await self.call_devicecall_device(ClientRequest("setState", {"state": {"alarm": state}}))
100  self._attr_is_on_attr_is_on = self.entity_descriptionentity_description.value("alert" if state else "normal")
101  self.async_write_ha_stateasync_write_ha_state()
102 
103  async def async_turn_on(self, **kwargs: Any) -> None:
104  """Turn the entity on."""
105  await self.call_state_changecall_state_change(True)
106 
107  async def async_turn_off(self, **kwargs: Any) -> None:
108  """Turn the entity off."""
109  await self.call_state_changecall_state_change(False)