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

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.""" """The test for the Trend sensor platform."""
from datetime import timedelta 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 import homeassistant.util.dt as dt_util
from tests.async_mock import patch from tests.async_mock import patch
@ -370,3 +373,47 @@ class TestTrendBinarySensor:
self.hass, "binary_sensor", {"binary_sensor": {"platform": "trend"}} self.hass, "binary_sensor", {"binary_sensor": {"platform": "trend"}}
) )
assert self.hass.states.all() == [] 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