From 9efb759a980c98dcd016667ca8b7892460f19fb6 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Fri, 26 Jul 2019 20:30:51 -0700 Subject: [PATCH] Fire lovelace updated event when update detected (#25507) --- homeassistant/components/lovelace/__init__.py | 16 ++++++++---- tests/components/lovelace/test_init.py | 25 ++++++++++++++++++- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/lovelace/__init__.py b/homeassistant/components/lovelace/__init__.py index b1b9cf1a524..c2357c842ff 100644 --- a/homeassistant/components/lovelace/__init__.py +++ b/homeassistant/components/lovelace/__init__.py @@ -115,9 +115,8 @@ class LovelaceStorage: if self._data is None: await self._load() self._data['config'] = config - await self._store.async_save(self._data) - self._hass.bus.async_fire(EVENT_LOVELACE_UPDATED) + await self._store.async_save(self._data) async def _load(self): """Load the config.""" @@ -148,7 +147,12 @@ class LovelaceYAML: async def async_load(self, force): """Load config.""" - return await self.hass.async_add_executor_job(self._load_config, force) + is_updated, config = await self.hass.async_add_executor_job( + self._load_config, force + ) + if is_updated: + self.hass.bus.async_fire(EVENT_LOVELACE_UPDATED) + return config def _load_config(self, force): """Load the actual config.""" @@ -158,7 +162,9 @@ class LovelaceYAML: config, last_update = self._cache modtime = os.path.getmtime(fname) if config and last_update > modtime: - return config + return False, config + + is_updated = self._cache is not None try: config = load_yaml(fname) @@ -166,7 +172,7 @@ class LovelaceYAML: raise ConfigNotFound from None self._cache = (config, time.time()) - return config + return is_updated, config async def async_save(self, config): """Save config.""" diff --git a/tests/components/lovelace/test_init.py b/tests/components/lovelace/test_init.py index 7aa4ef0f5b3..56c3282e860 100644 --- a/tests/components/lovelace/test_init.py +++ b/tests/components/lovelace/test_init.py @@ -4,7 +4,7 @@ from unittest.mock import patch from homeassistant.setup import async_setup_component from homeassistant.components import frontend, lovelace -from tests.common import get_system_health_info +from tests.common import get_system_health_info, async_capture_events async def test_lovelace_from_storage(hass, hass_ws_client, hass_storage): @@ -26,6 +26,8 @@ async def test_lovelace_from_storage(hass, hass_ws_client, hass_storage): assert response['error']['code'] == 'config_not_found' # Store new config + events = async_capture_events(hass, lovelace.EVENT_LOVELACE_UPDATED) + await client.send_json({ 'id': 6, 'type': 'lovelace/config/save', @@ -38,6 +40,7 @@ async def test_lovelace_from_storage(hass, hass_ws_client, hass_storage): assert hass_storage[lovelace.STORAGE_KEY]['data'] == { 'config': {'yo': 'hello'} } + assert len(events) == 1 # Load new config await client.send_json({ @@ -108,6 +111,8 @@ async def test_lovelace_from_yaml(hass, hass_ws_client): assert not response['success'] # Patch data + events = async_capture_events(hass, lovelace.EVENT_LOVELACE_UPDATED) + with patch('homeassistant.components.lovelace.load_yaml', return_value={ 'hello': 'yo' }): @@ -120,6 +125,24 @@ async def test_lovelace_from_yaml(hass, hass_ws_client): assert response['success'] assert response['result'] == {'hello': 'yo'} + assert len(events) == 0 + + # Fake new data to see we fire event + with patch('homeassistant.components.lovelace.load_yaml', return_value={ + 'hello': 'yo2' + }): + await client.send_json({ + 'id': 8, + 'type': 'lovelace/config', + 'force': True, + }) + response = await client.receive_json() + + assert response['success'] + assert response['result'] == {'hello': 'yo2'} + + assert len(events) == 1 + async def test_system_health_info_autogen(hass): """Test system health info endpoint."""