Add Comelit alarm zones sensor (#106421)

* Add Comelit alarm zones sensor

* apply review comment

* add translation key

* capitalize

* cleanup

* apply review comment

* apply review comment

* more review comment
pull/106486/head
Simone Chemelli 2023-12-27 16:47:59 +01:00 committed by GitHub
parent 817c71747f
commit a6d8a82f3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 120 additions and 12 deletions

View File

@ -18,6 +18,7 @@ BRIDGE_PLATFORMS = [
]
VEDO_PLATFORMS = [
Platform.ALARM_CONTROL_PANEL,
Platform.SENSOR,
]

View File

@ -8,6 +8,7 @@ from aiocomelit import (
ComelitSerialBridgeObject,
ComelitVedoApi,
ComelitVedoAreaObject,
ComelitVedoZoneObject,
exceptions,
)
from aiocomelit.api import ComelitCommonApi
@ -53,7 +54,9 @@ class ComelitBaseCoordinator(DataUpdateCoordinator[dict[str, Any]]):
def platform_device_info(
self,
object_class: ComelitVedoAreaObject | ComelitSerialBridgeObject,
object_class: ComelitVedoZoneObject
| ComelitVedoAreaObject
| ComelitSerialBridgeObject,
object_type: str,
) -> dr.DeviceInfo:
"""Set platform device info."""

View File

@ -3,8 +3,8 @@ from __future__ import annotations
from typing import Final
from aiocomelit import ComelitSerialBridgeObject
from aiocomelit.const import OTHER
from aiocomelit import ComelitSerialBridgeObject, ComelitVedoZoneObject
from aiocomelit.const import ALARM_ZONES, BRIDGE, OTHER, AlarmZoneState
from homeassistant.components.sensor import (
SensorDeviceClass,
@ -12,16 +12,16 @@ from homeassistant.components.sensor import (
SensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfPower
from homeassistant.const import CONF_TYPE, UnitOfPower
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN
from .coordinator import ComelitSerialBridge
from .coordinator import ComelitSerialBridge, ComelitVedoSystem
SENSOR_TYPES: Final = (
SENSOR_BRIDGE_TYPES: Final = (
SensorEntityDescription(
key="power",
native_unit_of_measurement=UnitOfPower.WATT,
@ -29,6 +29,17 @@ SENSOR_TYPES: Final = (
),
)
SENSOR_VEDO_TYPES: Final = (
SensorEntityDescription(
key="human_status",
translation_key="zone_status",
name=None,
device_class=SensorDeviceClass.ENUM,
icon="mdi:shield-check",
options=[zone_state.value for zone_state in AlarmZoneState],
),
)
async def async_setup_entry(
hass: HomeAssistant,
@ -37,23 +48,57 @@ async def async_setup_entry(
) -> None:
"""Set up Comelit sensors."""
if config_entry.data.get(CONF_TYPE, BRIDGE) == BRIDGE:
await async_setup_bridge_entry(hass, config_entry, async_add_entities)
else:
await async_setup_vedo_entry(hass, config_entry, async_add_entities)
async def async_setup_bridge_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Comelit Bridge sensors."""
coordinator: ComelitSerialBridge = hass.data[DOMAIN][config_entry.entry_id]
entities: list[ComelitSensorEntity] = []
entities: list[ComelitBridgeSensorEntity] = []
for device in coordinator.data[OTHER].values():
entities.extend(
ComelitSensorEntity(coordinator, device, config_entry.entry_id, sensor_desc)
for sensor_desc in SENSOR_TYPES
ComelitBridgeSensorEntity(
coordinator, device, config_entry.entry_id, sensor_desc
)
for sensor_desc in SENSOR_BRIDGE_TYPES
)
async_add_entities(entities)
class ComelitSensorEntity(CoordinatorEntity[ComelitSerialBridge], SensorEntity):
async def async_setup_vedo_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Comelit VEDO sensors."""
coordinator: ComelitVedoSystem = hass.data[DOMAIN][config_entry.entry_id]
entities: list[ComelitVedoSensorEntity] = []
for device in coordinator.data[ALARM_ZONES].values():
entities.extend(
ComelitVedoSensorEntity(
coordinator, device, config_entry.entry_id, sensor_desc
)
for sensor_desc in SENSOR_VEDO_TYPES
)
async_add_entities(entities)
class ComelitBridgeSensorEntity(CoordinatorEntity[ComelitSerialBridge], SensorEntity):
"""Sensor device."""
_attr_has_entity_name = True
entity_description: SensorEntityDescription
_attr_name = None
def __init__(
self,
@ -80,3 +125,45 @@ class ComelitSensorEntity(CoordinatorEntity[ComelitSerialBridge], SensorEntity):
self.coordinator.data[OTHER][self._device.index],
self.entity_description.key,
)
class ComelitVedoSensorEntity(CoordinatorEntity[ComelitVedoSystem], SensorEntity):
"""Sensor device."""
_attr_has_entity_name = True
def __init__(
self,
coordinator: ComelitVedoSystem,
zone: ComelitVedoZoneObject,
config_entry_entry_id: str,
description: SensorEntityDescription,
) -> None:
"""Init sensor entity."""
self._api = coordinator.api
self._zone = zone
super().__init__(coordinator)
# Use config_entry.entry_id as base for unique_id
# because no serial number or mac is available
self._attr_unique_id = f"{config_entry_entry_id}-{zone.index}"
self._attr_device_info = coordinator.platform_device_info(zone, "zone")
self.entity_description = description
@property
def _zone_object(self) -> ComelitVedoZoneObject:
"""Zone object."""
return self.coordinator.data[ALARM_ZONES][self._zone.index]
@property
def available(self) -> bool:
"""Sensor availability."""
return self._zone_object.human_status != AlarmZoneState.UNAVAILABLE
@property
def native_value(self) -> StateType:
"""Sensor value."""
if (status := self._zone_object.human_status) == AlarmZoneState.UNKNOWN:
return None
return status.value

View File

@ -31,5 +31,22 @@
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
"unknown": "[%key:common::config_flow::error::unknown%]"
}
},
"entity": {
"sensor": {
"zone_status": {
"state": {
"alarm": "Alarm",
"armed": "Armed",
"open": "Open",
"excluded": "Excluded",
"faulty": "Faulty",
"inhibited": "Inhibited",
"isolated": "Isolated",
"rest": "Rest",
"sabotated": "Sabotated"
}
}
}
}
}