mirror of
https://github.com/home-assistant/core
synced 2024-09-06 10:29:55 +02:00
Add support for reloading min_max from yaml (#39327)
* Add support for reloading min_max from yaml * git add
This commit is contained in:
parent
e6141ae558
commit
d2195e2b37
@ -1 +1,4 @@
|
||||
"""The min_max component."""
|
||||
|
||||
DOMAIN = "min_max"
|
||||
PLATFORMS = ["sensor"]
|
||||
|
@ -15,6 +15,9 @@ from homeassistant.core import callback
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.event import async_track_state_change_event
|
||||
from homeassistant.helpers.reload import async_setup_reload_service
|
||||
|
||||
from . import DOMAIN, PLATFORMS
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@ -72,6 +75,8 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
||||
sensor_type = config.get(CONF_TYPE)
|
||||
round_digits = config.get(CONF_ROUND_DIGITS)
|
||||
|
||||
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
|
||||
|
||||
async_add_entities(
|
||||
[MinMaxSensor(hass, entity_ids, name, sensor_type, round_digits)], True
|
||||
)
|
||||
@ -188,8 +193,10 @@ class MinMaxSensor(Entity):
|
||||
|
||||
hass.async_add_job(self.async_update_ha_state, True)
|
||||
|
||||
async_track_state_change_event(
|
||||
hass, entity_ids, async_min_max_sensor_state_listener
|
||||
self.async_on_remove(
|
||||
async_track_state_change_event(
|
||||
hass, entity_ids, async_min_max_sensor_state_listener
|
||||
)
|
||||
)
|
||||
|
||||
@property
|
||||
|
2
homeassistant/components/min_max/services.yaml
Normal file
2
homeassistant/components/min_max/services.yaml
Normal file
@ -0,0 +1,2 @@
|
||||
reload:
|
||||
description: Reload all min_max entities.
|
@ -1,16 +1,22 @@
|
||||
"""The test for the min/max sensor platform."""
|
||||
from os import path
|
||||
import statistics
|
||||
import unittest
|
||||
|
||||
from asynctest.mock import patch
|
||||
|
||||
from homeassistant import config as hass_config
|
||||
from homeassistant.components.min_max import DOMAIN
|
||||
from homeassistant.const import (
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
SERVICE_RELOAD,
|
||||
STATE_UNAVAILABLE,
|
||||
STATE_UNKNOWN,
|
||||
TEMP_CELSIUS,
|
||||
TEMP_FAHRENHEIT,
|
||||
UNIT_PERCENTAGE,
|
||||
)
|
||||
from homeassistant.setup import setup_component
|
||||
from homeassistant.setup import async_setup_component, setup_component
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
|
||||
@ -332,3 +338,50 @@ class TestMinMaxSensor(unittest.TestCase):
|
||||
assert self.max == state.attributes.get("max_value")
|
||||
assert self.mean == state.attributes.get("mean")
|
||||
assert self.median == state.attributes.get("median")
|
||||
|
||||
|
||||
async def test_reload(hass):
|
||||
"""Verify we can reload filter sensors."""
|
||||
hass.states.async_set("sensor.test_1", 12345)
|
||||
hass.states.async_set("sensor.test_2", 45678)
|
||||
|
||||
await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "min_max",
|
||||
"name": "test",
|
||||
"type": "mean",
|
||||
"entity_ids": ["sensor.test_1", "sensor.test_2"],
|
||||
}
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.states.async_all()) == 3
|
||||
|
||||
assert hass.states.get("sensor.test")
|
||||
|
||||
yaml_path = path.join(
|
||||
_get_fixtures_base_path(),
|
||||
"fixtures",
|
||||
"min_max/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()) == 3
|
||||
|
||||
assert hass.states.get("sensor.test") is None
|
||||
assert hass.states.get("sensor.second_test")
|
||||
|
||||
|
||||
def _get_fixtures_base_path():
|
||||
return path.dirname(path.dirname(path.dirname(__file__)))
|
||||
|
7
tests/fixtures/min_max/configuration.yaml
vendored
Normal file
7
tests/fixtures/min_max/configuration.yaml
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
sensor:
|
||||
- platform: min_max
|
||||
entity_ids:
|
||||
- sensor.test_1
|
||||
- sensor.test_2
|
||||
name: second_test
|
||||
type: mean
|
Loading…
Reference in New Issue
Block a user