1
mirror of https://github.com/home-assistant/core synced 2024-09-28 03:04:04 +02:00

Remove deprecated history WS API (#82136)

This commit is contained in:
Erik Montnemery 2022-11-16 12:54:03 +01:00 committed by GitHub
parent 7999f109d1
commit 1582d88957
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 1 additions and 120 deletions

View File

@ -13,11 +13,7 @@ import voluptuous as vol
from homeassistant.components import frontend, websocket_api
from homeassistant.components.http import HomeAssistantView
from homeassistant.components.recorder import (
get_instance,
history,
websocket_api as recorder_ws,
)
from homeassistant.components.recorder import get_instance, history
from homeassistant.components.recorder.filters import (
Filters,
sqlalchemy_filter_from_include_exclude_conf,
@ -61,52 +57,11 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
hass.http.register_view(HistoryPeriodView(filters, use_include_order))
frontend.async_register_built_in_panel(hass, "history", "history", "hass:chart-box")
websocket_api.async_register_command(hass, ws_get_statistics_during_period)
websocket_api.async_register_command(hass, ws_get_list_statistic_ids)
websocket_api.async_register_command(hass, ws_get_history_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_ids"): [str],
vol.Required("period"): vol.Any("5minute", "hour", "day", "month"),
}
)
@websocket_api.async_response
async def ws_get_statistics_during_period(
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any]
) -> None:
"""Handle statistics websocket command."""
_LOGGER.warning(
"WS API 'history/statistics_during_period' is deprecated and will be removed in "
"Home Assistant Core 2022.12. Use 'recorder/statistics_during_period' instead"
)
await recorder_ws.ws_handle_get_statistics_during_period(hass, connection, msg)
@websocket_api.websocket_command(
{
vol.Required("type"): "history/list_statistic_ids",
vol.Optional("statistic_type"): vol.Any("sum", "mean"),
}
)
@websocket_api.async_response
async def ws_get_list_statistic_ids(
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any]
) -> None:
"""Fetch a list of available statistic_id."""
_LOGGER.warning(
"WS API 'history/list_statistic_ids' is deprecated and will be removed in "
"Home Assistant Core 2022.12. Use 'recorder/list_statistic_ids' instead"
)
await recorder_ws.ws_handle_list_statistic_ids(hass, connection, msg)
def _ws_get_significant_states(
hass: HomeAssistant,
msg_id: int,

View File

@ -10,10 +10,6 @@ import pytest
from homeassistant.components import history
from homeassistant.components.recorder.history import get_significant_states
from homeassistant.components.recorder.models import process_timestamp
from homeassistant.components.recorder.websocket_api import (
ws_handle_get_statistics_during_period,
ws_handle_list_statistic_ids,
)
from homeassistant.const import CONF_DOMAINS, CONF_ENTITIES, CONF_EXCLUDE, CONF_INCLUDE
import homeassistant.core as ha
from homeassistant.helpers.json import JSONEncoder
@ -844,76 +840,6 @@ async def test_entity_ids_limit_via_api_with_skip_initial_state(
assert response_json[1][0]["entity_id"] == "light.cow"
async def test_statistics_during_period(recorder_mock, hass, hass_ws_client, caplog):
"""Test history/statistics_during_period forwards to recorder."""
now = dt_util.utcnow()
await async_setup_component(hass, "history", {})
client = await hass_ws_client()
# Test the WS API works and issues a warning
await client.send_json(
{
"id": 1,
"type": "history/statistics_during_period",
"start_time": now.isoformat(),
"end_time": now.isoformat(),
"statistic_ids": ["sensor.test"],
"period": "hour",
}
)
response = await client.receive_json()
assert response["success"]
assert response["result"] == {}
assert (
"WS API 'history/statistics_during_period' is deprecated and will be removed in "
"Home Assistant Core 2022.12. Use 'recorder/statistics_during_period' instead"
) in caplog.text
# Test the WS API forwards to recorder
with patch(
"homeassistant.components.history.recorder_ws.ws_handle_get_statistics_during_period",
wraps=ws_handle_get_statistics_during_period,
) as ws_mock:
await client.send_json(
{
"id": 2,
"type": "history/statistics_during_period",
"start_time": now.isoformat(),
"end_time": now.isoformat(),
"statistic_ids": ["sensor.test"],
"period": "hour",
}
)
await client.receive_json()
ws_mock.assert_awaited_once()
async def test_list_statistic_ids(recorder_mock, hass, hass_ws_client, caplog):
"""Test history/list_statistic_ids forwards to recorder."""
await async_setup_component(hass, "history", {})
client = await hass_ws_client()
# Test the WS API works and issues a warning
await client.send_json({"id": 1, "type": "history/list_statistic_ids"})
response = await client.receive_json()
assert response["success"]
assert response["result"] == []
assert (
"WS API 'history/list_statistic_ids' is deprecated and will be removed in "
"Home Assistant Core 2022.12. Use 'recorder/list_statistic_ids' instead"
) in caplog.text
with patch(
"homeassistant.components.history.recorder_ws.ws_handle_list_statistic_ids",
wraps=ws_handle_list_statistic_ids,
) as ws_mock:
await client.send_json({"id": 2, "type": "history/list_statistic_ids"})
await client.receive_json()
ws_mock.assert_called_once()
async def test_history_during_period(recorder_mock, hass, hass_ws_client):
"""Test history_during_period."""
now = dt_util.utcnow()