2024-12-17 20:15:42 +00:00
|
|
|
"""Implementation of camera module."""
|
2024-10-24 16:22:45 +00:00
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-11-21 18:39:15 +00:00
|
|
|
import base64
|
|
|
|
import logging
|
2024-12-05 16:49:35 +00:00
|
|
|
from enum import StrEnum
|
2024-12-17 20:15:42 +00:00
|
|
|
from typing import Annotated
|
2024-10-25 17:30:21 +00:00
|
|
|
from urllib.parse import quote_plus
|
|
|
|
|
|
|
|
from ...credentials import Credentials
|
2024-10-24 16:22:45 +00:00
|
|
|
from ...device_type import DeviceType
|
|
|
|
from ...feature import Feature
|
2024-11-21 18:39:15 +00:00
|
|
|
from ...json import loads as json_loads
|
2024-12-17 20:15:42 +00:00
|
|
|
from ...module import FeatureAttribute, Module
|
2024-11-23 08:07:47 +00:00
|
|
|
from ..smartcammodule import SmartCamModule
|
2024-10-24 16:22:45 +00:00
|
|
|
|
2024-11-21 18:39:15 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2024-10-25 17:30:21 +00:00
|
|
|
LOCAL_STREAMING_PORT = 554
|
2024-12-05 16:49:35 +00:00
|
|
|
ONVIF_PORT = 2020
|
|
|
|
|
|
|
|
|
|
|
|
class StreamResolution(StrEnum):
|
|
|
|
"""Class for stream resolution."""
|
|
|
|
|
|
|
|
HD = "HD"
|
|
|
|
SD = "SD"
|
2024-10-25 17:30:21 +00:00
|
|
|
|
2024-10-24 16:22:45 +00:00
|
|
|
|
2024-11-23 08:07:47 +00:00
|
|
|
class Camera(SmartCamModule):
|
2024-10-24 16:22:45 +00:00
|
|
|
"""Implementation of device module."""
|
|
|
|
|
|
|
|
def _initialize_features(self) -> None:
|
|
|
|
"""Initialize features after the initial update."""
|
2024-12-17 20:15:42 +00:00
|
|
|
if Module.LensMask in self._device.modules:
|
|
|
|
self._add_feature(
|
|
|
|
Feature(
|
|
|
|
self._device,
|
|
|
|
id="state",
|
|
|
|
name="State",
|
|
|
|
attribute_getter="is_on",
|
|
|
|
attribute_setter="set_state",
|
|
|
|
type=Feature.Type.Switch,
|
|
|
|
category=Feature.Category.Primary,
|
|
|
|
)
|
2024-10-24 16:22:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
2024-12-17 20:15:42 +00:00
|
|
|
"""Return the device on state."""
|
|
|
|
if lens_mask := self._device.modules.get(Module.LensMask):
|
|
|
|
return lens_mask.state
|
|
|
|
return True
|
|
|
|
|
|
|
|
async def set_state(self, on: bool) -> Annotated[dict, FeatureAttribute()]:
|
|
|
|
"""Set the device on state.
|
|
|
|
|
|
|
|
If the device does not support setting state will do nothing.
|
|
|
|
"""
|
|
|
|
if lens_mask := self._device.modules.get(Module.LensMask):
|
|
|
|
# Turning off enables the privacy mask which is why value is reversed.
|
|
|
|
return await lens_mask.set_state(not on)
|
|
|
|
return {}
|
2024-10-25 17:30:21 +00:00
|
|
|
|
2024-11-21 18:39:15 +00:00
|
|
|
def _get_credentials(self) -> Credentials | None:
|
|
|
|
"""Get credentials from ."""
|
|
|
|
config = self._device.config
|
|
|
|
if credentials := config.credentials:
|
|
|
|
return credentials
|
|
|
|
|
|
|
|
if credentials_hash := config.credentials_hash:
|
|
|
|
try:
|
|
|
|
decoded = json_loads(
|
|
|
|
base64.b64decode(credentials_hash.encode()).decode()
|
|
|
|
)
|
|
|
|
except Exception:
|
|
|
|
_LOGGER.warning(
|
|
|
|
"Unable to deserialize credentials_hash: %s", credentials_hash
|
|
|
|
)
|
|
|
|
return None
|
|
|
|
if (username := decoded.get("un")) and (password := decoded.get("pwd")):
|
|
|
|
return Credentials(username, password)
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2024-12-05 16:49:35 +00:00
|
|
|
def stream_rtsp_url(
|
|
|
|
self,
|
|
|
|
credentials: Credentials | None = None,
|
|
|
|
*,
|
|
|
|
stream_resolution: StreamResolution = StreamResolution.HD,
|
|
|
|
) -> str | None:
|
2024-10-25 17:30:21 +00:00
|
|
|
"""Return the local rtsp streaming url.
|
|
|
|
|
|
|
|
:param credentials: Credentials for camera account.
|
|
|
|
These could be different credentials to tplink cloud credentials.
|
|
|
|
If not provided will use tplink credentials if available
|
|
|
|
:return: rtsp url with escaped credentials or None if no credentials or
|
|
|
|
camera is off.
|
|
|
|
"""
|
2024-12-05 16:49:35 +00:00
|
|
|
streams = {
|
|
|
|
StreamResolution.HD: "stream1",
|
|
|
|
StreamResolution.SD: "stream2",
|
|
|
|
}
|
|
|
|
if (stream := streams.get(stream_resolution)) is None:
|
2024-10-25 17:30:21 +00:00
|
|
|
return None
|
2024-12-05 16:49:35 +00:00
|
|
|
|
2024-10-25 17:30:21 +00:00
|
|
|
if not credentials:
|
2024-11-21 18:39:15 +00:00
|
|
|
credentials = self._get_credentials()
|
|
|
|
|
2024-10-25 17:30:21 +00:00
|
|
|
if not credentials or not credentials.username or not credentials.password:
|
|
|
|
return None
|
2024-12-05 16:49:35 +00:00
|
|
|
|
2024-10-25 17:30:21 +00:00
|
|
|
username = quote_plus(credentials.username)
|
|
|
|
password = quote_plus(credentials.password)
|
2024-12-05 16:49:35 +00:00
|
|
|
|
|
|
|
return f"rtsp://{username}:{password}@{self._device.host}:{LOCAL_STREAMING_PORT}/{stream}"
|
|
|
|
|
|
|
|
def onvif_url(self) -> str | None:
|
|
|
|
"""Return the onvif url."""
|
|
|
|
return f"http://{self._device.host}:{ONVIF_PORT}/onvif/device_service"
|
2024-10-24 16:22:45 +00:00
|
|
|
|
|
|
|
async def _check_supported(self) -> bool:
|
|
|
|
"""Additional check to see if the module is supported by the device."""
|
|
|
|
return self._device.device_type is DeviceType.Camera
|