From 5018e53b33382e10c8465d87cd8fa483b864dbd9 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 25 Aug 2020 19:05:29 -0500 Subject: [PATCH] Add the ability to reload the rest platforms from yaml (#39257) * Add the ability to reload rest platforms from yaml * Revert changes to notify as these will be done in another pass --- homeassistant/components/rest/__init__.py | 3 ++ .../components/rest/binary_sensor.py | 5 ++ homeassistant/components/rest/sensor.py | 5 ++ homeassistant/components/rest/services.yaml | 2 + homeassistant/components/rest/switch.py | 6 +++ tests/components/rest/test_sensor.py | 53 ++++++++++++++++++- tests/fixtures/rest/configuration.yaml | 5 ++ 7 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 homeassistant/components/rest/services.yaml create mode 100644 tests/fixtures/rest/configuration.yaml diff --git a/homeassistant/components/rest/__init__.py b/homeassistant/components/rest/__init__.py index fcdf39e8398..e0b743c1d37 100644 --- a/homeassistant/components/rest/__init__.py +++ b/homeassistant/components/rest/__init__.py @@ -1 +1,4 @@ """The rest component.""" + +DOMAIN = "rest" +PLATFORMS = ["binary_sensor", "sensor", "switch"] diff --git a/homeassistant/components/rest/binary_sensor.py b/homeassistant/components/rest/binary_sensor.py index a78c6aa5f2b..c5b8f16162a 100644 --- a/homeassistant/components/rest/binary_sensor.py +++ b/homeassistant/components/rest/binary_sensor.py @@ -29,7 +29,9 @@ from homeassistant.const import ( ) from homeassistant.exceptions import PlatformNotReady import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.reload import setup_reload_service +from . import DOMAIN, PLATFORMS from .sensor import RestData _LOGGER = logging.getLogger(__name__) @@ -68,6 +70,9 @@ PLATFORM_SCHEMA = vol.All( def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the REST binary sensor.""" + + setup_reload_service(hass, DOMAIN, PLATFORMS) + name = config.get(CONF_NAME) resource = config.get(CONF_RESOURCE) resource_template = config.get(CONF_RESOURCE_TEMPLATE) diff --git a/homeassistant/components/rest/sensor.py b/homeassistant/components/rest/sensor.py index 2c8df9625cd..9925eb016cb 100644 --- a/homeassistant/components/rest/sensor.py +++ b/homeassistant/components/rest/sensor.py @@ -33,6 +33,9 @@ from homeassistant.const import ( from homeassistant.exceptions import PlatformNotReady import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import Entity +from homeassistant.helpers.reload import setup_reload_service + +from . import DOMAIN, PLATFORMS _LOGGER = logging.getLogger(__name__) @@ -78,6 +81,8 @@ PLATFORM_SCHEMA = vol.All( def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the RESTful sensor.""" + setup_reload_service(hass, DOMAIN, PLATFORMS) + name = config.get(CONF_NAME) resource = config.get(CONF_RESOURCE) resource_template = config.get(CONF_RESOURCE_TEMPLATE) diff --git a/homeassistant/components/rest/services.yaml b/homeassistant/components/rest/services.yaml new file mode 100644 index 00000000000..717859d0c12 --- /dev/null +++ b/homeassistant/components/rest/services.yaml @@ -0,0 +1,2 @@ +reload: + description: Reload all rest entities. diff --git a/homeassistant/components/rest/switch.py b/homeassistant/components/rest/switch.py index ab880201072..6a9b4b2ac5b 100644 --- a/homeassistant/components/rest/switch.py +++ b/homeassistant/components/rest/switch.py @@ -21,6 +21,9 @@ from homeassistant.const import ( ) from homeassistant.helpers.aiohttp_client import async_get_clientsession import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.reload import async_setup_reload_service + +from . import DOMAIN, PLATFORMS _LOGGER = logging.getLogger(__name__) @@ -58,6 +61,9 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up the RESTful switch.""" + + await async_setup_reload_service(hass, DOMAIN, PLATFORMS) + body_off = config.get(CONF_BODY_OFF) body_on = config.get(CONF_BODY_ON) is_on_template = config.get(CONF_IS_ON_TEMPLATE) diff --git a/tests/components/rest/test_sensor.py b/tests/components/rest/test_sensor.py index 90a8b8d361e..39197680887 100644 --- a/tests/components/rest/test_sensor.py +++ b/tests/components/rest/test_sensor.py @@ -1,4 +1,5 @@ """The tests for the REST sensor platform.""" +from os import path import unittest import pytest @@ -8,12 +9,13 @@ from requests.exceptions import RequestException, Timeout from requests.structures import CaseInsensitiveDict import requests_mock +from homeassistant import config as hass_config import homeassistant.components.rest.sensor as rest import homeassistant.components.sensor as sensor -from homeassistant.const import DATA_MEGABYTES +from homeassistant.const import DATA_MEGABYTES, SERVICE_RELOAD from homeassistant.exceptions import PlatformNotReady from homeassistant.helpers.config_validation import template -from homeassistant.setup import setup_component +from homeassistant.setup import async_setup_component, setup_component from tests.async_mock import Mock, patch from tests.common import assert_setup_component, get_test_home_assistant @@ -677,3 +679,50 @@ class TestRestData(unittest.TestCase): """Test update when a request exception occurs.""" self.rest.update() assert self.rest.data is None + + +async def test_reload(hass): + """Verify we can reload reset sensors.""" + + with requests_mock.Mocker() as mock_req: + mock_req.get("http://localhost", text="test data") + await async_setup_component( + hass, + "sensor", + { + "sensor": { + "platform": "rest", + "method": "GET", + "name": "mockrest", + "resource": "http://localhost", + } + }, + ) + await hass.async_block_till_done() + await hass.async_start() + await hass.async_block_till_done() + + assert len(hass.states.async_all()) == 1 + + assert hass.states.get("sensor.mockrest") + + yaml_path = path.join( + _get_fixtures_base_path(), "fixtures", "rest/configuration.yaml", + ) + with patch.object( + hass_config, "YAML_CONFIG_FILE", yaml_path + ), requests_mock.Mocker() as mock_req: + mock_req.get("http://localhost", text="test data 2") + await hass.services.async_call( + "rest", SERVICE_RELOAD, {}, blocking=True, + ) + await hass.async_block_till_done() + + assert len(hass.states.async_all()) == 1 + + assert hass.states.get("sensor.mockreset") is None + assert hass.states.get("sensor.rollout") + + +def _get_fixtures_base_path(): + return path.dirname(path.dirname(path.dirname(__file__))) diff --git a/tests/fixtures/rest/configuration.yaml b/tests/fixtures/rest/configuration.yaml new file mode 100644 index 00000000000..7848a429026 --- /dev/null +++ b/tests/fixtures/rest/configuration.yaml @@ -0,0 +1,5 @@ +sensor: + - platform: rest + resource: "http://localhost" + method: GET + name: rollout