Add smartcam pet detection toggle module (#1465)

This commit is contained in:
DawidPietrykowski 2025-01-18 13:58:26 +01:00 committed by GitHub
parent 980f6a38ca
commit fd6067e5a0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 99 additions and 0 deletions

View File

@ -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",

View 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
)

View File

@ -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"
)

View 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)