2024-10-24 08:36:18 +00:00
|
|
|
"""Tests for smart camera devices."""
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-11-21 18:39:15 +00:00
|
|
|
import base64
|
|
|
|
import json
|
2024-10-25 17:30:21 +00:00
|
|
|
from unittest.mock import patch
|
2024-10-24 18:11:21 +00:00
|
|
|
|
2024-10-24 08:36:18 +00:00
|
|
|
import pytest
|
|
|
|
|
2024-12-05 16:49:35 +00:00
|
|
|
from kasa import Credentials, Device, DeviceType, Module, StreamResolution
|
2024-10-24 08:36:18 +00:00
|
|
|
|
2024-12-05 16:49:35 +00:00
|
|
|
from ...conftest import camera_smartcam, device_smartcam
|
2024-10-24 08:36:18 +00:00
|
|
|
|
|
|
|
|
2024-11-23 08:07:47 +00:00
|
|
|
@device_smartcam
|
2024-10-24 08:36:18 +00:00
|
|
|
async def test_state(dev: Device):
|
|
|
|
if dev.device_type is DeviceType.Hub:
|
|
|
|
pytest.skip("Hubs cannot be switched on and off")
|
|
|
|
|
|
|
|
state = dev.is_on
|
|
|
|
await dev.set_state(not state)
|
|
|
|
await dev.update()
|
|
|
|
assert dev.is_on is not state
|
2024-10-24 16:22:45 +00:00
|
|
|
|
|
|
|
|
2024-11-23 08:07:47 +00:00
|
|
|
@camera_smartcam
|
2024-10-25 17:30:21 +00:00
|
|
|
async def test_stream_rtsp_url(dev: Device):
|
|
|
|
camera_module = dev.modules.get(Module.Camera)
|
|
|
|
assert camera_module
|
|
|
|
|
|
|
|
await camera_module.set_state(True)
|
|
|
|
await dev.update()
|
|
|
|
assert camera_module.is_on
|
|
|
|
url = camera_module.stream_rtsp_url(Credentials("foo", "bar"))
|
|
|
|
assert url == "rtsp://foo:bar@127.0.0.123:554/stream1"
|
|
|
|
|
2024-12-05 16:49:35 +00:00
|
|
|
url = camera_module.stream_rtsp_url(
|
|
|
|
Credentials("foo", "bar"), stream_resolution=StreamResolution.HD
|
|
|
|
)
|
|
|
|
assert url == "rtsp://foo:bar@127.0.0.123:554/stream1"
|
|
|
|
|
|
|
|
url = camera_module.stream_rtsp_url(
|
|
|
|
Credentials("foo", "bar"), stream_resolution=StreamResolution.SD
|
|
|
|
)
|
|
|
|
assert url == "rtsp://foo:bar@127.0.0.123:554/stream2"
|
|
|
|
|
2024-11-21 18:39:15 +00:00
|
|
|
with patch.object(dev.config, "credentials", Credentials("bar", "foo")):
|
2024-10-25 17:30:21 +00:00
|
|
|
url = camera_module.stream_rtsp_url()
|
|
|
|
assert url == "rtsp://bar:foo@127.0.0.123:554/stream1"
|
|
|
|
|
2024-11-21 18:39:15 +00:00
|
|
|
with patch.object(dev.config, "credentials", Credentials("bar", "")):
|
2024-10-25 17:30:21 +00:00
|
|
|
url = camera_module.stream_rtsp_url()
|
|
|
|
assert url is None
|
|
|
|
|
2024-11-21 18:39:15 +00:00
|
|
|
with patch.object(dev.config, "credentials", Credentials("", "Foo")):
|
|
|
|
url = camera_module.stream_rtsp_url()
|
|
|
|
assert url is None
|
|
|
|
|
|
|
|
# Test with credentials_hash
|
|
|
|
cred = json.dumps({"un": "bar", "pwd": "foobar"})
|
|
|
|
cred_hash = base64.b64encode(cred.encode()).decode()
|
|
|
|
with (
|
|
|
|
patch.object(dev.config, "credentials", None),
|
|
|
|
patch.object(dev.config, "credentials_hash", cred_hash),
|
|
|
|
):
|
|
|
|
url = camera_module.stream_rtsp_url()
|
|
|
|
assert url == "rtsp://bar:foobar@127.0.0.123:554/stream1"
|
|
|
|
|
|
|
|
# Test with invalid credentials_hash
|
|
|
|
with (
|
|
|
|
patch.object(dev.config, "credentials", None),
|
|
|
|
patch.object(dev.config, "credentials_hash", b"238472871"),
|
|
|
|
):
|
|
|
|
url = camera_module.stream_rtsp_url()
|
|
|
|
assert url is None
|
|
|
|
|
|
|
|
# Test with no credentials
|
|
|
|
with (
|
|
|
|
patch.object(dev.config, "credentials", None),
|
|
|
|
patch.object(dev.config, "credentials_hash", None),
|
|
|
|
):
|
2024-10-25 17:30:21 +00:00
|
|
|
url = camera_module.stream_rtsp_url()
|
|
|
|
assert url is None
|
|
|
|
|
2024-10-24 18:11:21 +00:00
|
|
|
|
2024-12-05 16:49:35 +00:00
|
|
|
@camera_smartcam
|
|
|
|
async def test_onvif_url(dev: Device):
|
|
|
|
"""Test the onvif url."""
|
|
|
|
camera_module = dev.modules.get(Module.Camera)
|
|
|
|
assert camera_module
|
2024-10-24 18:11:21 +00:00
|
|
|
|
2024-12-05 16:49:35 +00:00
|
|
|
url = camera_module.onvif_url()
|
|
|
|
assert url == "http://127.0.0.123:2020/onvif/device_service"
|