From 24feeb4fe49f340d023840df90aab6b7fc0c13d7 Mon Sep 17 00:00:00 2001 From: Darek Date: Tue, 14 Apr 2026 01:59:26 -0500 Subject: [PATCH] Exclude binary_sensor entities from HA battery entity list --- ha_client.py | 2 ++ tests/test_ha_integration.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/ha_client.py b/ha_client.py index 620ad53..6b6019c 100644 --- a/ha_client.py +++ b/ha_client.py @@ -54,6 +54,8 @@ class HomeAssistantClient: result = [] for s in resp.json(): eid = s.get("entity_id", "") + if eid.startswith("binary_sensor."): + continue attrs = s.get("attributes", {}) if (attrs.get("device_class") == "battery" or "battery" in eid.lower()): diff --git a/tests/test_ha_integration.py b/tests/test_ha_integration.py index 5a903c3..be75c3a 100644 --- a/tests/test_ha_integration.py +++ b/tests/test_ha_integration.py @@ -547,3 +547,22 @@ def test_ha_entities_endpoint(ha_app, ha_client_f): # friendly names present tv = next(e for e in data if e["entity_id"] == "sensor.tv_battery") assert tv["friendly_name"] == "TV Battery" + + +def test_ha_entities_excludes_binary_sensors(ha_app, ha_client_f): + """binary_sensor. entities are excluded even when device_class is battery.""" + mock_resp = MagicMock() + mock_resp.json.return_value = [ + {"entity_id": "sensor.remote_battery", "attributes": {"device_class": "battery", "friendly_name": "Remote"}}, + {"entity_id": "binary_sensor.door_battery_low", "attributes": {"device_class": "battery", "friendly_name": "Door low"}}, + {"entity_id": "binary_sensor.smoke_battery", "attributes": {"friendly_name": "Smoke"}}, + ] + mock_resp.raise_for_status.return_value = None + + with patch("ha_client.requests.get", return_value=mock_resp): + resp = ha_client_f.get("/ha/entities") + + assert resp.status_code == 200 + data = resp.get_json() + entity_ids = [e["entity_id"] for e in data] + assert entity_ids == ["sensor.remote_battery"]