Skip to content

Commit

Permalink
feat(entity): add connection status binary sensor to provide feedback…
Browse files Browse the repository at this point in the history
… in case of a disconnection
  • Loading branch information
palazzem committed Mar 1, 2024
1 parent 72ed533 commit 61c7193
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
4 changes: 4 additions & 0 deletions custom_components/econnect_metronet/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ async def async_setup_entry(
unique_id = f"{entry.entry_id}_{DOMAIN}_{name}"
sensors.append(AlertBinarySensor(unique_id, alert_id, entry, name, coordinator, device))

# Binary sensor to keep track of the device connection status
unique_id = f"{entry.entry_id}_{DOMAIN}_connection_status"
sensors.append(AlertBinarySensor(unique_id, -1, entry, "connection_status", coordinator, device))

async_add_entities(sensors)


Expand Down
5 changes: 5 additions & 0 deletions custom_components/econnect_metronet/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ def get_status(self, query: int, id: int) -> Union[bool, int]:
Returns:
int: The status of the item.
"""
if query == q.ALERTS and id == -1:
# Connection Status Alert
# NOTE: we should turn on the sensor (alert) if the device is not connected, hence the `not`
return not self.connected

return self._inventory[query][id]["status"]

def update(self):
Expand Down
7 changes: 7 additions & 0 deletions custom_components/econnect_metronet/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@
},
"entity": {
"binary_sensor": {
"connection_status": {
"name": "Connection Status",
"state": {
"on": "Disconnected",
"off": "Connected"
}
},
"anomalies_led": {
"name": "General Anomaly",
"state": {
Expand Down
7 changes: 7 additions & 0 deletions custom_components/econnect_metronet/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@
}
},
"binary_sensor": {
"connection_status": {
"name": "Connection Status",
"state": {
"on": "Disconnected",
"off": "Connected"
}
},
"anomalies_led": {
"name": "General Anomaly",
"state": {
Expand Down
7 changes: 7 additions & 0 deletions custom_components/econnect_metronet/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@
}
},
"binary_sensor": {
"connection_status": {
"name": "Stato Connessione",
"state": {
"on": "Disconnessa",
"off": "Connessa"
}
},
"anomalies_led": {
"name": "Anomalia Generale",
"state": {
Expand Down
22 changes: 21 additions & 1 deletion tests/test_binary_sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,31 @@ async def test_async_setup_entry_in_use(hass, config_entry, alarm_device, coordi

# Test
def ensure_only_in_use(sensors):
assert len(sensors) == 28
assert len(sensors) == 29

await async_setup_entry(hass, config_entry, ensure_only_in_use)


@pytest.mark.asyncio
async def test_async_setup_entry_connection_status(hass, config_entry, alarm_device, coordinator):
# Ensure the async setup loads the device connection status
hass.data[DOMAIN][config_entry.entry_id] = {
"device": alarm_device,
"coordinator": coordinator,
}

# Test
def check_connection_status(sensors):
connection_status = sensors[-1]
assert connection_status.unique_id == "test_entry_id_econnect_metronet_connection_status"
assert connection_status.entity_id == "econnect_metronet.econnect_metronet_test_user_connection_status"
assert connection_status.is_on is False
alarm_device.connected = False
assert connection_status.is_on is True

await async_setup_entry(hass, config_entry, check_connection_status)


@pytest.mark.asyncio
async def test_async_setup_entry_unused_input(hass, config_entry, alarm_device, coordinator):
# Ensure the async setup don't load inputs that are not in use
Expand Down

0 comments on commit 61c7193

Please sign in to comment.