From 1883b1d2a264ebdae30c2dd975df95262c192ec5 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Tue, 2 Jun 2020 00:19:00 +0200 Subject: [PATCH] Add config reloaded events for automation and scene (#34664) --- homeassistant/components/automation/__init__.py | 3 +++ homeassistant/components/homeassistant/scene.py | 3 +++ tests/components/automation/test_init.py | 9 ++++++++- tests/components/homeassistant/test_scene.py | 8 ++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index e5b66594d2f..42f8c9777f5 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -61,6 +61,8 @@ CONDITION_TYPE_OR = "or" DEFAULT_CONDITION_TYPE = CONDITION_TYPE_AND DEFAULT_INITIAL_STATE = True +EVENT_AUTOMATION_RELOADED = "automation_reloaded" + ATTR_LAST_TRIGGERED = "last_triggered" ATTR_VARIABLES = "variables" SERVICE_TRIGGER = "trigger" @@ -214,6 +216,7 @@ async def async_setup(hass, config): if conf is None: return await _async_process_config(hass, conf, component) + hass.bus.async_fire(EVENT_AUTOMATION_RELOADED, context=service_call.context) async_register_admin_service( hass, DOMAIN, SERVICE_RELOAD, reload_service_handler, schema=vol.Schema({}) diff --git a/homeassistant/components/homeassistant/scene.py b/homeassistant/components/homeassistant/scene.py index d14ef438a66..7e4a0433344 100644 --- a/homeassistant/components/homeassistant/scene.py +++ b/homeassistant/components/homeassistant/scene.py @@ -75,6 +75,7 @@ def _ensure_no_intersection(value): CONF_SCENE_ID = "scene_id" CONF_SNAPSHOT = "snapshot_entities" DATA_PLATFORM = "homeassistant_scene" +EVENT_SCENE_RELOADED = "scene_reloaded" STATES_SCHEMA = vol.All(dict, _convert_states) @@ -182,6 +183,8 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= _process_scenes_config(hass, async_add_entities, p_config) + hass.bus.async_fire(EVENT_SCENE_RELOADED, context=call.context) + hass.helpers.service.async_register_admin_service( SCENE_DOMAIN, SERVICE_RELOAD, reload_config ) diff --git a/tests/components/automation/test_init.py b/tests/components/automation/test_init.py index 7a082ba1931..a8baa9cfdb7 100644 --- a/tests/components/automation/test_init.py +++ b/tests/components/automation/test_init.py @@ -4,7 +4,7 @@ from datetime import timedelta import pytest import homeassistant.components.automation as automation -from homeassistant.components.automation import DOMAIN +from homeassistant.components.automation import DOMAIN, EVENT_AUTOMATION_RELOADED from homeassistant.const import ( ATTR_ENTITY_ID, ATTR_NAME, @@ -483,6 +483,11 @@ async def test_reload_config_service(hass, calls, hass_admin_user, hass_read_onl assert len(calls) == 1 assert calls[0].data.get("event") == "test_event" + test_reload_event = [] + hass.bus.async_listen( + EVENT_AUTOMATION_RELOADED, lambda event: test_reload_event.append(event) + ) + with patch( "homeassistant.config.load_yaml_config_file", autospec=True, @@ -505,6 +510,8 @@ async def test_reload_config_service(hass, calls, hass_admin_user, hass_read_onl # De-flake ?! await hass.async_block_till_done() + assert len(test_reload_event) == 1 + assert hass.states.get("automation.hello") is None assert hass.states.get("automation.bye") is not None listeners = hass.bus.async_listeners() diff --git a/tests/components/homeassistant/test_scene.py b/tests/components/homeassistant/test_scene.py index 98afbd23e22..a1f502a3475 100644 --- a/tests/components/homeassistant/test_scene.py +++ b/tests/components/homeassistant/test_scene.py @@ -3,6 +3,7 @@ import pytest import voluptuous as vol from homeassistant.components.homeassistant import scene as ha_scene +from homeassistant.components.homeassistant.scene import EVENT_SCENE_RELOADED from homeassistant.setup import async_setup_component from tests.async_mock import patch @@ -13,6 +14,11 @@ async def test_reload_config_service(hass): """Test the reload config service.""" assert await async_setup_component(hass, "scene", {}) + test_reloaded_event = [] + hass.bus.async_listen( + EVENT_SCENE_RELOADED, lambda event: test_reloaded_event.append(event) + ) + with patch( "homeassistant.config.load_yaml_config_file", autospec=True, @@ -22,6 +28,7 @@ async def test_reload_config_service(hass): await hass.async_block_till_done() assert hass.states.get("scene.hallo") is not None + assert len(test_reloaded_event) == 1 with patch( "homeassistant.config.load_yaml_config_file", @@ -31,6 +38,7 @@ async def test_reload_config_service(hass): await hass.services.async_call("scene", "reload", blocking=True) await hass.async_block_till_done() + assert len(test_reloaded_event) == 2 assert hass.states.get("scene.hallo") is None assert hass.states.get("scene.bye") is not None