Bump nam backend library (#72771)

* Update config flow

* Fix discovery with auth

* Call check_credentials() on init

* Update tests

* Bump library version

* Cleaning

* Return dataclass instead of tuple

* Fix pylint error
This commit is contained in:
Maciej Bieniek 2022-06-07 18:56:11 +02:00 committed by GitHub
parent a5dc7c5f28
commit 0b5c0f8249
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 99 additions and 75 deletions

View file

@ -23,6 +23,8 @@ DISCOVERY_INFO = zeroconf.ZeroconfServiceInfo(
)
VALID_CONFIG = {"host": "10.10.2.3"}
VALID_AUTH = {"username": "fake_username", "password": "fake_password"}
DEVICE_CONFIG = {"www_basicauth_enabled": False}
DEVICE_CONFIG_AUTH = {"www_basicauth_enabled": True}
async def test_form_create_entry_without_auth(hass):
@ -34,7 +36,10 @@ async def test_form_create_entry_without_auth(hass):
assert result["step_id"] == SOURCE_USER
assert result["errors"] == {}
with patch("homeassistant.components.nam.NettigoAirMonitor.initialize"), patch(
with patch(
"homeassistant.components.nam.NettigoAirMonitor.async_check_credentials",
return_value=DEVICE_CONFIG,
), patch(
"homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address",
return_value="aa:bb:cc:dd:ee:ff",
), patch(
@ -62,24 +67,22 @@ async def test_form_create_entry_with_auth(hass):
assert result["errors"] == {}
with patch(
"homeassistant.components.nam.NettigoAirMonitor.initialize",
side_effect=AuthFailed("Auth Error"),
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
VALID_CONFIG,
)
await hass.async_block_till_done()
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "credentials"
with patch("homeassistant.components.nam.NettigoAirMonitor.initialize"), patch(
"homeassistant.components.nam.NettigoAirMonitor.async_check_credentials",
return_value=DEVICE_CONFIG_AUTH,
), patch(
"homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address",
return_value="aa:bb:cc:dd:ee:ff",
), patch(
"homeassistant.components.nam.async_setup_entry", return_value=True
) as mock_setup_entry:
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
VALID_CONFIG,
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "credentials"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
VALID_AUTH,
@ -104,7 +107,10 @@ async def test_reauth_successful(hass):
)
entry.add_to_hass(hass)
with patch("homeassistant.components.nam.NettigoAirMonitor.initialize"), patch(
with patch(
"homeassistant.components.nam.NettigoAirMonitor.async_check_credentials",
return_value=DEVICE_CONFIG_AUTH,
), patch(
"homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address",
return_value="aa:bb:cc:dd:ee:ff",
):
@ -137,7 +143,7 @@ async def test_reauth_unsuccessful(hass):
entry.add_to_hass(hass)
with patch(
"homeassistant.components.nam.NettigoAirMonitor.initialize",
"homeassistant.components.nam.NettigoAirMonitor.async_check_credentials",
side_effect=ApiError("API Error"),
):
result = await hass.config_entries.flow.async_init(
@ -171,8 +177,11 @@ async def test_form_with_auth_errors(hass, error):
"""Test we handle errors when auth is required."""
exc, base_error = error
with patch(
"homeassistant.components.nam.NettigoAirMonitor.initialize",
"homeassistant.components.nam.NettigoAirMonitor.async_check_credentials",
side_effect=AuthFailed("Auth Error"),
), patch(
"homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address",
return_value="aa:bb:cc:dd:ee:ff",
):
result = await hass.config_entries.flow.async_init(
DOMAIN,
@ -221,26 +230,13 @@ async def test_form_errors(hass, error):
async def test_form_abort(hass):
"""Test we handle abort after error."""
with patch("homeassistant.components.nam.NettigoAirMonitor.initialize"), patch(
"homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address",
side_effect=CannotGetMac("Cannot get MAC address from device"),
):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_USER},
data=VALID_CONFIG,
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "device_unsupported"
async def test_form_with_auth_abort(hass):
"""Test we handle abort after error."""
with patch(
"homeassistant.components.nam.NettigoAirMonitor.initialize",
side_effect=AuthFailed("Auth Error"),
"homeassistant.components.nam.NettigoAirMonitor.async_check_credentials",
return_value=DEVICE_CONFIG,
), patch(
"homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address",
side_effect=CannotGetMac("Cannot get MAC address from device"),
):
result = await hass.config_entries.flow.async_init(
DOMAIN,
@ -248,18 +244,6 @@ async def test_form_with_auth_abort(hass):
data=VALID_CONFIG,
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "credentials"
with patch("homeassistant.components.nam.NettigoAirMonitor.initialize"), patch(
"homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address",
side_effect=CannotGetMac("Cannot get MAC address from device"),
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
VALID_AUTH,
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "device_unsupported"
@ -275,7 +259,10 @@ async def test_form_already_configured(hass):
DOMAIN, context={"source": SOURCE_USER}
)
with patch("homeassistant.components.nam.NettigoAirMonitor.initialize"), patch(
with patch(
"homeassistant.components.nam.NettigoAirMonitor.async_check_credentials",
return_value=DEVICE_CONFIG,
), patch(
"homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address",
return_value="aa:bb:cc:dd:ee:ff",
):
@ -293,7 +280,10 @@ async def test_form_already_configured(hass):
async def test_zeroconf(hass):
"""Test we get the form."""
with patch("homeassistant.components.nam.NettigoAirMonitor.initialize"), patch(
with patch(
"homeassistant.components.nam.NettigoAirMonitor.async_check_credentials",
return_value=DEVICE_CONFIG,
), patch(
"homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address",
return_value="aa:bb:cc:dd:ee:ff",
):
@ -332,8 +322,11 @@ async def test_zeroconf(hass):
async def test_zeroconf_with_auth(hass):
"""Test that the zeroconf step with auth works."""
with patch(
"homeassistant.components.nam.NettigoAirMonitor.initialize",
"homeassistant.components.nam.NettigoAirMonitor.async_check_credentials",
side_effect=AuthFailed("Auth Error"),
), patch(
"homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address",
return_value="aa:bb:cc:dd:ee:ff",
):
result = await hass.config_entries.flow.async_init(
DOMAIN,
@ -351,7 +344,10 @@ async def test_zeroconf_with_auth(hass):
assert result["errors"] == {}
assert context["title_placeholders"]["host"] == "10.10.2.3"
with patch("homeassistant.components.nam.NettigoAirMonitor.initialize"), patch(
with patch(
"homeassistant.components.nam.NettigoAirMonitor.async_check_credentials",
return_value=DEVICE_CONFIG_AUTH,
), patch(
"homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address",
return_value="aa:bb:cc:dd:ee:ff",
), patch(

View file

@ -51,7 +51,7 @@ async def test_config_auth_failed(hass):
)
with patch(
"homeassistant.components.nam.NettigoAirMonitor.initialize",
"homeassistant.components.nam.NettigoAirMonitor.async_check_credentials",
side_effect=AuthFailed("Authorization has failed"),
):
entry.add_to_hass(hass)