From 8ed8747225a5516b09dab2feccf7625073de7f1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Mon, 31 May 2021 14:06:11 +0200 Subject: [PATCH] Resolve addon repository slug for device registry (#51287) * Resolve addon repository slug for device registry * typo * Adjust onboarding test * Use /store --- homeassistant/components/hassio/__init__.py | 28 +++++++++++++++++++-- homeassistant/components/hassio/handler.py | 8 ++++++ tests/components/hassio/test_init.py | 23 +++++++++++------ tests/components/onboarding/test_views.py | 3 +++ 4 files changed, 53 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/hassio/__init__.py b/homeassistant/components/hassio/__init__.py index e33c689c59e..d391817f964 100644 --- a/homeassistant/components/hassio/__init__.py +++ b/homeassistant/components/hassio/__init__.py @@ -71,6 +71,7 @@ CONFIG_SCHEMA = vol.Schema( DATA_CORE_INFO = "hassio_core_info" DATA_HOST_INFO = "hassio_host_info" +DATA_STORE = "hassio_store" DATA_INFO = "hassio_info" DATA_OS_INFO = "hassio_os_info" DATA_SUPERVISOR_INFO = "hassio_supervisor_info" @@ -291,6 +292,16 @@ def get_host_info(hass): return hass.data.get(DATA_HOST_INFO) +@callback +@bind_hass +def get_store(hass): + """Return store information. + + Async friendly. + """ + return hass.data.get(DATA_STORE) + + @callback @bind_hass def get_supervisor_info(hass): @@ -456,6 +467,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: # noqa: try: hass.data[DATA_INFO] = await hassio.get_info() hass.data[DATA_HOST_INFO] = await hassio.get_host_info() + hass.data[DATA_STORE] = await hassio.get_store() hass.data[DATA_CORE_INFO] = await hassio.get_core_info() hass.data[DATA_SUPERVISOR_INFO] = await hassio.get_supervisor_info() hass.data[DATA_OS_INFO] = await hassio.get_os_info() @@ -627,10 +639,22 @@ class HassioDataUpdateCoordinator(DataUpdateCoordinator): async def _async_update_data(self) -> dict[str, Any]: """Update data via library.""" new_data = {} - addon_data = get_supervisor_info(self.hass) + supervisor_info = get_supervisor_info(self.hass) + store_data = get_store(self.hass) + + repositories = { + repo[ATTR_SLUG]: repo[ATTR_NAME] + for repo in store_data.get("repositories", []) + } new_data["addons"] = { - addon[ATTR_SLUG]: addon for addon in addon_data.get("addons", []) + addon[ATTR_SLUG]: { + **addon, + ATTR_REPOSITORY: repositories.get( + addon.get(ATTR_REPOSITORY), addon.get(ATTR_REPOSITORY, "") + ), + } + for addon in supervisor_info.get("addons", []) } if self.is_hass_os: new_data["os"] = get_os_info(self.hass) diff --git a/homeassistant/components/hassio/handler.py b/homeassistant/components/hassio/handler.py index 301d353faf0..37b645eb7d3 100644 --- a/homeassistant/components/hassio/handler.py +++ b/homeassistant/components/hassio/handler.py @@ -118,6 +118,14 @@ class HassIO: """ return self.send_command(f"/addons/{addon}/info", method="get") + @api_data + def get_store(self): + """Return data from the store. + + This method return a coroutine. + """ + return self.send_command("/store", method="get") + @api_data def get_ingress_panels(self): """Return data for Add-on ingress panels. diff --git a/tests/components/hassio/test_init.py b/tests/components/hassio/test_init.py index 5bf8a45ab52..7e9d7cd91c8 100644 --- a/tests/components/hassio/test_init.py +++ b/tests/components/hassio/test_init.py @@ -32,6 +32,13 @@ def mock_all(aioclient_mock, request): "data": {"supervisor": "222", "homeassistant": "0.110.0", "hassos": None}, }, ) + aioclient_mock.get( + "http://127.0.0.1/store", + json={ + "result": "ok", + "data": {"addons": [], "repositories": []}, + }, + ) aioclient_mock.get( "http://127.0.0.1/host/info", json={ @@ -67,6 +74,7 @@ def mock_all(aioclient_mock, request): "update_available": False, "version": "1.0.0", "version_latest": "1.0.0", + "repository": "core", "url": "https://github.com/home-assistant/addons/test", }, { @@ -76,6 +84,7 @@ def mock_all(aioclient_mock, request): "update_available": False, "version": "1.0.0", "version_latest": "1.0.0", + "repository": "core", "url": "https://github.com", }, ], @@ -92,7 +101,7 @@ async def test_setup_api_ping(hass, aioclient_mock): result = await async_setup_component(hass, "hassio", {}) assert result - assert aioclient_mock.call_count == 9 + assert aioclient_mock.call_count == 10 assert hass.components.hassio.get_core_info()["version_latest"] == "1.0.0" assert hass.components.hassio.is_hassio() @@ -131,7 +140,7 @@ async def test_setup_api_push_api_data(hass, aioclient_mock): ) assert result - assert aioclient_mock.call_count == 9 + assert aioclient_mock.call_count == 10 assert not aioclient_mock.mock_calls[1][2]["ssl"] assert aioclient_mock.mock_calls[1][2]["port"] == 9999 assert aioclient_mock.mock_calls[1][2]["watchdog"] @@ -147,7 +156,7 @@ async def test_setup_api_push_api_data_server_host(hass, aioclient_mock): ) assert result - assert aioclient_mock.call_count == 9 + assert aioclient_mock.call_count == 10 assert not aioclient_mock.mock_calls[1][2]["ssl"] assert aioclient_mock.mock_calls[1][2]["port"] == 9999 assert not aioclient_mock.mock_calls[1][2]["watchdog"] @@ -159,7 +168,7 @@ async def test_setup_api_push_api_data_default(hass, aioclient_mock, hass_storag result = await async_setup_component(hass, "hassio", {"http": {}, "hassio": {}}) assert result - assert aioclient_mock.call_count == 9 + assert aioclient_mock.call_count == 10 assert not aioclient_mock.mock_calls[1][2]["ssl"] assert aioclient_mock.mock_calls[1][2]["port"] == 8123 refresh_token = aioclient_mock.mock_calls[1][2]["refresh_token"] @@ -206,7 +215,7 @@ async def test_setup_api_existing_hassio_user(hass, aioclient_mock, hass_storage result = await async_setup_component(hass, "hassio", {"http": {}, "hassio": {}}) assert result - assert aioclient_mock.call_count == 9 + assert aioclient_mock.call_count == 10 assert not aioclient_mock.mock_calls[1][2]["ssl"] assert aioclient_mock.mock_calls[1][2]["port"] == 8123 assert aioclient_mock.mock_calls[1][2]["refresh_token"] == token.token @@ -220,7 +229,7 @@ async def test_setup_core_push_timezone(hass, aioclient_mock): result = await async_setup_component(hass, "hassio", {"hassio": {}}) assert result - assert aioclient_mock.call_count == 9 + assert aioclient_mock.call_count == 10 assert aioclient_mock.mock_calls[2][2]["timezone"] == "testzone" with patch("homeassistant.util.dt.set_default_time_zone"): @@ -237,7 +246,7 @@ async def test_setup_hassio_no_additional_data(hass, aioclient_mock): result = await async_setup_component(hass, "hassio", {"hassio": {}}) assert result - assert aioclient_mock.call_count == 9 + assert aioclient_mock.call_count == 10 assert aioclient_mock.mock_calls[-1][3]["X-Hassio-Key"] == "123456" diff --git a/tests/components/onboarding/test_views.py b/tests/components/onboarding/test_views.py index d8ae50b851f..a921dfe39d4 100644 --- a/tests/components/onboarding/test_views.py +++ b/tests/components/onboarding/test_views.py @@ -73,6 +73,9 @@ async def mock_supervisor_fixture(hass, aioclient_mock): ), patch( "homeassistant.components.hassio.HassIO.get_host_info", return_value={}, + ), patch( + "homeassistant.components.hassio.HassIO.get_store", + return_value={}, ), patch( "homeassistant.components.hassio.HassIO.get_supervisor_info", return_value={"diagnostics": True},