Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ The following devices have been tested and confirmed as working. If your device

- **Plugs**: EP10, EP25<sup>\*</sup>, HS100<sup>\*\*</sup>, HS103, HS105, HS110, KP100, KP105, KP115, KP125, KP125M<sup>\*</sup>, KP401
- **Power Strips**: EP40, HS107, HS300, KP200, KP303, KP400
- **Wall Switches**: ES20M, HS200, HS210, HS220, KP405, KS200M, KS205<sup>\*</sup>, KS220M, KS225<sup>\*</sup>, KS230
- **Wall Switches**: ES20M, HS200, HS210, HS220, KP405, KS200M, KS205<sup>\*</sup>, KS220M, KS225<sup>\*</sup>, KS230, KS240<sup>\*</sup>
- **Bulbs**: KL110, KL120, KL125, KL130, KL135, KL50, KL60, LB110
- **Light Strips**: KL400L5, KL420L5, KL430
- **Hubs**: KH100<sup>\*</sup>
Expand Down
3 changes: 3 additions & 0 deletions SUPPORTED.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ Some newer Kasa devices require authentication. These are marked with <sup>*</su
- Hardware: 1.0 (US) / Firmware: 1.0.2<sup>\*</sup>
- **KS230**
- Hardware: 1.0 (US) / Firmware: 1.0.14
- **KS240**
- Hardware: 1.0 (US) / Firmware: 1.0.4<sup>\*</sup>
- Hardware: 1.0 (US) / Firmware: 1.0.5<sup>\*</sup>

### Bulbs

Expand Down
4 changes: 4 additions & 0 deletions kasa/smart/modules/brightness.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,7 @@ async def set_brightness(self, brightness: int):
)

return await self.call("set_device_info", {"brightness": brightness})

async def _check_supported(self):
"""Additional check to see if the module is supported by the device."""
return "brightness" in self.data
4 changes: 4 additions & 0 deletions kasa/smart/modules/fanmodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@ def sleep_mode(self) -> bool:
async def set_sleep_mode(self, on: bool):
"""Set sleep mode."""
return await self.call("set_device_info", {"fan_sleep_mode_on": on})

async def _check_supported(self):
"""Is the module available on this device."""
return "fan_speed_level" in self.data
12 changes: 12 additions & 0 deletions kasa/smart/modules/lighttransitionmodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ def turn_on_transition(self) -> int:

Available only from v2.
"""
if "fade_on_time" in self._device.sys_info:
return self._device.sys_info["fade_on_time"]
return self._turn_on["duration"]

@property
Expand Down Expand Up @@ -138,6 +140,8 @@ def turn_off_transition(self) -> int:

Available only from v2.
"""
if "fade_off_time" in self._device.sys_info:
return self._device.sys_info["fade_off_time"]
return self._turn_off["duration"]

@property
Expand Down Expand Up @@ -166,3 +170,11 @@ async def set_turn_off_transition(self, seconds: int):
"set_on_off_gradually_info",
{"off_state": {**self._turn_on, "duration": seconds}},
)

def query(self) -> dict:
"""Query to execute during the update cycle."""
# Some devices have the required info in the device info.
if "gradually_on_mode" in self._device.sys_info:
return {}
else:
return {self.QUERY_GETTER_NAME: None}
30 changes: 28 additions & 2 deletions kasa/smart/smartdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
if TYPE_CHECKING:
from .smartmodule import SmartModule

# List of modules that wall switches with children, i.e. ks240 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, TimeModule, Firmware, CloudModule] # noqa: F405


class SmartDevice(Device):
"""Base class to represent a SMART protocol based device."""
Expand Down Expand Up @@ -78,6 +84,9 @@ async def _initialize_children(self):
@property
def children(self) -> Sequence[SmartDevice]:
"""Return list of children."""
# Wall switches with children report all modules on the parent only
if self.device_type == DeviceType.WallSwitch:
return []
return list(self._children.values())

def _try_get_response(self, responses: dict, request: str, default=None) -> dict:
Expand Down Expand Up @@ -162,17 +171,32 @@ async def _initialize_modules(self):
"""Initialize modules based on component negotiation response."""
from .smartmodule import SmartModule

# Some wall switches (like ks240) are internally presented as having child
# devices which report the child's components on the parent's sysinfo, even
# when they need to be accessed through the children.
# The logic below ensures that such devices report all but whitelisted, the
# child modules at the parent level to create an illusion of a single device.
if self._parent and self._parent.device_type == DeviceType.WallSwitch:
modules = self._parent.modules
skip_parent_only_modules = True
else:
modules = self.modules
skip_parent_only_modules = False

for mod in SmartModule.REGISTERED_MODULES.values():
_LOGGER.debug("%s requires %s", mod, mod.REQUIRED_COMPONENT)

if skip_parent_only_modules and mod in WALL_SWITCH_PARENT_ONLY_MODULES:
continue
if mod.REQUIRED_COMPONENT in self._components:
_LOGGER.debug(
"Found required %s, adding %s to modules.",
mod.REQUIRED_COMPONENT,
mod.__name__,
)
module = mod(self, mod.REQUIRED_COMPONENT)
if await module._check_supported():
self.modules[module.name] = module
if module.name not in modules and await module._check_supported():
modules[module.name] = module

async def _initialize_features(self):
"""Initialize device features."""
Expand Down Expand Up @@ -568,6 +592,8 @@ def _get_device_type_from_components(
return DeviceType.Plug
if "light_strip" in components:
return DeviceType.LightStrip
if "SWITCH" in device_type and "child_device" in components:
return DeviceType.WallSwitch
if "dimmer_calibration" in components:
return DeviceType.Dimmer
if "brightness" in components:
Expand Down
1 change: 1 addition & 0 deletions kasa/tests/device_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
SWITCHES_SMART = {
"KS205",
"KS225",
"KS240",
"S500D",
"S505",
}
Expand Down
21 changes: 20 additions & 1 deletion kasa/tests/fakeprotocol_smart.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,29 @@ def _handle_control_child(self, params: dict):

# We only support get & set device info for now.
if child_method == "get_device_info":
return {"result": info, "error_code": 0}
result = copy.deepcopy(info)
return {"result": result, "error_code": 0}
elif child_method == "set_device_info":
info.update(child_params)
return {"error_code": 0}
elif (
# FIXTURE_MISSING is for service calls not in place when
# SMART fixtures started to be generated
missing_result := self.FIXTURE_MISSING_MAP.get(child_method)
) and missing_result[0] in self.components:
result = copy.deepcopy(missing_result[1])
retval = {"result": result, "error_code": 0}
return retval
else:
# PARAMS error returned for KS240 when get_device_usage called
# on parent device. Could be any error code though.
# TODO: Try to figure out if there's a way to prevent the KS240 smartdevice
# calling the unsupported device in the first place.
retval = {
"error_code": SmartErrorCode.PARAMS_ERROR.value,
"method": child_method,
}
return retval

raise NotImplementedError(
"Method %s not implemented for children" % child_method
Expand Down
Loading