Support NVR Hikvision devices (#159253)

This commit is contained in:
Paul Tarjan
2025-12-19 23:08:48 -10:00
committed by GitHub
parent 9f886e66c7
commit 3050a5c896
3 changed files with 34 additions and 0 deletions

View File

@@ -70,6 +70,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: HikvisionConfigEntry) ->
device_type=device_type,
)
# For NVRs or devices with no detected events, try to fetch events from ISAPI
if device_type == "NVR" or not camera.current_event_states:
def fetch_and_inject_nvr_events() -> None:
"""Fetch and inject NVR events in a single executor job."""
if nvr_events := camera.get_event_triggers(None):
camera.inject_events(nvr_events)
await hass.async_add_executor_job(fetch_and_inject_nvr_events)
# Start the event stream
await hass.async_add_executor_job(camera.start_stream)

View File

@@ -82,9 +82,20 @@ def mock_hikcamera() -> Generator[MagicMock]:
None,
"2024-01-01T00:00:00Z",
)
camera.get_event_triggers.return_value = {}
yield hikcamera_mock
@pytest.fixture
def mock_hik_nvr(mock_hikcamera: MagicMock) -> MagicMock:
"""Return a mocked HikCamera configured as an NVR."""
camera = mock_hikcamera.return_value
camera.get_type = "NVR"
camera.current_event_states = {}
camera.get_event_triggers.return_value = {"Motion": [1, 2]}
return mock_hikcamera
@pytest.fixture
async def init_integration(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_hikcamera: MagicMock

View File

@@ -89,3 +89,16 @@ async def test_setup_entry_no_device_id(
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
async def test_setup_entry_nvr_fetches_events(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_hik_nvr: MagicMock,
) -> None:
"""Test setup fetches NVR events for NVR devices."""
await setup_integration(hass, mock_config_entry)
assert mock_config_entry.state is ConfigEntryState.LOADED
mock_hik_nvr.return_value.get_event_triggers.assert_called_once()
mock_hik_nvr.return_value.inject_events.assert_called_once()