From 8d6247446bac4060d47ded1761b8a44e2b61bb55 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sat, 23 Jul 2022 17:47:12 +0200 Subject: [PATCH] Automatically set up Bluetooth during onboarding (#75658) --- .../components/bluetooth/config_flow.py | 3 ++- .../components/bluetooth/test_config_flow.py | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/bluetooth/config_flow.py b/homeassistant/components/bluetooth/config_flow.py index 2193170810f..8fe01be769d 100644 --- a/homeassistant/components/bluetooth/config_flow.py +++ b/homeassistant/components/bluetooth/config_flow.py @@ -3,6 +3,7 @@ from __future__ import annotations from typing import Any +from homeassistant.components import onboarding from homeassistant.config_entries import ConfigFlow from homeassistant.data_entry_flow import FlowResult @@ -27,7 +28,7 @@ class BluetoothConfigFlow(ConfigFlow, domain=DOMAIN): if self._async_current_entries(): return self.async_abort(reason="already_configured") - if user_input is not None: + if user_input is not None or not onboarding.async_is_onboarded(self.hass): return self.async_create_entry(title=DEFAULT_NAME, data={}) return self.async_show_form(step_id="enable_bluetooth") diff --git a/tests/components/bluetooth/test_config_flow.py b/tests/components/bluetooth/test_config_flow.py index 550f5a583d7..5c6199b9bf0 100644 --- a/tests/components/bluetooth/test_config_flow.py +++ b/tests/components/bluetooth/test_config_flow.py @@ -64,6 +64,27 @@ async def test_async_step_integration_discovery(hass): assert len(mock_setup_entry.mock_calls) == 1 +async def test_async_step_integration_discovery_during_onboarding(hass): + """Test setting up from integration discovery during onboarding.""" + + with patch( + "homeassistant.components.bluetooth.async_setup_entry", return_value=True + ) as mock_setup_entry, patch( + "homeassistant.components.onboarding.async_is_onboarded", + return_value=False, + ) as mock_onboarding: + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY}, + data={}, + ) + assert result["type"] == FlowResultType.CREATE_ENTRY + assert result["title"] == "Bluetooth" + assert result["data"] == {} + assert len(mock_setup_entry.mock_calls) == 1 + assert len(mock_onboarding.mock_calls) == 1 + + async def test_async_step_integration_discovery_already_exists(hass): """Test setting up from integration discovery when an entry already exists.""" entry = MockConfigEntry(domain=DOMAIN)