diff --git a/homeassistant/components/recorder/websocket_api.py b/homeassistant/components/recorder/websocket_api.py index 2ddb5564225..29c0808e6ad 100644 --- a/homeassistant/components/recorder/websocket_api.py +++ b/homeassistant/components/recorder/websocket_api.py @@ -112,7 +112,7 @@ def _ws_get_statistic_during_period( @websocket_api.websocket_command( { vol.Required("type"): "recorder/statistic_during_period", - vol.Optional("statistic_id"): str, + vol.Required("statistic_id"): str, vol.Optional("types"): vol.All( [vol.Any("max", "mean", "min", "change")], vol.Coerce(set) ), @@ -139,7 +139,7 @@ async def ws_get_statistic_during_period( msg["id"], start_time, end_time, - msg.get("statistic_id"), + msg["statistic_id"], msg.get("types"), msg.get("units"), ) @@ -208,7 +208,7 @@ async def ws_handle_get_statistics_during_period( msg["id"], start_time, end_time, - msg.get("statistic_ids"), + msg["statistic_ids"], msg.get("period"), msg.get("units"), types, @@ -221,7 +221,7 @@ async def ws_handle_get_statistics_during_period( vol.Required("type"): "recorder/statistics_during_period", vol.Required("start_time"): str, vol.Optional("end_time"): str, - vol.Optional("statistic_ids"): [str], + vol.Required("statistic_ids"): vol.All([str], vol.Length(min=1)), vol.Required("period"): vol.Any("5minute", "hour", "day", "week", "month"), vol.Optional("units"): UNIT_SCHEMA, vol.Optional("types"): vol.All( diff --git a/tests/components/recorder/test_websocket_api.py b/tests/components/recorder/test_websocket_api.py index 151cf148cf6..7a8a59e865c 100644 --- a/tests/components/recorder/test_websocket_api.py +++ b/tests/components/recorder/test_websocket_api.py @@ -1089,6 +1089,7 @@ async def test_statistics_during_period_invalid_unit_conversion( "id": 1, "type": "recorder/statistics_during_period", "start_time": now.isoformat(), + "statistic_ids": ["sensor.test"], "period": "5minute", } ) @@ -1102,6 +1103,7 @@ async def test_statistics_during_period_invalid_unit_conversion( "id": 2, "type": "recorder/statistics_during_period", "start_time": now.isoformat(), + "statistic_ids": ["sensor.test"], "period": "5minute", "units": custom_units, } @@ -1242,6 +1244,7 @@ async def test_statistics_during_period_bad_start_time( "id": 1, "type": "recorder/statistics_during_period", "start_time": "cats", + "statistic_ids": ["sensor.test"], "period": "5minute", } ) @@ -1263,6 +1266,7 @@ async def test_statistics_during_period_bad_end_time( "type": "recorder/statistics_during_period", "start_time": now.isoformat(), "end_time": "dogs", + "statistic_ids": ["sensor.test"], "period": "5minute", } ) @@ -1271,6 +1275,49 @@ async def test_statistics_during_period_bad_end_time( assert response["error"]["code"] == "invalid_end_time" +async def test_statistics_during_period_no_statistic_ids( + recorder_mock, hass, hass_ws_client +): + """Test statistics_during_period without passing statistic_ids.""" + now = dt_util.utcnow() + + client = await hass_ws_client() + await client.send_json( + { + "id": 1, + "type": "recorder/statistics_during_period", + "start_time": now.isoformat(), + "end_time": (now + timedelta(seconds=1)).isoformat(), + "period": "5minute", + } + ) + response = await client.receive_json() + assert not response["success"] + assert response["error"]["code"] == "invalid_format" + + +async def test_statistics_during_period_empty_statistic_ids( + recorder_mock, hass, hass_ws_client +): + """Test statistics_during_period with passing an empty list of statistic_ids.""" + now = dt_util.utcnow() + + client = await hass_ws_client() + await client.send_json( + { + "id": 1, + "type": "recorder/statistics_during_period", + "start_time": now.isoformat(), + "statistic_ids": [], + "end_time": (now + timedelta(seconds=1)).isoformat(), + "period": "5minute", + } + ) + response = await client.receive_json() + assert not response["success"] + assert response["error"]["code"] == "invalid_format" + + @pytest.mark.parametrize( "units, attributes, display_unit, statistics_unit, unit_class", [ @@ -1606,6 +1653,7 @@ async def test_clear_statistics(recorder_mock, hass, hass_ws_client): "id": 1, "type": "recorder/statistics_during_period", "start_time": now.isoformat(), + "statistic_ids": ["sensor.test1", "sensor.test2", "sensor.test3"], "period": "5minute", } ) @@ -1667,6 +1715,7 @@ async def test_clear_statistics(recorder_mock, hass, hass_ws_client): { "id": 3, "type": "recorder/statistics_during_period", + "statistic_ids": ["sensor.test1", "sensor.test2", "sensor.test3"], "start_time": now.isoformat(), "period": "5minute", } @@ -1691,6 +1740,7 @@ async def test_clear_statistics(recorder_mock, hass, hass_ws_client): { "id": 5, "type": "recorder/statistics_during_period", + "statistic_ids": ["sensor.test1", "sensor.test2", "sensor.test3"], "start_time": now.isoformat(), "period": "5minute", }