diff --git a/homeassistant/components/marketplace/repositories/theme.py b/homeassistant/components/marketplace/repositories/theme.py index c69f7477f6cb..65a1d51dc6c4 100644 --- a/homeassistant/components/marketplace/repositories/theme.py +++ b/homeassistant/components/marketplace/repositories/theme.py @@ -58,6 +58,21 @@ class ThemeRepository(Repository): configuration = self.marketplace.configuration return f"{self.marketplace.core.config_path}/{configuration.theme_path}" + @override + async def remove_local_directory(self) -> bool: + """Remove the theme, unless it is not known which folder holds it.""" + # The custom integration did not store the file name, and with the repository + # gone from GitHub nothing tells it anymore. Guessing could hit another theme. + if not self.directory: + self.logger.warning( + "%s Not known which folder in %s holds this theme, remove it by hand", + self.string, + self._category_directory(), + ) + return True + + return await super().remove_local_directory() + @override def _tree_of_the_content(self) -> list[GitHubGitTreeEntryModel]: """Return the tree, or only the theme file when the root is the content.""" diff --git a/tests/components/marketplace/test_remove.py b/tests/components/marketplace/test_remove.py index 19a9ca5049ce..ade0ee4c3d7b 100644 --- a/tests/components/marketplace/test_remove.py +++ b/tests/components/marketplace/test_remove.py @@ -61,6 +61,27 @@ async def test_removing_a_theme_keeps_a_handwritten_theme( assert handwritten.exists() +async def test_removing_a_theme_without_a_known_folder( + marketplace: MarketplaceManager, + config_dir: Path, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test a theme HACS stored without its file name can still be removed.""" + repository = marketplace.repositories.get_by_id(THEME_ID) + repository.data.installed = True + repository.data.file_name = "" + repository.data.directory = None + other_theme = config_dir / "themes/other/other.yaml" + other_theme.parent.mkdir(parents=True) + other_theme.write_text("Other Theme:\n primary-color: red\n") + + await repository.uninstall() + + assert not repository.data.installed + assert other_theme.exists() + assert "remove it by hand" in caplog.text + + async def test_template_refresh_keeps_the_installed_file_name( marketplace: MarketplaceManager, ) -> None: