Add config flow to statistics (#120496)

This commit is contained in:
G Johansson 2024-06-26 16:06:35 +02:00 committed by GitHub
parent 30a3e9af2b
commit 3d5d4f8ddb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 749 additions and 8 deletions

View file

@ -19,10 +19,20 @@ from homeassistant.components.sensor import (
SensorStateClass,
)
from homeassistant.components.statistics import DOMAIN as STATISTICS_DOMAIN
from homeassistant.components.statistics.sensor import StatisticsSensor
from homeassistant.components.statistics.sensor import (
CONF_KEEP_LAST_SAMPLE,
CONF_PERCENTILE,
CONF_PRECISION,
CONF_SAMPLES_MAX_BUFFER_SIZE,
CONF_STATE_CHARACTERISTIC,
STAT_MEAN,
StatisticsSensor,
)
from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_UNIT_OF_MEASUREMENT,
CONF_ENTITY_ID,
CONF_NAME,
DEGREE,
SERVICE_RELOAD,
STATE_UNAVAILABLE,
@ -35,7 +45,7 @@ from homeassistant.helpers import entity_registry as er
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
from tests.common import async_fire_time_changed, get_fixture_path
from tests.common import MockConfigEntry, async_fire_time_changed, get_fixture_path
from tests.components.recorder.common import async_wait_recording_done
VALUES_BINARY = ["on", "off", "on", "off", "on", "off", "on", "off", "on"]
@ -171,6 +181,35 @@ async def test_sensor_defaults_numeric(hass: HomeAssistant) -> None:
assert new_state.attributes.get("source_value_valid") is False
@pytest.mark.parametrize(
"get_config",
[
{
CONF_NAME: "test",
CONF_ENTITY_ID: "sensor.test_monitored",
CONF_STATE_CHARACTERISTIC: STAT_MEAN,
CONF_SAMPLES_MAX_BUFFER_SIZE: 20.0,
CONF_KEEP_LAST_SAMPLE: False,
CONF_PERCENTILE: 50.0,
CONF_PRECISION: 2.0,
}
],
)
async def test_sensor_loaded_from_config_entry(
hass: HomeAssistant, loaded_entry: MockConfigEntry
) -> None:
"""Test the sensor loaded from a config entry."""
state = hass.states.get("sensor.test")
assert state is not None
assert state.state == str(round(sum(VALUES_NUMERIC) / len(VALUES_NUMERIC), 2))
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfTemperature.CELSIUS
assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT
assert state.attributes.get("buffer_usage_ratio") == round(9 / 20, 2)
assert state.attributes.get("source_value_valid") is True
assert "age_coverage_ratio" not in state.attributes
async def test_sensor_defaults_binary(hass: HomeAssistant) -> None:
"""Test the general behavior of the sensor, with binary source sensor."""
assert await async_setup_component(