Move migrate to separate WS command (#17890)
This commit is contained in:
1 parent
d6e4208665
commit
9cd8a86eb4
2 files changed
+42
-4
No files matched your search
@@ -24,6 +24,7 @@ FORMAT_JSON = 'json'
|
||||
OLD_WS_TYPE_GET_LOVELACE_UI = 'frontend/lovelace_config'
|
||||
WS_TYPE_GET_LOVELACE_UI = 'lovelace/config'
|
||||
|
||||
WS_TYPE_MIGRATE_CONFIG = 'lovelace/config/migrate'
|
||||
WS_TYPE_GET_CARD = 'lovelace/config/card/get'
|
||||
WS_TYPE_UPDATE_CARD = 'lovelace/config/card/update'
|
||||
WS_TYPE_ADD_CARD = 'lovelace/config/card/add'
|
||||
@@ -35,6 +36,10 @@ SCHEMA_GET_LOVELACE_UI = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend({
|
||||
OLD_WS_TYPE_GET_LOVELACE_UI),
|
||||
})
|
||||
|
||||
SCHEMA_MIGRATE_CONFIG = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend({
|
||||
vol.Required('type'): WS_TYPE_MIGRATE_CONFIG,
|
||||
})
|
||||
|
||||
SCHEMA_GET_CARD = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend({
|
||||
vol.Required('type'): WS_TYPE_GET_CARD,
|
||||
vol.Required('card_id'): str,
|
||||
@@ -150,6 +155,11 @@ def load_yaml(fname: str) -> JSON_TYPE:
|
||||
|
||||
|
||||
def load_config(fname: str) -> JSON_TYPE:
|
||||
"""Load a YAML file."""
|
||||
return load_yaml(fname)
|
||||
|
||||
|
||||
def migrate_config(fname: str) -> JSON_TYPE:
|
||||
"""Load a YAML file and adds id to views and cards if not present."""
|
||||
config = load_yaml(fname)
|
||||
# Check if all views and cards have a unique id or else add one
|
||||
@@ -341,6 +351,10 @@ async def async_setup(hass, config):
|
||||
OLD_WS_TYPE_GET_LOVELACE_UI, websocket_lovelace_config,
|
||||
SCHEMA_GET_LOVELACE_UI)
|
||||
|
||||
hass.components.websocket_api.async_register_command(
|
||||
WS_TYPE_MIGRATE_CONFIG, websocket_lovelace_migrate_config,
|
||||
SCHEMA_MIGRATE_CONFIG)
|
||||
|
||||
hass.components.websocket_api.async_register_command(
|
||||
WS_TYPE_GET_LOVELACE_UI, websocket_lovelace_config,
|
||||
SCHEMA_GET_LOVELACE_UI)
|
||||
@@ -392,6 +406,30 @@ async def websocket_lovelace_config(hass, connection, msg):
|
||||
connection.send_message(message)
|
||||
|
||||
|
||||
@websocket_api.async_response
|
||||
async def websocket_lovelace_migrate_config(hass, connection, msg):
|
||||
"""Migrate lovelace UI config."""
|
||||
error = None
|
||||
try:
|
||||
config = await hass.async_add_executor_job(
|
||||
migrate_config, hass.config.path(LOVELACE_CONFIG_FILE))
|
||||
message = websocket_api.result_message(
|
||||
msg['id'], config
|
||||
)
|
||||
except FileNotFoundError:
|
||||
error = ('file_not_found',
|
||||
'Could not find ui-lovelace.yaml in your config dir.')
|
||||
except UnsupportedYamlError as err:
|
||||
error = 'unsupported_error', str(err)
|
||||
except HomeAssistantError as err:
|
||||
error = 'load_error', str(err)
|
||||
|
||||
if error is not None:
|
||||
message = websocket_api.error_message(msg['id'], *error)
|
||||
|
||||
connection.send_message(message)
|
||||
|
||||
|
||||
@websocket_api.async_response
|
||||
async def websocket_lovelace_get_card(hass, connection, msg):
|
||||
"""Send lovelace card config over websocket config."""
|
||||
|
||||
Reference in new issue
Block a user