Require title, separate show sidebar option (#32479)

* Require title, separate show sidebar option

* Fix list command not updating

* Some more test checks
This commit is contained in:
Paulus Schoutsen 2020-03-05 11:52:12 -08:00 committed by GitHub
parent d885853b35
commit 7c51318861
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 37 deletions

View File

@ -16,10 +16,11 @@ from .const import (
CONF_MODE, CONF_MODE,
CONF_REQUIRE_ADMIN, CONF_REQUIRE_ADMIN,
CONF_RESOURCES, CONF_RESOURCES,
CONF_SIDEBAR, CONF_SHOW_IN_SIDEBAR,
CONF_TITLE, CONF_TITLE,
CONF_URL_PATH, CONF_URL_PATH,
DASHBOARD_BASE_CREATE_FIELDS, DASHBOARD_BASE_CREATE_FIELDS,
DEFAULT_ICON,
DOMAIN, DOMAIN,
MODE_STORAGE, MODE_STORAGE,
MODE_YAML, MODE_YAML,
@ -171,6 +172,7 @@ async def async_setup(hass, config):
update = False update = False
else: else:
hass.data[DOMAIN]["dashboards"][url_path].config = item
update = True update = True
try: try:
@ -207,8 +209,8 @@ def _register_panel(hass, url_path, mode, config, update):
"update": update, "update": update,
} }
if CONF_SIDEBAR in config: if config[CONF_SHOW_IN_SIDEBAR]:
kwargs["sidebar_title"] = config[CONF_SIDEBAR][CONF_TITLE] kwargs["sidebar_title"] = config[CONF_TITLE]
kwargs["sidebar_icon"] = config[CONF_SIDEBAR][CONF_ICON] kwargs["sidebar_icon"] = config.get(CONF_ICON, DEFAULT_ICON)
frontend.async_register_built_in_panel(hass, DOMAIN, **kwargs) frontend.async_register_built_in_panel(hass, DOMAIN, **kwargs)

View File

@ -11,6 +11,8 @@ from homeassistant.util import slugify
DOMAIN = "lovelace" DOMAIN = "lovelace"
EVENT_LOVELACE_UPDATED = "lovelace_updated" EVENT_LOVELACE_UPDATED = "lovelace_updated"
DEFAULT_ICON = "hass:view-dashboard"
CONF_MODE = "mode" CONF_MODE = "mode"
MODE_YAML = "yaml" MODE_YAML = "yaml"
MODE_STORAGE = "storage" MODE_STORAGE = "storage"
@ -39,24 +41,23 @@ RESOURCE_UPDATE_FIELDS = {
vol.Optional(CONF_URL): cv.string, vol.Optional(CONF_URL): cv.string,
} }
CONF_SIDEBAR = "sidebar"
CONF_TITLE = "title" CONF_TITLE = "title"
CONF_REQUIRE_ADMIN = "require_admin" CONF_REQUIRE_ADMIN = "require_admin"
CONF_SHOW_IN_SIDEBAR = "show_in_sidebar"
SIDEBAR_FIELDS = {
vol.Required(CONF_ICON): cv.icon,
vol.Required(CONF_TITLE): cv.string,
}
DASHBOARD_BASE_CREATE_FIELDS = { DASHBOARD_BASE_CREATE_FIELDS = {
vol.Optional(CONF_REQUIRE_ADMIN, default=False): cv.boolean, vol.Optional(CONF_REQUIRE_ADMIN, default=False): cv.boolean,
vol.Optional(CONF_SIDEBAR): SIDEBAR_FIELDS, vol.Optional(CONF_ICON): cv.icon,
vol.Required(CONF_TITLE): cv.string,
vol.Optional(CONF_SHOW_IN_SIDEBAR, default=True): cv.boolean,
} }
DASHBOARD_BASE_UPDATE_FIELDS = { DASHBOARD_BASE_UPDATE_FIELDS = {
vol.Optional(CONF_REQUIRE_ADMIN): cv.boolean, vol.Optional(CONF_REQUIRE_ADMIN): cv.boolean,
vol.Optional(CONF_SIDEBAR): vol.Any(None, SIDEBAR_FIELDS), vol.Optional(CONF_ICON): vol.Any(cv.icon, None),
vol.Optional(CONF_TITLE): cv.string,
vol.Optional(CONF_SHOW_IN_SIDEBAR): cv.boolean,
} }
@ -68,9 +69,7 @@ STORAGE_DASHBOARD_CREATE_FIELDS = {
vol.Optional(CONF_MODE, default=MODE_STORAGE): MODE_STORAGE, vol.Optional(CONF_MODE, default=MODE_STORAGE): MODE_STORAGE,
} }
STORAGE_DASHBOARD_UPDATE_FIELDS = { STORAGE_DASHBOARD_UPDATE_FIELDS = DASHBOARD_BASE_UPDATE_FIELDS
**DASHBOARD_BASE_UPDATE_FIELDS,
}
def url_slug(value: Any) -> str: def url_slug(value: Any) -> str:

