Repository navigation
Find themes in dashboards that do not exist #1930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2cecabd
Find themes in dashboards that do not exist
frenck 122b548
Only follow the cards containers put on, and keep whitespace themes
frenck 340b5a1
Only check themes the view's layout draws, and sections in sections
frenck 7c7e927
Pair each themed card with the frontend card that puts it on
frenck 5164e4c
Only read what is in a section with the grid layout
frenck 27ae4af
Pick the view layout with JavaScript's idea of set
frenck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
custom_components/spook/ectoplasms/lovelace/repairs/unknown_theme_references.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| """Spook - Your homie.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| from homeassistant.components.frontend import DATA_THEMES, DEFAULT_THEME, VALUE_NO_THEME | ||
| from homeassistant.components.lovelace import DOMAIN | ||
| from homeassistant.const import ( | ||
| EVENT_COMPONENT_LOADED, | ||
| EVENT_LOVELACE_UPDATED, | ||
| EVENT_THEMES_UPDATED, | ||
| ) | ||
| from homeassistant.core import callback | ||
|
|
||
| from ....const import LOGGER | ||
| from ....dashboard_extraction import extract_themes_from_dashboard_view | ||
| from ....repairs import AbstractSpookRepair | ||
| from ..dashboards import async_dashboard_configs | ||
|
|
||
| if TYPE_CHECKING: | ||
| from homeassistant.components.lovelace.dashboard import ( | ||
| LovelaceStorage, | ||
| LovelaceYAML, | ||
| ) | ||
|
|
||
| # The frontend's own look, built in and never in the list of themes, and the | ||
| # name Home Assistant itself uses for no theme at all. | ||
| _BUILT_IN_THEMES = frozenset({DEFAULT_THEME, VALUE_NO_THEME}) | ||
|
|
||
|
|
||
| class SpookRepair(AbstractSpookRepair): | ||
| """Spook repair tries to find themes in dashboards that do not exist. | ||
|
|
||
| The frontend only puts on a theme it has. A card or view naming one that | ||
| was renamed or removed gets the default look, and nothing says why. | ||
| """ | ||
|
|
||
| domain = DOMAIN | ||
| repair = "lovelace_unknown_theme_references" | ||
| inspect_events = { | ||
| EVENT_COMPONENT_LOADED, | ||
| EVENT_LOVELACE_UPDATED, | ||
| EVENT_THEMES_UPDATED, | ||
| } | ||
| inspect_config_entry_changed = True | ||
| inspect_on_reload = True | ||
| automatically_clean_up_issues = True | ||
|
|
||
| _dashboards: dict[str | None, LovelaceStorage | LovelaceYAML] | ||
|
|
||
| async def async_activate(self) -> None: | ||
| """Handle the activating a repair.""" | ||
| self._dashboards = self.hass.data["lovelace"].dashboards | ||
| await super().async_activate() | ||
|
|
||
| async def async_inspect(self) -> None: | ||
| """Trigger a inspection.""" | ||
| LOGGER.debug("Spook is inspecting: %s", self.repair) | ||
|
|
||
| # Without the frontend there is no list of themes to hold anything | ||
| # against, and nothing that puts one on either. Home Assistant sets it | ||
| # up before anything else, so this is a guard, not a waiting room. | ||
| if (known_themes := self.hass.data.get(DATA_THEMES)) is None: | ||
| return | ||
|
|
||
| async for dashboard, url_path, config in async_dashboard_configs( | ||
| self._dashboards | ||
| ): | ||
| self.possible_issue_ids.add(url_path) | ||
| if config is None: | ||
| continue | ||
|
|
||
| themes = self.__async_extract_themes(config) | ||
| unknown_themes = { | ||
| theme | ||
| for theme in themes | ||
| if theme not in known_themes and theme not in _BUILT_IN_THEMES | ||
| } | ||
| if not unknown_themes: | ||
| continue | ||
|
|
||
| first_view_path = next( | ||
| view_path | ||
| for theme, view_path in themes.items() | ||
| if theme in unknown_themes | ||
| ) | ||
| title = "Overview" | ||
| if dashboard.config: | ||
| title = dashboard.config.get("title", url_path) | ||
| self.async_create_issue( | ||
| issue_id=url_path, | ||
| references=unknown_themes, | ||
| translation_placeholders={ | ||
| "themes": "\n".join( | ||
| f"- `{theme}`" for theme in sorted(unknown_themes) | ||
| ), | ||
| "dashboard": title, | ||
| "edit": f"/{url_path}/{first_view_path}?edit=1", | ||
| }, | ||
| ) | ||
|
|
||
| @callback | ||
| def __async_extract_themes(self, config: Any) -> dict[str, int | str]: | ||
| """Extract the themes a dashboard puts on, keyed by their view path. | ||
|
|
||
| A dashboard run by a strategy has no views stored, only the strategy, | ||
| and the views it makes are not known until it opens. | ||
| """ | ||
| themes: dict[str, int | str] = {} | ||
| if ( | ||
| not isinstance(config, dict) | ||
| or "strategy" in config | ||
| or not isinstance(views := config.get("views"), list) | ||
| ): | ||
| return themes | ||
|
|
||
| for view_index, view in enumerate(views): | ||
| if not isinstance(view, dict): | ||
| continue | ||
|
|
||
| view_path: int | str = view.get("path") or view_index | ||
| for theme in sorted(extract_themes_from_dashboard_view(view)): | ||
| themes.setdefault(theme, view_path) | ||
|
|
||
| return themes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.