Skip to content

Commit

Permalink
Use attributes in totalconnect alarm (home-assistant#74113)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Jun 28, 2022
1 parent 3b30d8a commit 4335caf
Showing 1 changed file with 21 additions and 34 deletions.
55 changes: 21 additions & 34 deletions homeassistant/components/totalconnect/alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_platform
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

Expand Down Expand Up @@ -85,43 +86,33 @@ def __init__(self, coordinator, name, location_id, partition_id):
self._partition = self._location.partitions[partition_id]
self._device = self._location.devices[self._location.security_device_id]
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
for most users with new support for partitions.
Add _# for partition 2 and beyond.
"""
if partition_id == 1:
self._name = name
self._unique_id = f"{location_id}"
self._attr_name = name
self._attr_unique_id = f"{location_id}"
else:
self._name = f"{name} partition {partition_id}"
self._unique_id = f"{location_id}_{partition_id}"
self._attr_name = f"{name} partition {partition_id}"
self._attr_unique_id = f"{location_id}_{partition_id}"

@property
def name(self):
"""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):
def device_info(self) -> DeviceInfo:
"""Return device info."""
return {
"identifiers": {(DOMAIN, self._device.serial_number)},
"name": self._device.name,
}
return DeviceInfo(
identifiers={(DOMAIN, self._device.serial_number)},
name=self._device.name,
)

@property
def state(self):
def state(self) -> str | None:
"""Return the state of the device."""
attr = {
"location_name": self._name,
"location_name": self.name,
"location_id": self._location_id,
"partition": self._partition_id,
"ac_loss": self._location.ac_loss,
Expand All @@ -131,6 +122,7 @@ def state(self):
"triggered_zone": None,
}

state = None
if self._partition.arming_state.is_disarmed():
state = STATE_ALARM_DISARMED
elif self._partition.arming_state.is_armed_night():
Expand All @@ -156,15 +148,10 @@ def state(self):
attr["triggered_source"] = "Carbon Monoxide"

self._state = state
self._extra_state_attributes = attr
self._attr_extra_state_attributes = attr

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:
"""Send disarm command."""
try:
Expand All @@ -176,7 +163,7 @@ async def async_alarm_disarm(self, code: str | None = None) -> None:
) from error
except BadResultCodeError as error:
raise HomeAssistantError(
f"TotalConnect failed to disarm {self._name}."
f"TotalConnect failed to disarm {self.name}."
) from error
await self.coordinator.async_request_refresh()

Expand All @@ -195,7 +182,7 @@ async def async_alarm_arm_home(self, code: str | None = None) -> None:
) from error
except BadResultCodeError as error:
raise HomeAssistantError(
f"TotalConnect failed to arm home {self._name}."
f"TotalConnect failed to arm home {self.name}."
) from error
await self.coordinator.async_request_refresh()

Expand All @@ -214,7 +201,7 @@ async def async_alarm_arm_away(self, code: str | None = None) -> None:
) from error
except BadResultCodeError as error:
raise HomeAssistantError(
f"TotalConnect failed to arm away {self._name}."
f"TotalConnect failed to arm away {self.name}."
) from error
await self.coordinator.async_request_refresh()

Expand All @@ -233,7 +220,7 @@ async def async_alarm_arm_night(self, code: str | None = None) -> None:
) from error
except BadResultCodeError as error:
raise HomeAssistantError(
f"TotalConnect failed to arm night {self._name}."
f"TotalConnect failed to arm night {self.name}."
) from error
await self.coordinator.async_request_refresh()

Expand All @@ -252,7 +239,7 @@ async def async_alarm_arm_home_instant(self, code: str | None = None) -> None:
) from error
except BadResultCodeError as error:
raise HomeAssistantError(
f"TotalConnect failed to arm home instant {self._name}."
f"TotalConnect failed to arm home instant {self.name}."
) from error
await self.coordinator.async_request_refresh()

Expand All @@ -271,7 +258,7 @@ async def async_alarm_arm_away_instant(self, code: str | None = None) -> None:
) from error
except BadResultCodeError as error:
raise HomeAssistantError(
f"TotalConnect failed to arm away instant {self._name}."
f"TotalConnect failed to arm away instant {self.name}."
) from error
await self.coordinator.async_request_refresh()

Expand Down

0 comments on commit 4335caf

Please sign in to comment.