1
mirror of https://github.com/home-assistant/core synced 2024-09-06 10:29:55 +02:00

Add the ability to reload trend platforms from yaml (#39341)

This commit is contained in:
J. Nick Koston 2020-08-27 23:50:28 -05:00 committed by GitHub
parent 526c418e1e
commit 0db5bb27a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 68 additions and 3 deletions

View File

@ -1 +1,4 @@
"""A sensor that monitors trends in other components."""
DOMAIN = "trend"
PLATFORMS = ["binary_sensor"]

View File

@ -26,8 +26,11 @@ from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import generate_entity_id
from homeassistant.helpers.event import async_track_state_change_event
from homeassistant.helpers.reload import setup_reload_service
from homeassistant.util import utcnow
from . import DOMAIN, PLATFORMS
_LOGGER = logging.getLogger(__name__)
ATTR_ATTRIBUTE = "attribute"
@ -63,6 +66,9 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the trend sensors."""
setup_reload_service(hass, DOMAIN, PLATFORMS)
sensors = []
for device_id, device_config in config[CONF_SENSORS].items():
@ -179,8 +185,10 @@ class SensorTrend(BinarySensorEntity):
except (ValueError, TypeError) as ex:
_LOGGER.error(ex)
async_track_state_change_event(
self.hass, [self._entity_id], trend_sensor_state_listener
self.async_on_remove(
async_track_state_change_event(
self.hass, [self._entity_id], trend_sensor_state_listener
)
)
async def async_update(self):

View File

@ -0,0 +1,2 @@
reload:
description: Reload all trend entities.

View File

@ -1,7 +1,10 @@
"""The test for the Trend sensor platform."""
from datetime import timedelta
from os import path
from homeassistant import setup
from homeassistant import config as hass_config, setup
from homeassistant.components.trend import DOMAIN
from homeassistant.const import SERVICE_RELOAD
import homeassistant.util.dt as dt_util
from tests.async_mock import patch
@ -370,3 +373,47 @@ class TestTrendBinarySensor:
self.hass, "binary_sensor", {"binary_sensor": {"platform": "trend"}}
)
assert self.hass.states.all() == []
async def test_reload(hass):
"""Verify we can reload trend sensors."""
hass.states.async_set("sensor.test_state", 1234)
await setup.async_setup_component(
hass,
"binary_sensor",
{
"binary_sensor": {
"platform": "trend",
"sensors": {"test_trend_sensor": {"entity_id": "sensor.test_state"}},
}
},
)
await hass.async_block_till_done()
assert len(hass.states.async_all()) == 2
assert hass.states.get("binary_sensor.test_trend_sensor")
yaml_path = path.join(
_get_fixtures_base_path(),
"fixtures",
"trend/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("binary_sensor.test_trend_sensor") is None
assert hass.states.get("binary_sensor.second_test_trend_sensor")
def _get_fixtures_base_path():
return path.dirname(path.dirname(path.dirname(__file__)))

View File

@ -0,0 +1,5 @@
binary_sensor:
- platform: trend
sensors:
second_test_trend_sensor:
entity_id: sensor.test_state