From 4624c859e152fa0f04e5805f0e5dd2c1c943ab6a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 26 Feb 2024 07:04:33 -1000 Subject: [PATCH] Use discovery flow helper for hardware integrations (#111437) * Use discovery flow helper for hardware integrations The discovery flow helper defers loading discovered integrations until after startup to improve startup reliability. * Use discovery flow helper for hardware integrations The discovery flow helper defers loading discovered integrations until after startup to improve startup reliability. Since hardware was not listed in as a discovery integration, the notification for new discoveries was missing. --- .../components/homeassistant_sky_connect/__init__.py | 8 +++++--- homeassistant/components/homeassistant_yellow/__init__.py | 8 +++++--- homeassistant/config_entries.py | 2 ++ tests/components/zha/test_config_flow.py | 6 +++--- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/homeassistant_sky_connect/__init__.py b/homeassistant/components/homeassistant_sky_connect/__init__.py index 218e0c3e88d..4880d2e375f 100644 --- a/homeassistant/components/homeassistant_sky_connect/__init__.py +++ b/homeassistant/components/homeassistant_sky_connect/__init__.py @@ -7,9 +7,10 @@ from homeassistant.components.homeassistant_hardware.silabs_multiprotocol_addon get_zigbee_socket, multi_pan_addon_using_device, ) -from homeassistant.config_entries import ConfigEntry +from homeassistant.config_entries import SOURCE_HARDWARE, ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import ConfigEntryNotReady, HomeAssistantError +from homeassistant.helpers import discovery_flow from .const import DOMAIN from .util import get_usb_service_info @@ -51,9 +52,10 @@ async def _async_usb_scan_done(hass: HomeAssistant, entry: ConfigEntry) -> None: }, "radio_type": "ezsp", } - await hass.config_entries.flow.async_init( + discovery_flow.async_create_flow( + hass, "zha", - context={"source": "hardware"}, + context={"source": SOURCE_HARDWARE}, data=hw_discovery_data, ) diff --git a/homeassistant/components/homeassistant_yellow/__init__.py b/homeassistant/components/homeassistant_yellow/__init__.py index 092911d1532..84ad464e779 100644 --- a/homeassistant/components/homeassistant_yellow/__init__.py +++ b/homeassistant/components/homeassistant_yellow/__init__.py @@ -7,9 +7,10 @@ from homeassistant.components.homeassistant_hardware.silabs_multiprotocol_addon get_zigbee_socket, multi_pan_addon_using_device, ) -from homeassistant.config_entries import ConfigEntry +from homeassistant.config_entries import SOURCE_HARDWARE, ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady, HomeAssistantError +from homeassistant.helpers import discovery_flow from .const import RADIO_DEVICE, ZHA_HW_DISCOVERY_DATA @@ -47,9 +48,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: "radio_type": "ezsp", } - await hass.config_entries.flow.async_init( + discovery_flow.async_create_flow( + hass, "zha", - context={"source": "hardware"}, + context={"source": SOURCE_HARDWARE}, data=hw_discovery_data, ) diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py index f2753322bc0..6ed9911cab5 100644 --- a/homeassistant/config_entries.py +++ b/homeassistant/config_entries.py @@ -78,6 +78,7 @@ _LOGGER = logging.getLogger(__name__) SOURCE_BLUETOOTH = "bluetooth" SOURCE_DHCP = "dhcp" SOURCE_DISCOVERY = "discovery" +SOURCE_HARDWARE = "hardware" SOURCE_HASSIO = "hassio" SOURCE_HOMEKIT = "homekit" SOURCE_IMPORT = "import" @@ -159,6 +160,7 @@ DISCOVERY_SOURCES = { SOURCE_BLUETOOTH, SOURCE_DHCP, SOURCE_DISCOVERY, + SOURCE_HARDWARE, SOURCE_HOMEKIT, SOURCE_IMPORT, SOURCE_INTEGRATION_DISCOVERY, diff --git a/tests/components/zha/test_config_flow.py b/tests/components/zha/test_config_flow.py index e95c39e5f82..3cd20771e6e 100644 --- a/tests/components/zha/test_config_flow.py +++ b/tests/components/zha/test_config_flow.py @@ -976,7 +976,7 @@ async def test_hardware(onboarded, hass: HomeAssistant) -> None: "homeassistant.components.onboarding.async_is_onboarded", return_value=onboarded ): result1 = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": "hardware"}, data=data + DOMAIN, context={"source": config_entries.SOURCE_HARDWARE}, data=data ) if onboarded: @@ -1029,7 +1029,7 @@ async def test_hardware_already_setup(hass: HomeAssistant) -> None: }, } result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": "hardware"}, data=data + DOMAIN, context={"source": config_entries.SOURCE_HARDWARE}, data=data ) assert result["type"] == FlowResultType.ABORT @@ -1043,7 +1043,7 @@ async def test_hardware_invalid_data(hass: HomeAssistant, data) -> None: """Test onboarding flow -- invalid data.""" result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": "hardware"}, data=data + DOMAIN, context={"source": config_entries.SOURCE_HARDWARE}, data=data ) assert result["type"] == FlowResultType.ABORT