Skip to content

Commit

Permalink
fix: use alerts last ids during polling (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
xtimmy86x authored Oct 24, 2023
1 parent 8e9f604 commit 6e33bc4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
4 changes: 3 additions & 1 deletion custom_components/econnect_metronet/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __init__(self, connection, config=None):
self._last_ids = {
q.SECTORS: 0,
q.INPUTS: 0,
q.ALERTS: 0,
}

# Load user configuration
Expand Down Expand Up @@ -240,11 +241,12 @@ def update(self):
# Update the _inventory
self._inventory.update({q.SECTORS: sectors["sectors"]})
self._inventory.update({q.INPUTS: inputs["inputs"]})
self._inventory.update({q.ALERTS: alerts})
self._inventory.update({q.ALERTS: alerts["alerts"]})

# Update the _last_ids
self._last_ids[q.SECTORS] = sectors.get("last_id", 0)
self._last_ids[q.INPUTS] = inputs.get("last_id", 0)
self._last_ids[q.ALERTS] = alerts.get("last_id", 0)

# Update the internal state machine (mapping state)
self.state = self.get_state()
Expand Down
2 changes: 1 addition & 1 deletion custom_components/econnect_metronet/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"custom_components.econnect_metronet"
],
"requirements": [
"econnect-python==0.8.0"
"econnect-python==0.8.1"
],
"version": "2.0.0"
}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ classifiers = [
"Programming Language :: Python :: Implementation :: CPython",
]
dependencies = [
"econnect-python==0.8.0",
"econnect-python==0.8.1",
"homeassistant",
]

Expand Down
12 changes: 7 additions & 5 deletions tests/test_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_device_constructor(client):
# Test
assert device._connection == client
assert device._inventory == {}
assert device._last_ids == {10: 0, 9: 0}
assert device._last_ids == {10: 0, 9: 0, 11: 0}
assert device._sectors_home == []
assert device._sectors_night == []
assert device._sectors_vacation == []
Expand All @@ -44,7 +44,7 @@ def test_device_constructor_with_config(client):
# Test
assert device._connection == client
assert device._inventory == {}
assert device._last_ids == {10: 0, 9: 0}
assert device._last_ids == {10: 0, 9: 0, 11: 0}
assert device._sectors_home == [3, 4]
assert device._sectors_night == [1, 2, 3]
assert device._sectors_vacation == [5, 3]
Expand Down Expand Up @@ -230,11 +230,12 @@ def test_device_has_updates(client, mocker):
device.connect("username", "password")
device._last_ids[q.SECTORS] = 20
device._last_ids[q.INPUTS] = 20
device._last_ids[q.ALERTS] = 20
mocker.spy(device._connection, "poll")
# Test
device.has_updates()
assert device._connection.poll.call_count == 1
assert {9: 20, 10: 20} in device._connection.poll.call_args[0]
assert {9: 20, 10: 20, 11: 20} in device._connection.poll.call_args[0]


def test_device_has_updates_ids_immutable(client, mocker):
Expand Down Expand Up @@ -265,7 +266,7 @@ def test_device_has_updates_errors(client, mocker):
with pytest.raises(HTTPError):
device.has_updates()
assert device._connection.poll.call_count == 1
assert {9: 0, 10: 0} == device._last_ids
assert {9: 0, 10: 0, 11: 0} == device._last_ids


def test_device_has_updates_parse_errors(client, mocker):
Expand All @@ -278,7 +279,7 @@ def test_device_has_updates_parse_errors(client, mocker):
with pytest.raises(ParseError):
device.has_updates()
assert device._connection.poll.call_count == 1
assert {9: 0, 10: 0} == device._last_ids
assert {9: 0, 10: 0, 11: 0} == device._last_ids


def test_device_update_success(client, mocker):
Expand All @@ -292,6 +293,7 @@ def test_device_update_success(client, mocker):
assert device._last_ids == {
9: 4,
10: 42,
11: 1,
}


Expand Down

0 comments on commit 6e33bc4

Please sign in to comment.