Skip to content

Commit

Permalink
Netgear catch no info error (#100212)
Browse files Browse the repository at this point in the history
  • Loading branch information
starkillerOG authored Sep 13, 2023
1 parent d44db6e commit 80aa192
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 41 deletions.
6 changes: 4 additions & 2 deletions homeassistant/components/netgear/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,6 @@ async def async_step_user(self, user_input=None):
)
except CannotLoginException:
errors["base"] = "config"

if errors:
return await self._show_setup_form(user_input, errors)

config_data = {
Expand All @@ -204,6 +202,10 @@ async def async_step_user(self, user_input=None):

# Check if already configured
info = await self.hass.async_add_executor_job(api.get_info)
if info is None:
errors["base"] = "info"
return await self._show_setup_form(user_input, errors)

await self.async_set_unique_id(info["SerialNumber"], raise_on_progress=False)
self._abort_if_unique_id_configured(updates=config_data)

Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/netgear/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
}
},
"error": {
"config": "Connection or login error: please check your configuration"
"config": "Connection or login error: please check your configuration",
"info": "Failed to get info from router"
},
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
Expand Down
64 changes: 26 additions & 38 deletions tests/components/netgear/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,41 +76,6 @@ def mock_controller_service():
yield service_mock


@pytest.fixture(name="service_5555")
def mock_controller_service_5555():
"""Mock a successful service."""
with patch(
"homeassistant.components.netgear.async_setup_entry", return_value=True
), patch("homeassistant.components.netgear.router.Netgear") as service_mock:
service_mock.return_value.get_info = Mock(return_value=ROUTER_INFOS)
service_mock.return_value.port = 5555
service_mock.return_value.ssl = True
yield service_mock


@pytest.fixture(name="service_incomplete")
def mock_controller_service_incomplete():
"""Mock a successful service."""
router_infos = ROUTER_INFOS.copy()
router_infos.pop("DeviceName")
with patch(
"homeassistant.components.netgear.async_setup_entry", return_value=True
), patch("homeassistant.components.netgear.router.Netgear") as service_mock:
service_mock.return_value.get_info = Mock(return_value=router_infos)
service_mock.return_value.port = 80
service_mock.return_value.ssl = False
yield service_mock


@pytest.fixture(name="service_failed")
def mock_controller_service_failed():
"""Mock a failed service."""
with patch("homeassistant.components.netgear.router.Netgear") as service_mock:
service_mock.return_value.login_try_port = Mock(return_value=None)
service_mock.return_value.get_info = Mock(return_value=None)
yield service_mock


async def test_user(hass: HomeAssistant, service) -> None:
"""Test user step."""
result = await hass.config_entries.flow.async_init(
Expand Down Expand Up @@ -138,15 +103,31 @@ async def test_user(hass: HomeAssistant, service) -> None:
assert result["data"][CONF_PASSWORD] == PASSWORD


async def test_user_connect_error(hass: HomeAssistant, service_failed) -> None:
async def test_user_connect_error(hass: HomeAssistant, service) -> None:
"""Test user step with connection failure."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "user"

service.return_value.get_info = Mock(return_value=None)

# Have to provide all config
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_HOST: HOST,
CONF_USERNAME: USERNAME,
CONF_PASSWORD: PASSWORD,
},
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == {"base": "info"}

service.return_value.login_try_port = Mock(return_value=None)

result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
Expand All @@ -160,14 +141,18 @@ async def test_user_connect_error(hass: HomeAssistant, service_failed) -> None:
assert result["errors"] == {"base": "config"}


async def test_user_incomplete_info(hass: HomeAssistant, service_incomplete) -> None:
async def test_user_incomplete_info(hass: HomeAssistant, service) -> None:
"""Test user step with incomplete device info."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "user"

router_infos = ROUTER_INFOS.copy()
router_infos.pop("DeviceName")
service.return_value.get_info = Mock(return_value=router_infos)

# Have to provide all config
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
Expand Down Expand Up @@ -313,7 +298,7 @@ async def test_ssdp(hass: HomeAssistant, service) -> None:
assert result["data"][CONF_PASSWORD] == PASSWORD


async def test_ssdp_port_5555(hass: HomeAssistant, service_5555) -> None:
async def test_ssdp_port_5555(hass: HomeAssistant, service) -> None:
"""Test ssdp step with port 5555."""
result = await hass.config_entries.flow.async_init(
DOMAIN,
Expand All @@ -332,6 +317,9 @@ async def test_ssdp_port_5555(hass: HomeAssistant, service_5555) -> None:
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "user"

service.return_value.port = 5555
service.return_value.ssl = True

result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_PASSWORD: PASSWORD}
)
Expand Down

0 comments on commit 80aa192

Please sign in to comment.