Replace quantiles by percentile characteristic for statistics component (#81027)

* Remove quantiles characteristic

* Add percentile characteristic
This commit is contained in:
Thomas Dietrich 2022-11-14 09:23:02 +01:00 committed by GitHub
parent 727dcd6df6
commit a0b0e4088c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 36 deletions

View file

@ -391,7 +391,7 @@ async def test_age_limit_expiry(hass: HomeAssistant):
async def test_precision(hass: HomeAssistant):
"""Test correct result with precision set."""
"""Test correct results with precision set."""
assert await async_setup_component(
hass,
"sensor",
@ -433,6 +433,60 @@ async def test_precision(hass: HomeAssistant):
assert state.state == str(round(mean, 3))
async def test_percentile(hass: HomeAssistant):
"""Test correct results for percentile characteristic."""
assert await async_setup_component(
hass,
"sensor",
{
"sensor": [
{
"platform": "statistics",
"name": "test_percentile_omitted",
"entity_id": "sensor.test_monitored",
"state_characteristic": "percentile",
"sampling_size": 20,
},
{
"platform": "statistics",
"name": "test_percentile_default",
"entity_id": "sensor.test_monitored",
"state_characteristic": "percentile",
"sampling_size": 20,
"percentile": 50,
},
{
"platform": "statistics",
"name": "test_percentile_min",
"entity_id": "sensor.test_monitored",
"state_characteristic": "percentile",
"sampling_size": 20,
"percentile": 1,
},
]
},
)
await hass.async_block_till_done()
for value in VALUES_NUMERIC:
hass.states.async_set(
"sensor.test_monitored",
str(value),
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS},
)
await hass.async_block_till_done()
state = hass.states.get("sensor.test_percentile_omitted")
assert state is not None
assert state.state == str(9.2)
state = hass.states.get("sensor.test_percentile_default")
assert state is not None
assert state.state == str(9.2)
state = hass.states.get("sensor.test_percentile_min")
assert state is not None
assert state.state == str(2.72)
async def test_device_class(hass: HomeAssistant):
"""Test device class, which depends on the source entity."""
assert await async_setup_component(
@ -753,13 +807,11 @@ async def test_state_characteristics(hass: HomeAssistant):
},
{
"source_sensor_domain": "sensor",
"name": "quantiles",
"name": "percentile",
"value_0": STATE_UNKNOWN,
"value_1": STATE_UNKNOWN,
"value_9": [
round(quantile, 2) for quantile in statistics.quantiles(VALUES_NUMERIC)
],
"unit": None,
"value_9": 9.2,
"unit": "°C",
},
{
"source_sensor_domain": "sensor",