Skip to content

Do not expose child modules on parent devices #964

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 1 commit into from
Jun 10, 2024
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
17 changes: 1 addition & 16 deletions kasa/smart/smartdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ def __init__(
self._components: dict[str, int] = {}
self._state_information: dict[str, Any] = {}
self._modules: dict[str | ModuleName[Module], SmartModule] = {}
self._exposes_child_modules = False
self._parent: SmartDevice | None = None
self._children: Mapping[str, SmartDevice] = {}
self._last_update = {}
Expand Down Expand Up @@ -99,16 +98,6 @@ def children(self) -> Sequence[SmartDevice]:
@property
def modules(self) -> ModuleMapping[SmartModule]:
"""Return the device modules."""
if self._exposes_child_modules:
modules = {k: v for k, v in self._modules.items()}
for child in self._children.values():
for k, v in child._modules.items():
if k not in modules:
modules[k] = v
if TYPE_CHECKING:
return cast(ModuleMapping[SmartModule], modules)
return modules

if TYPE_CHECKING: # Needed for python 3.8
return cast(ModuleMapping[SmartModule], self._modules)
return self._modules
Expand Down Expand Up @@ -213,7 +202,6 @@ async def _initialize_modules(self):
skip_parent_only_modules = True
elif self._children and self.device_type == DeviceType.WallSwitch:
# _initialize_modules is called on the parent after the children
self._exposes_child_modules = True
for child in self._children.values():
child_modules_to_skip.update(**child.modules)

Expand Down Expand Up @@ -332,10 +320,7 @@ async def _initialize_features(self):
)

for module in self.modules.values():
# Check if module features have already been initialized.
# i.e. when _exposes_child_modules is true
if not module._module_features:
module._initialize_features()
module._initialize_features()
for feat in module._module_features.values():
self._add_feature(feat)
for child in self._children.values():
Expand Down
13 changes: 13 additions & 0 deletions kasa/tests/device_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,16 @@ async def dev(request) -> AsyncGenerator[Device, None]:
yield dev

await dev.disconnect()


def get_parent_and_child_modules(device: Device, module_name):
"""Return iterator of module if exists on parent and children.

Useful for testing devices that have components listed on the parent that are only
supported on the children, i.e. ks240.
"""
if module_name in device.modules:
yield device.modules[module_name]
for child in device.children:
if module_name in child.modules:
yield child.modules[module_name]
6 changes: 3 additions & 3 deletions kasa/tests/smart/features/test_brightness.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

from kasa.iot import IotDevice
from kasa.smart import SmartDevice
from kasa.tests.conftest import dimmable_iot, parametrize
from kasa.tests.conftest import dimmable_iot, get_parent_and_child_modules, parametrize

brightness = parametrize("brightness smart", component_filter="brightness")


@brightness
async def test_brightness_component(dev: SmartDevice):
"""Test brightness feature."""
brightness = dev.modules.get("Brightness")
brightness = next(get_parent_and_child_modules(dev, "Brightness"))
assert brightness
assert isinstance(dev, SmartDevice)
assert "brightness" in dev._components

# Test getting the value
feature = dev.features["brightness"]
feature = brightness._device.features["brightness"]
assert isinstance(feature.value, int)
assert feature.value > 1 and feature.value <= 100

Expand Down
13 changes: 6 additions & 7 deletions kasa/tests/smart/modules/test_fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@

from kasa import Module
from kasa.smart import SmartDevice
from kasa.tests.device_fixtures import parametrize
from kasa.tests.device_fixtures import get_parent_and_child_modules, parametrize

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


@fan
async def test_fan_speed(dev: SmartDevice, mocker: MockerFixture):
"""Test fan speed feature."""
fan = dev.modules.get(Module.Fan)
fan = next(get_parent_and_child_modules(dev, Module.Fan))
assert fan

