Skip to content

Commit

Permalink
fix(device): discard Cloud APIs empty states (connection resets) (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
palazzem authored Mar 5, 2024
1 parent d081596 commit 0d14aec
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
6 changes: 6 additions & 0 deletions custom_components/econnect_metronet/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,12 @@ def update(self):
self.connected = False
raise err

# `last_id` equal to 1 means the connection has been reset and the update
# is an empty state. See: https://github.com/palazzem/ha-econnect-alarm/issues/148
if sectors.get("last_id") == 1:
_LOGGER.debug("Device | The connection has been reset, skipping the update")
return self._inventory

# Update the _inventory
self.connected = True
self._inventory.update({q.SECTORS: sectors["sectors"]})
Expand Down
59 changes: 59 additions & 0 deletions tests/test_devices.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import responses
from elmo import query as q
from elmo.api.exceptions import CodeError, CredentialError, LockError, ParseError
from homeassistant.const import (
Expand All @@ -22,6 +23,8 @@
)
from custom_components.econnect_metronet.devices import AlarmDevice

from .fixtures import responses as r


def test_device_constructor(client):
"""Should initialize defaults attributes to run properly."""
Expand Down Expand Up @@ -601,6 +604,62 @@ def test_device_inventory_update_managed_sectors(alarm_device):
}


def test_device_inventory_update_after_connection_reset(mocker, alarm_device):
# Ensure that after a connection reset (last_id == 1), the inventory is not updated
# Regression test for: https://github.com/palazzem/ha-econnect-alarm/issues/148
AREAS = """[
{
"Active": false,
"ActivePartial": false,
"Max": false,
"Activable": false,
"ActivablePartial": false,
"InUse": true,
"Id": 1,
"Index": 0,
"Element": 1,
"CommandId": 0,
"InProgress": false
},
{
"Active": false,
"ActivePartial": false,
"Max": false,
"Activable": false,
"ActivablePartial": false,
"InUse": true,
"Id": 1,
"Index": 1,
"Element": 2,
"CommandId": 0,
"InProgress": false
},
{
"Active": false,
"ActivePartial": false,
"Max": false,
"Activable": false,
"ActivablePartial": false,
"InUse": true,
"Id": 1,
"Index": 2,
"Element": 3,
"CommandId": 0,
"InProgress": false
}
]"""
# Test
with responses.RequestsMock() as server:
server.add(responses.POST, "https://example.com/api/areas", body=AREAS, status=200)
server.add(responses.POST, "https://example.com/api/inputs", body=r.INPUTS, status=200)
server.add(responses.POST, "https://example.com/api/outputs", body=r.OUTPUTS, status=200)
server.add(responses.POST, "https://example.com/api/statusadv", body=r.STATUS, status=200)
inventory = alarm_device.update()
assert inventory[q.SECTORS][0]["status"] is True
assert inventory[q.SECTORS][1]["status"] is True
assert inventory[q.SECTORS][2]["status"] is False


class TestInputsView:
def test_property_populated(self, alarm_device):
"""Should check if the device property is correctly populated"""
Expand Down

0 comments on commit 0d14aec

Please sign in to comment.