From 2d90ffcbf0c5c04db426ecbce6df083ca53b127d Mon Sep 17 00:00:00 2001 From: starkillerOG Date: Fri, 18 Oct 2024 10:00:28 +0200 Subject: [PATCH] Update Reolink config entry port info if needed (#128589) --- homeassistant/components/reolink/__init__.py | 22 ++++++++++++++++++-- tests/components/reolink/test_init.py | 18 +++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/reolink/__init__.py b/homeassistant/components/reolink/__init__.py index 4f0b8ae2664..867cbe6c953 100644 --- a/homeassistant/components/reolink/__init__.py +++ b/homeassistant/components/reolink/__init__.py @@ -10,7 +10,7 @@ from reolink_aio.api import RETRY_ATTEMPTS from reolink_aio.exceptions import CredentialsInvalidError, ReolinkError from homeassistant.config_entries import ConfigEntryState -from homeassistant.const import EVENT_HOMEASSISTANT_STOP, Platform +from homeassistant.const import CONF_PORT, EVENT_HOMEASSISTANT_STOP, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.helpers import ( @@ -22,7 +22,7 @@ from homeassistant.helpers.device_registry import format_mac from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed -from .const import DOMAIN +from .const import CONF_USE_HTTPS, DOMAIN from .exceptions import PasswordIncompatible, ReolinkException, UserNotAdmin from .host import ReolinkHost from .services import async_setup_services @@ -83,6 +83,24 @@ async def async_setup_entry( hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, host.stop) ) + # update the port info if needed for the next time + if ( + host.api.port != config_entry.data[CONF_PORT] + or host.api.use_https != config_entry.data[CONF_USE_HTTPS] + ): + _LOGGER.warning( + "HTTP(s) port of Reolink %s, changed from %s to %s", + host.api.nvr_name, + config_entry.data[CONF_PORT], + host.api.port, + ) + data = { + **config_entry.data, + CONF_PORT: host.api.port, + CONF_USE_HTTPS: host.api.use_https, + } + hass.config_entries.async_update_entry(config_entry, data=data) + async def async_device_config_update() -> None: """Update the host state cache and renew the ONVIF-subscription.""" async with asyncio.timeout(host.api.timeout * (RETRY_ATTEMPTS + 2)): diff --git a/tests/components/reolink/test_init.py b/tests/components/reolink/test_init.py index 82cdbfa9139..e1e67ee2129 100644 --- a/tests/components/reolink/test_init.py +++ b/tests/components/reolink/test_init.py @@ -17,7 +17,7 @@ from homeassistant.components.reolink import ( from homeassistant.components.reolink.const import DOMAIN from homeassistant.config import async_process_ha_core_config from homeassistant.config_entries import ConfigEntryState -from homeassistant.const import STATE_OFF, STATE_UNAVAILABLE, Platform +from homeassistant.const import CONF_PORT, STATE_OFF, STATE_UNAVAILABLE, Platform from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant from homeassistant.helpers import ( device_registry as dr, @@ -31,6 +31,7 @@ from .conftest import ( TEST_HOST_MODEL, TEST_MAC, TEST_NVR_NAME, + TEST_PORT, TEST_UID, TEST_UID_CAM, ) @@ -623,3 +624,18 @@ async def test_new_device_discovered( await hass.async_block_till_done() assert reolink_connect.logout.call_count == 1 + + +async def test_port_changed( + hass: HomeAssistant, + reolink_connect: MagicMock, + config_entry: MockConfigEntry, +) -> None: + """Test config_entry port update when it has changed during initial login.""" + assert config_entry.data[CONF_PORT] == TEST_PORT + reolink_connect.port = 4567 + + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + assert config_entry.data[CONF_PORT] == 4567