From 9a827d27cd19d5ddf7043fc85ddddebcaf44d74e Mon Sep 17 00:00:00 2001 From: sdb9696 Date: Thu, 6 Jun 2024 16:59:58 +0100 Subject: [PATCH 1/2] Add state features to iot strip sockets --- kasa/iot/iotstrip.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/kasa/iot/iotstrip.py b/kasa/iot/iotstrip.py index 7c6368b02..e0b8c89d9 100755 --- a/kasa/iot/iotstrip.py +++ b/kasa/iot/iotstrip.py @@ -10,6 +10,7 @@ from ..device_type import DeviceType from ..deviceconfig import DeviceConfig from ..exceptions import KasaException +from ..feature import Feature from ..module import Module from ..protocol import BaseProtocol from .iotdevice import ( @@ -125,6 +126,8 @@ async def update(self, update_children: bool = True): ) for child in children } + for child in self._children.values(): + await child._initialize_features() if update_children and self.has_emeter: for plug in self.children: @@ -250,6 +253,34 @@ async def _initialize_modules(self): await super()._initialize_modules() self.add_module("time", Time(self, "time")) + async def _initialize_features(self): + """Initialize common features.""" + self._add_feature( + Feature( + self, + id="state", + name="State", + attribute_getter="is_on", + attribute_setter="set_state", + type=Feature.Type.Switch, + category=Feature.Category.Primary, + ) + ) + self._add_feature( + Feature( + device=self, + id="on_since", + name="On since", + attribute_getter="on_since", + icon="mdi:clock", + ) + ) + + for module in self._modules.values(): + module._initialize_features() + for module_feat in module._module_features.values(): + self._add_feature(module_feat) + async def update(self, update_children: bool = True): """Query the device to update the data. From 998f869017c70c257608cc56cf312ca58fbdfa44 Mon Sep 17 00:00:00 2001 From: sdb9696 Date: Thu, 6 Jun 2024 17:52:04 +0100 Subject: [PATCH 2/2] Add test and remove code missing coverage --- kasa/iot/iotstrip.py | 8 +++----- kasa/tests/test_strip.py | 8 ++++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/kasa/iot/iotstrip.py b/kasa/iot/iotstrip.py index e0b8c89d9..dde57faaf 100755 --- a/kasa/iot/iotstrip.py +++ b/kasa/iot/iotstrip.py @@ -275,11 +275,9 @@ async def _initialize_features(self): icon="mdi:clock", ) ) - - for module in self._modules.values(): - module._initialize_features() - for module_feat in module._module_features.values(): - self._add_feature(module_feat) + # If the strip plug has it's own modules we should call initialize + # features for the modules here. However the _initialize_modules function + # above does not seem to be called. async def update(self, update_children: bool = True): """Query the device to update the data. diff --git a/kasa/tests/test_strip.py b/kasa/tests/test_strip.py index e5285accb..4c576d1b2 100644 --- a/kasa/tests/test_strip.py +++ b/kasa/tests/test_strip.py @@ -88,6 +88,14 @@ async def test_get_plug_by_index(dev: IotStrip): dev.get_plug_by_index(len(dev.children)) +@strip +async def test_plug_features(dev: IotStrip): + """Test the child plugs have default features.""" + for child in dev.children: + assert "state" in child.features + assert "on_since" in child.features + + @pytest.mark.skip("this test will wear out your relays") async def test_all_binary_states(dev): # test every binary state