diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index 3863ab0c88d..2b775e3a602 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -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( diff --git a/tests/components/automation/common.py b/tests/components/automation/common.py index c7aa8f1eced..729a6bb7212 100644 --- a/tests/components/automation/common.py +++ b/tests/components/automation/common.py @@ -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 + ) diff --git a/tests/components/automation/test_init.py b/tests/components/automation/test_init.py index a0573ce7c1b..d5498c04814 100644 --- a/tests/components/automation/test_init.py +++ b/tests/components/automation/test_init.py @@ -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()