10000 Expose PIR enabled setting for iot dimmers (#1174) · python-kasa/python-kasa@9975bbf · GitHub
[go: up one dir, main page]

Skip to content

Commit 9975bbf

Browse files
authored
Expose PIR enabled setting for iot dimmers (#1174)
This adds PIR enabled feature to iot dimmers, making it possible to enable and disable the motion detection.
1 parent 530cf4b commit 9975bbf

File tree

3 files changed

+93
-8
lines changed
  • tests/iot/modules
  • 3 files changed

    +93
    -8
    lines changed

    kasa/iot/modules/motion.py

    Lines changed: 36 additions & 8 deletions
    Original file line numberDiff line numberDiff line change
    @@ -2,11 +2,15 @@
    22

    33
    from __future__ import annotations
    44

    5+
    import logging
    56
    from enum import Enum
    67

    78
    from ...exceptions import KasaException
    9+
    from ...feature import Feature
    810
    from ..iotmodule import IotModule
    911

    12+
    _LOGGER = logging.getLogger(__name__)
    13+
    1014

    1115
    class Range(Enum):
    1216
    """Range for motion detection."""
    @@ -17,27 +21,51 @@ class Range(Enum):
    1721
    Custom = 3
    1822

    1923

    20-
    # TODO: use the config reply in tests
    21-
    # {"enable":0,"version":"1.0","trigger_index":2,"cold_time":60000,
    22-
    # "min_adc":0,"max_adc":4095,"array":[80,50,20,0],"err_code":0}}}
    23-
    24-
    2524
    class Motion(IotModule):
    2625
    """Implements the motion detection (PIR) module."""
    2726

    27+
    def _initialize_features(self):
    28+
    """Initialize features after the initial update."""
    29+
    # Only add features if the device supports the module
    30+
    if "get_config" not in self.data:
    31+
    return
    32+
    33+
    if "enable" not in self.config:
    34+
    _LOGGER.warning("%r initialized, but no enable in response")
    35+
    return
    36+
    37+
    self._add_feature(
    38+
    Feature(
    39+
    device=self._device,
    40+
    container=self,
    41+
    id="pir_enabled",
    42+
    name="PIR enabled",
    43+
    icon="mdi:motion-sensor",
    44+
    attribute_getter="enabled",
    45+
    attribute_setter="set_enabled",
    46+
    type=Feature.Type.Switch,
    47+
    category=Feature.Category.Config,
    48+
    )
    49+
    )
    50+
    2851
    def query(self):
    2952
    """Request PIR configuration."""
    3053
    return self.query_for_command("get_config")
    3154

    55+
    @property
    56+
    def config(self) -> dict:
    57+
    """Return current configuration."""
    58+
    return self.data["get_config"]
    59+
    3260
    @property
    3361
    def range(self) -> Range:
    3462
    """Return motion detection range."""
    35-
    return Range(self.data["trigger_index"])
    63+
    return Range(self.config["trigger_index"])
    3664

    3765
    @property
    3866
    def enabled(self) -> bool:
    3967
    """Return True if module is enabled."""
    40-
    return bool(self.data["enable"])
    68+
    return bool(self.config["enable"])
    4169

    4270
    async def set_enabled(self, state: bool):
    4371
    """Enable/disable PIR."""
    @@ -63,7 +91,7 @@ async def set_range(
    6391
    @property
    6492
    def inactivity_timeout(self) -> int:
    6593
    """Return inactivity timeout in milliseconds."""
    66-
    return self.data["cold_time"]
    94+
    return self.config["cold_time"]
    6795

    6896
    async def set_inactivity_timeout(self, timeout: int):
    6997
    """Set inactivity timeout in milliseconds.

    kasa/tests/iot/modules/__init__.py

    Whitespace-only changes.

    kasa/tests/iot/modules/test_motion.py

    Lines changed: 57 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,57 @@
    1+
    from pytest_mock import MockerFixture
    2+
    3+
    from kasa import Module
    4+
    from kasa.iot import IotDimmer
    5+
    from kasa.iot.modules.motion import Motion, Range
    6+
    from kasa.tests.device_fixtures import dimmer_iot
    7+
    8+
    9+
    @dimmer_iot
    10+
    def test_motion_getters(dev: IotDimmer):
    11+
    assert Module.IotMotion in dev.modules
    12+
    motion: Motion = dev.modules[Module.IotMotion]
    13+
    14+
    assert motion.enabled == motion.config["enable"]
    15+
    assert motion.inactivity_timeout == motion.config["cold_time"]
    16+
    assert motion.range.value == motion.config["trigger_index"]
    17+
    18+
    19+
    @dimmer_iot
    20+
    async def test_motion_setters(dev: IotDimmer, mocker: MockerFixture):
    21+
    motion: Motion = dev.modules[Module.IotMotion]
    22+
    query_helper = mocker.patch("kasa.iot.IotDimmer._query_helper")
    23+
    24+
    await motion.set_enabled(True)
    25+
    query_helper.assert_called_with("smartlife.iot.PIR", "set_enable", {"enable": True})
    26+
    27+
    await motion.set_inactivity_timeout(10)
    28+
    query_helper.assert_called_with(
    29+
    "smartlife.iot.PIR", "set_cold_time", {"cold_time": 10}
    30+
    )
    31+
    32+
    33+
    @dimmer_iot
    34+
    async def test_motion_range(dev: IotDimmer, mocker: MockerFixture):
    35+
    motion: Motion = dev.modules[Module.IotMotion]
    36+
    query_helper = mocker.patch("kasa.iot.IotDimmer._query_helper")
    37+
    38+
    await motion.set_range(custom_range=123)
    39+
    query_helper.assert_called_with(
    40+
    "smartlife.iot.PIR",
    41+
    "set_trigger_sens",
    42+
    {"index": Range.Custom.value, "value": 123},
    43+
    )
    44+
    45+
    await motion.set_range(range=Range.Far)
    46+
    query_helper.assert_called_with(
    47+
    "smartlife.iot.PIR", "set_trigger_sens", {"index": Range.Far.value}
    48+
    )
    49+
    50+
    51+
    @dimmer_iot
    52+
    def test_motion_feature(dev: IotDimmer):
    53+
    assert Module.IotMotion in dev.modules
    54+
    motion: Motion = dev.modules[Module.IotMotion]
    55+
    56+
    pir_enabled = dev.features["pir_enabled"]
    57+
    assert motion.enabled == pir_enabled.value

    0 commit comments

    Comments
     (0)
    0