Coverage for custom_components/appliance_autonotify/dashboards.py: 100%

14 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-10-12 15:40 +0000

1"""The dashboards a notification can open when tapped, chosen from those there are so never a URL of anyone's making""" 

2 

3from __future__ import annotations 

4 

5from typing import TYPE_CHECKING 

6 

7from .const import DEFAULT_DASHBOARD, DEFAULT_DASHBOARD_TITLE, LOVELACE_DOMAIN 

8 

9if TYPE_CHECKING: 

10 from homeassistant.core import HomeAssistant 

11 

12 

13def dashboards(hass: HomeAssistant) -> dict[str, str]: 

14 """The title of each dashboard, by the path it's found at""" 

15 found: dict[str, str] = {} 

16 for url_path, dashboard in getattr(hass.data.get(LOVELACE_DOMAIN), "dashboards", {}).items(): 

17 if url_path is None: 

18 found[DEFAULT_DASHBOARD] = DEFAULT_DASHBOARD_TITLE 

19 else: 

20 found[url_path] = (getattr(dashboard, "config", None) or {}).get("title") or url_path 

21 # those that would look the same in a list are told apart by their paths 

22 titles = [title.lower() for title in found.values()] 

23 found = {path: f"{title} ({path})" if titles.count(title.lower()) > 1 else title for path, title in found.items()} 

24 return dict(sorted(found.items(), key=lambda item: item[1].lower())) 

25 

26 

27def dashboard_url(hass: HomeAssistant, path: str | None) -> str | None: 

28 """Where to send a tap, only ever to a dashboard that exists""" 

29 return f"/{path}" if path and path in dashboards(hass) else None