Skip to content

Commit

Permalink
fix(device): avoid state update while arming or disarming (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
palazzem authored Mar 7, 2024
1 parent c8a2221 commit cc43a7a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
9 changes: 7 additions & 2 deletions custom_components/econnect_metronet/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
STATE_ALARM_ARMED_HOME,
STATE_ALARM_ARMED_NIGHT,
STATE_ALARM_ARMED_VACATION,
STATE_ALARM_ARMING,
STATE_ALARM_DISARMED,
STATE_ALARM_DISARMING,
STATE_UNAVAILABLE,
)
from requests.exceptions import HTTPError
Expand Down Expand Up @@ -221,6 +223,11 @@ def get_state(self):
Returns:
str: One of the predefined HA alarm states.
"""
# If the system is arming or disarming, return the current state
# to prevent the state from being updated while the system is in transition.
if self.state in [STATE_ALARM_ARMING, STATE_ALARM_DISARMING]:
return self.state

sectors_armed = dict(self.items(q.SECTORS, status=True))
if not sectors_armed:
return STATE_ALARM_DISARMED
Expand Down Expand Up @@ -340,7 +347,6 @@ def arm(self, code, sectors=None):

with self._connection.lock(code, user_id=user_id):
self._connection.arm(sectors=sectors)
self.state = STATE_ALARM_ARMED_AWAY
except HTTPError as err:
_LOGGER.error(f"Device | Error while arming the system: {err.response.text}")
raise err
Expand Down Expand Up @@ -368,7 +374,6 @@ def disarm(self, code, sectors=None):

with self._connection.lock(code, user_id=user_id):
self._connection.disarm(sectors=sectors)
self.state = STATE_ALARM_DISARMED
except HTTPError as err:
_LOGGER.error(f"Device | Error while disarming the system: {err.response.text}")
raise err
Expand Down
24 changes: 24 additions & 0 deletions tests/test_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
STATE_ALARM_ARMED_HOME,
STATE_ALARM_ARMED_NIGHT,
STATE_ALARM_ARMED_VACATION,
STATE_ALARM_ARMING,
STATE_ALARM_DISARMED,
STATE_ALARM_DISARMING,
STATE_UNAVAILABLE,
)
from requests.exceptions import HTTPError
Expand Down Expand Up @@ -1604,6 +1606,28 @@ def test_get_state_armed_away_with_config(alarm_device):
assert alarm_device.get_state() == STATE_ALARM_ARMED_AWAY


def test_get_state_while_disarming(alarm_device):
# Ensure that the state is not changed while disarming
# Regression test for: https://github.com/palazzem/ha-econnect-alarm/issues/154
alarm_device._sectors_home = []
alarm_device._sectors_night = []
alarm_device._inventory = {9: {}}
alarm_device.state = STATE_ALARM_DISARMING
# Test
assert alarm_device.get_state() == STATE_ALARM_DISARMING


def test_get_state_while_arming(alarm_device):
# Ensure that the state is not changed while arming
# Regression test for: https://github.com/palazzem/ha-econnect-alarm/issues/154
alarm_device._sectors_home = []
alarm_device._sectors_night = []
alarm_device._inventory = {9: {}}
alarm_device.state = STATE_ALARM_ARMING
# Test
assert alarm_device.get_state() == STATE_ALARM_ARMING


class TestTurnOff:
def test_required_authentication(self, alarm_device, mocker, caplog):
# Ensure that API calls are not made when the output requires authentication
Expand Down

0 comments on commit cc43a7a

Please sign in to comment.