Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add statistics websocket endpoint #51044

Merged
merged 2 commits into from
May 25, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions homeassistant/components/history/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
from sqlalchemy import not_, or_
import voluptuous as vol

from homeassistant.components import websocket_api
from homeassistant.components.http import HomeAssistantView
from homeassistant.components.recorder import history
from homeassistant.components.recorder.models import States
from homeassistant.components.recorder.statistics import statistics_during_period
from homeassistant.components.recorder.util import session_scope
from homeassistant.const import (
CONF_DOMAINS,
Expand Down Expand Up @@ -101,10 +103,56 @@ async def async_setup(hass, config):
hass.components.frontend.async_register_built_in_panel(
"history", "history", "hass:poll-box"
)
hass.components.websocket_api.async_register_command(
ws_get_statistics_during_period
)

return True


@websocket_api.websocket_command(
{
vol.Required("type"): "history/statistics_during_period",
vol.Required("start_time"): str,
vol.Optional("end_time"): str,
vol.Optional("statistic_id"): str,
}
)
@websocket_api.async_response
async def ws_get_statistics_during_period(
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict
) -> None:
"""Handle statistics websocket command."""
start_time_str = msg["start_time"]
end_time_str = msg.get("end_time")

start_time = dt_util.parse_datetime(start_time_str)
if start_time:
start_time = dt_util.as_utc(start_time)
else:
connection.send_error(msg["id"], "invalid_start_time", "Invalid start_time")
return

if end_time_str:
end_time = dt_util.parse_datetime(end_time_str)
if end_time:
end_time = dt_util.as_utc(end_time)
else:
connection.send_error(msg["id"], "invalid_end_time", "Invalid end_time")
return
else:
end_time = None

statistics = await hass.async_add_executor_job(
statistics_during_period,
hass,
start_time,
end_time,
msg.get("statistic_id"),
)
connection.send_result(msg["id"], {"statistics": statistics})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How big will this data be? Should it use await connection.send_big_result(…) instead?



class HistoryPeriodView(HomeAssistantView):
"""Handle history period requests."""

Expand Down