diff --git a/homeassistant/components/marketplace/base.py b/homeassistant/components/marketplace/base.py index c7506e9c7bb1..462e32b10ffc 100644 --- a/homeassistant/components/marketplace/base.py +++ b/homeassistant/components/marketplace/base.py @@ -88,7 +88,7 @@ from .repositories.base import ( REPOSITORY_MANIFEST_KEYS_TO_EXPORT, RepositoryData, RepositoryManifest, - valid_integration_categories, + valid_string_list, ) from .utils.data import MarketplaceData from .utils.file_system import async_exists, async_run_to_completion @@ -1252,7 +1252,7 @@ class MarketplaceManager: # The catalog calls them categories, here that word means the kind of # repository. Taken on every pass: storage from before they existed # has none, and the catalog entry need not be newer than that. - repository.data.integration_categories = valid_integration_categories( + repository.data.integration_categories = valid_string_list( repo_data.get("categories") ) @@ -1274,6 +1274,10 @@ class MarketplaceManager: repository.data.last_version = repo_data.get("last_version") repository.data.last_commit = repo_data.get("last_commit") + # Set on every pass, like the categories. It comes after update_data, + # because update_data takes the catalog value without a check. + repository.data.connectivity = valid_string_list(repo_data.get("connectivity")) + def _async_unregister_stale_custom_repositories( self, category: RepositoryCategory ) -> None: diff --git a/homeassistant/components/marketplace/repositories/base.py b/homeassistant/components/marketplace/repositories/base.py index e998522eeb1c..6238619dfaa1 100644 --- a/homeassistant/components/marketplace/repositories/base.py +++ b/homeassistant/components/marketplace/repositories/base.py @@ -289,11 +289,11 @@ class RepositoryArchive: archive.extractall(local, extractable) -def valid_integration_categories(value: Any) -> list[str]: - """Return the integration categories, or none when they are not valid. +def valid_string_list(value: Any) -> list[str]: + """Return the list of strings, or an empty list when it is not valid. - The catalog only lists known categories. A broken value loses just the - categories, not the whole repository. + The catalog only lists known values. A broken value loses just that + field, not the whole repository. """ if not isinstance(value, list) or not all(isinstance(item, str) for item in value): return [] @@ -308,6 +308,7 @@ class RepositoryData: authors: list[str] = field(default_factory=list) category: str = "" config_flow: bool = False + connectivity: list[str] = field(default_factory=list) default_branch: str | None = None description: str = "" domain: str | None = None diff --git a/homeassistant/components/marketplace/utils/data.py b/homeassistant/components/marketplace/utils/data.py index 6238f0a86875..93321b683ebf 100644 --- a/homeassistant/components/marketplace/utils/data.py +++ b/homeassistant/components/marketplace/utils/data.py @@ -18,7 +18,7 @@ from ..repositories.base import ( TOPIC_FILTER, Repository, RepositoryManifest, - valid_integration_categories, + valid_string_list, ) from .backup import backed_up_paths from .identity import one_stored_entry_per_name @@ -42,6 +42,7 @@ EXPORTED_REPOSITORY_DATA: tuple[tuple[str, Any], ...] = ( *EXPORTED_BASE_DATA, ("authors", []), ("category", ""), + ("connectivity", []), ("description", ""), ("domain", None), ("downloads", 0), @@ -410,9 +411,12 @@ class MarketplaceData: if topic not in TOPIC_FILTER ] repository.data.domain = repository_data.get("domain") - repository.data.integration_categories = valid_integration_categories( + repository.data.integration_categories = valid_string_list( repository_data.get("integration_categories", []) ) + repository.data.connectivity = valid_string_list( + repository_data.get("connectivity", []) + ) repository.data.stargazers_count = repository_data.get( "stargazers_count" ) or repository_data.get("stars", 0) diff --git a/homeassistant/components/marketplace/utils/validate.py b/homeassistant/components/marketplace/utils/validate.py index d1645284c219..a76c4085ab48 100644 --- a/homeassistant/components/marketplace/utils/validate.py +++ b/homeassistant/components/marketplace/utils/validate.py @@ -172,8 +172,9 @@ V2_COMMON_DATA_JSON_SCHEMA = { V2_INTEGRATION_DATA_JSON_SCHEMA = { **V2_COMMON_DATA_JSON_SCHEMA, - # Checked where it is used, a bad value loses just the categories + # Checked where they are used, a bad value loses just that field probatio.Optional("categories"): object, + probatio.Optional("connectivity"): object, probatio.Required("domain"): probatio.All(str, probatio.Match(VALID_DOMAIN)), probatio.Required("manifest_name"): str, } diff --git a/homeassistant/components/marketplace/websocket/repositories.py b/homeassistant/components/marketplace/websocket/repositories.py index f6998f324b81..40cb90fd614d 100644 --- a/homeassistant/components/marketplace/websocket/repositories.py +++ b/homeassistant/components/marketplace/websocket/repositories.py @@ -35,6 +35,7 @@ def repository_summary( "can_install": repository.can_install, "category": repository.data.category, "config_flow": repository.data.config_flow, + "connectivity": repository.data.connectivity, "custom": not marketplace.repositories.is_default(repository.data.id), # The catalog can have no description, the panel always shows text "description": repository.data.description or "", diff --git a/tests/components/marketplace/snapshots/test_diagnostics.ambr b/tests/components/marketplace/snapshots/test_diagnostics.ambr index 9ea0d78d6282..54a217a95aa0 100644 --- a/tests/components/marketplace/snapshots/test_diagnostics.ambr +++ b/tests/components/marketplace/snapshots/test_diagnostics.ambr @@ -131,6 +131,8 @@ ]), 'category': 'integration', 'config_flow': False, + 'connectivity': list([ + ]), 'default_branch': None, 'description': 'This your first repo!', 'directory': None, @@ -195,6 +197,8 @@ ]), 'category': 'plugin', 'config_flow': False, + 'connectivity': list([ + ]), 'default_branch': None, 'description': 'This your first repo!', 'directory': 'plugin-basic', diff --git a/tests/components/marketplace/snapshots/test_repositories.ambr b/tests/components/marketplace/snapshots/test_repositories.ambr index 77e3d3d0c5a1..b6b26ba05065 100644 --- a/tests/components/marketplace/snapshots/test_repositories.ambr +++ b/tests/components/marketplace/snapshots/test_repositories.ambr @@ -132,6 +132,8 @@ 'can_install': True, 'category': 'integration', 'config_flow': False, + 'connectivity': list([ + ]), 'custom': True, 'default_branch': 'main', 'description': 'This your first repo!', @@ -180,6 +182,8 @@ 'can_install': True, 'category': 'plugin', 'config_flow': False, + 'connectivity': list([ + ]), 'custom': True, 'default_branch': 'main', 'description': 'This your first repo!', diff --git a/tests/components/marketplace/snapshots/test_websocket_api.ambr b/tests/components/marketplace/snapshots/test_websocket_api.ambr index 1d3ee4c4a1b2..f90fc5a11e64 100644 --- a/tests/components/marketplace/snapshots/test_websocket_api.ambr +++ b/tests/components/marketplace/snapshots/test_websocket_api.ambr @@ -16,6 +16,8 @@ 'can_install': True, 'category': 'integration', 'config_flow': True, + 'connectivity': list([ + ]), 'custom': False, 'default_branch': 'main', 'description': 'This your first repo!', diff --git a/tests/components/marketplace/test_base.py b/tests/components/marketplace/test_base.py index 43c897ec9b18..2a4d512caae1 100644 --- a/tests/components/marketplace/test_base.py +++ b/tests/components/marketplace/test_base.py @@ -243,6 +243,48 @@ async def test_catalog_integration_categories( assert repository.data.integration_categories == expected +@pytest.mark.parametrize( + ("connectivity", "expected"), + [ + pytest.param(["zigbee", "cloud"], ["zigbee", "cloud"], id="connectivity"), + pytest.param(None, [], id="removed"), + pytest.param("zigbee", [], id="not-a-list"), + pytest.param([1], [], id="not-strings"), + ], +) +@pytest.mark.parametrize("newer", [True, False], ids=["newer", "same-entry"]) +async def test_catalog_integration_connectivity( + marketplace: MarketplaceManager, + connectivity: Any, + expected: list[str], + newer: bool, +) -> None: + """Test the catalog sets the connectivity of an integration. + + A broken value only loses it. It does not wait for a newer entry: storage + from before it existed has none. + """ + repository = marketplace.repositories.get_by_full_name(REPOSITORY_INTEGRATION) + repository.data.connectivity = ["bluetooth"] + + entry = { + "domain": repository.data.domain, + "full_name": repository.data.full_name, + "last_fetched": repository.data.last_fetched.timestamp() + int(newer), + "manifest": {}, + "manifest_name": "Example", + } + if connectivity is not None: + entry["connectivity"] = connectivity + feed = {repository.data.id: entry} + with patch.object(marketplace.data_client, "async_get_category", return_value=feed): + await marketplace.async_get_category_repositories_from_catalog( + RepositoryCategory.INTEGRATION + ) + + assert repository.data.connectivity == expected + + OTHER_INTEGRATION_FEED = { "99": { "full_name": "owner/other-integration", diff --git a/tests/components/marketplace/test_data.py b/tests/components/marketplace/test_data.py index 955dd5162411..bce7822ee2f9 100644 --- a/tests/components/marketplace/test_data.py +++ b/tests/components/marketplace/test_data.py @@ -78,6 +78,7 @@ async def test_stored_data_is_a_copy( mock_repository.data.topics = ["lights"] mock_repository.data.authors = ["@frenck"] mock_repository.data.integration_categories = ["lighting"] + mock_repository.data.connectivity = ["zigbee"] marketplace.repositories.register(mock_repository) marketplace.data.async_store_repository_data(mock_repository) @@ -90,6 +91,8 @@ async def test_stored_data_is_a_copy( stored["integration_categories"] is not mock_repository.data.integration_categories ) + assert stored["connectivity"] == ["zigbee"] + assert stored["connectivity"] is not mock_repository.data.connectivity assert stored["authors"] is not mock_repository.data.authors assert ( stored["repository_manifest"] @@ -216,6 +219,41 @@ async def test_restore_integration_categories( ) +@pytest.mark.parametrize( + ("stored_connectivity", "expected"), + [ + pytest.param(["zigbee", "cloud"], ["zigbee", "cloud"], id="connectivity"), + pytest.param("zigbee", [], id="broken"), + ], +) +@pytest.mark.usefixtures("init_integration") +async def test_restore_connectivity( + marketplace: MarketplaceManager, + config_dir: Path, + stored_connectivity: Any, + expected: list[str], +) -> None: + """Test the connectivity of an integration survives a restart.""" + create_install_folders(config_dir, RESTORED_REPOSITORIES) + data = MarketplaceData(marketplace) + stored = { + entry: dict(repo_data) for entry, repo_data in RESTORED_REPOSITORIES.items() + } + stored["1296269"]["connectivity"] = stored_connectivity + + async def mocked_load(hass: HomeAssistant, storage_key: str) -> Any: + """Return the stored repositories.""" + return stored if storage_key == "repositories" else {} + + with patch( + "homeassistant.components.marketplace.utils.data.async_load_from_storage", + side_effect=mocked_load, + ): + assert await data.restore() + + assert marketplace.repositories.get_by_id("1296269").data.connectivity == expected + + @pytest.mark.usefixtures("init_integration") async def test_restore_skips_placeholder_repository( marketplace: MarketplaceManager, diff --git a/tests/components/marketplace/test_validate.py b/tests/components/marketplace/test_validate.py index f515acafab05..ed38050c69ec 100644 --- a/tests/components/marketplace/test_validate.py +++ b/tests/components/marketplace/test_validate.py @@ -377,6 +377,16 @@ def test_common_repo_data_json_schema_bad_data( does_not_raise(), id="broken-categories-keep-the-repository", ), + pytest.param( + GOOD_INTEGRATION_DATA | {"connectivity": ["zigbee", "cloud"]}, + does_not_raise(), + id="connectivity", + ), + pytest.param( + GOOD_INTEGRATION_DATA | {"connectivity": "zigbee"}, + does_not_raise(), + id="broken-connectivity-keeps-the-repository", + ), ], ) def test_integration_repo_data_json_schema_bad_data(