Allow fetching multiple statistics (#51996)

This commit is contained in:
Paulus Schoutsen 2021-06-18 12:03:13 -07:00 committed by GitHub
parent 87a43eacb7
commit 805ef3f90b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 47 deletions

View File

@ -119,7 +119,7 @@ class LazyState(history_models.LazyState):
vol.Required("type"): "history/statistics_during_period",
vol.Required("start_time"): str,
vol.Optional("end_time"): str,
vol.Optional("statistic_id"): str,
vol.Optional("statistic_ids"): [str],
}
)
@websocket_api.async_response
@ -152,9 +152,9 @@ async def ws_get_statistics_during_period(
hass,
start_time,
end_time,
msg.get("statistic_id"),
msg.get("statistic_ids"),
)
connection.send_result(msg["id"], {"statistics": statistics})
connection.send_result(msg["id"], statistics)
class HistoryPeriodView(HomeAssistantView):

View File

@ -74,7 +74,7 @@ def compile_statistics(instance: Recorder, start: datetime.datetime) -> bool:
return True
def statistics_during_period(hass, start_time, end_time=None, statistic_id=None):
def statistics_during_period(hass, start_time, end_time=None, statistic_ids=None):
"""Return states changes during UTC period start_time - end_time."""
with session_scope(hass=hass) as session:
baked_query = hass.data[STATISTICS_BAKERY](
@ -86,20 +86,20 @@ def statistics_during_period(hass, start_time, end_time=None, statistic_id=None)
if end_time is not None:
baked_query += lambda q: q.filter(Statistics.start < bindparam("end_time"))
if statistic_id is not None:
baked_query += lambda q: q.filter_by(statistic_id=bindparam("statistic_id"))
statistic_id = statistic_id.lower()
if statistic_ids is not None:
baked_query += lambda q: q.filter(
Statistics.statistic_id.in_(bindparam("statistic_ids"))
)
statistic_ids = [statistic_id.lower() for statistic_id in statistic_ids]
baked_query += lambda q: q.order_by(Statistics.statistic_id, Statistics.start)
stats = execute(
baked_query(session).params(
start_time=start_time, end_time=end_time, statistic_id=statistic_id
start_time=start_time, end_time=end_time, statistic_ids=statistic_ids
)
)
statistic_ids = [statistic_id] if statistic_id is not None else None
return _sorted_statistics_to_dict(stats, statistic_ids)

View File

@ -834,11 +834,7 @@ async def test_statistics_during_period(hass, hass_ws_client):
now = dt_util.utcnow()
await hass.async_add_executor_job(init_recorder_component, hass)
await async_setup_component(
hass,
"history",
{"history": {}},
)
await async_setup_component(hass, "history", {})
await async_setup_component(hass, "sensor", {})
await hass.async_add_executor_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
hass.states.async_set(
@ -861,12 +857,12 @@ async def test_statistics_during_period(hass, hass_ws_client):
"type": "history/statistics_during_period",
"start_time": now.isoformat(),
"end_time": now.isoformat(),
"statistic_id": "sensor.test",
"statistic_ids": ["sensor.test"],
}
)
response = await client.receive_json()
assert response["success"]
assert response["result"] == {"statistics": {}}
assert response["result"] == {}
client = await hass_ws_client()
await client.send_json(
@ -874,13 +870,12 @@ async def test_statistics_during_period(hass, hass_ws_client):
"id": 1,
"type": "history/statistics_during_period",
"start_time": now.isoformat(),
"statistic_id": "sensor.test",
"statistic_ids": ["sensor.test"],
}
)
response = await client.receive_json()
assert response["success"]
assert response["result"] == {
"statistics": {
"sensor.test": [
{
"statistic_id": "sensor.test",
@ -894,7 +889,6 @@ async def test_statistics_during_period(hass, hass_ws_client):
}
]
}
}
async def test_statistics_during_period_bad_start_time(hass, hass_ws_client):

View File

@ -26,7 +26,8 @@ def test_compile_hourly_statistics(hass_recorder):
recorder.do_adhoc_statistics(period="hourly", start=zero)
wait_recording_done(hass)
stats = statistics_during_period(hass, zero)
for kwargs in ({}, {"statistic_ids": ["sensor.test1"]}):
stats = statistics_during_period(hass, zero, **kwargs)
assert stats == {
"sensor.test1": [
{