forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use snapshot in Axis camera tests (home-assistant#122677)
- Loading branch information
Showing
2 changed files
with
147 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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', | ||
}) | ||
# --- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,77 @@ | ||
"""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 | ||
|
||
from tests.common import snapshot_platform | ||
|
||
@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}" | ||
@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 | ||
|
||
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:[email protected]/axis-media/media.amp?videocodec=h264" | ||
) | ||
PROPERTY_DATA = f"""root.Properties.API.HTTP.Version=3 | ||
root.Properties.API.Metadata.Metadata=yes | ||
root.Properties.API.Metadata.Version=1.0 | ||
root.Properties.EmbeddedDevelopment.Version=2.16 | ||
root.Properties.Firmware.BuildDate=Feb 15 2019 09:42 | ||
root.Properties.Firmware.BuildNumber=26 | ||
root.Properties.Firmware.Version=9.10.1 | ||
root.Properties.System.SerialNumber={MAC} | ||
""" # No image format data to signal camera support | ||
|
||
|
||
@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 | ||
@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}" | ||
|
||
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" | ||
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:[email protected]/axis-media/media.amp?videocodec=h264&streamprofile=profile_1" | ||
== "rtsp://root:[email protected]/axis-media/media.amp?videocodec=h264" | ||
f"{"" if not stream_profile else f"&{stream_profile}"}" | ||
) | ||
|
||
|
||
PROPERTY_DATA = f"""root.Properties.API.HTTP.Version=3 | ||
root.Properties.API.Metadata.Metadata=yes | ||
root.Properties.API.Metadata.Version=1.0 | ||
root.Properties.EmbeddedDevelopment.Version=2.16 | ||
root.Properties.Firmware.BuildDate=Feb 15 2019 09:42 | ||
root.Properties.Firmware.BuildNumber=26 | ||
root.Properties.Firmware.Version=9.10.1 | ||
root.Properties.System.SerialNumber={MAC} | ||
""" # No image format data to signal camera support | ||
|
||
|
||
@pytest.mark.parametrize("param_properties_payload", [PROPERTY_DATA]) | ||
@pytest.mark.usefixtures("config_entry_setup") | ||
async def test_camera_disabled(hass: HomeAssistant) -> None: | ||
|