View File

@ -13,7 +13,7 @@ from homeassistant.helpers import collection, storage
from homeassistant.util.yaml import load_yaml from homeassistant.util.yaml import load_yaml
from .const import ( from .const import (
CONF_SIDEBAR, CONF_ICON,
CONF_URL_PATH, CONF_URL_PATH,
DOMAIN, DOMAIN,
EVENT_LOVELACE_UPDATED, EVENT_LOVELACE_UPDATED,
@ -246,7 +246,7 @@ class DashboardsCollection(collection.StorageCollection):
update_data = self.UPDATE_SCHEMA(update_data) update_data = self.UPDATE_SCHEMA(update_data)
updated = {**data, **update_data} updated = {**data, **update_data}
if CONF_SIDEBAR in updated and updated[CONF_SIDEBAR] is None: if CONF_ICON in updated and updated[CONF_ICON] is None:
updated.pop(CONF_SIDEBAR) updated.pop(CONF_ICON)
return updated return updated

View File

@ -209,10 +209,16 @@ async def test_dashboard_from_yaml(hass, hass_ws_client, url_path):
"test-panel": { "test-panel": {
"mode": "yaml", "mode": "yaml",
"filename": "bla.yaml", "filename": "bla.yaml",
"sidebar": {"title": "Test Panel", "icon": "mdi:test-icon"}, "title": "Test Panel",
"icon": "mdi:test-icon",
"show_in_sidebar": False,
"require_admin": True, "require_admin": True,
}, },
"test-panel-no-sidebar": {"mode": "yaml", "filename": "bla2.yaml"}, "test-panel-no-sidebar": {
"title": "Title No Sidebar",
"mode": "yaml",
"filename": "bla2.yaml",
},
} }
} }
}, },
@ -233,13 +239,15 @@ async def test_dashboard_from_yaml(hass, hass_ws_client, url_path):
assert with_sb["mode"] == "yaml" assert with_sb["mode"] == "yaml"
assert with_sb["filename"] == "bla.yaml" assert with_sb["filename"] == "bla.yaml"
assert with_sb["sidebar"] == {"title": "Test Panel", "icon": "mdi:test-icon"} assert with_sb["title"] == "Test Panel"
assert with_sb["icon"] == "mdi:test-icon"
assert with_sb["show_in_sidebar"] is False
assert with_sb["require_admin"] is True assert with_sb["require_admin"] is True
assert with_sb["url_path"] == "test-panel" assert with_sb["url_path"] == "test-panel"
assert without_sb["mode"] == "yaml" assert without_sb["mode"] == "yaml"
assert without_sb["filename"] == "bla2.yaml" assert without_sb["filename"] == "bla2.yaml"
assert "sidebar" not in without_sb assert without_sb["show_in_sidebar"] is True
assert without_sb["require_admin"] is False assert without_sb["require_admin"] is False
assert without_sb["url_path"] == "test-panel-no-sidebar" assert without_sb["url_path"] == "test-panel-no-sidebar"
@ -315,16 +323,15 @@ async def test_storage_dashboards(hass, hass_ws_client, hass_storage):
"type": "lovelace/dashboards/create", "type": "lovelace/dashboards/create",
"url_path": "created_url_path", "url_path": "created_url_path",
"require_admin": True, "require_admin": True,
"sidebar": {"title": "Updated Title", "icon": "mdi:map"}, "title": "New Title",
"icon": "mdi:map",
} }
) )
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
assert response["result"]["require_admin"] is True assert response["result"]["require_admin"] is True
assert response["result"]["sidebar"] == { assert response["result"]["title"] == "New Title"
"title": "Updated Title", assert response["result"]["icon"] == "mdi:map"
"icon": "mdi:map",
}
dashboard_id = response["result"]["id"] dashboard_id = response["result"]["id"]
@ -335,10 +342,9 @@ async def test_storage_dashboards(hass, hass_ws_client, hass_storage):
assert response["success"] assert response["success"]
assert len(response["result"]) == 1 assert len(response["result"]) == 1
assert response["result"][0]["mode"] == "storage" assert response["result"][0]["mode"] == "storage"
assert response["result"][0]["sidebar"] == { assert response["result"][0]["title"] == "New Title"
"title": "Updated Title", assert response["result"][0]["icon"] == "mdi:map"
"icon": "mdi:map", assert response["result"][0]["show_in_sidebar"] is True
}
assert response["result"][0]["require_admin"] is True assert response["result"][0]["require_admin"] is True
# Fetch config # Fetch config
@ -382,24 +388,42 @@ async def test_storage_dashboards(hass, hass_ws_client, hass_storage):
"type": "lovelace/dashboards/update", "type": "lovelace/dashboards/update",
"dashboard_id": dashboard_id, "dashboard_id": dashboard_id,
"require_admin": False, "require_admin": False,
"sidebar": None, "icon": "mdi:updated",
"show_in_sidebar": False,
"title": "Updated Title",
} }
) )
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
assert response["result"]["mode"] == "storage"
assert response["result"]["url_path"] == "created_url_path"
assert response["result"]["title"] == "Updated Title"
assert response["result"]["icon"] == "mdi:updated"
assert response["result"]["show_in_sidebar"] is False
assert response["result"]["require_admin"] is False assert response["result"]["require_admin"] is False
assert "sidebar" not in response["result"]
# List dashboards again and make sure we see latest config
await client.send_json({"id": 12, "type": "lovelace/dashboards/list"})
response = await client.receive_json()
assert response["success"]
assert len(response["result"]) == 1
assert response["result"][0]["mode"] == "storage"
assert response["result"][0]["url_path"] == "created_url_path"
assert response["result"][0]["title"] == "Updated Title"
assert response["result"][0]["icon"] == "mdi:updated"
assert response["result"][0]["show_in_sidebar"] is False
assert response["result"][0]["require_admin"] is False
# Add dashboard with existing url path # Add dashboard with existing url path
await client.send_json( await client.send_json(
{"id": 12, "type": "lovelace/dashboards/create", "url_path": "created_url_path"} {"id": 13, "type": "lovelace/dashboards/create", "url_path": "created_url_path"}
) )
response = await client.receive_json() response = await client.receive_json()
assert not response["success"] assert not response["success"]
# Delete dashboards # Delete dashboards
await client.send_json( await client.send_json(
{"id": 13, "type": "lovelace/dashboards/delete", "dashboard_id": dashboard_id} {"id": 14, "type": "lovelace/dashboards/delete", "dashboard_id": dashboard_id}
) )
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
@ -416,7 +440,11 @@ async def test_websocket_list_dashboards(hass, hass_ws_client):
{ {
"lovelace": { "lovelace": {
"dashboards": { "dashboards": {
"test-panel-no-sidebar": {"mode": "yaml", "filename": "bla.yaml"}, "test-panel-no-sidebar": {
"title": "Test YAML",
"mode": "yaml",
"filename": "bla.yaml",
},
} }
} }
}, },
@ -426,7 +454,12 @@ async def test_websocket_list_dashboards(hass, hass_ws_client):
# Create a storage dashboard # Create a storage dashboard
await client.send_json( await client.send_json(
{"id": 6, "type": "lovelace/dashboards/create", "url_path": "created_url_path"} {
"id": 6,
"type": "lovelace/dashboards/create",
"url_path": "created_url_path",
"title": "Test Storage",
}
) )
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
@ -439,8 +472,10 @@ async def test_websocket_list_dashboards(hass, hass_ws_client):
with_sb, without_sb = response["result"] with_sb, without_sb = response["result"]
assert with_sb["mode"] == "yaml" assert with_sb["mode"] == "yaml"
assert with_sb["title"] == "Test YAML"
assert with_sb["filename"] == "bla.yaml" assert with_sb["filename"] == "bla.yaml"
assert with_sb["url_path"] == "test-panel-no-sidebar" assert with_sb["url_path"] == "test-panel-no-sidebar"
assert without_sb["mode"] == "storage" assert without_sb["mode"] == "storage"
assert without_sb["title"] == "Test Storage"
assert without_sb["url_path"] == "created_url_path" assert without_sb["url_path"] == "created_url_path"