mirror of
https://github.com/home-assistant/core.git
synced 2025-12-02 14:08:12 +00:00
117 lines
3.6 KiB
Python
117 lines
3.6 KiB
Python
"""Support for Tuya siren."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from tuya_sharing import CustomerDevice, Manager
|
|
|
|
from homeassistant.components.siren import (
|
|
SirenEntity,
|
|
SirenEntityDescription,
|
|
SirenEntityFeature,
|
|
)
|
|
from homeassistant.const import EntityCategory
|
|
from homeassistant.core import HomeAssistant, callback
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
|
|
|
from . import TuyaConfigEntry
|
|
from .const import TUYA_DISCOVERY_NEW, DeviceCategory, DPCode
|
|
from .entity import TuyaEntity
|
|
from .models import DPCodeBooleanWrapper
|
|
|
|
SIRENS: dict[DeviceCategory, tuple[SirenEntityDescription, ...]] = {
|
|
DeviceCategory.CO2BJ: (
|
|
SirenEntityDescription(
|
|
key=DPCode.ALARM_SWITCH,
|
|
entity_category=EntityCategory.CONFIG,
|
|
),
|
|
),
|
|
DeviceCategory.DGNBJ: (
|
|
SirenEntityDescription(
|
|
key=DPCode.ALARM_SWITCH,
|
|
),
|
|
),
|
|
DeviceCategory.SGBJ: (
|
|
SirenEntityDescription(
|
|
key=DPCode.ALARM_SWITCH,
|
|
),
|
|
),
|
|
DeviceCategory.SP: (
|
|
SirenEntityDescription(
|
|
key=DPCode.SIREN_SWITCH,
|
|
),
|
|
),
|
|
}
|
|
|
|
# Smart Camera - Low power consumption camera (duplicate of `sp`)
|
|
SIRENS[DeviceCategory.DGHSXJ] = SIRENS[DeviceCategory.SP]
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
entry: TuyaConfigEntry,
|
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
|
) -> None:
|
|
"""Set up Tuya siren dynamically through Tuya discovery."""
|
|
manager = entry.runtime_data.manager
|
|
|
|
@callback
|
|
def async_discover_device(device_ids: list[str]) -> None:
|
|
"""Discover and add a discovered Tuya siren."""
|
|
entities: list[TuyaSirenEntity] = []
|
|
for device_id in device_ids:
|
|
device = manager.device_map[device_id]
|
|
if descriptions := SIRENS.get(device.category):
|
|
entities.extend(
|
|
TuyaSirenEntity(device, manager, description, dpcode_wrapper)
|
|
for description in descriptions
|
|
if (
|
|
dpcode_wrapper := DPCodeBooleanWrapper.find_dpcode(
|
|
device, description.key, prefer_function=True
|
|
)
|
|
)
|
|
)
|
|
|
|
async_add_entities(entities)
|
|
|
|
async_discover_device([*manager.device_map])
|
|
|
|
entry.async_on_unload(
|
|
async_dispatcher_connect(hass, TUYA_DISCOVERY_NEW, async_discover_device)
|
|
)
|
|
|
|
|
|
class TuyaSirenEntity(TuyaEntity, SirenEntity):
|
|
"""Tuya Siren Entity."""
|
|
|
|
_attr_supported_features = SirenEntityFeature.TURN_ON | SirenEntityFeature.TURN_OFF
|
|
_attr_name = None
|
|
|
|
def __init__(
|
|
self,
|
|
device: CustomerDevice,
|
|
device_manager: Manager,
|
|
description: SirenEntityDescription,
|
|
dpcode_wrapper: DPCodeBooleanWrapper,
|
|
) -> None:
|
|
"""Init Tuya Siren."""
|
|
super().__init__(device, device_manager)
|
|
self.entity_description = description
|
|
self._attr_unique_id = f"{super().unique_id}{description.key}"
|
|
self._dpcode_wrapper = dpcode_wrapper
|
|
|
|
@property
|
|
def is_on(self) -> bool | None:
|
|
"""Return true if siren is on."""
|
|
return self._dpcode_wrapper.read_device_status(self.device)
|
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
|
"""Turn the siren on."""
|
|
await self._async_send_dpcode_update(self._dpcode_wrapper, True)
|
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
|
"""Turn the siren off."""
|
|
await self._async_send_dpcode_update(self._dpcode_wrapper, False)
|