mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-10-15 20:08:06 +00:00
Add core device, child and camera modules to smartcamera (#1193)
Co-authored-by: Teemu R. <tpr@iki.fi>
This commit is contained in:
11
kasa/experimental/modules/__init__.py
Normal file
11
kasa/experimental/modules/__init__.py
Normal file
@@ -0,0 +1,11 @@
|
||||
"""Modules for SMARTCAMERA devices."""
|
||||
|
||||
from .camera import Camera
|
||||
from .childdevice import ChildDevice
|
||||
from .device import DeviceModule
|
||||
|
||||
__all__ = [
|
||||
"Camera",
|
||||
"ChildDevice",
|
||||
"DeviceModule",
|
||||
]
|
45
kasa/experimental/modules/camera.py
Normal file
45
kasa/experimental/modules/camera.py
Normal file
@@ -0,0 +1,45 @@
|
||||
"""Implementation of device module."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from ...device_type import DeviceType
|
||||
from ...feature import Feature
|
||||
from ..smartcameramodule import SmartCameraModule
|
||||
|
||||
|
||||
class Camera(SmartCameraModule):
|
||||
"""Implementation of device module."""
|
||||
|
||||
QUERY_GETTER_NAME = "getLensMaskConfig"
|
||||
QUERY_MODULE_NAME = "lens_mask"
|
||||
QUERY_SECTION_NAMES = "lens_mask_info"
|
||||
|
||||
def _initialize_features(self) -> None:
|
||||
"""Initialize features after the initial update."""
|
||||
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,
|
||||
)
|
||||
)
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return the device id."""
|
||||
return self.data["lens_mask_info"]["enabled"] == "on"
|
||||
|
||||
async def set_state(self, on: bool) -> dict:
|
||||
"""Set the device state."""
|
||||
params = {"enabled": "on" if on else "off"}
|
||||
return await self._device._query_setter_helper(
|
||||
"setLensMaskConfig", self.QUERY_MODULE_NAME, "lens_mask_info", params
|
||||
)
|
||||
|
||||
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
|
23
kasa/experimental/modules/childdevice.py
Normal file
23
kasa/experimental/modules/childdevice.py
Normal file
@@ -0,0 +1,23 @@
|
||||
"""Module for child devices."""
|
||||
|
||||
from ...device_type import DeviceType
|
||||
from ..smartcameramodule import SmartCameraModule
|
||||
|
||||
|
||||
class ChildDevice(SmartCameraModule):
|
||||
"""Implementation for child devices."""
|
||||
|
||||
NAME = "childdevice"
|
||||
QUERY_GETTER_NAME = "getChildDeviceList"
|
||||
QUERY_MODULE_NAME = "childControl"
|
||||
|
||||
def query(self) -> dict:
|
||||
"""Query to execute during the update cycle.
|
||||
|
||||
Default implementation uses the raw query getter w/o parameters.
|
||||
"""
|
||||
return {self.QUERY_GETTER_NAME: {self.QUERY_MODULE_NAME: {"start_index": 0}}}
|
||||
|
||||
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.Hub
|
40
kasa/experimental/modules/device.py
Normal file
40
kasa/experimental/modules/device.py
Normal file
@@ -0,0 +1,40 @@
|
||||
"""Implementation of device module."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from ...feature import Feature
|
||||
from ..smartcameramodule import SmartCameraModule
|
||||
|
||||
|
||||
class DeviceModule(SmartCameraModule):
|
||||
"""Implementation of device module."""
|
||||
|
||||
NAME = "devicemodule"
|
||||
QUERY_GETTER_NAME = "getDeviceInfo"
|
||||
QUERY_MODULE_NAME = "device_info"
|
||||
QUERY_SECTION_NAMES = ["basic_info", "info"]
|
||||
|
||||
def _initialize_features(self) -> None:
|
||||
"""Initialize features after the initial update."""
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="device_id",
|
||||
name="Device ID",
|
||||
attribute_getter="device_id",
|
||||
category=Feature.Category.Debug,
|
||||
type=Feature.Type.Sensor,
|
||||
)
|
||||
)
|
||||
|
||||
async def _post_update_hook(self) -> None:
|
||||
"""Overriden to prevent module disabling.
|
||||
|
||||
Overrides the default behaviour to disable a module if the query returns
|
||||
an error because this module is critical.
|
||||
"""
|
||||
|
||||
@property
|
||||
def device_id(self) -> str:
|
||||
"""Return the device id."""
|
||||
return self.data["basic_info"]["dev_id"]
|
Reference in New Issue
Block a user