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

Add strip segment module for configurable light strip length by nopoz · Pull Request #1744 · python-kasa/python-kasa · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (4) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
1 change: 1 addition & 0 deletions kasa/module.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class Module(ABC):
PowerProtection: Final[ModuleName[smart.PowerProtection]] = ModuleName(
"PowerProtection"
)
Segment: Final[ModuleName[smart.Segment]] = ModuleName("Segment")

HomeKit: Final[ModuleName[smart.HomeKit]] = ModuleName("HomeKit")
Matter: Final[ModuleName[smart.Matter]] = ModuleName("Matter")
Expand Down
2 changes: 2 additions & 0 deletions kasa/smart/modules/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from .overheatprotection import OverheatProtection
from .powerprotection import PowerProtection
from .reportmode import ReportMode
from .segment import Segment
from .speaker import Speaker
from .temperaturecontrol import TemperatureControl
from .temperaturesensor import TemperatureSensor
Expand Down Expand Up @@ -83,6 +84,7 @@
"SmartLightEffect",
"PowerProtection",
"OverheatProtection",
"Segment",
"Speaker",
"HomeKit",
"Matter",
Expand Down
52 changes: 52 additions & 0 deletions kasa/smart/modules/segment.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Implementation of the light strip segment module."""

from __future__ import annotations

from ...feature import Feature
from ..smartmodule import SmartModule

# The device reports no maximum, so it cannot be queried: a run is 5m in 10cm
# segments. Multi-spool products drive every run from this one value, making
# the limit per-run rather than per-model.
SEGMENTS_MIN = 0
SEGMENTS_MAX = 50


class Segment(SmartModule):
"""Implementation of the configurable light strip length."""

REQUIRED_COMPONENT = "segment"
QUERY_GETTER_NAME = "get_device_segment"

def _initialize_features(self) -> None:
"""Initialize features."""
self._add_feature(
Feature(
self._device,
id="strip_segments",
name="Strip segments",
container=self,
attribute_getter="segments",
attribute_setter="set_segments",
range_getter=lambda: (SEGMENTS_MIN, SEGMENTS_MAX),
type=Feature.Type.Number,
category=Feature.Category.Config,
)
)

@property
def segments(self) -> int:
"""Return the number of 10cm segments the strip is configured for."""
return self.data["segment"]

async def set_segments(self, segments: int) -> dict:
"""Set the number of 10cm segments the strip is cut to."""
if not isinstance(segments, int) or not (
SEGMENTS_MIN <= segments <= SEGMENTS_MAX
):
raise ValueError(
f"Invalid segment count: {segments} "
f"(valid range: {SEGMENTS_MIN}-{SEGMENTS_MAX})"
)

return await self.call("set_device_segment", {"segment": segments})
57 changes: 57 additions & 0 deletions tests/smart/modules/test_segment.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import pytest
from pytest_mock import MockerFixture

from kasa import Module
from kasa.smart import SmartDevice
from kasa.smart.modules.segment import SEGMENTS_MAX, SEGMENTS_MIN

from ...device_fixtures import parametrize

segment = parametrize(
"has segment", component_filter="segment", protocol_filter={"SMART"}
)


@segment
async def test_feature(dev: SmartDevice) -> None:
"""Test that the strip length feature is registered and reads the device value."""
segment_module = dev.modules[Module.Segment]

feat = dev.features["strip_segments"]
assert feat.value == segment_module.segments
assert isinstance(feat.value, int)
assert (feat.minimum_value, feat.maximum_value) == (SEGMENTS_MIN, SEGMENTS_MAX)


@segment
async def test_set_segments(dev: SmartDevice, mocker: MockerFixture) -> None:
"""Test that setting the length calls the device with the right payload."""
segment_module = dev.modules[Module.Segment]
call_spy = mocker.spy(segment_module, "call")

await segment_module.set_segments(47)

call_spy.assert_called_once_with("set_device_segment", {"segment": 47})


@segment
@pytest.mark.parametrize("value", [SEGMENTS_MIN - 1, SEGMENTS_MAX + 1, 1.5, "47"])
async def test_set_segments_out_of_range(dev: SmartDevice, value: object) -> None:
"""Test that invalid lengths are rejected before reaching the device."""
segment_module = dev.modules[Module.Segment]

with pytest.raises(ValueError, match="Invalid segment count"):
await segment_module.set_segments(value) # type: ignore[arg-type]


@segment
async def test_set_segments_via_feature(
dev: SmartDevice, mocker: MockerFixture
) -> None:
"""Test that the feature setter reaches the module."""
segment_module = dev.modules[Module.Segment]
call_spy = mocker.spy(segment_module, "call")

await dev.features["strip_segments"].set_value(SEGMENTS_MAX)

call_spy.assert_called_once_with("set_device_segment", {"segment": SEGMENTS_MAX})
Loading

Back | FazBrowse Home | New Git URL