Skip to content

Commit

Permalink
Fix manual setup when roomba is on different subnet (home-assistant#5…
Browse files Browse the repository at this point in the history
…4639)

Co-authored-by: J. Nick Koston <[email protected]>
Co-authored-by: Franck Nijhof <[email protected]>
  • Loading branch information
3 people authored Aug 22, 2021
1 parent 45baf88 commit b942454
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 70 deletions.
13 changes: 8 additions & 5 deletions homeassistant/components/roomba/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,20 @@ async def async_step_manual(self, user_input=None):
step_id="manual",
description_placeholders={AUTH_HELP_URL_KEY: AUTH_HELP_URL_VALUE},
data_schema=vol.Schema(
{
vol.Required(CONF_HOST, default=self.host): str,
vol.Required(CONF_BLID, default=self.blid): str,
}
{vol.Required(CONF_HOST, default=self.host): str}
),
)

self._async_abort_entries_match({CONF_HOST: user_input["host"]})

self.host = user_input[CONF_HOST]
self.blid = user_input[CONF_BLID].upper()

devices = await _async_discover_roombas(self.hass, self.host)
if not devices:
return self.async_abort(reason="cannot_connect")
self.blid = devices[0].blid
self.name = devices[0].robot_name

await self.async_set_unique_id(self.blid, raise_on_progress=False)
self._abort_if_unique_id_configured()
return await self.async_step_link()
Expand Down
5 changes: 2 additions & 3 deletions homeassistant/components/roomba/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
},
"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-` or `Roomba-`. Please follow the steps outlined in the documentation at: {auth_help_url}",
"description": "No Roomba or Braava have been discovered on your network.",
"data": {
"host": "[%key:common::config_flow::data::host%]",
"blid": "BLID"
"host": "[%key:common::config_flow::data::host%]"
}
},
"link": {
Expand Down
3 changes: 1 addition & 2 deletions homeassistant/components/roomba/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@
},
"manual": {
"data": {
"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-` or `Roomba-`. Please follow the steps outlined in the documentation at: {auth_help_url}",
"description": "No Roomba or Braava have been discovered on your network.",
"title": "Manually connect to the device"
},
"user": {
Expand Down
118 changes: 58 additions & 60 deletions tests/components/roomba/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ async def test_form_user_no_devices_found_discovery_aborts_already_configured(ha

result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: MOCK_IP, CONF_BLID: "blid"},
{CONF_HOST: MOCK_IP},
)
await hass.async_block_till_done()
assert result2["type"] == data_entry_flow.RESULT_TYPE_ABORT
Expand Down Expand Up @@ -250,10 +250,14 @@ async def test_form_user_discovery_manual_and_auto_password_fetch(hass):
assert result2["errors"] is None
assert result2["step_id"] == "manual"

result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{CONF_HOST: MOCK_IP, CONF_BLID: "blid"},
)
with patch(
"homeassistant.components.roomba.config_flow.RoombaDiscovery", _mocked_discovery
):
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{CONF_HOST: MOCK_IP},
)

await hass.async_block_till_done()
assert result3["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result3["errors"] is None
Expand All @@ -275,7 +279,7 @@ async def test_form_user_discovery_manual_and_auto_password_fetch(hass):
await hass.async_block_till_done()

assert result4["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result4["title"] == "myroomba"
assert result4["title"] == "robot_name"
assert result4["result"].unique_id == "BLID"
assert result4["data"] == {
CONF_BLID: "BLID",
Expand Down Expand Up @@ -309,7 +313,7 @@ async def test_form_user_discover_fails_aborts_already_configured(hass):

result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: MOCK_IP, CONF_BLID: "blid"},
{CONF_HOST: MOCK_IP},
)
await hass.async_block_till_done()
assert result2["type"] == data_entry_flow.RESULT_TYPE_ABORT
Expand All @@ -322,12 +326,6 @@ async def test_form_user_discovery_manual_and_auto_password_fetch_but_cannot_con
"""Test discovery skipped and we can auto fetch the password then we fail to connect."""
await setup.async_setup_component(hass, "persistent_notification", {})

mocked_roomba = _create_mocked_roomba(
connect=RoombaConnectionError,
roomba_connected=True,
master_state={"state": {"reported": {"name": "myroomba"}}},
)

with patch(
"homeassistant.components.roomba.config_flow.RoombaDiscovery", _mocked_discovery
):
Expand All @@ -349,33 +347,18 @@ async def test_form_user_discovery_manual_and_auto_password_fetch_but_cannot_con
assert result2["errors"] is None
assert result2["step_id"] == "manual"

result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{CONF_HOST: MOCK_IP, CONF_BLID: "blid"},
)
await hass.async_block_till_done()
assert result3["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result3["errors"] is None

with patch(
"homeassistant.components.roomba.config_flow.RoombaFactory.create_roomba",
return_value=mocked_roomba,
), patch(
"homeassistant.components.roomba.config_flow.RoombaPassword",
_mocked_getpassword,
), patch(
"homeassistant.components.roomba.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result4 = await hass.config_entries.flow.async_configure(
result3["flow_id"],
{},
"homeassistant.components.roomba.config_flow.RoombaDiscovery",
_mocked_no_devices_found_discovery,
):
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{CONF_HOST: MOCK_IP},
)
await hass.async_block_till_done()
await hass.async_block_till_done()

