mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
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
This commit is contained in:
parent
817c71747f
commit
a6d8a82f3e
@ -18,6 +18,7 @@ BRIDGE_PLATFORMS = [
|
|||||||
]
|
]
|
||||||
VEDO_PLATFORMS = [
|
VEDO_PLATFORMS = [
|
||||||
Platform.ALARM_CONTROL_PANEL,
|
Platform.ALARM_CONTROL_PANEL,
|
||||||
|
Platform.SENSOR,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ from aiocomelit import (
|
|||||||
ComelitSerialBridgeObject,
|
ComelitSerialBridgeObject,
|
||||||
ComelitVedoApi,
|
ComelitVedoApi,
|
||||||
ComelitVedoAreaObject,
|
ComelitVedoAreaObject,
|
||||||
|
ComelitVedoZoneObject,
|
||||||
exceptions,
|
exceptions,
|
||||||
)
|
)
|
||||||
from aiocomelit.api import ComelitCommonApi
|
from aiocomelit.api import ComelitCommonApi
|
||||||
@ -53,7 +54,9 @@ class ComelitBaseCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
|||||||
|
|
||||||
def platform_device_info(
|
def platform_device_info(
|
||||||
self,
|
self,
|
||||||
object_class: ComelitVedoAreaObject | ComelitSerialBridgeObject,
|
object_class: ComelitVedoZoneObject
|
||||||
|
| ComelitVedoAreaObject
|
||||||
|
| ComelitSerialBridgeObject,
|
||||||
object_type: str,
|
object_type: str,
|
||||||
) -> dr.DeviceInfo:
|
) -> dr.DeviceInfo:
|
||||||
"""Set platform device info."""
|
"""Set platform device info."""
|
||||||
|
@ -3,8 +3,8 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from typing import Final
|
from typing import Final
|
||||||
|
|
||||||
from aiocomelit import ComelitSerialBridgeObject
|
from aiocomelit import ComelitSerialBridgeObject, ComelitVedoZoneObject
|
||||||
from aiocomelit.const import OTHER
|
from aiocomelit.const import ALARM_ZONES, BRIDGE, OTHER, AlarmZoneState
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
SensorDeviceClass,
|
SensorDeviceClass,
|
||||||
@ -12,16 +12,16 @@ from homeassistant.components.sensor import (
|
|||||||
SensorEntityDescription,
|
SensorEntityDescription,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
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.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import StateType
|
from homeassistant.helpers.typing import StateType
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import ComelitSerialBridge
|
from .coordinator import ComelitSerialBridge, ComelitVedoSystem
|
||||||
|
|
||||||
SENSOR_TYPES: Final = (
|
SENSOR_BRIDGE_TYPES: Final = (
|
||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
key="power",
|
key="power",
|
||||||
native_unit_of_measurement=UnitOfPower.WATT,
|
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(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
@ -37,23 +48,57 @@ async def async_setup_entry(
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Comelit sensors."""
|
"""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]
|
coordinator: ComelitSerialBridge = hass.data[DOMAIN][config_entry.entry_id]
|
||||||
|
|
||||||
entities: list[ComelitSensorEntity] = []
|
entities: list[ComelitBridgeSensorEntity] = []
|
||||||
for device in coordinator.data[OTHER].values():
|
for device in coordinator.data[OTHER].values():
|
||||||
entities.extend(
|
entities.extend(
|
||||||
ComelitSensorEntity(coordinator, device, config_entry.entry_id, sensor_desc)
|
ComelitBridgeSensorEntity(
|
||||||
for sensor_desc in SENSOR_TYPES
|
coordinator, device, config_entry.entry_id, sensor_desc
|
||||||
|
)
|
||||||
|
for sensor_desc in SENSOR_BRIDGE_TYPES
|
||||||
)
|
)
|
||||||
|
|
||||||
async_add_entities(entities)
|
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."""
|
"""Sensor device."""
|
||||||
|
|
||||||
_attr_has_entity_name = True
|
_attr_has_entity_name = True
|
||||||
entity_description: SensorEntityDescription
|
_attr_name = None
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@ -80,3 +125,45 @@ class ComelitSensorEntity(CoordinatorEntity[ComelitSerialBridge], SensorEntity):
|
|||||||
self.coordinator.data[OTHER][self._device.index],
|
self.coordinator.data[OTHER][self._device.index],
|
||||||
self.entity_description.key,
|
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
|
||||||
|
@ -31,5 +31,22 @@
|
|||||||
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
|
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
|
||||||
"unknown": "[%key:common::config_flow::error::unknown%]"
|
"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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user