diff --git a/homeassistant/components/baf/config_flow.py b/homeassistant/components/baf/config_flow.py index 1c2873eb759..2326d30937b 100644 --- a/homeassistant/components/baf/config_flow.py +++ b/homeassistant/components/baf/config_flow.py @@ -54,7 +54,7 @@ class BAFFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): uuid = properties["uuid"] model = properties["model"] name = properties["name"] - await self.async_set_unique_id(uuid, raise_on_progress=False) + await self.async_set_unique_id(uuid) self._abort_if_unique_id_configured(updates={CONF_IP_ADDRESS: ip_address}) self.discovery = BAFDiscovery(ip_address, name, uuid, model) return await self.async_step_discovery_confirm() @@ -98,7 +98,9 @@ class BAFFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): ) errors["base"] = "unknown" else: - await self.async_set_unique_id(device.dns_sd_uuid) + await self.async_set_unique_id( + device.dns_sd_uuid, raise_on_progress=False + ) self._abort_if_unique_id_configured( updates={CONF_IP_ADDRESS: ip_address} ) diff --git a/tests/components/baf/__init__.py b/tests/components/baf/__init__.py index e0432ed643a..4e435dc1a2e 100644 --- a/tests/components/baf/__init__.py +++ b/tests/components/baf/__init__.py @@ -1 +1,37 @@ """Tests for the Big Ass Fans integration.""" + + +import asyncio + +from aiobafi6 import Device + +MOCK_UUID = "1234" +MOCK_NAME = "Living Room Fan" + + +class MockBAFDevice(Device): + """A simple mock for a BAF Device.""" + + def __init__(self, async_wait_available_side_effect=None): + """Init simple mock.""" + self._async_wait_available_side_effect = async_wait_available_side_effect + + @property + def dns_sd_uuid(self): + """Mock the unique id.""" + return MOCK_UUID + + @property + def name(self): + """Mock the name of the device.""" + return MOCK_NAME + + async def async_wait_available(self): + """Mock async_wait_available.""" + if self._async_wait_available_side_effect: + raise self._async_wait_available_side_effect + return + + def async_run(self): + """Mock async_run.""" + return asyncio.Future() diff --git a/tests/components/baf/test_config_flow.py b/tests/components/baf/test_config_flow.py index 687a871ed4c..77a83a9673b 100644 --- a/tests/components/baf/test_config_flow.py +++ b/tests/components/baf/test_config_flow.py @@ -12,9 +12,20 @@ from homeassistant.data_entry_flow import ( RESULT_TYPE_FORM, ) +from . import MOCK_NAME, MOCK_UUID, MockBAFDevice + from tests.common import MockConfigEntry +def _patch_device_config_flow(side_effect=None): + """Mock out the BAF Device object.""" + + def _create_mock_baf(*args, **kwargs): + return MockBAFDevice(side_effect) + + return patch("homeassistant.components.baf.config_flow.Device", _create_mock_baf) + + async def test_form_user(hass): """Test we get the user form.""" @@ -24,9 +35,7 @@ async def test_form_user(hass): assert result["type"] == "form" assert result["errors"] == {} - with patch("homeassistant.components.baf.config_flow.Device.async_run",), patch( - "homeassistant.components.baf.config_flow.Device.async_wait_available", - ), patch( + with _patch_device_config_flow(), patch( "homeassistant.components.baf.async_setup_entry", return_value=True, ) as mock_setup_entry: @@ -37,7 +46,7 @@ async def test_form_user(hass): await hass.async_block_till_done() assert result2["type"] == RESULT_TYPE_CREATE_ENTRY - assert result2["title"] == "127.0.0.1" + assert result2["title"] == MOCK_NAME assert result2["data"] == {CONF_IP_ADDRESS: "127.0.0.1"} assert len(mock_setup_entry.mock_calls) == 1 @@ -48,10 +57,7 @@ async def test_form_cannot_connect(hass): DOMAIN, context={"source": config_entries.SOURCE_USER} ) - with patch("homeassistant.components.baf.config_flow.Device.async_run",), patch( - "homeassistant.components.baf.config_flow.Device.async_wait_available", - side_effect=asyncio.TimeoutError, - ): + with _patch_device_config_flow(asyncio.TimeoutError): result2 = await hass.config_entries.flow.async_configure( result["flow_id"], {CONF_IP_ADDRESS: "127.0.0.1"}, @@ -67,10 +73,7 @@ async def test_form_unknown_exception(hass): DOMAIN, context={"source": config_entries.SOURCE_USER} ) - with patch("homeassistant.components.baf.config_flow.Device.async_run",), patch( - "homeassistant.components.baf.config_flow.Device.async_wait_available", - side_effect=Exception, - ): + with _patch_device_config_flow(Exception): result2 = await hass.config_entries.flow.async_configure( result["flow_id"], {CONF_IP_ADDRESS: "127.0.0.1"}, @@ -92,7 +95,7 @@ async def test_zeroconf_discovery(hass): hostname="mock_hostname", name="testfan", port=None, - properties={"name": "My Fan", "model": "Haiku", "uuid": "1234"}, + properties={"name": "My Fan", "model": "Haiku", "uuid": MOCK_UUID}, type="mock_type", ), ) @@ -118,7 +121,7 @@ async def test_zeroconf_discovery(hass): async def test_zeroconf_updates_existing_ip(hass): """Test we can setup from zeroconf discovery.""" entry = MockConfigEntry( - domain=DOMAIN, data={CONF_IP_ADDRESS: "127.0.0.2"}, unique_id="1234" + domain=DOMAIN, data={CONF_IP_ADDRESS: "127.0.0.2"}, unique_id=MOCK_UUID ) entry.add_to_hass(hass) result = await hass.config_entries.flow.async_init( @@ -130,7 +133,7 @@ async def test_zeroconf_updates_existing_ip(hass): hostname="mock_hostname", name="testfan", port=None, - properties={"name": "My Fan", "model": "Haiku", "uuid": "1234"}, + properties={"name": "My Fan", "model": "Haiku", "uuid": MOCK_UUID}, type="mock_type", ), ) @@ -150,9 +153,48 @@ async def test_zeroconf_rejects_ipv6(hass): hostname="mock_hostname", name="testfan", port=None, - properties={"name": "My Fan", "model": "Haiku", "uuid": "1234"}, + properties={"name": "My Fan", "model": "Haiku", "uuid": MOCK_UUID}, type="mock_type", ), ) assert result["type"] == RESULT_TYPE_ABORT assert result["reason"] == "ipv6_not_supported" + + +async def test_user_flow_is_not_blocked_by_discovery(hass): + """Test we can setup from the user flow when there is also a discovery.""" + discovery_result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": config_entries.SOURCE_ZEROCONF}, + data=zeroconf.ZeroconfServiceInfo( + host="127.0.0.1", + addresses=["127.0.0.1"], + hostname="mock_hostname", + name="testfan", + port=None, + properties={"name": "My Fan", "model": "Haiku", "uuid": MOCK_UUID}, + type="mock_type", + ), + ) + assert discovery_result["type"] == RESULT_TYPE_FORM + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + assert result["type"] == "form" + assert result["errors"] == {} + + with _patch_device_config_flow(), patch( + "homeassistant.components.baf.async_setup_entry", + return_value=True, + ) as mock_setup_entry: + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], + {CONF_IP_ADDRESS: "127.0.0.1"}, + ) + await hass.async_block_till_done() + + assert result2["type"] == RESULT_TYPE_CREATE_ENTRY + assert result2["title"] == MOCK_NAME + assert result2["data"] == {CONF_IP_ADDRESS: "127.0.0.1"} + assert len(mock_setup_entry.mock_calls) == 1