assert result4["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result4["reason"] == "cannot_connect"
assert len(mock_setup_entry.mock_calls) == 0
assert result3["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result3["reason"] == "cannot_connect"


async def test_form_user_discovery_no_devices_found_and_auto_password_fetch(hass):
Expand All @@ -400,10 +383,13 @@ async def test_form_user_discovery_no_devices_found_and_auto_password_fetch(hass
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"},
)
with patch(
"homeassistant.components.roomba.config_flow.RoombaDiscovery", _mocked_discovery
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: MOCK_IP},
)
await hass.async_block_till_done()
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result2["errors"] is None
Expand All @@ -425,7 +411,7 @@ async def test_form_user_discovery_no_devices_found_and_auto_password_fetch(hass
await hass.async_block_till_done()

assert result3["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result3["title"] == "myroomba"
assert result3["title"] == "robot_name"
assert result3["result"].unique_id == "BLID"
assert result3["data"] == {
CONF_BLID: "BLID",
Expand Down Expand Up @@ -459,10 +445,13 @@ async def test_form_user_discovery_no_devices_found_and_password_fetch_fails(has
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"},
)
with patch(
"homeassistant.components.roomba.config_flow.RoombaDiscovery", _mocked_discovery
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: MOCK_IP},
)
await hass.async_block_till_done()
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result2["errors"] is None
Expand Down Expand Up @@ -528,10 +517,13 @@ async def test_form_user_discovery_not_devices_found_and_password_fetch_fails_an
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"},
)
with patch(
"homeassistant.components.roomba.config_flow.RoombaDiscovery", _mocked_discovery
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: MOCK_IP},
)
await hass.async_block_till_done()
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result2["errors"] is None
Expand Down Expand Up @@ -717,10 +709,13 @@ async def test_dhcp_discovery_falls_back_to_manual(hass, discovery_data):
assert result2["errors"] is None
assert result2["step_id"] == "manual"

result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{CONF_HOST: MOCK_IP, CONF_BLID: "blid"},
)
with patch(
"homeassistant.components.roomba.config_flow.RoombaDiscovery", _mocked_discovery
):
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{CONF_HOST: MOCK_IP},
)
await hass.async_block_till_done()
assert result3["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result3["errors"] is None
Expand All @@ -742,7 +737,7 @@ async def test_dhcp_discovery_falls_back_to_manual(hass, discovery_data):
await hass.async_block_till_done()

assert result4["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result4["title"] == "myroomba"
assert result4["title"] == "robot_name"
assert result4["result"].unique_id == "BLID"
assert result4["data"] == {
CONF_BLID: "BLID",
Expand Down Expand Up @@ -779,10 +774,13 @@ async def test_dhcp_discovery_no_devices_falls_back_to_manual(hass, discovery_da
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"},
)
with patch(
"homeassistant.components.roomba.config_flow.RoombaDiscovery", _mocked_discovery
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: MOCK_IP},
)
await hass.async_block_till_done()
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result2["errors"] is None
Expand All @@ -804,7 +802,7 @@ async def test_dhcp_discovery_no_devices_falls_back_to_manual(hass, discovery_da
await hass.async_block_till_done()

assert result3["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result3["title"] == "myroomba"
assert result3["title"] == "robot_name"
assert result3["result"].unique_id == "BLID"
assert result3["data"] == {
CONF_BLID: "BLID",
Expand Down

0 comments on commit b942454

Please sign in to comment.