mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 08:47:10 +00:00
Add command to delete lovelace config (#30558)
* Add command to delete lovelace config * Test + decorator for WS
This commit is contained in:
parent
1ce662d699
commit
16199b7a52
@ -36,23 +36,6 @@ EVENT_LOVELACE_UPDATED = "lovelace_updated"
|
||||
|
||||
LOVELACE_CONFIG_FILE = "ui-lovelace.yaml"
|
||||
|
||||
WS_TYPE_GET_LOVELACE_UI = "lovelace/config"
|
||||
WS_TYPE_SAVE_CONFIG = "lovelace/config/save"
|
||||
|
||||
SCHEMA_GET_LOVELACE_UI = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend(
|
||||
{
|
||||
vol.Required("type"): WS_TYPE_GET_LOVELACE_UI,
|
||||
vol.Optional("force", default=False): bool,
|
||||
}
|
||||
)
|
||||
|
||||
SCHEMA_SAVE_CONFIG = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend(
|
||||
{
|
||||
vol.Required("type"): WS_TYPE_SAVE_CONFIG,
|
||||
vol.Required("config"): vol.Any(str, dict),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class ConfigNotFound(HomeAssistantError):
|
||||
"""When no config available."""
|
||||
@ -72,12 +55,12 @@ async def async_setup(hass, config):
|
||||
else:
|
||||
hass.data[DOMAIN] = LovelaceStorage(hass)
|
||||
|
||||
hass.components.websocket_api.async_register_command(
|
||||
WS_TYPE_GET_LOVELACE_UI, websocket_lovelace_config, SCHEMA_GET_LOVELACE_UI
|
||||
)
|
||||
hass.components.websocket_api.async_register_command(websocket_lovelace_config)
|
||||
|
||||
hass.components.websocket_api.async_register_command(websocket_lovelace_save_config)
|
||||
|
||||
hass.components.websocket_api.async_register_command(
|
||||
WS_TYPE_SAVE_CONFIG, websocket_lovelace_save_config, SCHEMA_SAVE_CONFIG
|
||||
websocket_lovelace_delete_config
|
||||
)
|
||||
|
||||
hass.components.system_health.async_register_info(DOMAIN, system_health_info)
|
||||
@ -124,6 +107,10 @@ class LovelaceStorage:
|
||||
self._hass.bus.async_fire(EVENT_LOVELACE_UPDATED)
|
||||
await self._store.async_save(self._data)
|
||||
|
||||
async def async_delete(self):
|
||||
"""Delete config."""
|
||||
await self.async_save(None)
|
||||
|
||||
async def _load(self):
|
||||
"""Load the config."""
|
||||
data = await self._store.async_load()
|
||||
@ -185,6 +172,10 @@ class LovelaceYAML:
|
||||
"""Save config."""
|
||||
raise HomeAssistantError("Not supported")
|
||||
|
||||
async def async_delete(self):
|
||||
"""Delete config."""
|
||||
raise HomeAssistantError("Not supported")
|
||||
|
||||
|
||||
def handle_yaml_errors(func):
|
||||
"""Handle error with WebSocket calls."""
|
||||
@ -212,6 +203,9 @@ def handle_yaml_errors(func):
|
||||
|
||||
|
||||
@websocket_api.async_response
|
||||
@websocket_api.websocket_command(
|
||||
{"type": "lovelace/config", vol.Optional("force", default=False): bool}
|
||||
)
|
||||
@handle_yaml_errors
|
||||
async def websocket_lovelace_config(hass, connection, msg):
|
||||
"""Send Lovelace UI config over WebSocket configuration."""
|
||||
@ -219,12 +213,23 @@ async def websocket_lovelace_config(hass, connection, msg):
|
||||
|
||||
|
||||
@websocket_api.async_response
|
||||
@websocket_api.websocket_command(
|
||||
{"type": "lovelace/config/save", "config": vol.Any(str, dict)}
|
||||
)
|
||||
@handle_yaml_errors
|
||||
async def websocket_lovelace_save_config(hass, connection, msg):
|
||||
"""Save Lovelace UI configuration."""
|
||||
await hass.data[DOMAIN].async_save(msg["config"])
|
||||
|
||||
|
||||
@websocket_api.async_response
|
||||
@websocket_api.websocket_command({"type": "lovelace/config/delete"})
|
||||
@handle_yaml_errors
|
||||
async def websocket_lovelace_delete_config(hass, connection, msg):
|
||||
"""Delete Lovelace UI configuration."""
|
||||
await hass.data[DOMAIN].async_delete()
|
||||
|
||||
|
||||
async def system_health_info(hass):
|
||||
"""Get info for the info page."""
|
||||
return await hass.data[DOMAIN].async_get_info()
|
||||
|
@ -55,6 +55,32 @@ async def test_lovelace_from_storage_save_before_load(
|
||||
assert hass_storage[lovelace.STORAGE_KEY]["data"] == {"config": {"yo": "hello"}}
|
||||
|
||||
|
||||
async def test_lovelace_from_storage_delete(hass, hass_ws_client, hass_storage):
|
||||
"""Test we delete lovelace config from storage."""
|
||||
assert await async_setup_component(hass, "lovelace", {})
|
||||
client = await hass_ws_client(hass)
|
||||
|
||||
# Store new config
|
||||
await client.send_json(
|
||||
{"id": 6, "type": "lovelace/config/save", "config": {"yo": "hello"}}
|
||||
)
|
||||
response = await client.receive_json()
|
||||
assert response["success"]
|
||||
assert hass_storage[lovelace.STORAGE_KEY]["data"] == {"config": {"yo": "hello"}}
|
||||
|
||||
# Delete config
|
||||
await client.send_json({"id": 7, "type": "lovelace/config/delete"})
|
||||
response = await client.receive_json()
|
||||
assert response["success"]
|
||||
assert hass_storage[lovelace.STORAGE_KEY]["data"] == {"config": None}
|
||||
|
||||
# Fetch data
|
||||
await client.send_json({"id": 8, "type": "lovelace/config"})
|
||||
response = await client.receive_json()
|
||||
assert not response["success"]
|
||||
assert response["error"]["code"] == "config_not_found"
|
||||
|
||||
|
||||
async def test_lovelace_from_yaml(hass, hass_ws_client):
|
||||
"""Test we load lovelace config from yaml."""
|
||||
assert await async_setup_component(hass, "lovelace", {"lovelace": {"mode": "YAML"}})
|
||||
|
Loading…
x
Reference in New Issue
Block a user