Use attributes in nx584 alarm (#74105)

This commit is contained in:
epenet 2022-06-28 13:42:43 +02:00 committed by GitHub
parent dac8f242e0
commit c1f621e9c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -55,9 +55,9 @@ async def async_setup_platform(
discovery_info: DiscoveryInfoType | None = None, discovery_info: DiscoveryInfoType | None = None,
) -> None: ) -> None:
"""Set up the NX584 platform.""" """Set up the NX584 platform."""
name = config.get(CONF_NAME) name: str = config[CONF_NAME]
host = config.get(CONF_HOST) host: str = config[CONF_HOST]
port = config.get(CONF_PORT) port: int = config[CONF_PORT]
url = f"http://{host}:{port}" url = f"http://{host}:{port}"
@ -92,33 +92,19 @@ async def async_setup_platform(
class NX584Alarm(alarm.AlarmControlPanelEntity): class NX584Alarm(alarm.AlarmControlPanelEntity):
"""Representation of a NX584-based alarm panel.""" """Representation of a NX584-based alarm panel."""
_attr_code_format = alarm.CodeFormat.NUMBER
_attr_state: str | None
_attr_supported_features = ( _attr_supported_features = (
AlarmControlPanelEntityFeature.ARM_HOME AlarmControlPanelEntityFeature.ARM_HOME
| AlarmControlPanelEntityFeature.ARM_AWAY | AlarmControlPanelEntityFeature.ARM_AWAY
) )
def __init__(self, name, alarm_client, url): def __init__(self, name: str, alarm_client: client.Client, url: str) -> None:
"""Init the nx584 alarm panel.""" """Init the nx584 alarm panel."""
self._name = name self._attr_name = name
self._state = None
self._alarm = alarm_client self._alarm = alarm_client
self._url = url self._url = url
@property
def name(self):
"""Return the name of the device."""
return self._name
@property
def code_format(self):
"""Return one or more digits/characters."""
return alarm.CodeFormat.NUMBER
@property
def state(self):
"""Return the state of the device."""
return self._state
def update(self) -> None: def update(self) -> None:
"""Process new events from panel.""" """Process new events from panel."""
try: try:
@ -129,11 +115,11 @@ class NX584Alarm(alarm.AlarmControlPanelEntity):
"Unable to connect to %(host)s: %(reason)s", "Unable to connect to %(host)s: %(reason)s",
{"host": self._url, "reason": ex}, {"host": self._url, "reason": ex},
) )
self._state = None self._attr_state = None
zones = [] zones = []
except IndexError: except IndexError:
_LOGGER.error("NX584 reports no partitions") _LOGGER.error("NX584 reports no partitions")
self._state = None self._attr_state = None
zones = [] zones = []
bypassed = False bypassed = False
@ -147,15 +133,15 @@ class NX584Alarm(alarm.AlarmControlPanelEntity):
break break
if not part["armed"]: if not part["armed"]:
self._state = STATE_ALARM_DISARMED self._attr_state = STATE_ALARM_DISARMED
elif bypassed: elif bypassed:
self._state = STATE_ALARM_ARMED_HOME self._attr_state = STATE_ALARM_ARMED_HOME
else: else:
self._state = STATE_ALARM_ARMED_AWAY self._attr_state = STATE_ALARM_ARMED_AWAY
for flag in part["condition_flags"]: for flag in part["condition_flags"]:
if flag == "Siren on": if flag == "Siren on":
self._state = STATE_ALARM_TRIGGERED self._attr_state = STATE_ALARM_TRIGGERED
def alarm_disarm(self, code: str | None = None) -> None: def alarm_disarm(self, code: str | None = None) -> None:
"""Send disarm command.""" """Send disarm command."""
@ -169,10 +155,10 @@ class NX584Alarm(alarm.AlarmControlPanelEntity):
"""Send arm away command.""" """Send arm away command."""
self._alarm.arm("exit") self._alarm.arm("exit")
def alarm_bypass(self, zone): def alarm_bypass(self, zone: int) -> None:
"""Send bypass command.""" """Send bypass command."""
self._alarm.set_bypass(zone, True) self._alarm.set_bypass(zone, True)
def alarm_unbypass(self, zone): def alarm_unbypass(self, zone: int) -> None:
"""Send bypass command.""" """Send bypass command."""
self._alarm.set_bypass(zone, False) self._alarm.set_bypass(zone, False)