Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 100% coverage of Reolink camera platform #124381

Merged
merged 5 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/components/reolink/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
TEST_HOST_MODEL = "RLN8-410"
TEST_ITEM_NUMBER = "P000"
TEST_CAM_MODEL = "RLC-123"
TEST_DUO_MODEL = "Reolink Duo PoE"


@pytest.fixture
Expand Down
4 changes: 2 additions & 2 deletions tests/components/reolink/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er

from .conftest import TEST_NVR_NAME, TEST_UID
from .conftest import TEST_DUO_MODEL, TEST_NVR_NAME, TEST_UID

from tests.common import MockConfigEntry, async_fire_time_changed
from tests.typing import ClientSessionGenerator
Expand All @@ -25,7 +25,7 @@ async def test_motion_sensor(
entity_registry: er.EntityRegistry,
) -> None:
"""Test binary sensor entity with motion sensor."""
reolink_connect.model = "Reolink Duo PoE"
reolink_connect.model = TEST_DUO_MODEL
reolink_connect.motion_detected.return_value = True
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.BINARY_SENSOR]):
assert await hass.config_entries.async_setup(config_entry.entry_id)
Expand Down
76 changes: 76 additions & 0 deletions tests/components/reolink/test_camera.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""Test the Reolink camera platform."""

from unittest.mock import AsyncMock, MagicMock, patch

import pytest
from reolink_aio.exceptions import ReolinkError

from homeassistant.components.camera import async_get_image, async_get_stream_source
from homeassistant.components.reolink import const
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import STATE_IDLE, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er

from .conftest import TEST_DUO_MODEL, TEST_NVR_NAME, TEST_UID, TEST_UID_CAM

from tests.common import MockConfigEntry
from tests.typing import ClientSessionGenerator


async def test_camera(
hass: HomeAssistant,
hass_client_no_auth: ClientSessionGenerator,
config_entry: MockConfigEntry,
reolink_connect: MagicMock,
) -> None:
"""Test camera entity with fluent."""
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.CAMERA]):
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert config_entry.state is ConfigEntryState.LOADED

entity_id = f"{Platform.CAMERA}.{TEST_NVR_NAME}_fluent"
assert hass.states.get(entity_id).state == STATE_IDLE

# check getting a image from the camera
reolink_connect.get_snapshot.return_value = b"image"
assert (await async_get_image(hass, entity_id)).content == b"image"

reolink_connect.get_snapshot = AsyncMock(side_effect=ReolinkError("Test error"))
with pytest.raises(HomeAssistantError):
await async_get_image(hass, entity_id)

# check getting the stream source
assert await async_get_stream_source(hass, entity_id) is not None


async def test_camera_no_stream_source(
hass: HomeAssistant,
config_entry: MockConfigEntry,
reolink_connect: MagicMock,
entity_registry: er.EntityRegistry,
) -> None:
"""Test camera entity with no stream source."""
unique_id = f"{TEST_UID}_{TEST_UID_CAM}_snapshots_sub"
entity_id = f"{Platform.CAMERA}.{TEST_NVR_NAME}_snapshots_fluent"

# enable the snapshots camera entity
entity_registry.async_get_or_create(
domain=Platform.CAMERA,
platform=const.DOMAIN,
unique_id=unique_id,
config_entry=config_entry,
suggested_object_id=f"{TEST_NVR_NAME}_snapshots_fluent",
disabled_by=None,
)
starkillerOG marked this conversation as resolved.
Show resolved Hide resolved

reolink_connect.model = TEST_DUO_MODEL
reolink_connect.get_stream_source.return_value = None
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.CAMERA]):
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert config_entry.state is ConfigEntryState.LOADED

assert hass.states.get(entity_id).state == STATE_IDLE