Skip to content

Do not add parent only modules to strip sockets #963

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 2 commits 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
10 changes: 6 additions & 4 deletions kasa/smart/smartdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
_LOGGER = logging.getLogger(__name__)


# List of modules that wall switches with children, i.e. ks240 report on
# List of modules that non hub devices with children, i.e. ks240/P300, report on
# the child but only work on the parent. See longer note below in _initialize_modules.
# This list should be updated when creating new modules that could have the
# same issue, homekit perhaps?
WALL_SWITCH_PARENT_ONLY_MODULES = [DeviceModule, Time, Firmware, Cloud]
NON_HUB_PARENT_ONLY_MODULES = [DeviceModule, Time, Firmware, Cloud]


# Device must go last as the other interfaces also inherit Device
Expand Down Expand Up @@ -196,9 +196,11 @@ async def _initialize_modules(self):
# when they need to be accessed through the children.
# The logic below ensures that such devices add all but whitelisted, only on
# the child device.
# It also ensures that devices like power strips do not add modules such as
# firmware to the child devices.
skip_parent_only_modules = False
child_modules_to_skip = {}
if self._parent and self._parent.device_type == DeviceType.WallSwitch:
if self._parent and self._parent.device_type != DeviceType.Hub:
skip_parent_only_modules = True
elif self._children and self.device_type == DeviceType.WallSwitch:
# _initialize_modules is called on the parent after the children
Expand All @@ -209,7 +211,7 @@ async def _initialize_modules(self):
_LOGGER.debug("%s requires %s", mod, mod.REQUIRED_COMPONENT)

if (
skip_parent_only_modules and mod in WALL_SWITCH_PARENT_ONLY_MODULES
skip_parent_only_modules and mod in NON_HUB_PARENT_ONLY_MODULES
) or mod.__name__ in child_modules_to_skip:
continue
if (
Expand Down
18 changes: 18 additions & 0 deletions kasa/tests/device_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,24 @@ def parametrize_combine(parametrized: list[pytest.MarkDecorator]):
)


def parametrize_subtract(params: pytest.MarkDecorator, subtract: pytest.MarkDecorator):
"""Combine multiple pytest parametrize dev marks into one set of fixtures."""
if params.args[0] != "dev" or subtract.args[0] != "dev":
raise Exception(
f"Supplied mark is not for dev fixture: {params.args[0]} {subtract.args[0]}"
)
fixtures = []
for param in params.args[1]:
if param not in subtract.args[1]:
fixtures.append(param)
return pytest.mark.parametrize(
"dev",
sorted(fixtures),
indirect=True,
ids=idgenerator,
)


def parametrize(
desc,
*,
Expand Down
20 changes: 19 additions & 1 deletion kasa/tests/test_childdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@

import pytest

from kasa.device_type import DeviceType
from kasa.smart.smartchilddevice import SmartChildDevice
from kasa.smart.smartdevice import NON_HUB_PARENT_ONLY_MODULES
from kasa.smartprotocol import _ChildProtocolWrapper

from .conftest import strip_smart
from .conftest import parametrize, parametrize_subtract, strip_smart

has_children_smart = parametrize(
"has children", component_filter="control_child", protocol_filter={"SMART"}
)
hub_smart = parametrize(
"smart hub", device_type_filter=[DeviceType.Hub], protocol_filter={"SMART"}
)
non_hub_parent_smart = parametrize_subtract(has_children_smart, hub_smart)


@strip_smart
Expand Down Expand Up @@ -82,3 +92,11 @@ def _test_property_getters():
exceptions = list(_test_property_getters())
if exceptions:
raise ExceptionGroup("Accessing child properties caused exceptions", exceptions)


@non_hub_parent_smart
async def test_parent_only_modules(dev, dummy_protocol, mocker):
"""Test that parent only modules are not available on children."""
for child in dev.children:
for module in NON_HUB_PARENT_ONLY_MODULES:
assert module not in [type(module) for module in child.modules.values()]
Loading