diff --git a/homeassistant/components/bluetooth/__init__.py b/homeassistant/components/bluetooth/__init__.py index 9492642e0e0..a5204d50b68 100644 --- a/homeassistant/components/bluetooth/__init__.py +++ b/homeassistant/components/bluetooth/__init__.py @@ -249,12 +249,11 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: ) ) elif await _async_has_bluetooth_adapter(): - hass.async_create_task( - hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY}, - data={}, - ) + discovery_flow.async_create_flow( + hass, + DOMAIN, + context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY}, + data={}, ) return True diff --git a/homeassistant/components/discovery/__init__.py b/homeassistant/components/discovery/__init__.py index cc104cc2110..75016c28048 100644 --- a/homeassistant/components/discovery/__init__.py +++ b/homeassistant/components/discovery/__init__.py @@ -13,6 +13,7 @@ from homeassistant import config_entries from homeassistant.components import zeroconf from homeassistant.const import EVENT_HOMEASSISTANT_STARTED from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers import discovery_flow import homeassistant.helpers.config_validation as cv from homeassistant.helpers.discovery import async_discover, async_load_platform from homeassistant.helpers.event import async_track_point_in_utc_time @@ -173,7 +174,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: already_discovered.add(discovery_hash) if service in CONFIG_ENTRY_HANDLERS: - await hass.config_entries.flow.async_init( + discovery_flow.async_create_flow( + hass, CONFIG_ENTRY_HANDLERS[service], context={"source": config_entries.SOURCE_DISCOVERY}, data=info, diff --git a/homeassistant/components/elkm1/discovery.py b/homeassistant/components/elkm1/discovery.py index 326698c3686..50db2840753 100644 --- a/homeassistant/components/elkm1/discovery.py +++ b/homeassistant/components/elkm1/discovery.py @@ -10,7 +10,7 @@ from elkm1_lib.discovery import AIOELKDiscovery, ElkSystem from homeassistant import config_entries from homeassistant.components import network from homeassistant.core import HomeAssistant, callback -from homeassistant.helpers import device_registry as dr +from homeassistant.helpers import device_registry as dr, discovery_flow from .const import DISCOVER_SCAN_TIMEOUT, DOMAIN @@ -87,10 +87,9 @@ def async_trigger_discovery( ) -> None: """Trigger config flows for discovered devices.""" for device in discovered_devices: - hass.async_create_task( - hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY}, - data=asdict(device), - ) + discovery_flow.async_create_flow( + hass, + DOMAIN, + context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY}, + data=asdict(device), ) diff --git a/homeassistant/components/ezviz/camera.py b/homeassistant/components/ezviz/camera.py index 91bab5f83af..307e1fac185 100644 --- a/homeassistant/components/ezviz/camera.py +++ b/homeassistant/components/ezviz/camera.py @@ -17,7 +17,11 @@ from homeassistant.config_entries import ( ) from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant -from homeassistant.helpers import config_validation as cv, entity_platform +from homeassistant.helpers import ( + config_validation as cv, + discovery_flow, + entity_platform, +) from .const import ( ATTR_DIRECTION, @@ -93,15 +97,14 @@ async def async_setup_entry( else: - hass.async_create_task( - hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_INTEGRATION_DISCOVERY}, - data={ - ATTR_SERIAL: camera, - CONF_IP_ADDRESS: value["local_ip"], - }, - ) + discovery_flow.async_create_flow( + hass, + DOMAIN, + context={"source": SOURCE_INTEGRATION_DISCOVERY}, + data={ + ATTR_SERIAL: camera, + CONF_IP_ADDRESS: value["local_ip"], + }, ) _LOGGER.warning( diff --git a/homeassistant/components/flux_led/discovery.py b/homeassistant/components/flux_led/discovery.py index 67dbbc74e2e..ef0c131993e 100644 --- a/homeassistant/components/flux_led/discovery.py +++ b/homeassistant/components/flux_led/discovery.py @@ -27,7 +27,7 @@ from homeassistant.components import network from homeassistant.config_entries import ConfigEntry, ConfigEntryState from homeassistant.const import CONF_HOST, CONF_MODEL, CONF_NAME from homeassistant.core import HomeAssistant, callback -from homeassistant.helpers import device_registry as dr +from homeassistant.helpers import device_registry as dr, discovery_flow from homeassistant.util.network import is_ip_address from .const import ( @@ -221,10 +221,9 @@ def async_trigger_discovery( ) -> None: """Trigger config flows for discovered devices.""" for device in discovered_devices: - hass.async_create_task( - hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY}, - data={**device}, - ) + discovery_flow.async_create_flow( + hass, + DOMAIN, + context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY}, + data={**device}, ) diff --git a/homeassistant/components/hassio/discovery.py b/homeassistant/components/hassio/discovery.py index 587457f2ca2..e8cbbfc6bf5 100644 --- a/homeassistant/components/hassio/discovery.py +++ b/homeassistant/components/hassio/discovery.py @@ -14,6 +14,7 @@ from homeassistant.components.http import HomeAssistantView from homeassistant.const import ATTR_NAME, ATTR_SERVICE, EVENT_HOMEASSISTANT_START from homeassistant.core import HomeAssistant, callback from homeassistant.data_entry_flow import BaseServiceInfo +from homeassistant.helpers import discovery_flow from .const import ATTR_ADDON, ATTR_CONFIG, ATTR_DISCOVERY, ATTR_UUID from .handler import HassioAPIError @@ -99,7 +100,8 @@ class HassIODiscovery(HomeAssistantView): config_data[ATTR_ADDON] = addon_info[ATTR_NAME] # Use config flow - await self.hass.config_entries.flow.async_init( + discovery_flow.async_create_flow( + self.hass, service, context={"source": config_entries.SOURCE_HASSIO}, data=HassioServiceInfo(config=config_data), diff --git a/homeassistant/components/lifx/discovery.py b/homeassistant/components/lifx/discovery.py index 1c6e9ab3060..6e1507c92ca 100644 --- a/homeassistant/components/lifx/discovery.py +++ b/homeassistant/components/lifx/discovery.py @@ -10,6 +10,7 @@ from homeassistant import config_entries from homeassistant.components import network from homeassistant.const import CONF_HOST from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers import discovery_flow from .const import CONF_SERIAL, DOMAIN @@ -38,12 +39,11 @@ async def async_discover_devices(hass: HomeAssistant) -> Iterable[Light]: @callback def async_init_discovery_flow(hass: HomeAssistant, host: str, serial: str) -> None: """Start discovery of devices.""" - hass.async_create_task( - hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY}, - data={CONF_HOST: host, CONF_SERIAL: serial}, - ) + discovery_flow.async_create_flow( + hass, + DOMAIN, + context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY}, + data={CONF_HOST: host, CONF_SERIAL: serial}, ) diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index 2bea1a593d1..1121377a30e 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -22,7 +22,12 @@ from homeassistant.const import ( ) from homeassistant.core import HassJob, HomeAssistant, ServiceCall, callback from homeassistant.exceptions import TemplateError, Unauthorized -from homeassistant.helpers import config_validation as cv, event, template +from homeassistant.helpers import ( + config_validation as cv, + discovery_flow, + event, + template, +) from homeassistant.helpers.device_registry import DeviceEntry from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.reload import ( @@ -178,12 +183,11 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: # Create an import flow if the user has yaml configured entities etc. # but no broker configuration. Note: The intention is not for this to # import broker configuration from YAML because that has been deprecated. - hass.async_create_task( - hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY}, - data={}, - ) + discovery_flow.async_create_flow( + hass, + DOMAIN, + context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY}, + data={}, ) hass.data[DATA_MQTT_RELOAD_NEEDED] = True elif mqtt_entry_status is False: diff --git a/homeassistant/components/plex/config_flow.py b/homeassistant/components/plex/config_flow.py index 42d227154a6..e79b7e7ee04 100644 --- a/homeassistant/components/plex/config_flow.py +++ b/homeassistant/components/plex/config_flow.py @@ -29,6 +29,7 @@ from homeassistant.const import ( ) from homeassistant.core import callback from homeassistant.data_entry_flow import FlowResult +from homeassistant.helpers import discovery_flow from homeassistant.helpers.aiohttp_client import async_get_clientsession import homeassistant.helpers.config_validation as cv @@ -76,7 +77,8 @@ async def async_discover(hass): gdm = GDM() await hass.async_add_executor_job(gdm.scan) for server_data in gdm.entries: - await hass.config_entries.flow.async_init( + discovery_flow.async_create_flow( + hass, DOMAIN, context={CONF_SOURCE: config_entries.SOURCE_INTEGRATION_DISCOVERY}, data=server_data, diff --git a/homeassistant/components/senseme/discovery.py b/homeassistant/components/senseme/discovery.py index 624b18a8761..d3924ef16c0 100644 --- a/homeassistant/components/senseme/discovery.py +++ b/homeassistant/components/senseme/discovery.py @@ -8,6 +8,7 @@ from aiosenseme import SensemeDevice, SensemeDiscovery from homeassistant import config_entries from homeassistant.const import CONF_ID from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers import discovery_flow from .const import DISCOVERY, DOMAIN @@ -55,10 +56,9 @@ def async_trigger_discovery( """Trigger config flows for discovered devices.""" for device in discovered_devices: if device.uuid: - hass.async_create_task( - hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY}, - data={CONF_ID: device.uuid}, - ) + discovery_flow.async_create_flow( + hass, + DOMAIN, + context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY}, + data={CONF_ID: device.uuid}, ) diff --git a/homeassistant/components/squeezebox/media_player.py b/homeassistant/components/squeezebox/media_player.py index cd628a639c5..260228e4fdf 100644 --- a/homeassistant/components/squeezebox/media_player.py +++ b/homeassistant/components/squeezebox/media_player.py @@ -39,7 +39,11 @@ from homeassistant.const import ( STATE_PLAYING, ) from homeassistant.core import HomeAssistant, callback -from homeassistant.helpers import config_validation as cv, entity_platform +from homeassistant.helpers import ( + config_validation as cv, + discovery_flow, + entity_platform, +) from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.device_registry import format_mac from homeassistant.helpers.dispatcher import ( @@ -99,16 +103,15 @@ async def start_server_discovery(hass): """Start a server discovery task.""" def _discovered_server(server): - asyncio.create_task( - hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_INTEGRATION_DISCOVERY}, - data={ - CONF_HOST: server.host, - CONF_PORT: int(server.port), - "uuid": server.uuid, - }, - ) + discovery_flow.async_create_flow( + hass, + DOMAIN, + context={"source": SOURCE_INTEGRATION_DISCOVERY}, + data={ + CONF_HOST: server.host, + CONF_PORT: int(server.port), + "uuid": server.uuid, + }, ) hass.data.setdefault(DOMAIN, {}) diff --git a/homeassistant/components/steamist/discovery.py b/homeassistant/components/steamist/discovery.py index 7600503658f..cff97692979 100644 --- a/homeassistant/components/steamist/discovery.py +++ b/homeassistant/components/steamist/discovery.py @@ -11,7 +11,7 @@ from homeassistant import config_entries from homeassistant.components import network from homeassistant.const import CONF_MODEL, CONF_NAME from homeassistant.core import HomeAssistant, callback -from homeassistant.helpers import device_registry as dr +from homeassistant.helpers import device_registry as dr, discovery_flow from homeassistant.util.network import is_ip_address from .const import DISCOVER_SCAN_TIMEOUT, DISCOVERY, DOMAIN @@ -122,15 +122,14 @@ def async_trigger_discovery( ) -> None: """Trigger config flows for discovered devices.""" for device in discovered_devices: - hass.async_create_task( - hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY}, - data={ - "ipaddress": device.ipaddress, - "name": device.name, - "mac": device.mac, - "hostname": device.hostname, - }, - ) + discovery_flow.async_create_flow( + hass, + DOMAIN, + context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY}, + data={ + "ipaddress": device.ipaddress, + "name": device.name, + "mac": device.mac, + "hostname": device.hostname, + }, ) diff --git a/homeassistant/components/tplink/__init__.py b/homeassistant/components/tplink/__init__.py index 50c18000baa..9606dc29a44 100644 --- a/homeassistant/components/tplink/__init__.py +++ b/homeassistant/components/tplink/__init__.py @@ -19,7 +19,7 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import ConfigEntryNotReady -from homeassistant.helpers import device_registry as dr +from homeassistant.helpers import device_registry as dr, discovery_flow from homeassistant.helpers.event import async_track_time_interval from homeassistant.helpers.typing import ConfigType @@ -36,16 +36,15 @@ def async_trigger_discovery( ) -> None: """Trigger config flows for discovered devices.""" for formatted_mac, device in discovered_devices.items(): - hass.async_create_task( - hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY}, - data={ - CONF_NAME: device.alias, - CONF_HOST: device.host, - CONF_MAC: formatted_mac, - }, - ) + discovery_flow.async_create_flow( + hass, + DOMAIN, + context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY}, + data={ + CONF_NAME: device.alias, + CONF_HOST: device.host, + CONF_MAC: formatted_mac, + }, ) diff --git a/homeassistant/components/unifiprotect/discovery.py b/homeassistant/components/unifiprotect/discovery.py index 537e2fa1121..d58cad4e40a 100644 --- a/homeassistant/components/unifiprotect/discovery.py +++ b/homeassistant/components/unifiprotect/discovery.py @@ -11,6 +11,7 @@ from unifi_discovery import AIOUnifiScanner, UnifiDevice, UnifiService from homeassistant import config_entries from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers import discovery_flow from homeassistant.helpers.event import async_track_time_interval from .const import DOMAIN @@ -54,10 +55,9 @@ def async_trigger_discovery( """Trigger config flows for discovered devices.""" for device in discovered_devices: if device.services[UnifiService.Protect] and device.hw_addr: - hass.async_create_task( - hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY}, - data=asdict(device), - ) + discovery_flow.async_create_flow( + hass, + DOMAIN, + context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY}, + data=asdict(device), ) diff --git a/homeassistant/components/wiz/discovery.py b/homeassistant/components/wiz/discovery.py index 0b7015643ff..0f4be1d873e 100644 --- a/homeassistant/components/wiz/discovery.py +++ b/homeassistant/components/wiz/discovery.py @@ -10,6 +10,7 @@ from pywizlight.discovery import DiscoveredBulb, find_wizlights from homeassistant import config_entries from homeassistant.components import network from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers import discovery_flow from .const import DOMAIN @@ -46,10 +47,9 @@ def async_trigger_discovery( ) -> None: """Trigger config flows for discovered devices.""" for device in discovered_devices: - hass.async_create_task( - hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY}, - data=asdict(device), - ) + discovery_flow.async_create_flow( + hass, + DOMAIN, + context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY}, + data=asdict(device), ) diff --git a/homeassistant/components/yalexs_ble/__init__.py b/homeassistant/components/yalexs_ble/__init__.py index 3b9481b6982..6073bf7a032 100644 --- a/homeassistant/components/yalexs_ble/__init__.py +++ b/homeassistant/components/yalexs_ble/__init__.py @@ -12,6 +12,7 @@ from homeassistant.config_entries import SOURCE_INTEGRATION_DISCOVERY, ConfigEnt from homeassistant.const import CONF_ADDRESS, Platform from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import ConfigEntryNotReady +from homeassistant.helpers import discovery_flow from .const import CONF_KEY, CONF_LOCAL_NAME, CONF_SLOT, DEVICE_TIMEOUT, DOMAIN from .models import YaleXSBLEData @@ -33,12 +34,11 @@ class YaleXSBLEDiscovery(TypedDict): @callback def async_discovery(hass: HomeAssistant, discovery: YaleXSBLEDiscovery) -> None: """Update keys for the yalexs-ble integration if available.""" - hass.async_create_task( - hass.config_entries.flow.async_init( - "yalexs_ble", - context={"source": SOURCE_INTEGRATION_DISCOVERY}, - data=discovery, - ) + discovery_flow.async_create_flow( + hass, + DOMAIN, + context={"source": SOURCE_INTEGRATION_DISCOVERY}, + data=discovery, ) diff --git a/homeassistant/components/yeelight/scanner.py b/homeassistant/components/yeelight/scanner.py index 088071244b3..0b33512e315 100644 --- a/homeassistant/components/yeelight/scanner.py +++ b/homeassistant/components/yeelight/scanner.py @@ -15,6 +15,7 @@ from async_upnp_client.utils import CaseInsensitiveDict from homeassistant import config_entries from homeassistant.components import network, ssdp from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback +from homeassistant.helpers import discovery_flow from homeassistant.helpers.event import async_call_later, async_track_time_interval from .const import ( @@ -161,17 +162,16 @@ class YeelightScanner: def _async_discovered_by_ssdp(self, response: CaseInsensitiveDict) -> None: @callback def _async_start_flow(*_) -> None: - asyncio.create_task( - self._hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_SSDP}, - data=ssdp.SsdpServiceInfo( - ssdp_usn="", - ssdp_st=SSDP_ST, - ssdp_headers=response, - upnp={}, - ), - ) + discovery_flow.async_create_flow( + self._hass, + DOMAIN, + context={"source": config_entries.SOURCE_SSDP}, + data=ssdp.SsdpServiceInfo( + ssdp_usn="", + ssdp_st=SSDP_ST, + ssdp_headers=response, + upnp={}, + ), ) # Delay starting the flow in case the discovery is the result diff --git a/tests/components/hassio/test_discovery.py b/tests/components/hassio/test_discovery.py index 71f5f8acf96..30013c34f21 100644 --- a/tests/components/hassio/test_discovery.py +++ b/tests/components/hassio/test_discovery.py @@ -4,7 +4,7 @@ from unittest.mock import Mock, patch from homeassistant.components.hassio import HassioServiceInfo from homeassistant.components.hassio.handler import HassioAPIError -from homeassistant.const import EVENT_HOMEASSISTANT_START +from homeassistant.const import EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STARTED from homeassistant.setup import async_setup_component @@ -45,7 +45,8 @@ async def test_hassio_discovery_startup(hass, aioclient_mock, hassio_client): ) as mock_mqtt: hass.bus.async_fire(EVENT_HOMEASSISTANT_START) await hass.async_block_till_done() - + hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED) + await hass.async_block_till_done() assert aioclient_mock.call_count == 2 assert mock_mqtt.called mock_mqtt.assert_called_with( @@ -159,6 +160,8 @@ async def test_hassio_discovery_webhook(hass, aioclient_mock, hassio_client): json={"addon": "mosquitto", "service": "mqtt", "uuid": "testuuid"}, ) await hass.async_block_till_done() + hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED) + await hass.async_block_till_done() assert resp.status == HTTPStatus.OK assert aioclient_mock.call_count == 2 diff --git a/tests/components/plex/test_config_flow.py b/tests/components/plex/test_config_flow.py index f02abd834d7..fb5a0f06724 100644 --- a/tests/components/plex/test_config_flow.py +++ b/tests/components/plex/test_config_flow.py @@ -703,6 +703,7 @@ async def test_integration_discovery(hass): with patch("homeassistant.components.plex.config_flow.GDM", return_value=mock_gdm): await config_flow.async_discover(hass) + await hass.async_block_till_done() flows = hass.config_entries.flow.async_progress()