Define alarm_control_panel entity attributes as class variables (#51120)

* Define alarm_control_panel entity attributes as class variables

* Example Verisure

* Remove redundant AttributeError
This commit is contained in:
Franck Nijhof 2021-05-28 08:29:01 +02:00 committed by GitHub
parent e9b09325c9
commit 0b15f3aa98
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 22 deletions

View file

@ -1,7 +1,6 @@
"""Component to interface with an alarm control panel.""" """Component to interface with an alarm control panel."""
from __future__ import annotations from __future__ import annotations
from abc import abstractmethod
from datetime import timedelta from datetime import timedelta
import logging import logging
from typing import Any, Final, final from typing import Any, Final, final
@ -113,20 +112,25 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
class AlarmControlPanelEntity(Entity): class AlarmControlPanelEntity(Entity):
"""An abstract class for alarm control entities.""" """An abstract class for alarm control entities."""
_attr_changed_by: str | None = None
_attr_code_arm_required: bool = True
_attr_code_format: str | None = None
_attr_supported_features: int
@property @property
def code_format(self) -> str | None: def code_format(self) -> str | None:
"""Regex for code format or None if no code is required.""" """Regex for code format or None if no code is required."""
return None return self._attr_code_format
@property @property
def changed_by(self) -> str | None: def changed_by(self) -> str | None:
"""Last change triggered by.""" """Last change triggered by."""
return None return self._attr_changed_by
@property @property
def code_arm_required(self) -> bool: def code_arm_required(self) -> bool:
"""Whether the code is required for arm actions.""" """Whether the code is required for arm actions."""
return True return self._attr_code_arm_required
def alarm_disarm(self, code: str | None = None) -> None: def alarm_disarm(self, code: str | None = None) -> None:
"""Send disarm command.""" """Send disarm command."""
@ -177,9 +181,9 @@ class AlarmControlPanelEntity(Entity):
await self.hass.async_add_executor_job(self.alarm_arm_custom_bypass, code) await self.hass.async_add_executor_job(self.alarm_arm_custom_bypass, code)
@property @property
@abstractmethod
def supported_features(self) -> int: def supported_features(self) -> int:
"""Return the list of supported features.""" """Return the list of supported features."""
return self._attr_supported_features
@final @final
@property @property

View file

@ -35,8 +35,9 @@ class VerisureAlarm(CoordinatorEntity, AlarmControlPanelEntity):
coordinator: VerisureDataUpdateCoordinator coordinator: VerisureDataUpdateCoordinator
_attr_code_format = FORMAT_NUMBER
_attr_name = "Verisure Alarm" _attr_name = "Verisure Alarm"
_changed_by: str | None = None _attr_supported_features = SUPPORT_ALARM_ARM_HOME | SUPPORT_ALARM_ARM_AWAY
@property @property
def device_info(self) -> DeviceInfo: def device_info(self) -> DeviceInfo:
@ -48,26 +49,11 @@ class VerisureAlarm(CoordinatorEntity, AlarmControlPanelEntity):
"identifiers": {(DOMAIN, self.coordinator.entry.data[CONF_GIID])}, "identifiers": {(DOMAIN, self.coordinator.entry.data[CONF_GIID])},
} }
@property
def supported_features(self) -> int:
"""Return the list of supported features."""
return SUPPORT_ALARM_ARM_HOME | SUPPORT_ALARM_ARM_AWAY
@property @property
def unique_id(self) -> str: def unique_id(self) -> str:
"""Return the unique ID for this entity.""" """Return the unique ID for this entity."""
return self.coordinator.entry.data[CONF_GIID] return self.coordinator.entry.data[CONF_GIID]
@property
def code_format(self) -> str:
"""Return one or more digits/characters."""
return FORMAT_NUMBER
@property
def changed_by(self) -> str | None:
"""Return the last change triggered by."""
return self._changed_by
async def _async_set_arm_state(self, state: str, code: str | None = None) -> None: async def _async_set_arm_state(self, state: str, code: str | None = None) -> None:
"""Send set arm state command.""" """Send set arm state command."""
arm_state = await self.hass.async_add_executor_job( arm_state = await self.hass.async_add_executor_job(
@ -102,7 +88,7 @@ class VerisureAlarm(CoordinatorEntity, AlarmControlPanelEntity):
self._attr_state = ALARM_STATE_TO_HA.get( self._attr_state = ALARM_STATE_TO_HA.get(
self.coordinator.data["alarm"]["statusType"] self.coordinator.data["alarm"]["statusType"]
) )
self._changed_by = self.coordinator.data["alarm"].get("name") self._attr_changed_by = self.coordinator.data["alarm"].get("name")
super()._handle_coordinator_update() super()._handle_coordinator_update()
async def async_added_to_hass(self) -> None: async def async_added_to_hass(self) -> None: