Use attributes in risco alarm (#74117)

pull/74136/head
epenet 2022-06-28 15:26:50 +02:00 committed by GitHub
parent 0063274f83
commit 670af6fde3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 28 deletions

View File

@ -20,6 +20,7 @@ from homeassistant.const import (
STATE_ALARM_TRIGGERED,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import (
@ -65,44 +66,46 @@ async def async_setup_entry(
class RiscoAlarm(AlarmControlPanelEntity, RiscoEntity):
"""Representation of a Risco partition."""
_attr_code_format = CodeFormat.NUMBER
def __init__(self, coordinator, partition_id, code, options):
"""Init the partition."""
super().__init__(coordinator)
self._partition_id = partition_id
self._partition = self.coordinator.data.partitions[self._partition_id]
self._code = code
self._code_arm_required = options[CONF_CODE_ARM_REQUIRED]
self._attr_code_arm_required = options[CONF_CODE_ARM_REQUIRED]
self._code_disarm_required = options[CONF_CODE_DISARM_REQUIRED]
self._risco_to_ha = options[CONF_RISCO_STATES_TO_HA]
self._ha_to_risco = options[CONF_HA_STATES_TO_RISCO]
self._supported_states = 0
self._attr_supported_features = 0
for state in self._ha_to_risco:
self._supported_states |= STATES_TO_SUPPORTED_FEATURES[state]
self._attr_supported_features |= STATES_TO_SUPPORTED_FEATURES[state]
def _get_data_from_coordinator(self):
self._partition = self.coordinator.data.partitions[self._partition_id]
@property
def device_info(self):
def device_info(self) -> DeviceInfo:
"""Return device info for this device."""
return {
"identifiers": {(DOMAIN, self.unique_id)},
"name": self.name,
"manufacturer": "Risco",
}
return DeviceInfo(
identifiers={(DOMAIN, self.unique_id)},
name=self.name,
manufacturer="Risco",
)
@property
def name(self):
def name(self) -> str:
"""Return the name of the partition."""
return f"Risco {self._risco.site_name} Partition {self._partition_id}"
@property
def unique_id(self):
def unique_id(self) -> str:
"""Return a unique id for that partition."""
return f"{self._risco.site_uuid}_{self._partition_id}"
@property
def state(self):
def state(self) -> str | None:
"""Return the state of the device."""
if self._partition.triggered:
return STATE_ALARM_TRIGGERED
@ -121,21 +124,6 @@ class RiscoAlarm(AlarmControlPanelEntity, RiscoEntity):
return None
@property
def supported_features(self):
"""Return the list of supported features."""
return self._supported_states
@property
def code_arm_required(self):
"""Whether the code is required for arm actions."""
return self._code_arm_required
@property
def code_format(self):
"""Return one or more digits/characters."""
return CodeFormat.NUMBER
def _validate_code(self, code):
"""Validate given code."""
return code == self._code
@ -164,7 +152,7 @@ class RiscoAlarm(AlarmControlPanelEntity, RiscoEntity):
await self._arm(STATE_ALARM_ARMED_CUSTOM_BYPASS, code)
async def _arm(self, mode, code):
if self._code_arm_required and not self._validate_code(code):
if self.code_arm_required and not self._validate_code(code):
_LOGGER.warning("Wrong code entered for %s", mode)
return