mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Improve statistics validation (#56457)
This commit is contained in:
parent
fc4bb40a63
commit
6f36419c6f
@ -562,15 +562,13 @@ def validate_statistics(
|
|||||||
state = hass.states.get(entity_id)
|
state = hass.states.get(entity_id)
|
||||||
assert state is not None
|
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)
|
state_unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
|
||||||
metadata_unit = metadata["unit_of_measurement"]
|
|
||||||
|
|
||||||
if device_class not in UNIT_CONVERSIONS:
|
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:
|
if state_unit != metadata_unit:
|
||||||
validation_result[entity_id].append(
|
validation_result[entity_id].append(
|
||||||
statistics.ValidationIssue(
|
statistics.ValidationIssue(
|
||||||
|
@ -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 hass.async_add_executor_job(hass.data[DATA_INSTANCE].block_till_done)
|
||||||
await assert_validation_result(client, {})
|
await assert_validation_result(client, {})
|
||||||
|
|
||||||
# No statistics, invalid state - empty response
|
# No statistics, invalid state - expect error
|
||||||
hass.states.async_set(
|
hass.states.async_set(
|
||||||
"sensor.test", 11, attributes={**attributes, **{"unit_of_measurement": "dogs"}}
|
"sensor.test", 11, attributes={**attributes, **{"unit_of_measurement": "dogs"}}
|
||||||
)
|
)
|
||||||
await hass.async_add_executor_job(hass.data[DATA_INSTANCE].block_till_done)
|
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 = {
|
expected = {
|
||||||
"sensor.test": [
|
"sensor.test": [
|
||||||
{
|
{
|
||||||
@ -114,6 +105,15 @@ async def test_validate_statistics_supported_device_class(
|
|||||||
}
|
}
|
||||||
await assert_validation_result(client, expected)
|
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
|
# Valid state - empty response
|
||||||
hass.states.async_set(
|
hass.states.async_set(
|
||||||
"sensor.test", 13, attributes={**attributes, **{"unit_of_measurement": unit}}
|
"sensor.test", 13, attributes={**attributes, **{"unit_of_measurement": unit}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user