2021-09-18 19:59:04 +00:00
|
|
|
"""Support for deCONZ siren."""
|
2021-11-16 16:25:56 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
2021-09-18 19:59:04 +00:00
|
|
|
from pydeconz.light import Siren
|
|
|
|
|
|
|
|
from homeassistant.components.siren import (
|
|
|
|
ATTR_DURATION,
|
|
|
|
DOMAIN,
|
|
|
|
SirenEntity,
|
2022-04-08 07:13:12 +00:00
|
|
|
SirenEntityFeature,
|
2021-09-18 19:59:04 +00:00
|
|
|
)
|
2021-11-16 16:25:56 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-09-18 19:59:04 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2021-11-16 16:25:56 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-09-18 19:59:04 +00:00
|
|
|
|
|
|
|
from .deconz_device import DeconzDevice
|
2022-04-08 07:13:12 +00:00
|
|
|
from .gateway import get_gateway_from_config_entry
|
2021-09-18 19:59:04 +00:00
|
|
|
|
|
|
|
|
2021-11-16 16:25:56 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2021-09-18 19:59:04 +00:00
|
|
|
"""Set up sirens for deCONZ component."""
|
|
|
|
gateway = get_gateway_from_config_entry(hass, config_entry)
|
|
|
|
gateway.entities[DOMAIN] = set()
|
|
|
|
|
|
|
|
@callback
|
2022-04-05 20:44:04 +00:00
|
|
|
def async_add_siren(lights: list[Siren] | None = None) -> None:
|
2021-09-18 19:59:04 +00:00
|
|
|
"""Add siren from deCONZ."""
|
|
|
|
entities = []
|
|
|
|
|
2022-04-05 20:44:04 +00:00
|
|
|
if lights is None:
|
|
|
|
lights = list(gateway.api.lights.sirens.values())
|
|
|
|
|
2021-09-18 19:59:04 +00:00
|
|
|
for light in lights:
|
|
|
|
|
|
|
|
if (
|
|
|
|
isinstance(light, Siren)
|
|
|
|
and light.unique_id not in gateway.entities[DOMAIN]
|
|
|
|
):
|
|
|
|
entities.append(DeconzSiren(light, gateway))
|
|
|
|
|
|
|
|
if entities:
|
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
|
|
config_entry.async_on_unload(
|
|
|
|
async_dispatcher_connect(
|
2021-10-07 10:48:27 +00:00
|
|
|
hass,
|
|
|
|
gateway.signal_new_light,
|
|
|
|
async_add_siren,
|
2021-09-18 19:59:04 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
async_add_siren()
|
|
|
|
|
|
|
|
|
|
|
|
class DeconzSiren(DeconzDevice, SirenEntity):
|
|
|
|
"""Representation of a deCONZ siren."""
|
|
|
|
|
|
|
|
TYPE = DOMAIN
|
2022-04-08 07:13:12 +00:00
|
|
|
_attr_supported_features = (
|
|
|
|
SirenEntityFeature.TURN_ON
|
|
|
|
| SirenEntityFeature.TURN_OFF
|
|
|
|
| SirenEntityFeature.DURATION
|
|
|
|
)
|
2021-11-16 16:25:56 +00:00
|
|
|
_device: Siren
|
2021-09-18 19:59:04 +00:00
|
|
|
|
|
|
|
@property
|
2021-11-16 16:25:56 +00:00
|
|
|
def is_on(self) -> bool:
|
2021-09-18 19:59:04 +00:00
|
|
|
"""Return true if siren is on."""
|
2022-04-23 05:27:47 +00:00
|
|
|
return self._device.is_on
|
2021-09-18 19:59:04 +00:00
|
|
|
|
2021-11-16 16:25:56 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2021-09-18 19:59:04 +00:00
|
|
|
"""Turn on siren."""
|
|
|
|
data = {}
|
|
|
|
if (duration := kwargs.get(ATTR_DURATION)) is not None:
|
|
|
|
data["duration"] = duration * 10
|
|
|
|
await self._device.turn_on(**data)
|
|
|
|
|
2021-11-16 16:25:56 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2021-09-18 19:59:04 +00:00
|
|
|
"""Turn off siren."""
|
|
|
|
await self._device.turn_off()
|