Skip to content

Commit c761ce6

Browse files
authored
Tweak usage prediction common control algorithm (home-assistant#152490)
1 parent 40ebce4 commit c761ce6

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

homeassistant/components/usage_prediction/common_control.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from homeassistant.components.recorder.util import session_scope
1919
from homeassistant.const import Platform
2020
from homeassistant.core import HomeAssistant
21+
from homeassistant.helpers import entity_registry as er
2122
from homeassistant.util import dt as dt_util
2223
from homeassistant.util.json import json_loads_object
2324

@@ -41,8 +42,6 @@
4142
Platform.CAMERA,
4243
Platform.CLIMATE,
4344
Platform.COVER,
44-
Platform.DATE,
45-
Platform.DATETIME,
4645
Platform.FAN,
4746
Platform.HUMIDIFIER,
4847
Platform.IMAGE,
@@ -57,14 +56,9 @@
5756
Platform.SIREN,
5857
Platform.SWITCH,
5958
Platform.TEXT,
60-
Platform.TIME,
61-
Platform.TODO,
62-
Platform.UPDATE,
6359
Platform.VACUUM,
6460
Platform.VALVE,
65-
Platform.WAKE_WORD,
6661
Platform.WATER_HEATER,
67-
Platform.WEATHER,
6862
# Helpers with own domain
6963
"counter",
7064
"group",
@@ -105,14 +99,17 @@ async def async_predict_common_control(
10599
"""
106100
# Get the recorder instance to ensure it's ready
107101
recorder = get_instance(hass)
102+
ent_reg = er.async_get(hass)
108103

109104
# Execute the database operation in the recorder's executor
110105
return await recorder.async_add_executor_job(
111-
_fetch_with_session, hass, _fetch_and_process_data, user_id
106+
_fetch_with_session, hass, _fetch_and_process_data, ent_reg, user_id
112107
)
113108

114109

115-
def _fetch_and_process_data(session: Session, user_id: str) -> EntityUsagePredictions:
110+
def _fetch_and_process_data(
111+
session: Session, ent_reg: er.EntityRegistry, user_id: str
112+
) -> EntityUsagePredictions:
116113
"""Fetch and process service call events from the database."""
117114
# Prepare a dictionary to track results
118115
results: dict[str, Counter[str]] = {
@@ -198,6 +195,7 @@ def _fetch_and_process_data(session: Session, user_id: str) -> EntityUsagePredic
198195
entity_id
199196
for entity_id in entity_ids
200197
if entity_id.split(".")[0] in ALLOWED_DOMAINS
198+
and ((entry := ent_reg.async_get(entity_id)) is None or not entry.hidden)
201199
]
202200

203201
if not entity_ids:

tests/components/usage_prediction/test_common_control.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from homeassistant.components.usage_prediction.models import EntityUsagePredictions
1616
from homeassistant.const import EVENT_CALL_SERVICE
1717
from homeassistant.core import Context, HomeAssistant
18+
from homeassistant.helpers import entity_registry as er
1819

1920
from tests.components.recorder.common import async_wait_recording_done
2021

@@ -135,6 +136,21 @@ async def test_multiple_entities_in_one_call(hass: HomeAssistant) -> None:
135136
"""Test handling of service calls with multiple entity IDs."""
136137
user_id = str(uuid.uuid4())
137138

139+
ent_reg = er.async_get(hass)
140+
ent_reg.async_get_or_create(
141+
"light",
142+
"test",
143+
"living_room",
144+
suggested_object_id="living_room",
145+
hidden_by=er.RegistryEntryHider.USER,
146+
)
147+
ent_reg.async_get_or_create(
148+
"light",
149+
"test",
150+
"kitchen",
151+
suggested_object_id="kitchen",
152+
)
153+
138154
with freeze_time("2023-07-01 10:00:00+00:00"): # Morning
139155
hass.bus.async_fire(
140156
EVENT_CALL_SERVICE,
@@ -159,8 +175,9 @@ async def test_multiple_entities_in_one_call(hass: HomeAssistant) -> None:
159175
with freeze_time("2023-07-02 10:00:00+00:00"): # Next day, so events are recent
160176
results = await async_predict_common_control(hass, user_id)
161177

162-
# All three lights should be counted (10:00 UTC = 02:00 local = night)
163-
assert results.night == ["light.living_room", "light.kitchen", "light.hallway"]
178+
# Two lights should be counted (10:00 UTC = 02:00 local = night)
179+
# Living room is hidden via entity registry
180+
assert results.night == ["light.kitchen", "light.hallway"]
164181
assert results.morning == []
165182
assert results.afternoon == []
166183
assert results.evening == []

0 commit comments

Comments
 (0)