Exclude binary_sensor entities from HA battery entity list

This commit is contained in:
2026-04-14 01:59:26 -05:00
parent d00695cd51
commit 24feeb4fe4
2 changed files with 21 additions and 0 deletions
+2
View File
@@ -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()):
+19
View File
@@ -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"]