From 1477087c71a027e59b6bd9dcc0ba51cd3a314eda Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Sat, 28 Mar 2020 13:57:36 +0100 Subject: [PATCH] Fix Lovelace resources health info (#33309) * Fix Lovelace resources health info * Fix tests --- homeassistant/components/lovelace/__init__.py | 5 ++++- homeassistant/components/lovelace/dashboard.py | 3 +-- homeassistant/components/lovelace/resources.py | 12 ++++++++++++ tests/components/lovelace/test_dashboard.py | 8 +++++--- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/lovelace/__init__.py b/homeassistant/components/lovelace/__init__.py index 95508c2f8f3..9b944be556b 100644 --- a/homeassistant/components/lovelace/__init__.py +++ b/homeassistant/components/lovelace/__init__.py @@ -234,7 +234,10 @@ async def create_yaml_resource_col(hass, yaml_resources): async def system_health_info(hass): """Get info for the info page.""" - return await hass.data[DOMAIN]["dashboards"][None].async_get_info() + health_info = {"dashboards": len(hass.data[DOMAIN]["dashboards"])} + health_info.update(await hass.data[DOMAIN]["dashboards"][None].async_get_info()) + health_info.update(await hass.data[DOMAIN]["resources"].async_get_info()) + return health_info @callback diff --git a/homeassistant/components/lovelace/dashboard.py b/homeassistant/components/lovelace/dashboard.py index cdb104a150b..2d3196054e3 100644 --- a/homeassistant/components/lovelace/dashboard.py +++ b/homeassistant/components/lovelace/dashboard.py @@ -101,7 +101,7 @@ class LovelaceStorage(LovelaceConfig): return MODE_STORAGE async def async_get_info(self): - """Return the YAML storage mode.""" + """Return the Lovelace storage info.""" if self._data is None: await self._load() @@ -213,7 +213,6 @@ def _config_info(mode, config): """Generate info about the config.""" return { "mode": mode, - "resources": len(config.get("resources", [])), "views": len(config.get("views", [])), } diff --git a/homeassistant/components/lovelace/resources.py b/homeassistant/components/lovelace/resources.py index 57acaa487bd..78a23540ed4 100644 --- a/homeassistant/components/lovelace/resources.py +++ b/homeassistant/components/lovelace/resources.py @@ -34,6 +34,10 @@ class ResourceYAMLCollection: """Initialize a resource YAML collection.""" self.data = data + async def async_get_info(self): + """Return the resources info for YAML mode.""" + return {"resources": len(self.async_items() or [])} + @callback def async_items(self) -> List[dict]: """Return list of items in collection.""" @@ -55,6 +59,14 @@ class ResourceStorageCollection(collection.StorageCollection): ) self.ll_config = ll_config + async def async_get_info(self): + """Return the resources info for YAML mode.""" + if not self.loaded: + await self.async_load() + self.loaded = True + + return {"resources": len(self.async_items() or [])} + async def _async_load_data(self) -> Optional[dict]: """Load the data.""" data = await self.store.async_load() diff --git a/tests/components/lovelace/test_dashboard.py b/tests/components/lovelace/test_dashboard.py index 775b2760c96..8509ad37fcd 100644 --- a/tests/components/lovelace/test_dashboard.py +++ b/tests/components/lovelace/test_dashboard.py @@ -165,7 +165,7 @@ async def test_system_health_info_autogen(hass): """Test system health info endpoint.""" assert await async_setup_component(hass, "lovelace", {}) info = await get_system_health_info(hass, "lovelace") - assert info == {"mode": "auto-gen"} + assert info == {"dashboards": 1, "mode": "auto-gen", "resources": 0} async def test_system_health_info_storage(hass, hass_storage): @@ -177,7 +177,7 @@ async def test_system_health_info_storage(hass, hass_storage): } assert await async_setup_component(hass, "lovelace", {}) info = await get_system_health_info(hass, "lovelace") - assert info == {"mode": "storage", "resources": 0, "views": 0} + assert info == {"dashboards": 1, "mode": "storage", "resources": 0, "views": 0} async def test_system_health_info_yaml(hass): @@ -188,7 +188,7 @@ async def test_system_health_info_yaml(hass): return_value={"views": [{"cards": []}]}, ): info = await get_system_health_info(hass, "lovelace") - assert info == {"mode": "yaml", "resources": 0, "views": 1} + assert info == {"dashboards": 1, "mode": "yaml", "resources": 0, "views": 1} async def test_system_health_info_yaml_not_found(hass): @@ -196,8 +196,10 @@ async def test_system_health_info_yaml_not_found(hass): assert await async_setup_component(hass, "lovelace", {"lovelace": {"mode": "YAML"}}) info = await get_system_health_info(hass, "lovelace") assert info == { + "dashboards": 1, "mode": "yaml", "error": "{} not found".format(hass.config.path("ui-lovelace.yaml")), + "resources": 0, }