From 85ae66d276f9b19c60c607d7b1804a444891ff62 Mon Sep 17 00:00:00 2001 From: starkillerOG Date: Thu, 3 Oct 2024 16:51:27 +0200 Subject: [PATCH] Reolink auto add new cameras/chimes (#126268) --- homeassistant/components/reolink/__init__.py | 7 +++++++ tests/components/reolink/conftest.py | 1 + tests/components/reolink/test_init.py | 21 ++++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/homeassistant/components/reolink/__init__.py b/homeassistant/components/reolink/__init__.py index 0ff69c00f8c..4f0b8ae2664 100644 --- a/homeassistant/components/reolink/__init__.py +++ b/homeassistant/components/reolink/__init__.py @@ -9,6 +9,7 @@ import logging 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.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady @@ -102,6 +103,12 @@ async def async_setup_entry( async with asyncio.timeout(host.api.timeout * (RETRY_ATTEMPTS + 2)): await host.renew() + if host.api.new_devices and config_entry.state == ConfigEntryState.LOADED: + # Their are new cameras/chimes connected, reload to add them. + hass.async_create_task( + hass.config_entries.async_reload(config_entry.entry_id) + ) + async def async_check_firmware_update() -> None: """Check for firmware updates.""" async with asyncio.timeout(host.api.timeout * (RETRY_ATTEMPTS + 2)): diff --git a/tests/components/reolink/conftest.py b/tests/components/reolink/conftest.py index 79a63963bca..f9b8504f14f 100644 --- a/tests/components/reolink/conftest.py +++ b/tests/components/reolink/conftest.py @@ -80,6 +80,7 @@ def reolink_connect_class() -> Generator[MagicMock]: host_mock.protocol = "rtsp" host_mock.channels = [0] host_mock.stream_channels = [0] + host_mock.new_devices = False host_mock.sw_version_update_required = False host_mock.hardware_version = "IPC_00000" host_mock.sw_version = "v1.0.0.0.0.0000" diff --git a/tests/components/reolink/test_init.py b/tests/components/reolink/test_init.py index ffb2dfca6bc..0063ef08232 100644 --- a/tests/components/reolink/test_init.py +++ b/tests/components/reolink/test_init.py @@ -600,3 +600,24 @@ async def test_firmware_repair_issue( await hass.async_block_till_done() assert (DOMAIN, "firmware_update_host") in issue_registry.issues + + +async def test_new_device_discovered( + hass: HomeAssistant, + freezer: FrozenDateTimeFactory, + reolink_connect: MagicMock, + config_entry: MockConfigEntry, +) -> None: + """Test the entry is reloaded when a new camera or chime is detected.""" + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SWITCH]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + assert reolink_connect.logout.call_count == 0 + reolink_connect.new_devices = True + + freezer.tick(DEVICE_UPDATE_INTERVAL) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + assert reolink_connect.logout.call_count == 1