Skip to content

Commit

Permalink
feat: create InputSensors with _inventory (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
xtimmy86x authored Oct 19, 2023
1 parent bb7f0b7 commit b54375b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
13 changes: 7 additions & 6 deletions custom_components/econnect_metronet/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ async def async_setup_entry(
unique_id = f"{entry.entry_id}_{DOMAIN}_{query.SECTORS}_{sector_id}"
sensors.append(SectorSensor(unique_id, sector_id, entry, name, coordinator, device))

for sensor_id, name in inventory[query.INPUTS].items():
unique_id = f"{entry.entry_id}_{DOMAIN}_{query.INPUTS}_{sensor_id}"
sensors.append(InputSensor(unique_id, sensor_id, entry, name, coordinator, device))
# Iterate through the inputs of the provided device and create InputSensor objects
for input_id, name in device.inputs:
unique_id = f"{entry.entry_id}_{DOMAIN}_{query.INPUTS}_{input_id}"
sensors.append(InputSensor(unique_id, input_id, entry, name, coordinator, device))

# Iterate through the alerts of the provided device and create AlertSensor objects
for alert_id, name in device.alerts:
Expand Down Expand Up @@ -116,7 +117,7 @@ class InputSensor(CoordinatorEntity, BinarySensorEntity):
def __init__(
self,
unique_id: str,
sensor_id: int,
input_id: int,
config: ConfigEntry,
name: str,
coordinator: DataUpdateCoordinator,
Expand All @@ -132,7 +133,7 @@ def __init__(
self._name = name
self._device = device
self._unique_id = unique_id
self._sensor_id = sensor_id
self._input_id = input_id

@property
def unique_id(self) -> str:
Expand All @@ -152,7 +153,7 @@ def icon(self) -> str:
@property
def is_on(self) -> bool:
"""Return the binary sensor status (on/off)."""
return self._device.inputs_alerted.get(self._sensor_id) is not None
return bool(self._device.get_status(query.INPUTS, self._input_id))


class SectorSensor(CoordinatorEntity, BinarySensorEntity):
Expand Down
6 changes: 1 addition & 5 deletions custom_components/econnect_metronet/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ def __init__(self, connection, config=None):
self.state = STATE_UNAVAILABLE
self.sectors_armed = {}
self.sectors_disarmed = {}
self.inputs_alerted = {}
self.inputs_wait = {}
self._inventory = {}

@property
Expand Down Expand Up @@ -250,11 +248,9 @@ def update(self):
self._inventory.update({q.INPUTS: inputs["inputs"]})
self._inventory.update({q.ALERTS: alerts})

# Filter sectors and inputs
# Filter sectors
self.sectors_armed = _filter_data(sectors, "sectors", True)
self.sectors_disarmed = _filter_data(sectors, "sectors", False)
self.inputs_alerted = _filter_data(inputs, "inputs", True)
self.inputs_wait = _filter_data(inputs, "inputs", False)

self._last_ids[q.SECTORS] = sectors.get("last_id", 0)
self._last_ids[q.INPUTS] = inputs.get("last_id", 0)
Expand Down
1 change: 0 additions & 1 deletion tests/test_binary_sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ def test_binary_sensor_on(self, hass, config_entry, alarm_device):
# Ensure the sensor attribute is_on has the right status True
coordinator = DataUpdateCoordinator(hass, logging.getLogger(__name__), name="econnect_metronet")
entity = InputSensor("test_id", 1, config_entry, "Outdoor Sensor 1", coordinator, alarm_device)
alarm_device.inputs_alerted.update({entity._sensor_id: {}})
assert entity.is_on is True


Expand Down
40 changes: 24 additions & 16 deletions tests/test_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ def test_device_constructor(client):
assert device.state == STATE_UNAVAILABLE
assert device.sectors_armed == {}
assert device.sectors_disarmed == {}
assert device.inputs_alerted == {}
assert device.inputs_wait == {}


def test_device_constructor_with_config(client):
Expand All @@ -55,8 +53,6 @@ def test_device_constructor_with_config(client):
assert device.state == STATE_UNAVAILABLE
assert device.sectors_armed == {}
assert device.sectors_disarmed == {}
assert device.inputs_alerted == {}
assert device.inputs_wait == {}


class TestItemInputs:
Expand Down Expand Up @@ -300,21 +296,12 @@ def test_device_update_success(client, mocker):
sectors_disarmed = {
2: {"id": 3, "index": 2, "element": 3, "excluded": False, "status": False, "name": "S3 Outdoor"}
}
inputs_alerted = {
0: {"id": 1, "index": 0, "element": 1, "excluded": False, "status": True, "name": "Entryway Sensor"},
1: {"id": 2, "index": 1, "element": 2, "excluded": False, "status": True, "name": "Outdoor Sensor 1"},
}
inputs_wait = {
2: {"id": 3, "index": 2, "element": 3, "excluded": True, "status": False, "name": "Outdoor Sensor 2"}
}
device.connect("username", "password")
# Test
device.update()
assert device._connection.query.call_count == 3
assert device.sectors_armed == sectors_armed
assert device.sectors_disarmed == sectors_disarmed
assert device.inputs_alerted == inputs_alerted
assert device.inputs_wait == inputs_wait
assert device._last_ids == {
9: 4,
10: 42,
Expand Down Expand Up @@ -464,21 +451,42 @@ def test_alerts_property_empty(self, alarm_device):
assert dict(alarm_device.alerts) == {}


class TestGetStatus:
class TestGetStatusInputs:
def test_get_status_populated(self, alarm_device):
"""Should check if the device property is correctly populated"""
# Test
assert alarm_device.get_status(q.INPUTS, 2) is False

def test_inventory_empty(self, alarm_device):
"""Ensure the property returns a KeyError if _inventory is empty"""
# Test
alarm_device._inventory = {}
with pytest.raises(KeyError):
assert alarm_device.get_status(q.INPUTS, 2)

def test_alerts_property_empty(self, alarm_device):
"""Ensure the property returns a KeyError if inputs key is not in _inventory"""
# Test
alarm_device._inventory = {10: {}}
with pytest.raises(KeyError):
assert alarm_device.get_status(q.INPUTS, 2)


class TestGetStatusAlerts:
def test_get_status_populated(self, alarm_device):
"""Should check if the device property is correctly populated"""
# Test
assert alarm_device.get_status(q.ALERTS, 2) == 0

def test_inventory_empty(self, alarm_device):
"""Ensure the property returns an empty dict if _inventory is empty"""
"""Ensure the property returns a KeyError if _inventory is empty"""
# Test
alarm_device._inventory = {}
with pytest.raises(KeyError):
assert alarm_device.get_status(q.ALERTS, 2)

def test_alerts_property_empty(self, alarm_device):
"""Ensure the property returns an empty dict if alerts key is not in _inventory"""
"""Ensure the property returns a KeyError if alerts key is not in _inventory"""
# Test
alarm_device._inventory = {11: {}}
with pytest.raises(KeyError):
Expand Down

0 comments on commit b54375b

Please sign in to comment.