From f1d3c417f9498d0d520a3fc03d161478d62ef90b Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 5 Feb 2024 12:29:25 +0100 Subject: [PATCH] Remove homeassistant_yellow config entry if hassio is not present (#109686) --- .../homeassistant_yellow/__init__.py | 7 +++- .../homeassistant_yellow/manifest.json | 3 +- .../homeassistant_yellow/test_config_flow.py | 10 ++++++ .../homeassistant_yellow/test_hardware.py | 4 +++ .../homeassistant_yellow/test_init.py | 32 +++++++++++++++++++ 5 files changed, 54 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/homeassistant_yellow/__init__.py b/homeassistant/components/homeassistant_yellow/__init__.py index b61e01061c3..092911d1532 100644 --- a/homeassistant/components/homeassistant_yellow/__init__.py +++ b/homeassistant/components/homeassistant_yellow/__init__.py @@ -1,7 +1,7 @@ """The Home Assistant Yellow integration.""" from __future__ import annotations -from homeassistant.components.hassio import get_os_info +from homeassistant.components.hassio import get_os_info, is_hassio from homeassistant.components.homeassistant_hardware.silabs_multiprotocol_addon import ( check_multi_pan_addon, get_zigbee_socket, @@ -16,6 +16,11 @@ from .const import RADIO_DEVICE, ZHA_HW_DISCOVERY_DATA async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up a Home Assistant Yellow config entry.""" + if not is_hassio(hass): + # Not running under supervisor, Home Assistant may have been migrated + hass.async_create_task(hass.config_entries.async_remove(entry.entry_id)) + return False + if (os_info := get_os_info(hass)) is None: # The hassio integration has not yet fetched data from the supervisor raise ConfigEntryNotReady diff --git a/homeassistant/components/homeassistant_yellow/manifest.json b/homeassistant/components/homeassistant_yellow/manifest.json index dd74df9295f..a9715003172 100644 --- a/homeassistant/components/homeassistant_yellow/manifest.json +++ b/homeassistant/components/homeassistant_yellow/manifest.json @@ -1,9 +1,10 @@ { "domain": "homeassistant_yellow", "name": "Home Assistant Yellow", + "after_dependencies": ["hassio"], "codeowners": ["@home-assistant/core"], "config_flow": false, - "dependencies": ["hardware", "hassio", "homeassistant_hardware"], + "dependencies": ["hardware", "homeassistant_hardware"], "documentation": "https://www.home-assistant.io/integrations/homeassistant_yellow", "integration_type": "hardware" } diff --git a/tests/components/homeassistant_yellow/test_config_flow.py b/tests/components/homeassistant_yellow/test_config_flow.py index bd61400fa8e..1b80610953f 100644 --- a/tests/components/homeassistant_yellow/test_config_flow.py +++ b/tests/components/homeassistant_yellow/test_config_flow.py @@ -4,10 +4,12 @@ from unittest.mock import Mock, patch import pytest +from homeassistant.components.hassio import DOMAIN as HASSIO_DOMAIN from homeassistant.components.homeassistant_yellow.const import DOMAIN from homeassistant.components.zha.core.const import DOMAIN as ZHA_DOMAIN from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType +from homeassistant.setup import async_setup_component from tests.common import MockConfigEntry, MockModule, mock_integration @@ -52,6 +54,7 @@ def mock_reboot_host(): async def test_config_flow(hass: HomeAssistant) -> None: """Test the config flow.""" mock_integration(hass, MockModule("hassio")) + await async_setup_component(hass, HASSIO_DOMAIN, {}) with patch( "homeassistant.components.homeassistant_yellow.async_setup_entry", @@ -76,6 +79,7 @@ async def test_config_flow(hass: HomeAssistant) -> None: async def test_config_flow_single_entry(hass: HomeAssistant) -> None: """Test only a single entry is allowed.""" mock_integration(hass, MockModule("hassio")) + await async_setup_component(hass, HASSIO_DOMAIN, {}) # Setup the config entry config_entry = MockConfigEntry( @@ -109,6 +113,7 @@ async def test_option_flow_install_multi_pan_addon( ) -> None: """Test installing the multi pan addon.""" mock_integration(hass, MockModule("hassio")) + await async_setup_component(hass, HASSIO_DOMAIN, {}) # Setup the config entry config_entry = MockConfigEntry( @@ -179,6 +184,7 @@ async def test_option_flow_install_multi_pan_addon_zha( ) -> None: """Test installing the multi pan addon when a zha config entry exists.""" mock_integration(hass, MockModule("hassio")) + await async_setup_component(hass, HASSIO_DOMAIN, {}) # Setup the config entry config_entry = MockConfigEntry( @@ -270,6 +276,7 @@ async def test_option_flow_led_settings( ) -> None: """Test updating LED settings.""" mock_integration(hass, MockModule("hassio")) + await async_setup_component(hass, HASSIO_DOMAIN, {}) # Setup the config entry config_entry = MockConfigEntry( @@ -315,6 +322,7 @@ async def test_option_flow_led_settings_unchanged( ) -> None: """Test updating LED settings.""" mock_integration(hass, MockModule("hassio")) + await async_setup_component(hass, HASSIO_DOMAIN, {}) # Setup the config entry config_entry = MockConfigEntry( @@ -346,6 +354,7 @@ async def test_option_flow_led_settings_unchanged( async def test_option_flow_led_settings_fail_1(hass: HomeAssistant) -> None: """Test updating LED settings.""" mock_integration(hass, MockModule("hassio")) + await async_setup_component(hass, HASSIO_DOMAIN, {}) # Setup the config entry config_entry = MockConfigEntry( @@ -377,6 +386,7 @@ async def test_option_flow_led_settings_fail_2( ) -> None: """Test updating LED settings.""" mock_integration(hass, MockModule("hassio")) + await async_setup_component(hass, HASSIO_DOMAIN, {}) # Setup the config entry config_entry = MockConfigEntry( diff --git a/tests/components/homeassistant_yellow/test_hardware.py b/tests/components/homeassistant_yellow/test_hardware.py index 5fb662471aa..b7843e75dcf 100644 --- a/tests/components/homeassistant_yellow/test_hardware.py +++ b/tests/components/homeassistant_yellow/test_hardware.py @@ -3,8 +3,10 @@ from unittest.mock import patch import pytest +from homeassistant.components.hassio import DOMAIN as HASSIO_DOMAIN from homeassistant.components.homeassistant_yellow.const import DOMAIN from homeassistant.core import HomeAssistant +from homeassistant.setup import async_setup_component from tests.common import MockConfigEntry, MockModule, mock_integration from tests.typing import WebSocketGenerator @@ -15,6 +17,7 @@ async def test_hardware_info( ) -> None: """Test we can get the board info.""" mock_integration(hass, MockModule("hassio")) + await async_setup_component(hass, HASSIO_DOMAIN, {}) # Setup the config entry config_entry = MockConfigEntry( @@ -66,6 +69,7 @@ async def test_hardware_info_fail( ) -> None: """Test async_info raises if os_info is not as expected.""" mock_integration(hass, MockModule("hassio")) + await async_setup_component(hass, HASSIO_DOMAIN, {}) # Setup the config entry config_entry = MockConfigEntry( diff --git a/tests/components/homeassistant_yellow/test_init.py b/tests/components/homeassistant_yellow/test_init.py index f8cdcd8a13b..f042a7bf54d 100644 --- a/tests/components/homeassistant_yellow/test_init.py +++ b/tests/components/homeassistant_yellow/test_init.py @@ -4,10 +4,12 @@ from unittest.mock import patch import pytest from homeassistant.components import zha +from homeassistant.components.hassio import DOMAIN as HASSIO_DOMAIN from homeassistant.components.hassio.handler import HassioAPIError from homeassistant.components.homeassistant_yellow.const import DOMAIN from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant +from homeassistant.setup import async_setup_component from tests.common import MockConfigEntry, MockModule, mock_integration @@ -20,6 +22,7 @@ async def test_setup_entry( ) -> None: """Test setup of a config entry, including setup of zha.""" mock_integration(hass, MockModule("hassio")) + await async_setup_component(hass, HASSIO_DOMAIN, {}) # Setup the config entry config_entry = MockConfigEntry( @@ -62,6 +65,7 @@ async def test_setup_entry( async def test_setup_zha(hass: HomeAssistant, addon_store_info) -> None: """Test zha gets the right config.""" mock_integration(hass, MockModule("hassio")) + await async_setup_component(hass, HASSIO_DOMAIN, {}) # Setup the config entry config_entry = MockConfigEntry( @@ -110,6 +114,7 @@ async def test_setup_zha_multipan( ) -> None: """Test zha gets the right config.""" mock_integration(hass, MockModule("hassio")) + await async_setup_component(hass, HASSIO_DOMAIN, {}) addon_info.return_value["options"]["device"] = "/dev/ttyAMA1" @@ -160,6 +165,7 @@ async def test_setup_zha_multipan_other_device( ) -> None: """Test zha gets the right config.""" mock_integration(hass, MockModule("hassio")) + await async_setup_component(hass, HASSIO_DOMAIN, {}) addon_info.return_value["options"]["device"] = "/dev/not_yellow_radio" @@ -205,9 +211,32 @@ async def test_setup_zha_multipan_other_device( assert config_entry.title == "Yellow" +async def test_setup_entry_no_hassio(hass: HomeAssistant) -> None: + """Test setup of a config entry without hassio.""" + # Setup the config entry + config_entry = MockConfigEntry( + data={}, + domain=DOMAIN, + options={}, + title="Home Assistant Yellow", + ) + config_entry.add_to_hass(hass) + assert len(hass.config_entries.async_entries()) == 1 + + with patch( + "homeassistant.components.homeassistant_yellow.get_os_info" + ) as mock_get_os_info: + assert not await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + assert len(mock_get_os_info.mock_calls) == 0 + assert len(hass.config_entries.async_entries()) == 0 + + async def test_setup_entry_wrong_board(hass: HomeAssistant) -> None: """Test setup of a config entry with wrong board type.""" mock_integration(hass, MockModule("hassio")) + await async_setup_component(hass, HASSIO_DOMAIN, {}) # Setup the config entry config_entry = MockConfigEntry( @@ -233,6 +262,7 @@ async def test_setup_entry_wrong_board(hass: HomeAssistant) -> None: async def test_setup_entry_wait_hassio(hass: HomeAssistant) -> None: """Test setup of a config entry when hassio has not fetched os_info.""" mock_integration(hass, MockModule("hassio")) + await async_setup_component(hass, HASSIO_DOMAIN, {}) # Setup the config entry config_entry = MockConfigEntry( @@ -258,6 +288,7 @@ async def test_setup_entry_addon_info_fails( ) -> None: """Test setup of a config entry when fetching addon info fails.""" mock_integration(hass, MockModule("hassio")) + await async_setup_component(hass, HASSIO_DOMAIN, {}) addon_store_info.side_effect = HassioAPIError("Boom") # Setup the config entry @@ -285,6 +316,7 @@ async def test_setup_entry_addon_not_running( ) -> None: """Test the addon is started if it is not running.""" mock_integration(hass, MockModule("hassio")) + await async_setup_component(hass, HASSIO_DOMAIN, {}) # Setup the config entry config_entry = MockConfigEntry(