Files
core/tests/components/reolink/test_camera.py
T

99 lines
3.3 KiB
Python

"""Test the Reolink camera platform."""
from unittest.mock import MagicMock, patch
import pytest
from reolink_aio.exceptions import ReolinkError
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.camera import (
CameraState,
async_get_image,
async_get_stream_source,
)
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er
from . import setup_integration
from .conftest import TEST_CAM_NAME, TEST_DUO_MODEL
from tests.common import MockConfigEntry, snapshot_platform
from tests.typing import ClientSessionGenerator
@pytest.mark.usefixtures("entity_registry_enabled_by_default", "reolink_host")
async def test_all_entities(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test all entities."""
with (
patch(
"homeassistant.components.reolink.PLATFORMS",
[Platform.CAMERA],
),
# keep the camera access tokens deterministic for the snapshot
patch("random.SystemRandom.getrandbits", return_value=123123123123),
):
await setup_integration(hass, config_entry)
await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)
async def test_camera(
hass: HomeAssistant,
hass_client_no_auth: ClientSessionGenerator,
config_entry: MockConfigEntry,
reolink_host: MagicMock,
) -> None:
"""Test camera entity with fluent."""
def mock_supported(ch, capability):
if capability == "ext_stream":
return False
return True
reolink_host.supported = mock_supported
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_CAM_NAME}_fluent"
assert hass.states.get(entity_id).state == CameraState.IDLE
# check getting a image from the camera
reolink_host.get_snapshot.return_value = b"image"
assert (await async_get_image(hass, entity_id)).content == b"image"
reolink_host.get_snapshot.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
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_camera_no_stream_source(
hass: HomeAssistant,
config_entry: MockConfigEntry,
reolink_host: MagicMock,
) -> None:
"""Test camera entity with no stream source."""
reolink_host.model = TEST_DUO_MODEL
reolink_host.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
entity_id = f"{Platform.CAMERA}.{TEST_CAM_NAME}_lens_0_snapshots_fluent"
assert hass.states.get(entity_id).state == CameraState.IDLE