2024-11-23 08:07:47 +00:00
|
|
|
"""Module for SmartCamDevice."""
|
2024-10-16 15:53:52 +00:00
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-10-24 16:22:45 +00:00
|
|
|
import logging
|
2024-12-11 14:38:38 +00:00
|
|
|
from typing import Any, cast
|
2024-10-24 08:36:18 +00:00
|
|
|
|
2024-12-13 12:37:13 +00:00
|
|
|
from ..device import DeviceInfo
|
2024-10-16 15:53:52 +00:00
|
|
|
from ..device_type import DeviceType
|
2024-10-24 16:22:45 +00:00
|
|
|
from ..module import Module
|
2024-11-23 08:07:47 +00:00
|
|
|
from ..protocols.smartcamprotocol import _ChildCameraProtocolWrapper
|
2024-10-24 16:22:45 +00:00
|
|
|
from ..smart import SmartChildDevice, SmartDevice
|
2024-12-11 14:38:38 +00:00
|
|
|
from ..smart.smartdevice import ComponentsRaw
|
2024-11-13 19:59:42 +00:00
|
|
|
from .modules import ChildDevice, DeviceModule
|
2024-11-23 08:07:47 +00:00
|
|
|
from .smartcammodule import SmartCamModule
|
2024-10-24 16:22:45 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2024-10-16 15:53:52 +00:00
|
|
|
|
|
|
|
|
2024-11-23 08:07:47 +00:00
|
|
|
class SmartCamDevice(SmartDevice):
|
2024-10-16 15:53:52 +00:00
|
|
|
"""Class for smart cameras."""
|
|
|
|
|
2024-10-24 16:22:45 +00:00
|
|
|
# Modules that are called as part of the init procedure on first update
|
|
|
|
FIRST_UPDATE_MODULES = {DeviceModule, ChildDevice}
|
|
|
|
|
2024-10-24 08:36:18 +00:00
|
|
|
@staticmethod
|
|
|
|
def _get_device_type_from_sysinfo(sysinfo: dict[str, Any]) -> DeviceType:
|
|
|
|
"""Find type to be displayed as a supported device category."""
|
2024-11-18 13:14:39 +00:00
|
|
|
if (
|
|
|
|
sysinfo
|
|
|
|
and (device_type := sysinfo.get("device_type"))
|
|
|
|
and device_type.endswith("HUB")
|
|
|
|
):
|
2024-10-24 08:36:18 +00:00
|
|
|
return DeviceType.Hub
|
|
|
|
return DeviceType.Camera
|
|
|
|
|
2024-11-14 18:28:30 +00:00
|
|
|
@staticmethod
|
|
|
|
def _get_device_info(
|
|
|
|
info: dict[str, Any], discovery_info: dict[str, Any] | None
|
2024-12-13 12:37:13 +00:00
|
|
|
) -> DeviceInfo:
|
2024-11-14 18:28:30 +00:00
|
|
|
"""Get model information for a device."""
|
|
|
|
basic_info = info["getDeviceInfo"]["device_info"]["basic_info"]
|
|
|
|
short_name = basic_info["device_model"]
|
|
|
|
long_name = discovery_info["device_model"] if discovery_info else short_name
|
2024-11-23 08:07:47 +00:00
|
|
|
device_type = SmartCamDevice._get_device_type_from_sysinfo(basic_info)
|
2024-11-14 18:28:30 +00:00
|
|
|
fw_version_full = basic_info["sw_version"]
|
2024-11-18 14:53:11 +00:00
|
|
|
firmware_version, firmware_build = fw_version_full.split(" ", maxsplit=1)
|
2024-12-13 12:37:13 +00:00
|
|
|
return DeviceInfo(
|
2024-11-14 18:28:30 +00:00
|
|
|
short_name=basic_info["device_model"],
|
|
|
|
long_name=long_name,
|
|
|
|
brand="tapo",
|
|
|
|
device_family=basic_info["device_type"],
|
|
|
|
device_type=device_type,
|
|
|
|
hardware_version=basic_info["hw_version"],
|
2024-11-18 14:53:11 +00:00
|
|
|
firmware_version=firmware_version,
|
2024-11-14 18:28:30 +00:00
|
|
|
firmware_build=firmware_build,
|
|
|
|
requires_auth=True,
|
|
|
|
region=basic_info.get("region"),
|
|
|
|
)
|
|
|
|
|
2024-10-24 16:22:45 +00:00
|
|
|
def _update_internal_info(self, info_resp: dict) -> None:
|
|
|
|
"""Update the internal device info."""
|
|
|
|
info = self._try_get_response(info_resp, "getDeviceInfo")
|
|
|
|
self._info = self._map_info(info["device_info"])
|
|
|
|
|
|
|
|
def _update_children_info(self) -> None:
|
|
|
|
"""Update the internal child device info from the parent info."""
|
|
|
|
if child_info := self._try_get_response(
|
|
|
|
self._last_update, "getChildDeviceList", {}
|
|
|
|
):
|
|
|
|
for info in child_info["child_device_list"]:
|
2024-12-06 11:01:44 +00:00
|
|
|
child_id = info["device_id"]
|
|
|
|
if child_id not in self._children:
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Skipping child update for %s, probably unsupported device",
|
|
|
|
child_id,
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
self._children[child_id]._update_internal_state(info)
|
2024-10-24 16:22:45 +00:00
|
|
|
|
2024-11-13 10:21:12 +00:00
|
|
|
async def _initialize_smart_child(
|
2024-12-11 14:38:38 +00:00
|
|
|
self, info: dict, child_components_raw: ComponentsRaw
|
2024-11-13 10:21:12 +00:00
|
|
|
) -> SmartDevice:
|
2024-11-23 08:07:47 +00:00
|
|
|
"""Initialize a smart child device attached to a smartcam device."""
|
2024-10-24 16:22:45 +00:00
|
|
|
child_id = info["device_id"]
|
|
|
|
child_protocol = _ChildCameraProtocolWrapper(child_id, self.protocol)
|
|
|
|
try:
|
|
|
|
initial_response = await child_protocol.query(
|
2024-11-13 10:21:12 +00:00
|
|
|
{"get_connect_cloud_state": None}
|
2024-10-24 16:22:45 +00:00
|
|
|
)
|
|
|
|
except Exception as ex:
|
|
|
|
_LOGGER.exception("Error initialising child %s: %s", child_id, ex)
|
|
|
|
|
|
|
|
return await SmartChildDevice.create(
|
|
|
|
parent=self,
|
|
|
|
child_info=info,
|
2024-12-11 14:38:38 +00:00
|
|
|
child_components_raw=child_components_raw,
|
2024-10-24 16:22:45 +00:00
|
|
|
protocol=child_protocol,
|
|
|
|
last_update=initial_response,
|
|
|
|
)
|
|
|
|
|
|
|
|
async def _initialize_children(self) -> None:
|
|
|
|
"""Initialize children for hubs."""
|
2024-11-13 10:21:12 +00:00
|
|
|
child_info_query = {
|
|
|
|
"getChildDeviceList": {"childControl": {"start_index": 0}},
|
|
|
|
"getChildDeviceComponentList": {"childControl": {"start_index": 0}},
|
|
|
|
}
|
|
|
|
resp = await self.protocol.query(child_info_query)
|
|
|
|
self.internal_state.update(resp)
|
2024-10-24 16:22:45 +00:00
|
|
|
|
2024-11-26 11:37:14 +00:00
|
|
|
smart_children_components = {
|
2024-12-11 14:38:38 +00:00
|
|
|
child["device_id"]: child
|
2024-11-13 10:21:12 +00:00
|
|
|
for child in resp["getChildDeviceComponentList"]["child_component_list"]
|
|
|
|
}
|
2024-10-24 16:22:45 +00:00
|
|
|
children = {}
|
2024-11-13 10:21:12 +00:00
|
|
|
for info in resp["getChildDeviceList"]["child_device_list"]:
|
2024-10-24 16:22:45 +00:00
|
|
|
if (
|
2024-11-26 11:37:14 +00:00
|
|
|
(category := info.get("category"))
|
|
|
|
and category in SmartChildDevice.CHILD_DEVICE_TYPE_MAP
|
|
|
|
and (child_id := info.get("device_id"))
|
|
|
|
and (child_components := smart_children_components.get(child_id))
|
|
|
|
):
|
2024-11-13 10:21:12 +00:00
|
|
|
children[child_id] = await self._initialize_smart_child(
|
2024-11-26 11:37:14 +00:00
|
|
|
info, child_components
|
2024-11-13 10:21:12 +00:00
|
|
|
)
|
2024-10-24 16:22:45 +00:00
|
|
|
else:
|
|
|
|
_LOGGER.debug("Child device type not supported: %s", info)
|
|
|
|
|
|
|
|
self._children = children
|
|
|
|
|
|
|
|
async def _initialize_modules(self) -> None:
|
|
|
|
"""Initialize modules based on component negotiation response."""
|
2024-11-23 08:07:47 +00:00
|
|
|
for mod in SmartCamModule.REGISTERED_MODULES.values():
|
2024-11-13 10:21:12 +00:00
|
|
|
if (
|
|
|
|
mod.REQUIRED_COMPONENT
|
|
|
|
and mod.REQUIRED_COMPONENT not in self._components
|
2024-12-17 20:15:42 +00:00
|
|
|
# Always add Camera module to cameras
|
|
|
|
and (
|
|
|
|
mod._module_name() != Module.Camera
|
|
|
|
or self._device_type is not DeviceType.Camera
|
|
|
|
)
|
2024-11-13 10:21:12 +00:00
|
|
|
):
|
|
|
|
continue
|
2024-10-24 16:22:45 +00:00
|
|
|
module = mod(self, mod._module_name())
|
|
|
|
if await module._check_supported():
|
|
|
|
self._modules[module.name] = module
|
|
|
|
|
|
|
|
async def _initialize_features(self) -> None:
|
|
|
|
"""Initialize device features."""
|
|
|
|
for module in self.modules.values():
|
|
|
|
module._initialize_features()
|
|
|
|
for feat in module._module_features.values():
|
|
|
|
self._add_feature(feat)
|
|
|
|
|
|
|
|
for child in self._children.values():
|
|
|
|
await child._initialize_features()
|
|
|
|
|
|
|
|
async def _query_setter_helper(
|
|
|
|
self, method: str, module: str, section: str, params: dict | None = None
|
|
|
|
) -> dict:
|
|
|
|
res = await self.protocol.query({method: {module: {section: params}}})
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
async def _query_getter_helper(
|
|
|
|
self, method: str, module: str, sections: str | list[str]
|
|
|
|
) -> Any:
|
|
|
|
res = await self.protocol.query({method: {module: {"name": sections}}})
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
2024-12-11 14:38:38 +00:00
|
|
|
@staticmethod
|
|
|
|
def _parse_components(components_raw: ComponentsRaw) -> dict[str, int]:
|
|
|
|
return {
|
|
|
|
str(comp["name"]): int(comp["version"])
|
|
|
|
for comp in components_raw["app_component_list"]
|
|
|
|
}
|
|
|
|
|
2024-10-24 16:22:45 +00:00
|
|
|
async def _negotiate(self) -> None:
|
|
|
|
"""Perform initialization.
|
|
|
|
|
|
|
|
We fetch the device info and the available components as early as possible.
|
|
|
|
If the device reports supporting child devices, they are also initialized.
|
|
|
|
"""
|
2024-10-16 15:53:52 +00:00
|
|
|
initial_query = {
|
|
|
|
"getDeviceInfo": {"device_info": {"name": ["basic_info", "info"]}},
|
2024-11-13 10:21:12 +00:00
|
|
|
"getAppComponentList": {"app_component": {"name": "app_component_list"}},
|
2024-12-20 06:16:18 +00:00
|
|
|
"getConnectionType": {"network": {"get_connection_type": {}}},
|
2024-10-16 15:53:52 +00:00
|
|
|
}
|
|
|
|
resp = await self.protocol.query(initial_query)
|
|
|
|
self._last_update.update(resp)
|
2024-10-24 16:22:45 +00:00
|
|
|
self._update_internal_info(resp)
|
2024-11-13 10:21:12 +00:00
|
|
|
|
2024-12-11 14:38:38 +00:00
|
|
|
self._components_raw = cast(
|
|
|
|
ComponentsRaw, resp["getAppComponentList"]["app_component"]
|
|
|
|
)
|
|
|
|
self._components = self._parse_components(self._components_raw)
|
2024-11-13 10:21:12 +00:00
|
|
|
|
|
|
|
if "childControl" in self._components and not self.children:
|
|
|
|
await self._initialize_children()
|
2024-10-16 15:53:52 +00:00
|
|
|
|
|
|
|
def _map_info(self, device_info: dict) -> dict:
|
|
|
|
basic_info = device_info["basic_info"]
|
|
|
|
return {
|
|
|
|
"model": basic_info["device_model"],
|
2024-10-24 08:36:18 +00:00
|
|
|
"device_type": basic_info["device_type"],
|
2024-10-16 15:53:52 +00:00
|
|
|
"alias": basic_info["device_alias"],
|
|
|
|
"fw_ver": basic_info["sw_version"],
|
|
|
|
"hw_ver": basic_info["hw_version"],
|
|
|
|
"mac": basic_info["mac"],
|
2024-10-28 12:47:24 +00:00
|
|
|
"hwId": basic_info.get("hw_id"),
|
2024-10-16 15:53:52 +00:00
|
|
|
"oem_id": basic_info["oem_id"],
|
2024-12-06 09:40:44 +00:00
|
|
|
"device_id": basic_info["dev_id"],
|
2024-10-16 15:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return true if the device is on."""
|
2024-10-24 16:22:45 +00:00
|
|
|
if (camera := self.modules.get(Module.Camera)) and not camera.disabled:
|
|
|
|
return camera.is_on
|
|
|
|
|
|
|
|
return True
|
2024-10-16 15:53:52 +00:00
|
|
|
|
2024-10-24 16:22:45 +00:00
|
|
|
async def set_state(self, on: bool) -> dict:
|
2024-10-16 15:53:52 +00:00
|
|
|
"""Set the device state."""
|
2024-10-24 16:22:45 +00:00
|
|
|
if (camera := self.modules.get(Module.Camera)) and not camera.disabled:
|
|
|
|
return await camera.set_state(on)
|
|
|
|
|
|
|
|
return {}
|
2024-10-16 15:53:52 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_type(self) -> DeviceType:
|
|
|
|
"""Return the device type."""
|
2024-10-24 08:36:18 +00:00
|
|
|
if self._device_type == DeviceType.Unknown:
|
|
|
|
self._device_type = self._get_device_type_from_sysinfo(self._info)
|
|
|
|
return self._device_type
|
2024-10-16 15:53:52 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def alias(self) -> str | None:
|
|
|
|
"""Returns the device alias or nickname."""
|
|
|
|
if self._info:
|
|
|
|
return self._info.get("alias")
|
|
|
|
return None
|
|
|
|
|
2024-10-24 16:22:45 +00:00
|
|
|
async def set_alias(self, alias: str) -> dict:
|
|
|
|
"""Set the device name (alias)."""
|
|
|
|
return await self.protocol.query(
|
|
|
|
{
|
|
|
|
"setDeviceAlias": {"system": {"sys": {"dev_alias": alias}}},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-10-16 15:53:52 +00:00
|
|
|
@property
|
|
|
|
def hw_info(self) -> dict:
|
|
|
|
"""Return hardware info for the device."""
|
|
|
|
return {
|
2024-12-13 12:37:13 +00:00
|
|
|
"sw_ver": self._info.get("fw_ver"),
|
|
|
|
"hw_ver": self._info.get("hw_ver"),
|
2024-10-16 15:53:52 +00:00
|
|
|
"mac": self._info.get("mac"),
|
|
|
|
"type": self._info.get("type"),
|
|
|
|
"hwId": self._info.get("hwId"),
|
|
|
|
"dev_name": self.alias,
|
|
|
|
"oemId": self._info.get("oem_id"),
|
|
|
|
}
|
2024-12-20 06:16:18 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def rssi(self) -> int | None:
|
|
|
|
"""Return the device id."""
|
|
|
|
return self.modules[SmartCamModule.SmartCamDeviceModule].rssi
|