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
pull/39262/head
J. Nick Koston 2020-08-25 19:05:29 -05:00 committed by GitHub
parent 758c0adb5e
commit 5018e53b33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 77 additions and 2 deletions

View File

@ -1 +1,4 @@
"""The rest component."""
DOMAIN = "rest"
PLATFORMS = ["binary_sensor", "sensor", "switch"]

View File

@ -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)

View File

@ -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)

View File

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

View File

@ -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)

View File

@ -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__)))

View File

@ -0,0 +1,5 @@
sensor:
- platform: rest
resource: "http://localhost"
method: GET
name: rollout