Register automation.reload service as an admin service. (#29582)

* homeassistant/components/automation/__init__.py

* Register automation.reload as an admin service.
This commit is contained in:
Alexei Chetroi 2019-12-07 15:17:30 -05:00 committed by Paulus Schoutsen
parent d838a56c1d
commit ccb0fd5e32
3 changed files with 25 additions and 15 deletions

View File

@ -27,6 +27,7 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.config_validation import make_entity_service_schema
from homeassistant.helpers.entity import ToggleEntity
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.service import async_register_admin_service
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.typing import TemplateVarsType
from homeassistant.loader import bind_hass
@ -179,8 +180,12 @@ async def async_setup(hass, config):
DOMAIN, SERVICE_TRIGGER, trigger_service_handler, schema=TRIGGER_SERVICE_SCHEMA
)
hass.services.async_register(
DOMAIN, SERVICE_RELOAD, reload_service_handler, schema=RELOAD_SERVICE_SCHEMA
async_register_admin_service(
hass,
DOMAIN,
SERVICE_RELOAD,
reload_service_handler,
schema=RELOAD_SERVICE_SCHEMA,
)
hass.services.async_register(

View File

@ -44,6 +44,8 @@ async def async_trigger(hass, entity_id=ENTITY_MATCH_ALL):
@bind_hass
async def async_reload(hass):
async def async_reload(hass, context=None):
"""Reload the automation from config."""
await hass.services.async_call(DOMAIN, SERVICE_RELOAD)
await hass.services.async_call(
DOMAIN, SERVICE_RELOAD, blocking=True, context=context
)

View File

@ -1,28 +1,28 @@
"""The tests for the automation component."""
from datetime import timedelta
from unittest.mock import patch, Mock
from unittest.mock import Mock, patch
import pytest
from homeassistant.core import State, CoreState, Context
from homeassistant.setup import async_setup_component
import homeassistant.components.automation as automation
from homeassistant.const import (
ATTR_NAME,
ATTR_ENTITY_ID,
STATE_ON,
STATE_OFF,
EVENT_HOMEASSISTANT_START,
ATTR_NAME,
EVENT_AUTOMATION_TRIGGERED,
EVENT_HOMEASSISTANT_START,
STATE_OFF,
STATE_ON,
)
from homeassistant.exceptions import HomeAssistantError
from homeassistant.core import Context, CoreState, State
from homeassistant.exceptions import HomeAssistantError, Unauthorized
from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util
from tests.common import (
assert_setup_component,
async_fire_time_changed,
mock_restore_cache,
async_mock_service,
mock_restore_cache,
)
from tests.components.automation import common
@ -445,7 +445,7 @@ async def test_services(hass, calls):
assert automation.is_on(hass, entity_id)
async def test_reload_config_service(hass, calls):
async def test_reload_config_service(hass, calls, hass_admin_user, hass_read_only_user):
"""Test the reload config service."""
assert await async_setup_component(
hass,
@ -488,7 +488,10 @@ async def test_reload_config_service(hass, calls):
},
):
with patch("homeassistant.config.find_config_file", return_value=""):
await common.async_reload(hass)
with pytest.raises(Unauthorized):
await common.async_reload(hass, Context(user_id=hass_read_only_user.id))
await hass.async_block_till_done()
await common.async_reload(hass, Context(user_id=hass_admin_user.id))
await hass.async_block_till_done()
# De-flake ?!
await hass.async_block_till_done()