Exclude binary_sensor entities from HA battery entity list
This commit is contained in:
@@ -54,6 +54,8 @@ class HomeAssistantClient:
|
|||||||
result = []
|
result = []
|
||||||
for s in resp.json():
|
for s in resp.json():
|
||||||
eid = s.get("entity_id", "")
|
eid = s.get("entity_id", "")
|
||||||
|
if eid.startswith("binary_sensor."):
|
||||||
|
continue
|
||||||
attrs = s.get("attributes", {})
|
attrs = s.get("attributes", {})
|
||||||
if (attrs.get("device_class") == "battery"
|
if (attrs.get("device_class") == "battery"
|
||||||
or "battery" in eid.lower()):
|
or "battery" in eid.lower()):
|
||||||
|
|||||||
@@ -547,3 +547,22 @@ def test_ha_entities_endpoint(ha_app, ha_client_f):
|
|||||||
# friendly names present
|
# friendly names present
|
||||||
tv = next(e for e in data if e["entity_id"] == "sensor.tv_battery")
|
tv = next(e for e in data if e["entity_id"] == "sensor.tv_battery")
|
||||||
assert tv["friendly_name"] == "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"]
|
||||||
|
|||||||
Reference in New Issue
Block a user