Home Assistant Unofficial Reference 2024.12.1
siren.py
Go to the documentation of this file.
1 """Demo platform that offers a fake siren device."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.components.siren import SirenEntity, SirenEntityFeature
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 
12 SUPPORT_FLAGS = SirenEntityFeature.TURN_OFF | SirenEntityFeature.TURN_ON
13 
14 
16  hass: HomeAssistant,
17  config_entry: ConfigEntry,
18  async_add_entities: AddEntitiesCallback,
19 ) -> None:
20  """Set up the Demo siren devices config entry."""
22  [
23  DemoSiren(name="Siren"),
24  DemoSiren(
25  name="Siren with all features",
26  available_tones=["fire", "alarm"],
27  support_volume_set=True,
28  support_duration=True,
29  ),
30  ]
31  )
32 
33 
35  """Representation of a demo siren device."""
36 
37  def __init__(
38  self,
39  name: str,
40  available_tones: list[str | int] | None = None,
41  support_volume_set: bool = False,
42  support_duration: bool = False,
43  is_on: bool = True,
44  ) -> None:
45  """Initialize the siren device."""
46  self._attr_name_attr_name = name
47  self._attr_should_poll_attr_should_poll = False
48  self._attr_supported_features_attr_supported_features = SUPPORT_FLAGS
49  self._attr_is_on_attr_is_on = is_on
50  if available_tones is not None:
51  self._attr_supported_features_attr_supported_features |= SirenEntityFeature.TONES
52  if support_volume_set:
53  self._attr_supported_features_attr_supported_features |= SirenEntityFeature.VOLUME_SET
54  if support_duration:
55  self._attr_supported_features_attr_supported_features |= SirenEntityFeature.DURATION
56  self._attr_available_tones_attr_available_tones = available_tones
57 
58  async def async_turn_on(self, **kwargs: Any) -> None:
59  """Turn the siren on."""
60  self._attr_is_on_attr_is_on = True
61  self.async_write_ha_stateasync_write_ha_state()
62 
63  async def async_turn_off(self, **kwargs: Any) -> None:
64  """Turn the siren off."""
65  self._attr_is_on_attr_is_on = False
66  self.async_write_ha_stateasync_write_ha_state()
None __init__(self, str name, list[str|int]|None available_tones=None, bool support_volume_set=False, bool support_duration=False, bool is_on=True)
Definition: siren.py:44
None async_turn_off(self, **Any kwargs)
Definition: siren.py:63
None async_turn_on(self, **Any kwargs)
Definition: siren.py:58
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: siren.py:19