Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force refresh Lovelace #19073

Merged
merged 4 commits into from
Dec 7, 2018
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 28 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,31 @@ 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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All this code… that's kinda why we have voluptuous 😛

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) or
isinstance(view['id'], 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) or
isinstance(card['id'], 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 +548,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