Skip to content

Commit

Permalink
Remove deprecated history WS API (home-assistant#82136)
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery authored Nov 16, 2022
1 parent 7999f10 commit 1582d88
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 120 deletions.
47 changes: 1 addition & 46 deletions homeassistant/components/history/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@

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,
Expand Down Expand Up @@ -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,
Expand Down
74 changes: 0 additions & 74 deletions tests/components/history/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 1582d88

Please sign in to comment.