diff --git a/homeassistant/components/roomba/config_flow.py b/homeassistant/components/roomba/config_flow.py index de1492bc2d5..45c2d8b9a1b 100644 --- a/homeassistant/components/roomba/config_flow.py +++ b/homeassistant/components/roomba/config_flow.py @@ -23,15 +23,16 @@ from .const import ( ) ROOMBA_DISCOVERY_LOCK = "roomba_discovery_lock" +ALL_ATTEMPTS = 2 +HOST_ATTEMPTS = 6 +ROOMBA_WAKE_TIME = 6 DEFAULT_OPTIONS = {CONF_CONTINUOUS: DEFAULT_CONTINUOUS, CONF_DELAY: DEFAULT_DELAY} MAX_NUM_DEVICES_TO_DISCOVER = 25 AUTH_HELP_URL_KEY = "auth_help_url" -AUTH_HELP_URL_VALUE = ( - "https://www.home-assistant.io/integrations/roomba/#retrieving-your-credentials" -) +AUTH_HELP_URL_VALUE = "https://www.home-assistant.io/integrations/roomba/#manually-retrieving-your-credentials" async def validate_input(hass: core.HomeAssistant, data): @@ -43,11 +44,13 @@ async def validate_input(hass: core.HomeAssistant, data): address=data[CONF_HOST], blid=data[CONF_BLID], password=data[CONF_PASSWORD], - continuous=data[CONF_CONTINUOUS], + continuous=False, delay=data[CONF_DELAY], ) info = await async_connect_or_timeout(hass, roomba) + if info: + await async_disconnect_or_timeout(hass, roomba) return { ROOMBA_SESSION: info[ROOMBA_SESSION], @@ -83,14 +86,23 @@ class RoombaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): if not dhcp_discovery[HOSTNAME].startswith(("irobot-", "roomba-")): return self.async_abort(reason="not_irobot_device") - blid = _async_blid_from_hostname(dhcp_discovery[HOSTNAME]) - await self.async_set_unique_id(blid) - self._abort_if_unique_id_configured( - updates={CONF_HOST: dhcp_discovery[IP_ADDRESS]} - ) - self.host = dhcp_discovery[IP_ADDRESS] - self.blid = blid + self.blid = _async_blid_from_hostname(dhcp_discovery[HOSTNAME]) + await self.async_set_unique_id(self.blid) + self._abort_if_unique_id_configured(updates={CONF_HOST: self.host}) + + # Because the hostname is so long some sources may + # truncate the hostname since it will be longer than + # the valid allowed length. If we already have a flow + # going for a longer hostname we abort so the user + # does not see two flows if discovery fails. + for progress in self._async_in_progress(): + flow_unique_id = progress["context"]["unique_id"] + if flow_unique_id.startswith(self.blid): + return self.async_abort(reason="short_blid") + if self.blid.startswith(flow_unique_id): + self.hass.config_entries.flow.async_abort(progress["flow_id"]) + self.context["title_placeholders"] = {"host": self.host, "name": self.blid} return await self.async_step_user() @@ -118,10 +130,8 @@ class RoombaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): return await self._async_start_link() already_configured = self._async_current_ids(False) - discovery = _async_get_roomba_discovery() - async with self.hass.data.setdefault(ROOMBA_DISCOVERY_LOCK, asyncio.Lock()): - devices = await self.hass.async_add_executor_job(discovery.get_all) + devices = await _async_discover_roombas(self.hass, self.host) if devices: # Find already configured hosts @@ -130,13 +140,14 @@ class RoombaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): for device in devices if device.blid not in already_configured } - if self.host and self.host in self.discovered_robots: - # From discovery - self.context["title_placeholders"] = { - "host": self.host, - "name": self.discovered_robots[self.host].robot_name, - } - return await self._async_start_link() + + if self.host and self.host in self.discovered_robots: + # From discovery + self.context["title_placeholders"] = { + "host": self.host, + "name": self.discovered_robots[self.host].robot_name, + } + return await self._async_start_link() if not self.discovered_robots: return await self.async_step_manual() @@ -180,7 +191,7 @@ class RoombaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): return self.async_abort(reason="already_configured") self.host = user_input[CONF_HOST] - self.blid = user_input[CONF_BLID] + self.blid = user_input[CONF_BLID].upper() await self.async_set_unique_id(self.blid, raise_on_progress=False) self._abort_if_unique_id_configured() return await self.async_step_link() @@ -197,11 +208,11 @@ class RoombaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): description_placeholders={CONF_NAME: self.name or self.blid}, ) + roomba_pw = RoombaPassword(self.host) + try: - password = await self.hass.async_add_executor_job( - RoombaPassword(self.host).get_password - ) - except ConnectionRefusedError: + password = await self.hass.async_add_executor_job(roomba_pw.get_password) + except (OSError, ConnectionRefusedError): return await self.async_step_link_manual() if not password: @@ -220,7 +231,6 @@ class RoombaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): except CannotConnect: return self.async_abort(reason="cannot_connect") - await async_disconnect_or_timeout(self.hass, info[ROOMBA_SESSION]) self.name = info[CONF_NAME] return self.async_create_entry(title=self.name, data=config) @@ -242,7 +252,6 @@ class RoombaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): errors = {"base": "cannot_connect"} if not errors: - await async_disconnect_or_timeout(self.hass, info[ROOMBA_SESSION]) return self.async_create_entry(title=info[CONF_NAME], data=config) return self.async_show_form( @@ -305,4 +314,41 @@ def _async_get_roomba_discovery(): @callback def _async_blid_from_hostname(hostname): """Extract the blid from the hostname.""" - return hostname.split("-")[1].split(".")[0] + return hostname.split("-")[1].split(".")[0].upper() + + +async def _async_discover_roombas(hass, host): + discovered_hosts = set() + devices = [] + discover_lock = hass.data.setdefault(ROOMBA_DISCOVERY_LOCK, asyncio.Lock()) + discover_attempts = HOST_ATTEMPTS if host else ALL_ATTEMPTS + + for attempt in range(discover_attempts + 1): + async with discover_lock: + discovery = _async_get_roomba_discovery() + try: + if host: + discovered = [ + await hass.async_add_executor_job(discovery.get, host) + ] + else: + discovered = await hass.async_add_executor_job(discovery.get_all) + except OSError: + # Socket temporarily unavailable + await asyncio.sleep(ROOMBA_WAKE_TIME * attempt) + continue + else: + for device in discovered: + if device.ip in discovered_hosts: + continue + discovered_hosts.add(device.ip) + devices.append(device) + finally: + discovery.server_socket.close() + + if host and host in discovered_hosts: + return devices + + await asyncio.sleep(ROOMBA_WAKE_TIME) + + return devices diff --git a/homeassistant/components/roomba/strings.json b/homeassistant/components/roomba/strings.json index 59039a8d276..16371041a15 100644 --- a/homeassistant/components/roomba/strings.json +++ b/homeassistant/components/roomba/strings.json @@ -11,7 +11,7 @@ }, "manual": { "title": "Manually connect to the device", - "description": "No Roomba or Braava have been discovered on your network. The BLID is the portion of the device hostname after `iRobot-`. Please follow the steps outlined in the documentation at: {auth_help_url}", + "description": "No Roomba or Braava have been discovered on your network. The BLID is the portion of the device hostname after `iRobot-` or `Roomba-`. Please follow the steps outlined in the documentation at: {auth_help_url}", "data": { "host": "[%key:common::config_flow::data::host%]", "blid": "BLID" @@ -19,7 +19,7 @@ }, "link": { "title": "Retrieve Password", - "description": "Press and hold the Home button on {name} until the device generates a sound (about two seconds)." + "description": "Press and hold the Home button on {name} until the device generates a sound (about two seconds), then submit within 30 seconds." }, "link_manual": { "title": "Enter Password", @@ -35,7 +35,8 @@ "abort": { "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", - "not_irobot_device": "Discovered device is not an iRobot device" + "not_irobot_device": "Discovered device is not an iRobot device", + "short_blid": "The BLID was truncated" } }, "options": { diff --git a/homeassistant/components/roomba/translations/en.json b/homeassistant/components/roomba/translations/en.json index ddbf5a10a78..cd5aed67a0a 100644 --- a/homeassistant/components/roomba/translations/en.json +++ b/homeassistant/components/roomba/translations/en.json @@ -3,7 +3,8 @@ "abort": { "already_configured": "Device is already configured", "cannot_connect": "Failed to connect", - "not_irobot_device": "Discovered device is not an iRobot device" + "not_irobot_device": "Discovered device is not an iRobot device", + "short_blid": "The BLID was truncated" }, "error": { "cannot_connect": "Failed to connect" @@ -18,7 +19,7 @@ "title": "Automatically connect to the device" }, "link": { - "description": "Press and hold the Home button on {name} until the device generates a sound (about two seconds).", + "description": "Press and hold the Home button on {name} until the device generates a sound (about two seconds), then submit within 30 seconds.", "title": "Retrieve Password" }, "link_manual": { @@ -33,19 +34,8 @@ "blid": "BLID", "host": "Host" }, - "description": "No Roomba or Braava have been discovered on your network. The BLID is the portion of the device hostname after `iRobot-`. Please follow the steps outlined in the documentation at: {auth_help_url}", + "description": "No Roomba or Braava have been discovered on your network. The BLID is the portion of the device hostname after `iRobot-` or `Roomba-`. Please follow the steps outlined in the documentation at: {auth_help_url}", "title": "Manually connect to the device" - }, - "user": { - "data": { - "blid": "BLID", - "continuous": "Continuous", - "delay": "Delay", - "host": "Host", - "password": "Password" - }, - "description": "Currently retrieving the BLID and password is a manual process. Please follow the steps outlined in the documentation at: https://www.home-assistant.io/integrations/roomba/#retrieving-your-credentials", - "title": "Connect to the device" } } }, diff --git a/tests/components/roomba/test_config_flow.py b/tests/components/roomba/test_config_flow.py index 2d9a5afd4f6..ee3b7d4b497 100644 --- a/tests/components/roomba/test_config_flow.py +++ b/tests/components/roomba/test_config_flow.py @@ -7,13 +7,14 @@ from roombapy.roomba import RoombaInfo from homeassistant import config_entries, data_entry_flow, setup from homeassistant.components.dhcp import HOSTNAME, IP_ADDRESS, MAC_ADDRESS +from homeassistant.components.roomba import config_flow from homeassistant.components.roomba.const import CONF_BLID, CONF_CONTINUOUS, DOMAIN from homeassistant.const import CONF_DELAY, CONF_HOST, CONF_PASSWORD from tests.common import MockConfigEntry MOCK_IP = "1.2.3.4" -VALID_CONFIG = {CONF_HOST: MOCK_IP, CONF_BLID: "blid", CONF_PASSWORD: "password"} +VALID_CONFIG = {CONF_HOST: MOCK_IP, CONF_BLID: "BLID", CONF_PASSWORD: "password"} DHCP_DISCOVERY_DEVICES = [ { @@ -31,18 +32,25 @@ DHCP_DISCOVERY_DEVICES = [ DHCP_DISCOVERY_DEVICES_WITHOUT_MATCHING_IP = [ { - IP_ADDRESS: "1.1.1.1", + IP_ADDRESS: "4.4.4.4", MAC_ADDRESS: "50:14:79:DD:EE:FF", HOSTNAME: "irobot-blid", }, { - IP_ADDRESS: "1.1.1.1", + IP_ADDRESS: "5.5.5.5", MAC_ADDRESS: "80:A5:89:DD:EE:FF", HOSTNAME: "roomba-blid", }, ] +@pytest.fixture(autouse=True) +def roomba_no_wake_time(): + """Fixture that prevents sleep.""" + with patch.object(config_flow, "ROOMBA_WAKE_TIME", 0): + yield + + def _create_mocked_roomba( roomba_connected=None, master_state=None, connect=None, disconnect=None ): @@ -58,7 +66,7 @@ def _mocked_discovery(*_): roomba_discovery = MagicMock() roomba = RoombaInfo( - hostname="irobot-blid", + hostname="irobot-BLID", robot_name="robot_name", ip=MOCK_IP, mac="mac", @@ -68,12 +76,22 @@ def _mocked_discovery(*_): ) roomba_discovery.get_all = MagicMock(return_value=[roomba]) + roomba_discovery.get = MagicMock(return_value=roomba) + + return roomba_discovery + + +def _mocked_no_devices_found_discovery(*_): + roomba_discovery = MagicMock() + roomba_discovery.get_all = MagicMock(return_value=[]) + roomba_discovery.get = MagicMock(return_value=None) return roomba_discovery def _mocked_failed_discovery(*_): roomba_discovery = MagicMock() - roomba_discovery.get_all = MagicMock(return_value=[]) + roomba_discovery.get_all = MagicMock(side_effect=OSError) + roomba_discovery.get = MagicMock(side_effect=OSError) return roomba_discovery @@ -145,9 +163,9 @@ async def test_form_user_discovery_and_password_fetch(hass): assert result3["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result3["title"] == "robot_name" - assert result3["result"].unique_id == "blid" + assert result3["result"].unique_id == "BLID" assert result3["data"] == { - CONF_BLID: "blid", + CONF_BLID: "BLID", CONF_CONTINUOUS: True, CONF_DELAY: 1, CONF_HOST: MOCK_IP, @@ -161,7 +179,7 @@ async def test_form_user_discovery_skips_known(hass): """Test discovery proceeds to manual if all discovered are already known.""" await setup.async_setup_component(hass, "persistent_notification", {}) - entry = MockConfigEntry(domain=DOMAIN, data=VALID_CONFIG, unique_id="blid") + entry = MockConfigEntry(domain=DOMAIN, data=VALID_CONFIG, unique_id="BLID") entry.add_to_hass(hass) with patch( @@ -177,16 +195,16 @@ async def test_form_user_discovery_skips_known(hass): assert result["step_id"] == "manual" -async def test_form_user_failed_discovery_aborts_already_configured(hass): +async def test_form_user_no_devices_found_discovery_aborts_already_configured(hass): """Test if we manually configure an existing host we abort.""" await setup.async_setup_component(hass, "persistent_notification", {}) - entry = MockConfigEntry(domain=DOMAIN, data=VALID_CONFIG, unique_id="blid") + entry = MockConfigEntry(domain=DOMAIN, data=VALID_CONFIG, unique_id="BLID") entry.add_to_hass(hass) with patch( "homeassistant.components.roomba.config_flow.RoombaDiscovery", - _mocked_failed_discovery, + _mocked_no_devices_found_discovery, ): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} @@ -264,9 +282,9 @@ async def test_form_user_discovery_manual_and_auto_password_fetch(hass): assert result4["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result4["title"] == "myroomba" - assert result4["result"].unique_id == "blid" + assert result4["result"].unique_id == "BLID" assert result4["data"] == { - CONF_BLID: "blid", + CONF_BLID: "BLID", CONF_CONTINUOUS: True, CONF_DELAY: 1, CONF_HOST: MOCK_IP, @@ -276,6 +294,35 @@ async def test_form_user_discovery_manual_and_auto_password_fetch(hass): assert len(mock_setup_entry.mock_calls) == 1 +async def test_form_user_discover_fails_aborts_already_configured(hass): + """Test if we manually configure an existing host we abort after failed discovery.""" + await setup.async_setup_component(hass, "persistent_notification", {}) + + entry = MockConfigEntry(domain=DOMAIN, data=VALID_CONFIG, unique_id="BLID") + entry.add_to_hass(hass) + + with patch( + "homeassistant.components.roomba.config_flow.RoombaDiscovery", + _mocked_failed_discovery, + ): + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + await hass.async_block_till_done() + + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["errors"] is None + assert result["step_id"] == "manual" + + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], + {CONF_HOST: MOCK_IP, CONF_BLID: "blid"}, + ) + await hass.async_block_till_done() + assert result2["type"] == data_entry_flow.RESULT_TYPE_ABORT + assert result2["reason"] == "already_configured" + + async def test_form_user_discovery_manual_and_auto_password_fetch_but_cannot_connect( hass, ): @@ -341,8 +388,8 @@ async def test_form_user_discovery_manual_and_auto_password_fetch_but_cannot_con assert len(mock_setup_entry.mock_calls) == 0 -async def test_form_user_discovery_fails_and_auto_password_fetch(hass): - """Test discovery fails and we can auto fetch the password.""" +async def test_form_user_discovery_no_devices_found_and_auto_password_fetch(hass): + """Test discovery finds no devices and we can auto fetch the password.""" await setup.async_setup_component(hass, "persistent_notification", {}) mocked_roomba = _create_mocked_roomba( @@ -352,7 +399,7 @@ async def test_form_user_discovery_fails_and_auto_password_fetch(hass): with patch( "homeassistant.components.roomba.config_flow.RoombaDiscovery", - _mocked_failed_discovery, + _mocked_no_devices_found_discovery, ): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} @@ -391,9 +438,9 @@ async def test_form_user_discovery_fails_and_auto_password_fetch(hass): assert result3["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result3["title"] == "myroomba" - assert result3["result"].unique_id == "blid" + assert result3["result"].unique_id == "BLID" assert result3["data"] == { - CONF_BLID: "blid", + CONF_BLID: "BLID", CONF_CONTINUOUS: True, CONF_DELAY: 1, CONF_HOST: MOCK_IP, @@ -403,8 +450,8 @@ async def test_form_user_discovery_fails_and_auto_password_fetch(hass): assert len(mock_setup_entry.mock_calls) == 1 -async def test_form_user_discovery_fails_and_password_fetch_fails(hass): - """Test discovery fails and password fetch fails.""" +async def test_form_user_discovery_no_devices_found_and_password_fetch_fails(hass): + """Test discovery finds no devices and password fetch fails.""" await setup.async_setup_component(hass, "persistent_notification", {}) mocked_roomba = _create_mocked_roomba( @@ -414,7 +461,7 @@ async def test_form_user_discovery_fails_and_password_fetch_fails(hass): with patch( "homeassistant.components.roomba.config_flow.RoombaDiscovery", - _mocked_failed_discovery, + _mocked_no_devices_found_discovery, ): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} @@ -460,9 +507,9 @@ async def test_form_user_discovery_fails_and_password_fetch_fails(hass): assert result4["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result4["title"] == "myroomba" - assert result4["result"].unique_id == "blid" + assert result4["result"].unique_id == "BLID" assert result4["data"] == { - CONF_BLID: "blid", + CONF_BLID: "BLID", CONF_CONTINUOUS: True, CONF_DELAY: 1, CONF_HOST: MOCK_IP, @@ -472,10 +519,10 @@ async def test_form_user_discovery_fails_and_password_fetch_fails(hass): assert len(mock_setup_entry.mock_calls) == 1 -async def test_form_user_discovery_fails_and_password_fetch_fails_and_cannot_connect( +async def test_form_user_discovery_not_devices_found_and_password_fetch_fails_and_cannot_connect( hass, ): - """Test discovery fails and password fetch fails then we cannot connect.""" + """Test discovery finds no devices and password fetch fails then we cannot connect.""" await setup.async_setup_component(hass, "persistent_notification", {}) mocked_roomba = _create_mocked_roomba( @@ -486,7 +533,7 @@ async def test_form_user_discovery_fails_and_password_fetch_fails_and_cannot_con with patch( "homeassistant.components.roomba.config_flow.RoombaDiscovery", - _mocked_failed_discovery, + _mocked_no_devices_found_discovery, ): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} @@ -593,9 +640,9 @@ async def test_form_user_discovery_and_password_fetch_gets_connection_refused(ha assert result4["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result4["title"] == "myroomba" - assert result4["result"].unique_id == "blid" + assert result4["result"].unique_id == "BLID" assert result4["data"] == { - CONF_BLID: "blid", + CONF_BLID: "BLID", CONF_CONTINUOUS: True, CONF_DELAY: 1, CONF_HOST: MOCK_IP, @@ -650,9 +697,9 @@ async def test_dhcp_discovery_and_roomba_discovery_finds(hass, discovery_data): assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result2["title"] == "robot_name" - assert result2["result"].unique_id == "blid" + assert result2["result"].unique_id == "BLID" assert result2["data"] == { - CONF_BLID: "blid", + CONF_BLID: "BLID", CONF_CONTINUOUS: True, CONF_DELAY: 1, CONF_HOST: MOCK_IP, @@ -697,7 +744,7 @@ async def test_dhcp_discovery_falls_back_to_manual(hass, discovery_data): result3 = await hass.config_entries.flow.async_configure( result2["flow_id"], - {CONF_HOST: "1.1.1.1", CONF_BLID: "blid"}, + {CONF_HOST: MOCK_IP, CONF_BLID: "blid"}, ) await hass.async_block_till_done() assert result3["type"] == data_entry_flow.RESULT_TYPE_FORM @@ -723,12 +770,12 @@ async def test_dhcp_discovery_falls_back_to_manual(hass, discovery_data): assert result4["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result4["title"] == "myroomba" - assert result4["result"].unique_id == "blid" + assert result4["result"].unique_id == "BLID" assert result4["data"] == { - CONF_BLID: "blid", + CONF_BLID: "BLID", CONF_CONTINUOUS: True, CONF_DELAY: 1, - CONF_HOST: "1.1.1.1", + CONF_HOST: MOCK_IP, CONF_PASSWORD: "password", } assert len(mock_setup.mock_calls) == 1 @@ -749,7 +796,7 @@ async def test_dhcp_discovery_with_ignored(hass): DOMAIN, context={"source": config_entries.SOURCE_DHCP}, data={ - IP_ADDRESS: "1.1.1.1", + IP_ADDRESS: MOCK_IP, MAC_ADDRESS: "AA:BB:CC:DD:EE:FF", HOSTNAME: "irobot-blid", }, @@ -763,7 +810,7 @@ async def test_dhcp_discovery_already_configured_host(hass): """Test we abort if the host is already configured.""" await setup.async_setup_component(hass, "persistent_notification", {}) - config_entry = MockConfigEntry(domain=DOMAIN, data={CONF_HOST: "1.1.1.1"}) + config_entry = MockConfigEntry(domain=DOMAIN, data={CONF_HOST: MOCK_IP}) config_entry.add_to_hass(hass) with patch( @@ -773,7 +820,7 @@ async def test_dhcp_discovery_already_configured_host(hass): DOMAIN, context={"source": config_entries.SOURCE_DHCP}, data={ - IP_ADDRESS: "1.1.1.1", + IP_ADDRESS: MOCK_IP, MAC_ADDRESS: "AA:BB:CC:DD:EE:FF", HOSTNAME: "irobot-blid", }, @@ -789,7 +836,7 @@ async def test_dhcp_discovery_already_configured_blid(hass): await setup.async_setup_component(hass, "persistent_notification", {}) config_entry = MockConfigEntry( - domain=DOMAIN, data={CONF_BLID: "blid"}, unique_id="blid" + domain=DOMAIN, data={CONF_BLID: "BLID"}, unique_id="BLID" ) config_entry.add_to_hass(hass) @@ -800,7 +847,7 @@ async def test_dhcp_discovery_already_configured_blid(hass): DOMAIN, context={"source": config_entries.SOURCE_DHCP}, data={ - IP_ADDRESS: "1.1.1.1", + IP_ADDRESS: MOCK_IP, MAC_ADDRESS: "AA:BB:CC:DD:EE:FF", HOSTNAME: "irobot-blid", }, @@ -816,7 +863,7 @@ async def test_dhcp_discovery_not_irobot(hass): await setup.async_setup_component(hass, "persistent_notification", {}) config_entry = MockConfigEntry( - domain=DOMAIN, data={CONF_BLID: "blid"}, unique_id="blid" + domain=DOMAIN, data={CONF_BLID: "BLID"}, unique_id="BLID" ) config_entry.add_to_hass(hass) @@ -827,7 +874,7 @@ async def test_dhcp_discovery_not_irobot(hass): DOMAIN, context={"source": config_entries.SOURCE_DHCP}, data={ - IP_ADDRESS: "1.1.1.1", + IP_ADDRESS: MOCK_IP, MAC_ADDRESS: "AA:BB:CC:DD:EE:FF", HOSTNAME: "Notirobot-blid", }, @@ -836,3 +883,67 @@ async def test_dhcp_discovery_not_irobot(hass): assert result["type"] == "abort" assert result["reason"] == "not_irobot_device" + + +async def test_dhcp_discovery_partial_hostname(hass): + """Test we abort flows when we have a partial hostname.""" + await setup.async_setup_component(hass, "persistent_notification", {}) + + with patch( + "homeassistant.components.roomba.config_flow.RoombaDiscovery", _mocked_discovery + ): + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": config_entries.SOURCE_DHCP}, + data={ + IP_ADDRESS: MOCK_IP, + MAC_ADDRESS: "AA:BB:CC:DD:EE:FF", + HOSTNAME: "irobot-blid", + }, + ) + await hass.async_block_till_done() + + assert result["type"] == "form" + assert result["step_id"] == "link" + + with patch( + "homeassistant.components.roomba.config_flow.RoombaDiscovery", _mocked_discovery + ): + result2 = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": config_entries.SOURCE_DHCP}, + data={ + IP_ADDRESS: MOCK_IP, + MAC_ADDRESS: "AA:BB:CC:DD:EE:FF", + HOSTNAME: "irobot-blidthatislonger", + }, + ) + await hass.async_block_till_done() + + assert result2["type"] == "form" + assert result2["step_id"] == "link" + + current_flows = hass.config_entries.flow.async_progress() + assert len(current_flows) == 1 + assert current_flows[0]["flow_id"] == result2["flow_id"] + + with patch( + "homeassistant.components.roomba.config_flow.RoombaDiscovery", _mocked_discovery + ): + result3 = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": config_entries.SOURCE_DHCP}, + data={ + IP_ADDRESS: MOCK_IP, + MAC_ADDRESS: "AA:BB:CC:DD:EE:FF", + HOSTNAME: "irobot-bl", + }, + ) + await hass.async_block_till_done() + + assert result3["type"] == "abort" + assert result3["reason"] == "short_blid" + + current_flows = hass.config_entries.flow.async_progress() + assert len(current_flows) == 1 + assert current_flows[0]["flow_id"] == result2["flow_id"]