Fix Reolink DHCP IP update (#103654)

This commit is contained in:
starkillerOG 2023-11-10 09:27:33 +01:00 committed by GitHub
parent 2f26096469
commit 9f6eef7cca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 6 deletions

View file

@ -113,7 +113,9 @@ class ReolinkFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
raise AbortFlow("already_configured")
# check if the camera is reachable at the new IP
host = ReolinkHost(self.hass, existing_entry.data, existing_entry.options)
new_config = dict(existing_entry.data)
new_config[CONF_HOST] = discovery_info.ip
host = ReolinkHost(self.hass, new_config, existing_entry.options)
try:
await host.api.get_state("GetLocalLink")
await host.api.logout()

View file

@ -34,8 +34,10 @@ def mock_setup_entry() -> Generator[AsyncMock, None, None]:
@pytest.fixture
def reolink_connect(mock_get_source_ip: None) -> Generator[MagicMock, None, None]:
"""Mock reolink connection."""
def reolink_connect_class(
mock_get_source_ip: None,
) -> Generator[MagicMock, None, None]:
"""Mock reolink connection and return both the host_mock and host_mock_class."""
with patch(
"homeassistant.components.reolink.host.webhook.async_register",
return_value=True,
@ -65,7 +67,15 @@ def reolink_connect(mock_get_source_ip: None) -> Generator[MagicMock, None, None
host_mock.session_active = True
host_mock.timeout = 60
host_mock.renewtimer.return_value = 600
yield host_mock
yield host_mock_class
@pytest.fixture
def reolink_connect(
reolink_connect_class: MagicMock,
) -> Generator[MagicMock, None, None]:
"""Mock reolink connection."""
return reolink_connect_class.return_value
@pytest.fixture

View file

@ -2,7 +2,7 @@
from datetime import timedelta
import json
from typing import Any
from unittest.mock import AsyncMock, MagicMock
from unittest.mock import AsyncMock, MagicMock, call
import pytest
from reolink_aio.exceptions import ApiError, CredentialsInvalidError, ReolinkError
@ -12,6 +12,7 @@ from homeassistant.components import dhcp
from homeassistant.components.reolink import DEVICE_UPDATE_INTERVAL, const
from homeassistant.components.reolink.config_flow import DEFAULT_PROTOCOL
from homeassistant.components.reolink.exceptions import ReolinkWebhookException
from homeassistant.components.reolink.host import DEFAULT_TIMEOUT
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME
from homeassistant.core import HomeAssistant
@ -380,41 +381,47 @@ async def test_dhcp_flow(hass: HomeAssistant, mock_setup_entry: MagicMock) -> No
@pytest.mark.parametrize(
("last_update_success", "attr", "value", "expected"),
("last_update_success", "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(
hass: HomeAssistant,
reolink_connect_class: MagicMock,
reolink_connect: MagicMock,
last_update_success: bool,
attr: str,
value: Any,
expected: str,
host_call_list: list[str],
) -> None:
"""Test dhcp discovery aborts if already configured where the IP is updated if appropriate."""
config_entry = MockConfigEntry(
@ -459,6 +466,22 @@ async def test_dhcp_ip_update(
const.DOMAIN, context={"source": config_entries.SOURCE_DHCP}, data=dhcp_data
)
expected_calls = []
for host in host_call_list:
expected_calls.append(
call(
host,
TEST_USERNAME,
TEST_PASSWORD,
port=TEST_PORT,
use_https=TEST_USE_HTTPS,
protocol=DEFAULT_PROTOCOL,
timeout=DEFAULT_TIMEOUT,
)
)
assert reolink_connect_class.call_args_list == expected_calls
assert result["type"] is data_entry_flow.FlowResultType.ABORT
assert result["reason"] == "already_configured"