Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add smartcam pet detection toggle module #1465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions kasa/smartcam/modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .motiondetection import MotionDetection
from .pantilt import PanTilt
from .persondetection import PersonDetection
from .petdetection import PetDetection
from .tamperdetection import TamperDetection
from .time import Time

Expand All @@ -26,6 +27,7 @@
"Led",
"PanTilt",
"PersonDetection",
"PetDetection",
"Time",
"HomeKit",
"Matter",
Expand Down
49 changes: 49 additions & 0 deletions kasa/smartcam/modules/petdetection.py
Original file line number Diff line number Diff line change
@@ -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
)
3 changes: 3 additions & 0 deletions kasa/smartcam/smartcammodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
45 changes: 45 additions & 0 deletions tests/smartcam/modules/test_petdetection.py
Original file line number Diff line number Diff line change
@@ -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