From e726ef662cc49adc104f291e067638f1c118b928 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Thu, 7 Apr 2022 13:34:20 +0200 Subject: [PATCH] Fix adding OS entities for supervised installations (#69539) --- homeassistant/components/hassio/__init__.py | 8 ++++- tests/components/hassio/test_binary_sensor.py | 6 +++- tests/components/hassio/test_init.py | 29 +++++++++++++------ tests/components/hassio/test_sensor.py | 6 +++- tests/components/hassio/test_update.py | 28 +++++++++++++++++- 5 files changed, 64 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/hassio/__init__.py b/homeassistant/components/hassio/__init__.py index 7b8608a7fad..5f2cc386a87 100644 --- a/homeassistant/components/hassio/__init__.py +++ b/homeassistant/components/hassio/__init__.py @@ -824,7 +824,7 @@ class HassioDataUpdateCoordinator(DataUpdateCoordinator): self.data = {} self.entry_id = config_entry.entry_id self.dev_reg = dev_reg - self.is_hass_os = "hassos" in get_info(self.hass) + self.is_hass_os = (get_info(self.hass) or {}).get("hassos") is not None async def _async_update_data(self) -> dict[str, Any]: """Update data via library.""" @@ -891,6 +891,12 @@ class HassioDataUpdateCoordinator(DataUpdateCoordinator): if stale_addons := supervisor_addon_devices - set(new_data[DATA_KEY_ADDONS]): async_remove_addons_from_dev_reg(self.dev_reg, stale_addons) + if not self.is_hass_os and ( + dev := self.dev_reg.async_get_device({(DOMAIN, "OS")}) + ): + # Remove the OS device if it exists and the installation is not hassos + self.dev_reg.async_remove_device(dev.id) + # If there are new add-ons, we should reload the config entry so we can # create new devices and entities. We can return an empty dict because # coordinator will be recreated. diff --git a/tests/components/hassio/test_binary_sensor.py b/tests/components/hassio/test_binary_sensor.py index a49b27ba4e7..0f4691e2795 100644 --- a/tests/components/hassio/test_binary_sensor.py +++ b/tests/components/hassio/test_binary_sensor.py @@ -24,7 +24,11 @@ def mock_all(aioclient_mock, request): "http://127.0.0.1/info", json={ "result": "ok", - "data": {"supervisor": "222", "homeassistant": "0.110.0", "hassos": None}, + "data": { + "supervisor": "222", + "homeassistant": "0.110.0", + "hassos": "1.2.3", + }, }, ) aioclient_mock.get( diff --git a/tests/components/hassio/test_init.py b/tests/components/hassio/test_init.py index 527c98b615e..2e7bea90f68 100644 --- a/tests/components/hassio/test_init.py +++ b/tests/components/hassio/test_init.py @@ -30,7 +30,11 @@ def mock_all(aioclient_mock, request): "http://127.0.0.1/info", json={ "result": "ok", - "data": {"supervisor": "222", "homeassistant": "0.110.0", "hassos": None}, + "data": { + "supervisor": "222", + "homeassistant": "0.110.0", + "hassos": "1.2.3", + }, }, ) aioclient_mock.get( @@ -396,14 +400,14 @@ async def test_service_calls(hassio_env, hass, aioclient_mock, caplog): ) await hass.async_block_till_done() - assert aioclient_mock.call_count == 8 + assert aioclient_mock.call_count == 9 assert aioclient_mock.mock_calls[-1][2] == "test" await hass.services.async_call("hassio", "host_shutdown", {}) await hass.services.async_call("hassio", "host_reboot", {}) await hass.async_block_till_done() - assert aioclient_mock.call_count == 10 + assert aioclient_mock.call_count == 11 await hass.services.async_call("hassio", "backup_full", {}) await hass.services.async_call( @@ -418,7 +422,7 @@ async def test_service_calls(hassio_env, hass, aioclient_mock, caplog): ) await hass.async_block_till_done() - assert aioclient_mock.call_count == 12 + assert aioclient_mock.call_count == 13 assert aioclient_mock.mock_calls[-1][2] == { "homeassistant": True, "addons": ["test"], @@ -442,7 +446,7 @@ async def test_service_calls(hassio_env, hass, aioclient_mock, caplog): ) await hass.async_block_till_done() - assert aioclient_mock.call_count == 14 + assert aioclient_mock.call_count == 15 assert aioclient_mock.mock_calls[-1][2] == { "addons": ["test"], "folders": ["ssl"], @@ -461,12 +465,12 @@ async def test_service_calls_core(hassio_env, hass, aioclient_mock): await hass.services.async_call("homeassistant", "stop") await hass.async_block_till_done() - assert aioclient_mock.call_count == 4 + assert aioclient_mock.call_count == 5 await hass.services.async_call("homeassistant", "check_config") await hass.async_block_till_done() - assert aioclient_mock.call_count == 4 + assert aioclient_mock.call_count == 5 with patch( "homeassistant.config.async_check_ha_config_file", return_value=None @@ -475,7 +479,7 @@ async def test_service_calls_core(hassio_env, hass, aioclient_mock): await hass.async_block_till_done() assert mock_check_config.called - assert aioclient_mock.call_count == 5 + assert aioclient_mock.call_count == 6 async def test_entry_load_and_unload(hass): @@ -628,10 +632,17 @@ async def test_device_registry_calls(hass): ), patch( "homeassistant.components.hassio.HassIO.get_os_info", return_value=os_mock_data, + ), patch( + "homeassistant.components.hassio.HassIO.get_info", + return_value={ + "supervisor": "222", + "homeassistant": "0.110.0", + "hassos": None, + }, ): async_fire_time_changed(hass, dt_util.now() + timedelta(hours=3)) await hass.async_block_till_done() - assert len(dev_reg.devices) == 5 + assert len(dev_reg.devices) == 4 async def test_coordinator_updates(hass, caplog): diff --git a/tests/components/hassio/test_sensor.py b/tests/components/hassio/test_sensor.py index 9dc620ba94f..382d804eaac 100644 --- a/tests/components/hassio/test_sensor.py +++ b/tests/components/hassio/test_sensor.py @@ -24,7 +24,11 @@ def mock_all(aioclient_mock, request): "http://127.0.0.1/info", json={ "result": "ok", - "data": {"supervisor": "222", "homeassistant": "0.110.0", "hassos": None}, + "data": { + "supervisor": "222", + "homeassistant": "0.110.0", + "hassos": "1.2.3", + }, }, ) aioclient_mock.get( diff --git a/tests/components/hassio/test_update.py b/tests/components/hassio/test_update.py index e682562297d..3407c26fc2f 100644 --- a/tests/components/hassio/test_update.py +++ b/tests/components/hassio/test_update.py @@ -25,7 +25,11 @@ def mock_all(aioclient_mock, request): "http://127.0.0.1/info", json={ "result": "ok", - "data": {"supervisor": "222", "homeassistant": "0.110.0", "hassos": None}, + "data": { + "supervisor": "222", + "homeassistant": "0.110.0", + "hassos": "1.2.3", + }, }, ) aioclient_mock.get( @@ -483,3 +487,25 @@ async def test_not_release_notes(hass, aioclient_mock, hass_ws_client): ) result = await client.receive_json() assert result["result"] is None + + +async def test_no_os_entity(hass): + """Test handling where there is no os entity.""" + with patch.dict(os.environ, MOCK_ENVIRON), patch( + "homeassistant.components.hassio.HassIO.get_info", + return_value={ + "supervisor": "222", + "homeassistant": "0.110.0", + "hassos": None, + }, + ): + result = await async_setup_component( + hass, + "hassio", + {"http": {"server_port": 9999, "server_host": "127.0.0.1"}, "hassio": {}}, + ) + assert result + await hass.async_block_till_done() + + # Verify that the entity does not exist + assert not hass.states.get("update.home_assistant_operating_system_update")