Skip to content

Commit

Permalink
refactor:add device.item for query the inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
xtimmy86x committed Oct 18, 2023
1 parent 4fcb430 commit 4ebf7cf
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion custom_components/econnect_metronet/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def is_on(self) -> bool:
"""Return the binary sensor status (on/off)."""
for item_id, item in self._device.items(query.ALERTS):
if item_id == self._alert_id:
if item_id == 1:
if item.get("name") == "anomalies_led":
return True if item.get("status") > 1 else False
else:
return bool(item.get("status", False))
Expand Down
1 change: 0 additions & 1 deletion custom_components/econnect_metronet/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ def alerts(self):
>>> list(device.alerts)
[{1: "alarm_led", 2: "anomalies_led"}]
"""
# yield from self._inventory.get(q.ALERTS, {}).items()
for alert_id, item in self._inventory.get(q.ALERTS, {}).items():
yield alert_id, item["name"]

Expand Down
8 changes: 8 additions & 0 deletions tests/test_binary_sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ def test_binary_sensor_anomalies_led_is_off(self, hass, config_entry, alarm_devi
entity = AlertSensor("anomalies_led", 1, config_entry, "anomalies_led", coordinator, alarm_device)
assert entity.is_on is False

def test_binary_sensor_anomalies_led_is_on(self, hass, config_entry, alarm_device):
alarm_device.connect("username", "password")
alarm_device.update()
alarm_device._inventory[11][1]["status"] = 2
coordinator = DataUpdateCoordinator(hass, logging.getLogger(__name__), name="econnect_metronet")
entity = AlertSensor("anomalies_led", 1, config_entry, "anomalies_led", coordinator, alarm_device)
assert entity.is_on is True

def test_binary_sensor_name(hass, config_entry, alarm_device):
# Ensure the alert has the right translation key
coordinator = DataUpdateCoordinator(hass, logging.getLogger(__name__), name="econnect_metronet")
Expand Down
7 changes: 3 additions & 4 deletions tests/test_coordinator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from datetime import timedelta

import pytest
from elmo import query as q
from elmo.api.exceptions import CredentialError, InvalidToken
from homeassistant.exceptions import ConfigEntryNotReady
from requests.exceptions import HTTPError
Expand Down Expand Up @@ -35,7 +34,7 @@ async def test_coordinator_async_update_with_data(mocker, coordinator):
# Test
await coordinator.async_refresh()
assert coordinator.data == {
q.ALERTS: {
11: {
0: {"name": "alarm_led", "status": 0},
1: {"name": "anomalies_led", "status": 1},
2: {"name": "device_failure", "status": 0},
Expand All @@ -62,12 +61,12 @@ async def test_coordinator_async_update_with_data(mocker, coordinator):
23: {"name": "system_test", "status": 0},
24: {"name": "tamper_led", "status": 0},
},
q.INPUTS: {
10: {
0: {"element": 1, "excluded": False, "id": 1, "index": 0, "name": "Entryway Sensor", "status": True},
1: {"element": 2, "excluded": False, "id": 2, "index": 1, "name": "Outdoor Sensor 1", "status": True},
2: {"element": 3, "excluded": True, "id": 3, "index": 2, "name": "Outdoor Sensor 2", "status": False},
},
q.SECTORS: {
9: {
0: {"element": 1, "excluded": False, "id": 1, "index": 0, "name": "S1 Living Room", "status": True},
1: {"element": 2, "excluded": False, "id": 2, "index": 1, "name": "S2 Bedroom", "status": True},
2: {"element": 3, "excluded": False, "id": 3, "index": 2, "name": "S3 Outdoor", "status": False},
Expand Down
13 changes: 12 additions & 1 deletion tests/test_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def test_without_status(self, alarm_device):
alarm_device.update()
assert dict(alarm_device.items(q.ALERTS)) == alerts

def test_with_status(self, alarm_device):
def test_with_status_1(self, alarm_device):
"""Verify that querying items with specifying a status works correctly"""
alarm_device.connect("username", "password")
alerts_1 = {
Expand All @@ -175,6 +175,17 @@ def test_with_status(self, alarm_device):
alarm_device.update()
assert dict(alarm_device.items(q.ALERTS, status=1)) == alerts_1

def test_with_status_true(self, alarm_device):
"""Verify that querying items with specifying a status works correctly"""
alarm_device.connect("username", "password")
alerts_true = {
1: {"name": "anomalies_led", "status": True},
7: {"name": "device_tamper", "status": True},
}
# Test
alarm_device.update()
assert dict(alarm_device.items(q.ALERTS, status=1)) == alerts_true

def test_without_inventory(self, alarm_device):
"""Verify that querying items without inventory populated works correctly"""
alarm_device._inventory = {}
Expand Down

0 comments on commit 4ebf7cf

Please sign in to comment.