From 6f36419c6f4a5273de12ac0590b8d63444fdd603 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 20 Sep 2021 17:54:25 +0200 Subject: [PATCH] Improve statistics validation (#56457) --- homeassistant/components/sensor/recorder.py | 10 ++++------ .../components/recorder/test_websocket_api.py | 20 +++++++++---------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/sensor/recorder.py b/homeassistant/components/sensor/recorder.py index 02269439cb8..8ea2a52c278 100644 --- a/homeassistant/components/sensor/recorder.py +++ b/homeassistant/components/sensor/recorder.py @@ -562,15 +562,13 @@ def validate_statistics( state = hass.states.get(entity_id) assert state is not None - metadata = statistics.get_metadata(hass, entity_id) - if not metadata: - continue - state_unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) - metadata_unit = metadata["unit_of_measurement"] if device_class not in UNIT_CONVERSIONS: - + metadata = statistics.get_metadata(hass, entity_id) + if not metadata: + continue + metadata_unit = metadata["unit_of_measurement"] if state_unit != metadata_unit: validation_result[entity_id].append( statistics.ValidationIssue( diff --git a/tests/components/recorder/test_websocket_api.py b/tests/components/recorder/test_websocket_api.py index 51334432121..ed07d949808 100644 --- a/tests/components/recorder/test_websocket_api.py +++ b/tests/components/recorder/test_websocket_api.py @@ -86,20 +86,11 @@ async def test_validate_statistics_supported_device_class( await hass.async_add_executor_job(hass.data[DATA_INSTANCE].block_till_done) await assert_validation_result(client, {}) - # No statistics, invalid state - empty response + # No statistics, invalid state - expect error hass.states.async_set( "sensor.test", 11, attributes={**attributes, **{"unit_of_measurement": "dogs"}} ) await hass.async_add_executor_job(hass.data[DATA_INSTANCE].block_till_done) - await assert_validation_result(client, {}) - - # Statistics has run, invalid state - expect error - await hass.async_add_executor_job(hass.data[DATA_INSTANCE].block_till_done) - hass.data[DATA_INSTANCE].do_adhoc_statistics(start=now) - hass.states.async_set( - "sensor.test", 12, attributes={**attributes, **{"unit_of_measurement": "dogs"}} - ) - await hass.async_add_executor_job(hass.data[DATA_INSTANCE].block_till_done) expected = { "sensor.test": [ { @@ -114,6 +105,15 @@ async def test_validate_statistics_supported_device_class( } await assert_validation_result(client, expected) + # Statistics has run, invalid state - expect error + await hass.async_add_executor_job(hass.data[DATA_INSTANCE].block_till_done) + hass.data[DATA_INSTANCE].do_adhoc_statistics(start=now) + hass.states.async_set( + "sensor.test", 12, attributes={**attributes, **{"unit_of_measurement": "dogs"}} + ) + await hass.async_add_executor_job(hass.data[DATA_INSTANCE].block_till_done) + await assert_validation_result(client, expected) + # Valid state - empty response hass.states.async_set( "sensor.test", 13, attributes={**attributes, **{"unit_of_measurement": unit}}