Introduce base location entity for totalconnect (#115938)

* Introduce base location entity for totalconnect

* Update homeassistant/components/totalconnect/entity.py

Co-authored-by: TheJulianJES <TheJulianJES@users.noreply.github.com>

---------

Co-authored-by: TheJulianJES <TheJulianJES@users.noreply.github.com>
This commit is contained in:
Joost Lekkerkerker 2024-04-22 10:41:26 +02:00 committed by GitHub
parent 46941adb51
commit 09ae8b9f52
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 45 additions and 42 deletions

View File

@ -4,9 +4,12 @@ from __future__ import annotations
from total_connect_client import ArmingHelper from total_connect_client import ArmingHelper
from total_connect_client.exceptions import BadResultCodeError, UsercodeInvalid from total_connect_client.exceptions import BadResultCodeError, UsercodeInvalid
from total_connect_client.location import TotalConnectLocation
import homeassistant.components.alarm_control_panel as alarm from homeassistant.components.alarm_control_panel import (
from homeassistant.components.alarm_control_panel import AlarmControlPanelEntityFeature AlarmControlPanelEntity,
AlarmControlPanelEntityFeature,
)
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
STATE_ALARM_ARMED_AWAY, STATE_ALARM_ARMED_AWAY,
@ -21,12 +24,11 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_platform from homeassistant.helpers import entity_platform
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import TotalConnectDataUpdateCoordinator from . import TotalConnectDataUpdateCoordinator
from .const import DOMAIN from .const import DOMAIN
from .entity import TotalConnectEntity from .entity import TotalConnectLocationEntity
SERVICE_ALARM_ARM_AWAY_INSTANT = "arm_away_instant" SERVICE_ALARM_ARM_AWAY_INSTANT = "arm_away_instant"
SERVICE_ALARM_ARM_HOME_INSTANT = "arm_home_instant" SERVICE_ALARM_ARM_HOME_INSTANT = "arm_home_instant"
@ -40,14 +42,12 @@ async def async_setup_entry(
coordinator: TotalConnectDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] coordinator: TotalConnectDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
for location_id, location in coordinator.client.locations.items(): for location in coordinator.client.locations.values():
location_name = location.location_name
alarms.extend( alarms.extend(
TotalConnectAlarm( TotalConnectAlarm(
coordinator=coordinator, coordinator,
name=location_name, location,
location_id=location_id, partition_id,
partition_id=partition_id,
) )
for partition_id in location.partitions for partition_id in location.partitions
) )
@ -70,8 +70,8 @@ async def async_setup_entry(
) )
class TotalConnectAlarm(TotalConnectEntity, alarm.AlarmControlPanelEntity): class TotalConnectAlarm(TotalConnectLocationEntity, AlarmControlPanelEntity):
"""Represent an TotalConnect status.""" """Represent a TotalConnect alarm panel."""
_attr_supported_features = ( _attr_supported_features = (
AlarmControlPanelEntityFeature.ARM_HOME AlarmControlPanelEntityFeature.ARM_HOME
@ -82,19 +82,13 @@ class TotalConnectAlarm(TotalConnectEntity, alarm.AlarmControlPanelEntity):
def __init__( def __init__(
self, self,
coordinator: TotalConnectDataUpdateCoordinator, coordinator: TotalConnectDataUpdateCoordinator,
name, location: TotalConnectLocation,
location_id, partition_id: int,
partition_id,
) -> None: ) -> None:
"""Initialize the TotalConnect status.""" """Initialize the TotalConnect status."""
super().__init__(coordinator) super().__init__(coordinator, location)
self._location_id = location_id
self._location = coordinator.client.locations[location_id]
self._partition_id = partition_id self._partition_id = partition_id
self._partition = self._location.partitions[partition_id] self._partition = self._location.partitions[partition_id]
self._device = self._location.devices[self._location.security_device_id]
self._state: str | None = None
self._attr_extra_state_attributes = {}
""" """
Set unique_id to location_id for partition 1 to avoid breaking change Set unique_id to location_id for partition 1 to avoid breaking change
@ -102,27 +96,18 @@ class TotalConnectAlarm(TotalConnectEntity, alarm.AlarmControlPanelEntity):
Add _# for partition 2 and beyond. Add _# for partition 2 and beyond.
""" """
if partition_id == 1: if partition_id == 1:
self._attr_name = name self._attr_name = self.device.name
self._attr_unique_id = f"{location_id}" self._attr_unique_id = str(location.location_id)
else: else:
self._attr_name = f"{name} partition {partition_id}" self._attr_name = f"{self.device.name} partition {partition_id}"
self._attr_unique_id = f"{location_id}_{partition_id}" self._attr_unique_id = f"{location.location_id}_{partition_id}"
@property
def device_info(self) -> DeviceInfo:
"""Return device info."""
return DeviceInfo(
identifiers={(DOMAIN, self._device.serial_number)},
name=self._device.name,
serial_number=self._device.serial_number,
)
@property @property
def state(self) -> str | None: def state(self) -> str | None:
"""Return the state of the device.""" """Return the state of the device."""
attr = { attr = {
"location_name": self.name, "location_name": self.name,
"location_id": self._location_id, "location_id": self._location.location_id,
"partition": self._partition_id, "partition": self._partition_id,
"ac_loss": self._location.ac_loss, "ac_loss": self._location.ac_loss,
"low_battery": self._location.low_battery, "low_battery": self._location.low_battery,
@ -156,10 +141,9 @@ class TotalConnectAlarm(TotalConnectEntity, alarm.AlarmControlPanelEntity):
state = STATE_ALARM_TRIGGERED state = STATE_ALARM_TRIGGERED
attr["triggered_source"] = "Carbon Monoxide" attr["triggered_source"] = "Carbon Monoxide"
self._state = state
self._attr_extra_state_attributes = attr self._attr_extra_state_attributes = attr
return self._state return state
async def async_alarm_disarm(self, code: str | None = None) -> None: async def async_alarm_disarm(self, code: str | None = None) -> None:
"""Send disarm command.""" """Send disarm command."""

View File

@ -19,7 +19,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import TotalConnectDataUpdateCoordinator from . import TotalConnectDataUpdateCoordinator
from .const import DOMAIN from .const import DOMAIN
from .entity import TotalConnectEntity, TotalConnectZoneEntity from .entity import TotalConnectLocationEntity, TotalConnectZoneEntity
LOW_BATTERY = "low_battery" LOW_BATTERY = "low_battery"
TAMPER = "tamper" TAMPER = "tamper"
@ -181,7 +181,7 @@ class TotalConnectZoneBinarySensor(TotalConnectZoneEntity, BinarySensorEntity):
return super().device_class return super().device_class
class TotalConnectAlarmBinarySensor(TotalConnectEntity, BinarySensorEntity): class TotalConnectAlarmBinarySensor(TotalConnectLocationEntity, BinarySensorEntity):
"""Represent a TotalConnect alarm device binary sensors.""" """Represent a TotalConnect alarm device binary sensors."""
entity_description: TotalConnectAlarmBinarySensorEntityDescription entity_description: TotalConnectAlarmBinarySensorEntityDescription
@ -193,10 +193,9 @@ class TotalConnectAlarmBinarySensor(TotalConnectEntity, BinarySensorEntity):
location: TotalConnectLocation, location: TotalConnectLocation,
) -> None: ) -> None:
"""Initialize the TotalConnect alarm device binary sensor.""" """Initialize the TotalConnect alarm device binary sensor."""
super().__init__(coordinator) super().__init__(coordinator, location)
self.entity_description = entity_description self.entity_description = entity_description
self._location = location self._attr_name = f"{self.device.name}{entity_description.name}"
self._attr_name = f"{location.location_name}{entity_description.name}"
self._attr_unique_id = f"{location.location_id}_{entity_description.key}" self._attr_unique_id = f"{location.location_id}_{entity_description.key}"
self._attr_extra_state_attributes = { self._attr_extra_state_attributes = {
"location_id": location.location_id, "location_id": location.location_id,

View File

@ -1,5 +1,6 @@
"""Base class for TotalConnect entities.""" """Base class for TotalConnect entities."""
from total_connect_client.location import TotalConnectLocation
from total_connect_client.zone import TotalConnectZone from total_connect_client.zone import TotalConnectZone
from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.device_registry import DeviceInfo
@ -12,6 +13,25 @@ class TotalConnectEntity(CoordinatorEntity[TotalConnectDataUpdateCoordinator]):
"""Represent a TotalConnect entity.""" """Represent a TotalConnect entity."""
class TotalConnectLocationEntity(TotalConnectEntity):
"""Represent a TotalConnect location."""
def __init__(
self,
coordinator: TotalConnectDataUpdateCoordinator,
location: TotalConnectLocation,
) -> None:
"""Initialize the TotalConnect location."""
super().__init__(coordinator)
self._location = location
self.device = location.devices[location.security_device_id]
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, self.device.serial_number)},
name=self.device.name,
serial_number=self.device.serial_number,
)
class TotalConnectZoneEntity(TotalConnectEntity): class TotalConnectZoneEntity(TotalConnectEntity):
"""Represent a TotalConnect zone.""" """Represent a TotalConnect zone."""