Use snapshot in Axis camera tests (#122677)

pull/124926/head
Robert Svensson 2024-08-30 14:40:28 +02:00 committed by GitHub
parent a5bacf5652
commit c47b37af4f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 149 additions and 42 deletions

View File

@ -0,0 +1,101 @@
# serializer version: 1
# name: test_camera[config_entry_options0-][camera.home-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'camera',
'entity_category': None,
'entity_id': 'camera.home',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': None,
'platform': 'axis',
'previous_unique_id': None,
'supported_features': <CameraEntityFeature: 2>,
'translation_key': None,
'unique_id': '00:40:8c:12:34:56-camera',
'unit_of_measurement': None,
})
# ---
# name: test_camera[config_entry_options0-][camera.home-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'access_token': '1',
'entity_picture': '/api/camera_proxy/camera.home?token=1',
'friendly_name': 'home',
'frontend_stream_type': <StreamType.HLS: 'hls'>,
'supported_features': <CameraEntityFeature: 2>,
}),
'context': <ANY>,
'entity_id': 'camera.home',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'idle',
})
# ---
# name: test_camera[config_entry_options1-streamprofile=profile_1][camera.home-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'camera',
'entity_category': None,
'entity_id': 'camera.home',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': None,
'platform': 'axis',
'previous_unique_id': None,
'supported_features': <CameraEntityFeature: 2>,
'translation_key': None,
'unique_id': '00:40:8c:12:34:56-camera',
'unit_of_measurement': None,
})
# ---
# name: test_camera[config_entry_options1-streamprofile=profile_1][camera.home-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'access_token': '1',
'entity_picture': '/api/camera_proxy/camera.home?token=1',
'friendly_name': 'home',
'frontend_stream_type': <StreamType.HLS: 'hls'>,
'supported_features': <CameraEntityFeature: 2>,
}),
'context': <ANY>,
'entity_id': 'camera.home',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'idle',
})
# ---

View File

@ -1,58 +1,31 @@
"""Axis camera platform tests."""
from unittest.mock import patch
import pytest
from syrupy import SnapshotAssertion
from homeassistant.components import camera
from homeassistant.components.axis.const import CONF_STREAM_PROFILE
from homeassistant.components.camera import DOMAIN as CAMERA_DOMAIN
from homeassistant.const import STATE_IDLE
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from .conftest import ConfigEntryFactoryType
from .const import MAC, NAME
@pytest.mark.usefixtures("config_entry_setup")
async def test_camera(hass: HomeAssistant) -> None:
"""Test that Axis camera platform is loaded properly."""
assert len(hass.states.async_entity_ids(CAMERA_DOMAIN)) == 1
entity_id = f"{CAMERA_DOMAIN}.{NAME}"
cam = hass.states.get(entity_id)
assert cam.state == STATE_IDLE
assert cam.name == NAME
camera_entity = camera._get_camera_from_entity_id(hass, entity_id)
assert camera_entity.image_source == "http://1.2.3.4:80/axis-cgi/jpg/image.cgi"
assert camera_entity.mjpeg_source == "http://1.2.3.4:80/axis-cgi/mjpg/video.cgi"
assert (
await camera_entity.stream_source()
== "rtsp://root:pass@1.2.3.4/axis-media/media.amp?videocodec=h264"
)
from tests.common import snapshot_platform
@pytest.mark.parametrize("config_entry_options", [{CONF_STREAM_PROFILE: "profile_1"}])
@pytest.mark.usefixtures("config_entry_setup")
async def test_camera_with_stream_profile(hass: HomeAssistant) -> None:
"""Test that Axis camera entity is using the correct path with stream profike."""
assert len(hass.states.async_entity_ids(CAMERA_DOMAIN)) == 1
entity_id = f"{CAMERA_DOMAIN}.{NAME}"
cam = hass.states.get(entity_id)
assert cam.state == STATE_IDLE
assert cam.name == NAME
camera_entity = camera._get_camera_from_entity_id(hass, entity_id)
assert camera_entity.image_source == "http://1.2.3.4:80/axis-cgi/jpg/image.cgi"
assert (
camera_entity.mjpeg_source
== "http://1.2.3.4:80/axis-cgi/mjpg/video.cgi?streamprofile=profile_1"
)
assert (
await camera_entity.stream_source()
== "rtsp://root:pass@1.2.3.4/axis-media/media.amp?videocodec=h264&streamprofile=profile_1"
)
@pytest.fixture(autouse=True)
def mock_getrandbits():
"""Mock camera access token which normally is randomized."""
with patch(
"homeassistant.components.camera.SystemRandom.getrandbits",
return_value=1,
):
yield
PROPERTY_DATA = f"""root.Properties.API.HTTP.Version=3
@ -66,6 +39,39 @@ root.Properties.System.SerialNumber={MAC}
""" # No image format data to signal camera support
@pytest.mark.parametrize(
("config_entry_options", "stream_profile"),
[
({}, ""),
({CONF_STREAM_PROFILE: "profile_1"}, "streamprofile=profile_1"),
],
)
async def test_camera(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
config_entry_factory: ConfigEntryFactoryType,
snapshot: SnapshotAssertion,
stream_profile: str,
) -> None:
"""Test that Axis camera platform is loaded properly."""
with patch("homeassistant.components.deconz.PLATFORMS", [Platform.CAMERA]):
config_entry = await config_entry_factory()
await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)
entity_id = f"{CAMERA_DOMAIN}.{NAME}"
camera_entity = camera._get_camera_from_entity_id(hass, entity_id)
assert camera_entity.image_source == "http://1.2.3.4:80/axis-cgi/jpg/image.cgi"
assert (
camera_entity.mjpeg_source == "http://1.2.3.4:80/axis-cgi/mjpg/video.cgi"
f"{"" if not stream_profile else f"?{stream_profile}"}"
)
assert (
await camera_entity.stream_source()
== "rtsp://root:pass@1.2.3.4/axis-media/media.amp?videocodec=h264"
f"{"" if not stream_profile else f"&{stream_profile}"}"
)
@pytest.mark.parametrize("param_properties_payload", [PROPERTY_DATA])
@pytest.mark.usefixtures("config_entry_setup")
async def test_camera_disabled(hass: HomeAssistant) -> None: