Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to reload the rest platforms from yaml #39257

Merged
merged 2 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions homeassistant/components/rest/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
"""The rest component."""

DOMAIN = "rest"
PLATFORMS = ["binary_sensor", "sensor", "switch"]
5 changes: 5 additions & 0 deletions homeassistant/components/rest/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
)
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__)
Expand Down Expand Up @@ -68,6 +70,9 @@

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)
Expand Down
5 changes: 5 additions & 0 deletions homeassistant/components/rest/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
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__)

Expand Down Expand Up @@ -78,6 +81,8 @@

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)
Expand Down
2 changes: 2 additions & 0 deletions homeassistant/components/rest/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
reload:
description: Reload all rest entities.
6 changes: 6 additions & 0 deletions homeassistant/components/rest/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
)
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__)

Expand Down Expand Up @@ -58,6 +61,9 @@

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)
Expand Down
53 changes: 51 additions & 2 deletions tests/components/rest/test_sensor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""The tests for the REST sensor platform."""
from os import path
import unittest

import pytest
Expand All @@ -8,12 +9,13 @@
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
Expand Down Expand Up @@ -677,3 +679,50 @@ def test_update_request_exception(self, mock_req):
"""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:
bdraco marked this conversation as resolved.
Show resolved Hide resolved
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__)))
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 5 additions & 0 deletions tests/fixtures/rest/configuration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sensor:
- platform: rest
resource: "http://localhost"
method: GET
name: rollout