Skip to content

Commit

Permalink
Force refresh Lovelace (#19073)
Browse files Browse the repository at this point in the history
* Force refresh Lovelace

* Check config on load

* Update __init__.py

* Update __init__.py
  • Loading branch information
bramkragten authored and balloob committed Dec 7, 2018
1 parent 3eb646e commit ff9427d
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions homeassistant/components/lovelace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
SCHEMA_GET_LOVELACE_UI = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend({
vol.Required('type'):
vol.Any(WS_TYPE_GET_LOVELACE_UI, OLD_WS_TYPE_GET_LOVELACE_UI),
vol.Optional('force', default=False): bool,
})

SCHEMA_MIGRATE_CONFIG = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend({
Expand Down Expand Up @@ -144,12 +145,12 @@ class DuplicateIdError(HomeAssistantError):
"""Duplicate ID's."""


def load_config(hass) -> JSON_TYPE:
def load_config(hass, force: bool) -> JSON_TYPE:
"""Load a YAML file."""
fname = hass.config.path(LOVELACE_CONFIG_FILE)

# Check for a cached version of the config
if LOVELACE_DATA in hass.data:
if not force and LOVELACE_DATA in hass.data:
config, last_update = hass.data[LOVELACE_DATA]
modtime = os.path.getmtime(fname)
if config and last_update > modtime:
Expand All @@ -158,23 +159,29 @@ def load_config(hass) -> JSON_TYPE:
config = yaml.load_yaml(fname, False)
seen_card_ids = set()
seen_view_ids = set()
if 'views' in config and not isinstance(config['views'], list):
raise HomeAssistantError("Views should be a list.")
for view in config.get('views', []):
view_id = view.get('id')
if view_id:
view_id = str(view_id)
if view_id in seen_view_ids:
raise DuplicateIdError(
'ID `{}` has multiple occurances in views'.format(view_id))
seen_view_ids.add(view_id)
if 'id' in view and not isinstance(view['id'], (str, int)):
raise HomeAssistantError(
"Your config contains view(s) with invalid ID(s).")
view_id = str(view.get('id', ''))
if view_id in seen_view_ids:
raise DuplicateIdError(
'ID `{}` has multiple occurances in views'.format(view_id))
seen_view_ids.add(view_id)
if 'cards' in view and not isinstance(view['cards'], list):
raise HomeAssistantError("Cards should be a list.")
for card in view.get('cards', []):
card_id = card.get('id')
if card_id:
card_id = str(card_id)
if card_id in seen_card_ids:
raise DuplicateIdError(
'ID `{}` has multiple occurances in cards'
.format(card_id))
seen_card_ids.add(card_id)
if 'id' in card and not isinstance(card['id'], (str, int)):
raise HomeAssistantError(
"Your config contains card(s) with invalid ID(s).")
card_id = str(card.get('id', ''))
if card_id in seen_card_ids:
raise DuplicateIdError(
'ID `{}` has multiple occurances in cards'
.format(card_id))
seen_card_ids.add(card_id)
hass.data[LOVELACE_DATA] = (config, time.time())
return config

Expand Down Expand Up @@ -539,7 +546,8 @@ async def send_with_error_handling(hass, connection, msg):
@handle_yaml_errors
async def websocket_lovelace_config(hass, connection, msg):
"""Send Lovelace UI config over WebSocket configuration."""
return await hass.async_add_executor_job(load_config, hass)
return await hass.async_add_executor_job(load_config, hass,
msg.get('force', False))


@websocket_api.async_response
Expand Down

0 comments on commit ff9427d

Please sign in to comment.