level_feature = dev.features["fan_speed_level"]
level_feature = fan._module_features["fan_speed_level"]
assert (
level_feature.minimum_value
<= level_feature.value
Expand All @@ -36,9 +35,9 @@ async def test_fan_speed(dev: SmartDevice, mocker: MockerFixture):
@fan
async def test_sleep_mode(dev: SmartDevice, mocker: MockerFixture):
"""Test sleep mode feature."""
fan = dev.modules.get(Module.Fan)
fan = next(get_parent_and_child_modules(dev, Module.Fan))
assert fan
sleep_feature = dev.features["fan_sleep_mode"]
sleep_feature = fan._module_features["fan_sleep_mode"]
assert isinstance(sleep_feature.value, bool)

call = mocker.spy(fan, "call")
Expand All @@ -55,7 +54,7 @@ async def test_sleep_mode(dev: SmartDevice, mocker: MockerFixture):
async def test_fan_module(dev: SmartDevice, mocker: MockerFixture):
"""Test fan speed on device interface."""
assert isinstance(dev, SmartDevice)
fan = dev.modules.get(Module.Fan)
fan = next(get_parent_and_child_modules(dev, Module.Fan))
assert fan
device = fan._device

Expand Down
15 changes: 8 additions & 7 deletions kasa/tests/test_common_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
bulb_smart,
dimmable_iot,
dimmer_iot,
get_parent_and_child_modules,
lightstrip_iot,
parametrize,
parametrize_combine,
Expand Down Expand Up @@ -123,11 +124,11 @@ async def test_light_effect_module(dev: Device, mocker: MockerFixture):
async def test_light_brightness(dev: Device):
"""Test brightness setter and getter."""
assert isinstance(dev, Device)
light = dev.modules.get(Module.Light)
light = next(get_parent_and_child_modules(dev, Module.Light))
assert light

# Test getting the value
feature = dev.features["brightness"]
feature = light._device.features["brightness"]
assert feature.minimum_value == 0
assert feature.maximum_value == 100

Expand All @@ -146,7 +147,7 @@ async def test_light_brightness(dev: Device):
async def test_light_set_state(dev: Device):
"""Test brightness setter and getter."""
assert isinstance(dev, Device)
light = dev.modules.get(Module.Light)
light = next(get_parent_and_child_modules(dev, Module.Light))
assert light

await light.set_state(LightState(light_on=False))
Expand All @@ -169,11 +170,11 @@ async def test_light_set_state(dev: Device):
@light_preset
async def test_light_preset_module(dev: Device, mocker: MockerFixture):
"""Test light preset module."""
preset_mod = dev.modules[Module.LightPreset]
preset_mod = next(get_parent_and_child_modules(dev, Module.LightPreset))
assert preset_mod
light_mod = dev.modules[Module.Light]
light_mod = next(get_parent_and_child_modules(dev, Module.Light))
assert light_mod
feat = dev.features["light_preset"]
feat = preset_mod._device.features["light_preset"]

preset_list = preset_mod.preset_list
assert "Not set" in preset_list
Expand Down Expand Up @@ -220,7 +221,7 @@ async def test_light_preset_module(dev: Device, mocker: MockerFixture):
@light_preset
async def test_light_preset_save(dev: Device, mocker: MockerFixture):
"""Test saving a new preset value."""
preset_mod = dev.modules[Module.LightPreset]
preset_mod = next(get_parent_and_child_modules(dev, Module.LightPreset))
assert preset_mod
preset_list = preset_mod.preset_list
if len(preset_list) == 1:
Expand Down
8 changes: 3 additions & 5 deletions kasa/tests/test_smartdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from .conftest import (
device_smart,
get_device_for_fixture_protocol,
get_parent_and_child_modules,
)


Expand Down Expand Up @@ -144,11 +145,8 @@ async def test_get_modules():

# Modules on child
module = dummy_device.modules.get("Fan")
assert module
assert module._device != dummy_device
assert module._device._parent == dummy_device

module = dummy_device.modules.get(Module.Fan)
assert module is None
module = next(get_parent_and_child_modules(dummy_device, "Fan"))
assert module
assert module._device != dummy_device
assert module._device._parent == dummy_device
Expand Down
Loading