Skip to content

Commit

Permalink
Update Reolink config entry port info if needed (home-assistant#128589)
Browse files Browse the repository at this point in the history
  • Loading branch information
starkillerOG authored Oct 18, 2024
1 parent 5580c3f commit 2d90ffc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
22 changes: 20 additions & 2 deletions homeassistant/components/reolink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
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 (
Expand All @@ -22,7 +22,7 @@
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
Expand Down Expand Up @@ -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)):
Expand Down
18 changes: 17 additions & 1 deletion tests/components/reolink/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
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,
Expand All @@ -31,6 +31,7 @@
TEST_HOST_MODEL,
TEST_MAC,
TEST_NVR_NAME,
TEST_PORT,
TEST_UID,
TEST_UID_CAM,
)
Expand Down Expand Up @@ -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

0 comments on commit 2d90ffc

Please sign in to comment.