Add options flow for SimpliSafe (#32631)

* Fix bug where SimpliSafe ignored code from UI

* Fix tests

* Add options flow

* Fix tests

* Code review

* Code review

* Code review
This commit is contained in:
Aaron Bach 2020-03-12 23:00:00 -06:00 committed by GitHub
parent 94b6ab2862
commit 4f0997f6e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 186 additions and 78 deletions

View file

@ -67,10 +67,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
"""Set up a SimpliSafe alarm control panel based on a config entry."""
simplisafe = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id]
async_add_entities(
[
SimpliSafeAlarm(simplisafe, system, entry.data.get(CONF_CODE))
for system in simplisafe.systems.values()
],
[SimpliSafeAlarm(simplisafe, system) for system in simplisafe.systems.values()],
True,
)
@ -78,11 +75,10 @@ async def async_setup_entry(hass, entry, async_add_entities):
class SimpliSafeAlarm(SimpliSafeEntity, AlarmControlPanel):
"""Representation of a SimpliSafe alarm."""
def __init__(self, simplisafe, system, code):
def __init__(self, simplisafe, system):
"""Initialize the SimpliSafe alarm."""
super().__init__(simplisafe, system, "Alarm Control Panel")
self._changed_by = None
self._code = code
self._last_event = None
if system.alarm_going_off:
@ -125,9 +121,11 @@ class SimpliSafeAlarm(SimpliSafeEntity, AlarmControlPanel):
@property
def code_format(self):
"""Return one or more digits/characters."""
if not self._code:
if not self._simplisafe.options.get(CONF_CODE):
return None
if isinstance(self._code, str) and re.search("^\\d+$", self._code):
if isinstance(self._simplisafe.options[CONF_CODE], str) and re.search(
"^\\d+$", self._simplisafe.options[CONF_CODE]
):
return FORMAT_NUMBER
return FORMAT_TEXT
@ -141,16 +139,23 @@ class SimpliSafeAlarm(SimpliSafeEntity, AlarmControlPanel):
"""Return the list of supported features."""
return SUPPORT_ALARM_ARM_HOME | SUPPORT_ALARM_ARM_AWAY
def _validate_code(self, code, state):
"""Validate given code."""
check = self._code is None or code == self._code
if not check:
_LOGGER.warning("Wrong code entered for %s", state)
return check
@callback
def _is_code_valid(self, code, state):
"""Validate that a code matches the required one."""
if not self._simplisafe.options.get(CONF_CODE):
return True
if not code or code != self._simplisafe.options[CONF_CODE]:
_LOGGER.warning(
"Incorrect alarm code entered (target state: %s): %s", state, code
)
return False
return True
async def async_alarm_disarm(self, code=None):
"""Send disarm command."""
if not self._validate_code(code, "disarming"):
if not self._is_code_valid(code, STATE_ALARM_DISARMED):
return
try:
@ -163,7 +168,7 @@ class SimpliSafeAlarm(SimpliSafeEntity, AlarmControlPanel):
async def async_alarm_arm_home(self, code=None):
"""Send arm home command."""
if not self._validate_code(code, "arming home"):
if not self._is_code_valid(code, STATE_ALARM_ARMED_HOME):
return
try:
@ -176,7 +181,7 @@ class SimpliSafeAlarm(SimpliSafeEntity, AlarmControlPanel):
async def async_alarm_arm_away(self, code=None):
"""Send arm away command."""
if not self._validate_code(code, "arming away"):
if not self._is_code_valid(code, STATE_ALARM_ARMED_AWAY):
return
try: