Use attributes in totalconnect alarm (#74113)

This commit is contained in:
epenet 2022-06-28 14:01:49 +02:00 committed by GitHub
parent 3b30d8a279
commit 4335cafb3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,6 +20,7 @@ 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.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
@ -85,7 +86,7 @@ class TotalConnectAlarm(CoordinatorEntity, alarm.AlarmControlPanelEntity):
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._device = self._location.devices[self._location.security_device_id]
self._state = None self._state = None
self._extra_state_attributes = {} 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
@ -93,35 +94,25 @@ class TotalConnectAlarm(CoordinatorEntity, alarm.AlarmControlPanelEntity):
Add _# for partition 2 and beyond. Add _# for partition 2 and beyond.
""" """
if partition_id == 1: if partition_id == 1:
self._name = name self._attr_name = name
self._unique_id = f"{location_id}" self._attr_unique_id = f"{location_id}"
else: else:
self._name = f"{name} partition {partition_id}" self._attr_name = f"{name} partition {partition_id}"
self._unique_id = f"{location_id}_{partition_id}" self._attr_unique_id = f"{location_id}_{partition_id}"
@property @property
def name(self): def device_info(self) -> DeviceInfo:
"""Return the name of the device."""
return self._name
@property
def unique_id(self):
"""Return the unique id."""
return self._unique_id
@property
def device_info(self):
"""Return device info.""" """Return device info."""
return { return DeviceInfo(
"identifiers": {(DOMAIN, self._device.serial_number)}, identifiers={(DOMAIN, self._device.serial_number)},
"name": self._device.name, name=self._device.name,
} )
@property @property
def state(self): 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_id,
"partition": self._partition_id, "partition": self._partition_id,
"ac_loss": self._location.ac_loss, "ac_loss": self._location.ac_loss,
@ -131,6 +122,7 @@ class TotalConnectAlarm(CoordinatorEntity, alarm.AlarmControlPanelEntity):
"triggered_zone": None, "triggered_zone": None,
} }
state = None
if self._partition.arming_state.is_disarmed(): if self._partition.arming_state.is_disarmed():
state = STATE_ALARM_DISARMED state = STATE_ALARM_DISARMED
elif self._partition.arming_state.is_armed_night(): elif self._partition.arming_state.is_armed_night():
@ -156,15 +148,10 @@ class TotalConnectAlarm(CoordinatorEntity, alarm.AlarmControlPanelEntity):
attr["triggered_source"] = "Carbon Monoxide" attr["triggered_source"] = "Carbon Monoxide"
self._state = state self._state = state
self._extra_state_attributes = attr self._attr_extra_state_attributes = attr
return self._state return self._state
@property
def extra_state_attributes(self):
"""Return the state attributes of the device."""
return self._extra_state_attributes
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."""
try: try:
@ -176,7 +163,7 @@ class TotalConnectAlarm(CoordinatorEntity, alarm.AlarmControlPanelEntity):
) from error ) from error
except BadResultCodeError as error: except BadResultCodeError as error:
raise HomeAssistantError( raise HomeAssistantError(
f"TotalConnect failed to disarm {self._name}." f"TotalConnect failed to disarm {self.name}."
) from error ) from error
await self.coordinator.async_request_refresh() await self.coordinator.async_request_refresh()
@ -195,7 +182,7 @@ class TotalConnectAlarm(CoordinatorEntity, alarm.AlarmControlPanelEntity):
) from error ) from error
except BadResultCodeError as error: except BadResultCodeError as error:
raise HomeAssistantError( raise HomeAssistantError(
f"TotalConnect failed to arm home {self._name}." f"TotalConnect failed to arm home {self.name}."
) from error ) from error
await self.coordinator.async_request_refresh() await self.coordinator.async_request_refresh()
@ -214,7 +201,7 @@ class TotalConnectAlarm(CoordinatorEntity, alarm.AlarmControlPanelEntity):
) from error ) from error
except BadResultCodeError as error: except BadResultCodeError as error:
raise HomeAssistantError( raise HomeAssistantError(
f"TotalConnect failed to arm away {self._name}." f"TotalConnect failed to arm away {self.name}."
) from error ) from error
await self.coordinator.async_request_refresh() await self.coordinator.async_request_refresh()
@ -233,7 +220,7 @@ class TotalConnectAlarm(CoordinatorEntity, alarm.AlarmControlPanelEntity):
) from error ) from error
except BadResultCodeError as error: except BadResultCodeError as error:
raise HomeAssistantError( raise HomeAssistantError(
f"TotalConnect failed to arm night {self._name}." f"TotalConnect failed to arm night {self.name}."
) from error ) from error
await self.coordinator.async_request_refresh() await self.coordinator.async_request_refresh()
@ -252,7 +239,7 @@ class TotalConnectAlarm(CoordinatorEntity, alarm.AlarmControlPanelEntity):
) from error ) from error
except BadResultCodeError as error: except BadResultCodeError as error:
raise HomeAssistantError( raise HomeAssistantError(
f"TotalConnect failed to arm home instant {self._name}." f"TotalConnect failed to arm home instant {self.name}."
) from error ) from error
await self.coordinator.async_request_refresh() await self.coordinator.async_request_refresh()
@ -271,7 +258,7 @@ class TotalConnectAlarm(CoordinatorEntity, alarm.AlarmControlPanelEntity):
) from error ) from error
except BadResultCodeError as error: except BadResultCodeError as error:
raise HomeAssistantError( raise HomeAssistantError(
f"TotalConnect failed to arm away instant {self._name}." f"TotalConnect failed to arm away instant {self.name}."
) from error ) from error
await self.coordinator.async_request_refresh() await self.coordinator.async_request_refresh()