2020-08-22 23:30:26 +00:00
|
|
|
"""Support for Risco alarm zones."""
|
2022-08-24 11:09:54 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-10-25 11:43:09 +00:00
|
|
|
from collections.abc import Mapping
|
2022-08-24 11:09:54 +00:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from pyrisco.common import Zone
|
|
|
|
|
2020-08-22 23:30:26 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2021-12-16 13:41:20 +00:00
|
|
|
BinarySensorDeviceClass,
|
2020-08-22 23:30:26 +00:00
|
|
|
BinarySensorEntity,
|
|
|
|
)
|
2022-01-03 10:35:02 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-08-22 23:30:26 +00:00
|
|
|
|
2022-10-31 23:29:00 +00:00
|
|
|
from . import LocalData, RiscoDataUpdateCoordinator, is_local
|
2020-11-09 17:44:30 +00:00
|
|
|
from .const import DATA_COORDINATOR, DOMAIN
|
2022-10-31 23:29:00 +00:00
|
|
|
from .entity import RiscoCloudZoneEntity, RiscoLocalZoneEntity
|
2022-10-25 11:43:09 +00:00
|
|
|
|
|
|
|
|
2022-01-03 10:35:02 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-08-22 23:30:26 +00:00
|
|
|
"""Set up the Risco alarm control panel."""
|
2022-08-24 11:09:54 +00:00
|
|
|
if is_local(config_entry):
|
|
|
|
local_data: LocalData = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
async_add_entities(
|
2022-10-25 11:43:09 +00:00
|
|
|
RiscoLocalBinarySensor(local_data.system.id, zone_id, zone)
|
|
|
|
for zone_id, zone in local_data.system.zones.items()
|
|
|
|
)
|
|
|
|
async_add_entities(
|
|
|
|
RiscoLocalAlarmedBinarySensor(local_data.system.id, zone_id, zone)
|
2022-08-24 11:09:54 +00:00
|
|
|
for zone_id, zone in local_data.system.zones.items()
|
|
|
|
)
|
2022-11-12 20:29:31 +00:00
|
|
|
async_add_entities(
|
|
|
|
RiscoLocalArmedBinarySensor(local_data.system.id, zone_id, zone)
|
|
|
|
for zone_id, zone in local_data.system.zones.items()
|
|
|
|
)
|
2022-08-24 11:09:54 +00:00
|
|
|
else:
|
|
|
|
coordinator: RiscoDataUpdateCoordinator = hass.data[DOMAIN][
|
|
|
|
config_entry.entry_id
|
|
|
|
][DATA_COORDINATOR]
|
|
|
|
async_add_entities(
|
|
|
|
RiscoCloudBinarySensor(coordinator, zone_id, zone)
|
|
|
|
for zone_id, zone in coordinator.data.zones.items()
|
|
|
|
)
|
2020-08-22 23:30:26 +00:00
|
|
|
|
|
|
|
|
2022-10-31 23:29:00 +00:00
|
|
|
class RiscoCloudBinarySensor(RiscoCloudZoneEntity, BinarySensorEntity):
|
|
|
|
"""Representation of a Risco cloud zone as a binary sensor."""
|
2020-08-22 23:30:26 +00:00
|
|
|
|
2022-08-24 11:09:54 +00:00
|
|
|
_attr_device_class = BinarySensorDeviceClass.MOTION
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, coordinator: RiscoDataUpdateCoordinator, zone_id: int, zone: Zone
|
|
|
|
) -> None:
|
|
|
|
"""Init the zone."""
|
2022-10-31 23:29:00 +00:00
|
|
|
super().__init__(
|
|
|
|
coordinator=coordinator, name=None, suffix="", zone_id=zone_id, zone=zone
|
2022-08-24 11:09:54 +00:00
|
|
|
)
|
|
|
|
|
2022-10-31 23:29:00 +00:00
|
|
|
@property
|
|
|
|
def is_on(self) -> bool | None:
|
|
|
|
"""Return true if sensor is on."""
|
|
|
|
return self._zone.triggered
|
2020-08-28 01:39:27 +00:00
|
|
|
|
|
|
|
|
2022-10-31 23:29:00 +00:00
|
|
|
class RiscoLocalBinarySensor(RiscoLocalZoneEntity, BinarySensorEntity):
|
2022-08-24 11:09:54 +00:00
|
|
|
"""Representation of a Risco local zone as a binary sensor."""
|
|
|
|
|
2022-10-31 23:29:00 +00:00
|
|
|
_attr_device_class = BinarySensorDeviceClass.MOTION
|
2022-08-24 11:09:54 +00:00
|
|
|
|
2022-10-25 11:43:09 +00:00
|
|
|
def __init__(self, system_id: str, zone_id: int, zone: Zone) -> None:
|
2022-08-24 11:09:54 +00:00
|
|
|
"""Init the zone."""
|
2022-10-31 23:29:00 +00:00
|
|
|
super().__init__(
|
|
|
|
system_id=system_id, name=None, suffix="", zone_id=zone_id, zone=zone
|
2022-10-25 11:43:09 +00:00
|
|
|
)
|
2022-08-24 11:09:54 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def extra_state_attributes(self) -> Mapping[str, Any] | None:
|
|
|
|
"""Return the state attributes."""
|
|
|
|
return {
|
|
|
|
**(super().extra_state_attributes or {}),
|
|
|
|
"groups": self._zone.groups,
|
|
|
|
}
|
|
|
|
|
2022-10-31 23:29:00 +00:00
|
|
|
@property
|
|
|
|
def is_on(self) -> bool | None:
|
|
|
|
"""Return true if sensor is on."""
|
|
|
|
return self._zone.triggered
|
2022-10-25 11:43:09 +00:00
|
|
|
|
|
|
|
|
2022-10-31 23:29:00 +00:00
|
|
|
class RiscoLocalAlarmedBinarySensor(RiscoLocalZoneEntity, BinarySensorEntity):
|
2022-10-25 11:43:09 +00:00
|
|
|
"""Representation whether a zone in Risco local is currently triggering an alarm."""
|
|
|
|
|
|
|
|
def __init__(self, system_id: str, zone_id: int, zone: Zone) -> None:
|
|
|
|
"""Init the zone."""
|
2022-10-31 23:29:00 +00:00
|
|
|
super().__init__(
|
|
|
|
system_id=system_id,
|
|
|
|
name="Alarmed",
|
|
|
|
suffix="_alarmed",
|
|
|
|
zone_id=zone_id,
|
|
|
|
zone=zone,
|
2022-10-25 11:43:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool | None:
|
|
|
|
"""Return true if sensor is on."""
|
|
|
|
return self._zone.alarmed
|
2022-11-12 20:29:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RiscoLocalArmedBinarySensor(RiscoLocalZoneEntity, BinarySensorEntity):
|
|
|
|
"""Representation whether a zone in Risco local is currently armed."""
|
|
|
|
|
|
|
|
def __init__(self, system_id: str, zone_id: int, zone: Zone) -> None:
|
|
|
|
"""Init the zone."""
|
|
|
|
super().__init__(
|
|
|
|
system_id=system_id,
|
|
|
|
name="Armed",
|
|
|
|
suffix="_armed",
|
|
|
|
zone_id=zone_id,
|
|
|
|
zone=zone,
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool | None:
|
|
|
|
"""Return true if sensor is on."""
|
|
|
|
return self._zone.armed
|