mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-01-22 12:47:05 +00:00
Add smartcam pet detection toggle module (#1465)
This commit is contained in:
parent
980f6a38ca
commit
fd6067e5a0
@ -13,6 +13,7 @@ from .matter import Matter
|
||||
from .motiondetection import MotionDetection
|
||||
from .pantilt import PanTilt
|
||||
from .persondetection import PersonDetection
|
||||
from .petdetection import PetDetection
|
||||
from .tamperdetection import TamperDetection
|
||||
from .time import Time
|
||||
|
||||
@ -26,6 +27,7 @@ __all__ = [
|
||||
"Led",
|
||||
"PanTilt",
|
||||
"PersonDetection",
|
||||
"PetDetection",
|
||||
"Time",
|
||||
"HomeKit",
|
||||
"Matter",
|
||||
|
49
kasa/smartcam/modules/petdetection.py
Normal file
49
kasa/smartcam/modules/petdetection.py
Normal file
@ -0,0 +1,49 @@
|
||||
"""Implementation of pet detection module."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from ...feature import Feature
|
||||
from ...smart.smartmodule import allow_update_after
|
||||
from ..smartcammodule import SmartCamModule
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class PetDetection(SmartCamModule):
|
||||
"""Implementation of pet detection module."""
|
||||
|
||||
REQUIRED_COMPONENT = "petDetection"
|
||||
|
||||
QUERY_GETTER_NAME = "getPetDetectionConfig"
|
||||
QUERY_MODULE_NAME = "pet_detection"
|
||||
QUERY_SECTION_NAMES = "detection"
|
||||
|
||||
def _initialize_features(self) -> None:
|
||||
"""Initialize features after the initial update."""
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="pet_detection",
|
||||
name="Pet detection",
|
||||
container=self,
|
||||
attribute_getter="enabled",
|
||||
attribute_setter="set_enabled",
|
||||
type=Feature.Type.Switch,
|
||||
category=Feature.Category.Config,
|
||||
)
|
||||
)
|
||||
|
||||
@property
|
||||
def enabled(self) -> bool:
|
||||
"""Return the pet detection enabled state."""
|
||||
return self.data["detection"]["enabled"] == "on"
|
||||
|
||||
@allow_update_after
|
||||
async def set_enabled(self, enable: bool) -> dict:
|
||||
"""Set the pet detection enabled state."""
|
||||
params = {"enabled": "on" if enable else "off"}
|
||||
return await self._device._query_setter_helper(
|
||||
"setPetDetectionConfig", self.QUERY_MODULE_NAME, "detection", params
|
||||
)
|
@ -26,6 +26,9 @@ class SmartCamModule(SmartModule):
|
||||
SmartCamPersonDetection: Final[ModuleName[modules.PersonDetection]] = ModuleName(
|
||||
"PersonDetection"
|
||||
)
|
||||
SmartCamPetDetection: Final[ModuleName[modules.PetDetection]] = ModuleName(
|
||||
"PetDetection"
|
||||
)
|
||||
SmartCamTamperDetection: Final[ModuleName[modules.TamperDetection]] = ModuleName(
|
||||
"TamperDetection"
|
||||
)
|
||||
|
45
tests/smartcam/modules/test_petdetection.py
Normal file
45
tests/smartcam/modules/test_petdetection.py
Normal file
@ -0,0 +1,45 @@
|
||||
"""Tests for smartcam pet detection module."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from kasa import Device
|
||||
from kasa.smartcam.smartcammodule import SmartCamModule
|
||||
|
||||
from ...device_fixtures import parametrize
|
||||
|
||||
petdetection = parametrize(
|
||||
"has pet detection",
|
||||
component_filter="petDetection",
|
||||
protocol_filter={"SMARTCAM"},
|
||||
)
|
||||
|
||||
|
||||
@petdetection
|
||||
async def test_petdetection(dev: Device):
|
||||
"""Test device pet detection."""
|
||||
pet = dev.modules.get(SmartCamModule.SmartCamPetDetection)
|
||||
assert pet
|
||||
|
||||
pde_feat = dev.features.get("pet_detection")
|
||||
assert pde_feat
|
||||
|
||||
original_enabled = pet.enabled
|
||||
|
||||
try:
|
||||
await pet.set_enabled(not original_enabled)
|
||||
await dev.update()
|
||||
assert pet.enabled is not original_enabled
|
||||
assert pde_feat.value is not original_enabled
|
||||
|
||||
await pet.set_enabled(original_enabled)
|
||||
await dev.update()
|
||||
assert pet.enabled is original_enabled
|
||||
assert pde_feat.value is original_enabled
|
||||
|
||||
await pde_feat.set_value(not original_enabled)
|
||||
await dev.update()
|
||||
assert pet.enabled is not original_enabled
|
||||
assert pde_feat.value is not original_enabled
|
||||
|
||||
finally:
|
||||
await pet.set_enabled(original_enabled)
|
Loading…
Reference in New Issue
Block a user