1 """Component to interface with various sirens/chimes."""
3 from __future__
import annotations
5 from datetime
import timedelta
6 from functools
import partial
8 from typing
import Any, TypedDict, cast, final
10 from propcache
import cached_property
11 import voluptuous
as vol
18 all_with_deprecated_constants,
19 check_if_deprecated_constant,
20 dir_with_deprecated_constants,
28 _DEPRECATED_SUPPORT_DURATION,
29 _DEPRECATED_SUPPORT_TONES,
30 _DEPRECATED_SUPPORT_TURN_OFF,
31 _DEPRECATED_SUPPORT_TURN_ON,
32 _DEPRECATED_SUPPORT_VOLUME_SET,
41 _LOGGER = logging.getLogger(__name__)
43 DATA_COMPONENT: HassKey[EntityComponent[SirenEntity]] =
HassKey(DOMAIN)
44 PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA
45 PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE
48 TURN_ON_SCHEMA: VolDictType = {
49 vol.Optional(ATTR_TONE): vol.Any(vol.Coerce(int), cv.string),
50 vol.Optional(ATTR_DURATION): cv.positive_int,
51 vol.Optional(ATTR_VOLUME_LEVEL): cv.small_float,
56 """Represent possible parameters to siren.turn_on service data dict type."""
67 siren: SirenEntity, params: SirenTurnOnServiceParameters
68 ) -> SirenTurnOnServiceParameters:
69 """Process turn_on service params.
71 Filters out unsupported params and validates the rest.
74 if not siren.supported_features & SirenEntityFeature.TONES:
75 params.pop(ATTR_TONE,
None)
76 elif (tone := params.get(ATTR_TONE))
is not None:
78 is_tone_dict_value = bool(
79 isinstance(siren.available_tones, dict)
80 and tone
in siren.available_tones.values()
83 not siren.available_tones
84 or tone
not in siren.available_tones
85 and not is_tone_dict_value
88 f
"Invalid tone specified for entity {siren.entity_id}: {tone}, "
89 "check the available_tones attribute for valid tones to pass in"
94 if is_tone_dict_value:
95 assert isinstance(siren.available_tones, dict)
96 params[ATTR_TONE] = next(
97 key
for key, value
in siren.available_tones.items()
if value == tone
100 if not siren.supported_features & SirenEntityFeature.DURATION:
101 params.pop(ATTR_DURATION,
None)
102 if not siren.supported_features & SirenEntityFeature.VOLUME_SET:
103 params.pop(ATTR_VOLUME_LEVEL,
None)
108 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
109 """Set up siren devices."""
110 component = hass.data[DATA_COMPONENT] = EntityComponent[SirenEntity](
111 _LOGGER, DOMAIN, hass, SCAN_INTERVAL
113 await component.async_setup(config)
115 async
def async_handle_turn_on_service(
116 siren: SirenEntity, call: ServiceCall
118 """Handle turning a siren on."""
121 for k, v
in call.data.items()
122 if k
in (ATTR_TONE, ATTR_DURATION, ATTR_VOLUME_LEVEL)
124 await siren.async_turn_on(
128 component.async_register_entity_service(
131 async_handle_turn_on_service,
132 [SirenEntityFeature.TURN_ON],
134 component.async_register_entity_service(
135 SERVICE_TURN_OFF,
None,
"async_turn_off", [SirenEntityFeature.TURN_OFF]
137 component.async_register_entity_service(
141 [SirenEntityFeature.TURN_ON | SirenEntityFeature.TURN_OFF],
148 """Set up a config entry."""
153 """Unload a config entry."""
158 """A class that describes siren entities."""
160 available_tones: list[int | str] | dict[int, str] |
None =
None
163 CACHED_PROPERTIES_WITH_ATTR_ = {
165 "supported_features",
170 """Representation of a siren device."""
172 _entity_component_unrecorded_attributes = frozenset({ATTR_AVAILABLE_TONES})
174 entity_description: SirenEntityDescription
175 _attr_available_tones: list[int | str] | dict[int, str] |
None
181 """Return capability attributes."""
192 """Return a list of available tones.
194 Requires SirenEntityFeature.TONES.
196 if hasattr(self,
"_attr_available_tones"):
197 return self._attr_available_tones
198 if hasattr(self,
"entity_description"):
199 return self.entity_description.available_tones
204 """Return the list of supported features."""
205 features = self._attr_supported_features
206 if type(features)
is int:
216 __getattr__ = partial(check_if_deprecated_constant, module_globals=globals())
218 dir_with_deprecated_constants, module_globals_keys=[*globals().keys()]
SirenEntityFeature supported_features(self)
dict[str, Any]|None capability_attributes(self)
list[int|str]|dict[int, str]|None available_tones(self)
None _report_deprecated_supported_features_values(self, IntFlag replacement)
int|None supported_features(self)
SirenTurnOnServiceParameters process_turn_on_params(SirenEntity siren, SirenTurnOnServiceParameters params)
bool async_setup(HomeAssistant hass, ConfigType config)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
list[str] all_with_deprecated_constants(dict[str, Any] module_globals)