Improve Reolink config flow tests (#131693)

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
pull/132151/head
starkillerOG 2024-12-03 13:50:50 +01:00 committed by GitHub
parent f59cf8fa54
commit f6beefced3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 249 additions and 25 deletions

View File

@ -33,7 +33,8 @@
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]"
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]",
"unique_id_mismatch": "The mac address of the device does not match the previous mac address"
}
},
"options": {

View File

@ -85,6 +85,7 @@ async def test_config_flow_manual_success(
assert result["options"] == {
CONF_PROTOCOL: DEFAULT_PROTOCOL,
}
assert result["result"].unique_id == TEST_MAC
async def test_config_flow_errors(
@ -329,6 +330,55 @@ async def test_reauth(hass: HomeAssistant, mock_setup_entry: MagicMock) -> None:
assert config_entry.data[CONF_PASSWORD] == TEST_PASSWORD2
async def test_reauth_abort_unique_id_mismatch(
hass: HomeAssistant, mock_setup_entry: MagicMock, reolink_connect: MagicMock
) -> None:
"""Test a reauth flow."""
config_entry = MockConfigEntry(
domain=DOMAIN,
unique_id=format_mac(TEST_MAC),
data={
CONF_HOST: TEST_HOST,
CONF_USERNAME: TEST_USERNAME,
CONF_PASSWORD: TEST_PASSWORD,
CONF_PORT: TEST_PORT,
CONF_USE_HTTPS: TEST_USE_HTTPS,
},
options={
CONF_PROTOCOL: DEFAULT_PROTOCOL,
},
title=TEST_NVR_NAME,
)
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
reolink_connect.mac_address = "aa:aa:aa:aa:aa:aa"
result = await config_entry.start_reauth_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == {}
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_USERNAME: TEST_USERNAME2,
CONF_PASSWORD: TEST_PASSWORD2,
},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "unique_id_mismatch"
assert config_entry.data[CONF_HOST] == TEST_HOST
assert config_entry.data[CONF_USERNAME] == TEST_USERNAME
assert config_entry.data[CONF_PASSWORD] == TEST_PASSWORD
reolink_connect.mac_address = TEST_MAC
async def test_dhcp_flow(hass: HomeAssistant, mock_setup_entry: MagicMock) -> None:
"""Successful flow from DHCP discovery."""
dhcp_data = dhcp.DhcpServiceInfo(
@ -367,37 +417,97 @@ async def test_dhcp_flow(hass: HomeAssistant, mock_setup_entry: MagicMock) -> No
}
async def test_dhcp_ip_update_aborted_if_wrong_mac(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
reolink_connect_class: MagicMock,
reolink_connect: MagicMock,
) -> None:
"""Test dhcp discovery does not update the IP if the mac address does not match."""
config_entry = MockConfigEntry(
domain=DOMAIN,
unique_id=format_mac(TEST_MAC),
data={
CONF_HOST: TEST_HOST,
CONF_USERNAME: TEST_USERNAME,
CONF_PASSWORD: TEST_PASSWORD,
CONF_PORT: TEST_PORT,
CONF_USE_HTTPS: TEST_USE_HTTPS,
},
options={
CONF_PROTOCOL: DEFAULT_PROTOCOL,
},
title=TEST_NVR_NAME,
)
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert config_entry.state is ConfigEntryState.LOADED
# ensure the last_update_succes is False for the device_coordinator.
reolink_connect.get_states.side_effect = ReolinkError("Test error")
freezer.tick(DEVICE_UPDATE_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
dhcp_data = dhcp.DhcpServiceInfo(
ip=TEST_HOST2,
hostname="Reolink",
macaddress=DHCP_FORMATTED_MAC,
)
reolink_connect.mac_address = "aa:aa:aa:aa:aa:aa"
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_DHCP}, data=dhcp_data
)
for host in (TEST_HOST, TEST_HOST2):
expected_call = call(
host,
TEST_USERNAME,
TEST_PASSWORD,
port=TEST_PORT,
use_https=TEST_USE_HTTPS,
protocol=DEFAULT_PROTOCOL,
timeout=DEFAULT_TIMEOUT,
aiohttp_get_session_callback=ANY,
)
assert expected_call in reolink_connect_class.call_args_list
for exc_call in reolink_connect_class.call_args_list:
assert exc_call[0][0] in [TEST_HOST, TEST_HOST2]
get_session = exc_call[1]["aiohttp_get_session_callback"]
assert isinstance(get_session(), ClientSession)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
await hass.async_block_till_done()
# Check that IP was not updated
assert config_entry.data[CONF_HOST] == TEST_HOST
reolink_connect.get_states.side_effect = None
reolink_connect_class.reset_mock()
reolink_connect.mac_address = TEST_MAC
@pytest.mark.parametrize(
("last_update_success", "attr", "value", "expected", "host_call_list"),
("attr", "value", "expected", "host_call_list"),
[
(
False,
None,
None,
TEST_HOST2,
[TEST_HOST, TEST_HOST2],
),
(
True,
None,
None,
TEST_HOST,
[TEST_HOST],
),
(
False,
"get_state",
AsyncMock(side_effect=ReolinkError("Test error")),
TEST_HOST,
[TEST_HOST, TEST_HOST2],
),
(
False,
"mac_address",
"aa:aa:aa:aa:aa:aa",
TEST_HOST,
[TEST_HOST, TEST_HOST2],
),
],
)
async def test_dhcp_ip_update(
@ -405,7 +515,6 @@ async def test_dhcp_ip_update(
freezer: FrozenDateTimeFactory,
reolink_connect_class: MagicMock,
reolink_connect: MagicMock,
last_update_success: bool,
attr: str,
value: Any,
expected: str,
@ -433,12 +542,11 @@ async def test_dhcp_ip_update(
await hass.async_block_till_done()
assert config_entry.state is ConfigEntryState.LOADED
if not last_update_success:
# ensure the last_update_succes is False for the device_coordinator.
reolink_connect.get_states.side_effect = ReolinkError("Test error")
freezer.tick(DEVICE_UPDATE_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
# ensure the last_update_succes is False for the device_coordinator.
reolink_connect.get_states.side_effect = ReolinkError("Test error")
freezer.tick(DEVICE_UPDATE_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
dhcp_data = dhcp.DhcpServiceInfo(
ip=TEST_HOST2,
@ -484,6 +592,71 @@ async def test_dhcp_ip_update(
setattr(reolink_connect, attr, original)
async def test_dhcp_ip_update_ingnored_if_still_connected(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
reolink_connect_class: MagicMock,
reolink_connect: MagicMock,
) -> None:
"""Test dhcp discovery is ignored when the camera is still properly connected to HA."""
config_entry = MockConfigEntry(
domain=DOMAIN,
unique_id=format_mac(TEST_MAC),
data={
CONF_HOST: TEST_HOST,
CONF_USERNAME: TEST_USERNAME,
CONF_PASSWORD: TEST_PASSWORD,
CONF_PORT: TEST_PORT,
CONF_USE_HTTPS: TEST_USE_HTTPS,
},
options={
CONF_PROTOCOL: DEFAULT_PROTOCOL,
},
title=TEST_NVR_NAME,
)
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert config_entry.state is ConfigEntryState.LOADED
dhcp_data = dhcp.DhcpServiceInfo(
ip=TEST_HOST2,
hostname="Reolink",
macaddress=DHCP_FORMATTED_MAC,
)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_DHCP}, data=dhcp_data
)
expected_call = call(
TEST_HOST,
TEST_USERNAME,
TEST_PASSWORD,
port=TEST_PORT,
use_https=TEST_USE_HTTPS,
protocol=DEFAULT_PROTOCOL,
timeout=DEFAULT_TIMEOUT,
aiohttp_get_session_callback=ANY,
)
assert expected_call in reolink_connect_class.call_args_list
for exc_call in reolink_connect_class.call_args_list:
assert exc_call[0][0] == TEST_HOST
get_session = exc_call[1]["aiohttp_get_session_callback"]
assert isinstance(get_session(), ClientSession)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
await hass.async_block_till_done()
assert config_entry.data[CONF_HOST] == TEST_HOST
reolink_connect.get_states.side_effect = None
reolink_connect_class.reset_mock()
async def test_reconfig(hass: HomeAssistant, mock_setup_entry: MagicMock) -> None:
"""Test a reconfiguration flow."""
config_entry = MockConfigEntry(
@ -526,3 +699,53 @@ async def test_reconfig(hass: HomeAssistant, mock_setup_entry: MagicMock) -> Non
assert config_entry.data[CONF_HOST] == TEST_HOST2
assert config_entry.data[CONF_USERNAME] == TEST_USERNAME
assert config_entry.data[CONF_PASSWORD] == TEST_PASSWORD
async def test_reconfig_abort_unique_id_mismatch(
hass: HomeAssistant, mock_setup_entry: MagicMock, reolink_connect: MagicMock
) -> None:
"""Test a reconfiguration flow aborts if the unique id does not match."""
config_entry = MockConfigEntry(
domain=DOMAIN,
unique_id=format_mac(TEST_MAC),
data={
CONF_HOST: TEST_HOST,
CONF_USERNAME: TEST_USERNAME,
CONF_PASSWORD: TEST_PASSWORD,
CONF_PORT: TEST_PORT,
CONF_USE_HTTPS: TEST_USE_HTTPS,
},
options={
CONF_PROTOCOL: DEFAULT_PROTOCOL,
},
title=TEST_NVR_NAME,
)
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
reolink_connect.mac_address = "aa:aa:aa:aa:aa:aa"
result = await config_entry.start_reconfigure_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == {}
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_HOST: TEST_HOST2,
CONF_USERNAME: TEST_USERNAME,
CONF_PASSWORD: TEST_PASSWORD,
},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "unique_id_mismatch"
assert config_entry.data[CONF_HOST] == TEST_HOST
assert config_entry.data[CONF_USERNAME] == TEST_USERNAME
assert config_entry.data[CONF_PASSWORD] == TEST_PASSWORD
reolink_connect.mac_address = TEST_MAC