Skip to content

Commit

Permalink
Use attributes in manual alarm (home-assistant#74122)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Jun 29, 2022
1 parent 0769b33 commit 6dc6e71
Showing 1 changed file with 22 additions and 38 deletions.
60 changes: 22 additions & 38 deletions homeassistant/components/manual/alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import datetime
import logging
import re
from typing import Any

import voluptuous as vol

Expand Down Expand Up @@ -185,6 +186,16 @@ class ManualAlarm(alarm.AlarmControlPanelEntity, RestoreEntity):
A trigger_time of zero disables the alarm_trigger service.
"""

_attr_should_poll = False
_attr_supported_features = (
AlarmControlPanelEntityFeature.ARM_HOME
| AlarmControlPanelEntityFeature.ARM_AWAY
| AlarmControlPanelEntityFeature.ARM_NIGHT
| AlarmControlPanelEntityFeature.ARM_VACATION
| AlarmControlPanelEntityFeature.TRIGGER
| AlarmControlPanelEntityFeature.ARM_CUSTOM_BYPASS
)

def __init__(
self,
hass,
Expand All @@ -198,13 +209,13 @@ def __init__(
"""Init the manual alarm panel."""
self._state = STATE_ALARM_DISARMED
self._hass = hass
self._name = name
self._attr_name = name
if code_template:
self._code = code_template
self._code.hass = hass
else:
self._code = code or None
self._code_arm_required = code_arm_required
self._attr_code_arm_required = code_arm_required
self._disarm_after_trigger = disarm_after_trigger
self._previous_state = self._state
self._state_ts = None
Expand All @@ -222,17 +233,7 @@ def __init__(
}

@property
def should_poll(self):
"""Return the polling state."""
return False

@property
def name(self):
"""Return the name of the device."""
return self._name

@property
def state(self):
def state(self) -> str:
"""Return the state of the device."""
if self._state == STATE_ALARM_TRIGGERED:
if self._within_pending_time(self._state):
Expand All @@ -253,18 +254,6 @@ def state(self):

return self._state

@property
def supported_features(self) -> int:
"""Return the list of supported features."""
return (
AlarmControlPanelEntityFeature.ARM_HOME
| AlarmControlPanelEntityFeature.ARM_AWAY
| AlarmControlPanelEntityFeature.ARM_NIGHT
| AlarmControlPanelEntityFeature.ARM_VACATION
| AlarmControlPanelEntityFeature.TRIGGER
| AlarmControlPanelEntityFeature.ARM_CUSTOM_BYPASS
)

@property
def _active_state(self):
"""Get the current state."""
Expand All @@ -289,19 +278,14 @@ def _within_pending_time(self, state):
return self._state_ts + self._pending_time(state) > dt_util.utcnow()

@property
def code_format(self):
def code_format(self) -> alarm.CodeFormat | None:
"""Return one or more digits/characters."""
if self._code is None:
return None
if isinstance(self._code, str) and re.search("^\\d+$", self._code):
return alarm.CodeFormat.NUMBER
return alarm.CodeFormat.TEXT

@property
def code_arm_required(self):
"""Whether the code is required for arm actions."""
return self._code_arm_required

def alarm_disarm(self, code: str | None = None) -> None:
"""Send disarm command."""
if not self._validate_code(code, STATE_ALARM_DISARMED):
Expand All @@ -313,7 +297,7 @@ def alarm_disarm(self, code: str | None = None) -> None:

def alarm_arm_home(self, code: str | None = None) -> None:
"""Send arm home command."""
if self._code_arm_required and not self._validate_code(
if self.code_arm_required and not self._validate_code(
code, STATE_ALARM_ARMED_HOME
):
return
Expand All @@ -322,7 +306,7 @@ def alarm_arm_home(self, code: str | None = None) -> None:

def alarm_arm_away(self, code: str | None = None) -> None:
"""Send arm away command."""
if self._code_arm_required and not self._validate_code(
if self.code_arm_required and not self._validate_code(
code, STATE_ALARM_ARMED_AWAY
):
return
Expand All @@ -331,7 +315,7 @@ def alarm_arm_away(self, code: str | None = None) -> None:

def alarm_arm_night(self, code: str | None = None) -> None:
"""Send arm night command."""
if self._code_arm_required and not self._validate_code(
if self.code_arm_required and not self._validate_code(
code, STATE_ALARM_ARMED_NIGHT
):
return
Expand All @@ -340,7 +324,7 @@ def alarm_arm_night(self, code: str | None = None) -> None:

def alarm_arm_vacation(self, code: str | None = None) -> None:
"""Send arm vacation command."""
if self._code_arm_required and not self._validate_code(
if self.code_arm_required and not self._validate_code(
code, STATE_ALARM_ARMED_VACATION
):
return
Expand All @@ -349,7 +333,7 @@ def alarm_arm_vacation(self, code: str | None = None) -> None:

def alarm_arm_custom_bypass(self, code: str | None = None) -> None:
"""Send arm custom bypass command."""
if self._code_arm_required and not self._validate_code(
if self.code_arm_required and not self._validate_code(
code, STATE_ALARM_ARMED_CUSTOM_BYPASS
):
return
Expand All @@ -367,7 +351,7 @@ def alarm_trigger(self, code: str | None = None) -> None:
return
self._update_state(STATE_ALARM_TRIGGERED)

def _update_state(self, state):
def _update_state(self, state: str) -> None:
"""Update the state."""
if self._state == state:
return
Expand Down Expand Up @@ -414,7 +398,7 @@ def _validate_code(self, code, state):
return check

@property
def extra_state_attributes(self):
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the state attributes."""
if self.state in (STATE_ALARM_PENDING, STATE_ALARM_ARMING):
return {
Expand Down

0 comments on commit 6dc6e71

Please sign in to comment.