Fire lovelace updated event when update detected (#25507)

pull/25501/head
Paulus Schoutsen 2019-07-26 20:30:51 -07:00 committed by GitHub
parent 0a6d49b293
commit 9efb759a98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 6 deletions

View File

@ -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."""

View File

@ -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."""