mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 10:17:09 +00:00
Add the ability to reload statistics platforms from yaml (#39268)
This commit is contained in:
parent
e96d8a961c
commit
dc84196202
@ -1 +1,4 @@
|
|||||||
"""The statistics component."""
|
"""The statistics component."""
|
||||||
|
|
||||||
|
DOMAIN = "statistics"
|
||||||
|
PLATFORMS = ["sensor"]
|
||||||
|
@ -23,8 +23,11 @@ from homeassistant.helpers.event import (
|
|||||||
async_track_point_in_utc_time,
|
async_track_point_in_utc_time,
|
||||||
async_track_state_change_event,
|
async_track_state_change_event,
|
||||||
)
|
)
|
||||||
|
from homeassistant.helpers.reload import async_setup_reload_service
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
|
from . import DOMAIN, PLATFORMS
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
ATTR_AVERAGE_CHANGE = "average_change"
|
ATTR_AVERAGE_CHANGE = "average_change"
|
||||||
@ -66,6 +69,9 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||||||
|
|
||||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||||
"""Set up the Statistics sensor."""
|
"""Set up the Statistics sensor."""
|
||||||
|
|
||||||
|
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
|
||||||
|
|
||||||
entity_id = config.get(CONF_ENTITY_ID)
|
entity_id = config.get(CONF_ENTITY_ID)
|
||||||
name = config.get(CONF_NAME)
|
name = config.get(CONF_NAME)
|
||||||
sampling_size = config.get(CONF_SAMPLING_SIZE)
|
sampling_size = config.get(CONF_SAMPLING_SIZE)
|
||||||
@ -124,8 +130,10 @@ class StatisticsSensor(Entity):
|
|||||||
"""Add listener and get recorded state."""
|
"""Add listener and get recorded state."""
|
||||||
_LOGGER.debug("Startup for %s", self.entity_id)
|
_LOGGER.debug("Startup for %s", self.entity_id)
|
||||||
|
|
||||||
async_track_state_change_event(
|
self.async_on_remove(
|
||||||
self.hass, [self._entity_id], async_stats_sensor_state_listener
|
async_track_state_change_event(
|
||||||
|
self.hass, [self._entity_id], async_stats_sensor_state_listener
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
if "recorder" in self.hass.config.components:
|
if "recorder" in self.hass.config.components:
|
||||||
|
2
homeassistant/components/statistics/services.yaml
Normal file
2
homeassistant/components/statistics/services.yaml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
reload:
|
||||||
|
description: Reload all statistics entities.
|
@ -1,14 +1,21 @@
|
|||||||
"""The test for the statistics sensor platform."""
|
"""The test for the statistics sensor platform."""
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
from os import path
|
||||||
import statistics
|
import statistics
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from homeassistant import config as hass_config
|
||||||
from homeassistant.components import recorder
|
from homeassistant.components import recorder
|
||||||
from homeassistant.components.statistics.sensor import StatisticsSensor
|
from homeassistant.components.statistics.sensor import DOMAIN, StatisticsSensor
|
||||||
from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, STATE_UNKNOWN, TEMP_CELSIUS
|
from homeassistant.const import (
|
||||||
from homeassistant.setup import setup_component
|
ATTR_UNIT_OF_MEASUREMENT,
|
||||||
|
SERVICE_RELOAD,
|
||||||
|
STATE_UNKNOWN,
|
||||||
|
TEMP_CELSIUS,
|
||||||
|
)
|
||||||
|
from homeassistant.setup import async_setup_component, setup_component
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
from tests.async_mock import patch
|
from tests.async_mock import patch
|
||||||
@ -442,3 +449,49 @@ class TestStatisticsSensor(unittest.TestCase):
|
|||||||
assert mock_data["return_time"] == state.attributes.get("max_age") + timedelta(
|
assert mock_data["return_time"] == state.attributes.get("max_age") + timedelta(
|
||||||
hours=1
|
hours=1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_reload(hass):
|
||||||
|
"""Verify we can reload filter sensors."""
|
||||||
|
await hass.async_add_executor_job(
|
||||||
|
init_recorder_component, hass
|
||||||
|
) # force in memory db
|
||||||
|
|
||||||
|
hass.states.async_set("sensor.test_monitored", 12345)
|
||||||
|
await async_setup_component(
|
||||||
|
hass,
|
||||||
|
"sensor",
|
||||||
|
{
|
||||||
|
"sensor": {
|
||||||
|
"platform": "statistics",
|
||||||
|
"name": "test",
|
||||||
|
"entity_id": "sensor.test_monitored",
|
||||||
|
"sampling_size": 100,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_start()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert len(hass.states.async_all()) == 2
|
||||||
|
|
||||||
|
assert hass.states.get("sensor.test")
|
||||||
|
|
||||||
|
yaml_path = path.join(
|
||||||
|
_get_fixtures_base_path(), "fixtures", "statistics/configuration.yaml",
|
||||||
|
)
|
||||||
|
with patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path):
|
||||||
|
await hass.services.async_call(
|
||||||
|
DOMAIN, SERVICE_RELOAD, {}, blocking=True,
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert len(hass.states.async_all()) == 2
|
||||||
|
|
||||||
|
assert hass.states.get("sensor.test") is None
|
||||||
|
assert hass.states.get("sensor.cputest")
|
||||||
|
|
||||||
|
|
||||||
|
def _get_fixtures_base_path():
|
||||||
|
return path.dirname(path.dirname(path.dirname(__file__)))
|
||||||
|
4
tests/fixtures/statistics/configuration.yaml
vendored
Normal file
4
tests/fixtures/statistics/configuration.yaml
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
sensor:
|
||||||
|
- platform: statistics
|
||||||
|
entity_id: sensor.cpu
|
||||||
|
name: cputest
|
Loading…
x
Reference in New Issue
Block a user