Update Reolink config entry port info if needed (#128589)

pull/128590/head^2
starkillerOG 2024-10-18 10:00:28 +02:00 committed by GitHub
parent 5580c3fda0
commit 2d90ffcbf0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 3 deletions

View File

@ -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)):

View File

@ -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