Fire lovelace updated event when update detected (#25507)
This commit is contained in:
parent
0a6d49b293
commit
9efb759a98
2 changed files with 35 additions and 6 deletions
|
@ -115,9 +115,8 @@ class LovelaceStorage:
|
||||||
if self._data is None:
|
if self._data is None:
|
||||||
await self._load()
|
await self._load()
|
||||||
self._data['config'] = config
|
self._data['config'] = config
|
||||||
await self._store.async_save(self._data)
|
|
||||||
|
|
||||||
self._hass.bus.async_fire(EVENT_LOVELACE_UPDATED)
|
self._hass.bus.async_fire(EVENT_LOVELACE_UPDATED)
|
||||||
|
await self._store.async_save(self._data)
|
||||||
|
|
||||||
async def _load(self):
|
async def _load(self):
|
||||||
"""Load the config."""
|
"""Load the config."""
|
||||||
|
@ -148,7 +147,12 @@ class LovelaceYAML:
|
||||||
|
|
||||||
async def async_load(self, force):
|
async def async_load(self, force):
|
||||||
"""Load config."""
|
"""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):
|
def _load_config(self, force):
|
||||||
"""Load the actual config."""
|
"""Load the actual config."""
|
||||||
|
@ -158,7 +162,9 @@ class LovelaceYAML:
|
||||||
config, last_update = self._cache
|
config, last_update = self._cache
|
||||||
modtime = os.path.getmtime(fname)
|
modtime = os.path.getmtime(fname)
|
||||||
if config and last_update > modtime:
|
if config and last_update > modtime:
|
||||||
return config
|
return False, config
|
||||||
|
|
||||||
|
is_updated = self._cache is not None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
config = load_yaml(fname)
|
config = load_yaml(fname)
|
||||||
|
@ -166,7 +172,7 @@ class LovelaceYAML:
|
||||||
raise ConfigNotFound from None
|
raise ConfigNotFound from None
|
||||||
|
|
||||||
self._cache = (config, time.time())
|
self._cache = (config, time.time())
|
||||||
return config
|
return is_updated, config
|
||||||
|
|
||||||
async def async_save(self, config):
|
async def async_save(self, config):
|
||||||
"""Save config."""
|
"""Save config."""
|
||||||
|
|
|
@ -4,7 +4,7 @@ from unittest.mock import patch
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
from homeassistant.components import frontend, lovelace
|
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):
|
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'
|
assert response['error']['code'] == 'config_not_found'
|
||||||
|
|
||||||
# Store new config
|
# Store new config
|
||||||
|
events = async_capture_events(hass, lovelace.EVENT_LOVELACE_UPDATED)
|
||||||
|
|
||||||
await client.send_json({
|
await client.send_json({
|
||||||
'id': 6,
|
'id': 6,
|
||||||
'type': 'lovelace/config/save',
|
'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'] == {
|
assert hass_storage[lovelace.STORAGE_KEY]['data'] == {
|
||||||
'config': {'yo': 'hello'}
|
'config': {'yo': 'hello'}
|
||||||
}
|
}
|
||||||
|
assert len(events) == 1
|
||||||
|
|
||||||
# Load new config
|
# Load new config
|
||||||
await client.send_json({
|
await client.send_json({
|
||||||
|
@ -108,6 +111,8 @@ async def test_lovelace_from_yaml(hass, hass_ws_client):
|
||||||
assert not response['success']
|
assert not response['success']
|
||||||
|
|
||||||
# Patch data
|
# Patch data
|
||||||
|
events = async_capture_events(hass, lovelace.EVENT_LOVELACE_UPDATED)
|
||||||
|
|
||||||
with patch('homeassistant.components.lovelace.load_yaml', return_value={
|
with patch('homeassistant.components.lovelace.load_yaml', return_value={
|
||||||
'hello': 'yo'
|
'hello': 'yo'
|
||||||
}):
|
}):
|
||||||
|
@ -120,6 +125,24 @@ async def test_lovelace_from_yaml(hass, hass_ws_client):
|
||||||
assert response['success']
|
assert response['success']
|
||||||
assert response['result'] == {'hello': 'yo'}
|
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):
|
async def test_system_health_info_autogen(hass):
|
||||||
"""Test system health info endpoint."""
|
"""Test system health info endpoint."""
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue