Skip to content

Commit

Permalink
Fix Reolink DHCP IP update (#103654)
Browse files Browse the repository at this point in the history
  • Loading branch information
starkillerOG authored and frenck committed Nov 10, 2023
1 parent 0ffc1ba commit f946ed9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
4 changes: 3 additions & 1 deletion homeassistant/components/reolink/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowRes
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()
Expand Down
16 changes: 13 additions & 3 deletions tests/components/reolink/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
27 changes: 25 additions & 2 deletions tests/components/reolink/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -12,6 +12,7 @@
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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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"

Expand Down

0 comments on commit f946ed9

Please sign in to comment.