Home Assistant Unofficial Reference 2024.12.1
siren.py
Go to the documentation of this file.
1 """Component providing support for Reolink siren entities."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 from typing import Any
7 
8 from reolink_aio.exceptions import InvalidParameterError, ReolinkError
9 
11  ATTR_DURATION,
12  ATTR_VOLUME_LEVEL,
13  SirenEntity,
14  SirenEntityDescription,
15  SirenEntityFeature,
16 )
17 from homeassistant.core import HomeAssistant
18 from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from .entity import ReolinkChannelCoordinatorEntity, ReolinkChannelEntityDescription
22 from .util import ReolinkConfigEntry, ReolinkData
23 
24 PARALLEL_UPDATES = 0
25 
26 
27 @dataclass(frozen=True)
29  SirenEntityDescription, ReolinkChannelEntityDescription
30 ):
31  """A class that describes siren entities."""
32 
33 
34 SIREN_ENTITIES = (
36  key="siren",
37  translation_key="siren",
38  supported=lambda api, ch: api.supported(ch, "siren_play"),
39  ),
40 )
41 
42 
44  hass: HomeAssistant,
45  config_entry: ReolinkConfigEntry,
46  async_add_entities: AddEntitiesCallback,
47 ) -> None:
48  """Set up a Reolink siren entities."""
49  reolink_data: ReolinkData = config_entry.runtime_data
50 
52  ReolinkSirenEntity(reolink_data, channel, entity_description)
53  for entity_description in SIREN_ENTITIES
54  for channel in reolink_data.host.api.channels
55  if entity_description.supported(reolink_data.host.api, channel)
56  )
57 
58 
60  """Base siren entity class for Reolink IP cameras."""
61 
62  _attr_supported_features = (
63  SirenEntityFeature.TURN_ON
64  | SirenEntityFeature.TURN_OFF
65  | SirenEntityFeature.DURATION
66  | SirenEntityFeature.VOLUME_SET
67  )
68  entity_description: ReolinkSirenEntityDescription
69 
70  def __init__(
71  self,
72  reolink_data: ReolinkData,
73  channel: int,
74  entity_description: ReolinkSirenEntityDescription,
75  ) -> None:
76  """Initialize Reolink siren entity."""
77  self.entity_descriptionentity_description = entity_description
78  super().__init__(reolink_data, channel)
79 
80  async def async_turn_on(self, **kwargs: Any) -> None:
81  """Turn on the siren."""
82  if (volume := kwargs.get(ATTR_VOLUME_LEVEL)) is not None:
83  try:
84  await self._host_host.api.set_volume(self._channel_channel, int(volume * 100))
85  except InvalidParameterError as err:
86  raise ServiceValidationError(err) from err
87  except ReolinkError as err:
88  raise HomeAssistantError(err) from err
89  duration = kwargs.get(ATTR_DURATION)
90  try:
91  await self._host_host.api.set_siren(self._channel_channel, True, duration)
92  except InvalidParameterError as err:
93  raise ServiceValidationError(err) from err
94  except ReolinkError as err:
95  raise HomeAssistantError(err) from err
96 
97  async def async_turn_off(self, **kwargs: Any) -> None:
98  """Turn off the siren."""
99  try:
100  await self._host_host.api.set_siren(self._channel_channel, False, None)
101  except ReolinkError as err:
102  raise HomeAssistantError(err) from err