FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add smartcam pet detection toggle module (#1465) · rytilahti/python-kasa@a6db9cf · GitHub

Repository navigation

Commit a6db9cf

Browse files
authored andcommitted
Add smartcam pet detection toggle module (python-kasa#1465)
1 parent e645845 commit a6db9cf

4 files changed

Lines changed: 99 additions & 0 deletions

File tree

‎kasa/smartcam/modules/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from .motiondetection import MotionDetection
1414
from .pantilt import PanTilt
1515
from .persondetection import PersonDetection
16+
from .petdetection import PetDetection
1617
from .tamperdetection import TamperDetection
1718
from .time import Time
1819

@@ -26,6 +27,7 @@
2627
"Led",
2728
"PanTilt",
2829
"PersonDetection",
30+
"PetDetection",
2931
"Time",
3032
"HomeKit",
3133
"Matter",
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""Implementation of pet detection module."""
2+
3+
from __future__ import annotations
4+
5+
import logging
6+
7+
from ...feature import Feature
8+
from ...smart.smartmodule import allow_update_after
9+
from ..smartcammodule import SmartCamModule
10+
11+
_LOGGER = logging.getLogger(__name__)
12+
13+
14+
class PetDetection(SmartCamModule):
15+
"""Implementation of pet detection module."""
16+
17+
REQUIRED_COMPONENT = "petDetection"
18+
19+
QUERY_GETTER_NAME = "getPetDetectionConfig"
20+
QUERY_MODULE_NAME = "pet_detection"
21+
QUERY_SECTION_NAMES = "detection"
22+
23+
def _initialize_features(self) -> None:
24+
"""Initialize features after the initial update."""
25+
self._add_feature(
26+
Feature(
27+
self._device,
28+
id="pet_detection",
29+
name="Pet detection",
30+
container=self,
31+
attribute_getter="enabled",
32+
attribute_setter="set_enabled",
33+
type=Feature.Type.Switch,
34+
category=Feature.Category.Config,
35+
)
36+
)
37+
38+
@property
39+
def enabled(self) -> bool:
40+
"""Return the pet detection enabled state."""
41+
return self.data["detection"]["enabled"] == "on"
42+
43+
@allow_update_after
44+
async def set_enabled(self, enable: bool) -> dict:
45+
"""Set the pet detection enabled state."""
46+
params = {"enabled": "on" if enable else "off"}
47+
return await self._device._query_setter_helper(
48+
"setPetDetectionConfig", self.QUERY_MODULE_NAME, "detection", params
49+
)

‎kasa/smartcam/smartcammodule.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class SmartCamModule(SmartModule):
2626
SmartCamPersonDetection: Final[ModuleName[modules.PersonDetection]] = ModuleName(
2727
"PersonDetection"
2828
)
29+
SmartCamPetDetection: Final[ModuleName[modules.PetDetection]] = ModuleName(
30+
"PetDetection"
31+
)
2932
SmartCamTamperDetection: Final[ModuleName[modules.TamperDetection]] = ModuleName(
3033
"TamperDetection"
3134
)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""Tests for smartcam pet detection module."""
2+
3+
from __future__ import annotations
4+
5+
from kasa import Device
6+
from kasa.smartcam.smartcammodule import SmartCamModule
7+
8+
from ...device_fixtures import parametrize
9+
10+
petdetection = parametrize(
11+
"has pet detection",
12+
component_filter="petDetection",
13+
protocol_filter={"SMARTCAM"},
14+
)
15+
16+
17+
@petdetection
18+
async def test_petdetection(dev: Device):
19+
"""Test device pet detection."""
20+
pet = dev.modules.get(SmartCamModule.SmartCamPetDetection)
21+
assert pet
22+
23+
pde_feat = dev.features.get("pet_detection")
24+
assert pde_feat
25+
26+
original_enabled = pet.enabled
27+
28+
try:
29+
await pet.set_enabled(not original_enabled)
30+
await dev.update()
31+
assert pet.enabled is not original_enabled
32+
assert pde_feat.value is not original_enabled
33+
34+
await pet.set_enabled(original_enabled)
35+
await dev.update()
36+
assert pet.enabled is original_enabled
37+
assert pde_feat.value is original_enabled
38+
39+
await pde_feat.set_value(not original_enabled)
40+
await dev.update()
41+
assert pet.enabled is not original_enabled
42+
assert pde_feat.value is not original_enabled
43+
44+
finally:
45+
await pet.set_enabled(original_enabled)

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL