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
3 changes: 2 additions & 1 deletion kasa/smart/modules/devicemodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def query(self) -> dict:
"get_device_info": None,
}
# Device usage is not available on older firmware versions
if self.supported_version >= 2:
# or child devices of hubs
if self.supported_version >= 2 and not self._device._is_hub_child:
query["get_device_usage"] = None

return query
9 changes: 9 additions & 0 deletions kasa/smart/modules/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,12 @@ async def set_time(self, dt: datetime) -> dict:
if region:
params["region"] = region
return await self.call("set_device_time", params)

async def _check_supported(self):
"""Additional check to see if the module is supported by the device.

Hub attached sensors report the time module but do return device time.
"""
if self._device._is_hub_child:
return False
return await super()._check_supported()
9 changes: 7 additions & 2 deletions kasa/smart/smartdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,11 @@ async def _initialize_features(self):
for child in self._children.values():
await child._initialize_features()

@property
def _is_hub_child(self) -> bool:
"""Returns true if the device is a child of a hub."""
return self.parent is not None and self.parent.device_type is DeviceType.Hub

@property
def is_cloud_connected(self) -> bool:
"""Returns if the device is connected to the cloud."""
Expand Down Expand Up @@ -485,8 +490,8 @@ def alias(self) -> str | None:
@property
def time(self) -> datetime:
"""Return the time."""
if (self._parent and (time_mod := self._parent.modules.get(Module.Time))) or (
time_mod := self.modules.get(Module.Time)
if (time_mod := self.modules.get(Module.Time)) or (
self._parent and (time_mod := self._parent.modules.get(Module.Time))
):
return time_mod.time

Expand Down
14 changes: 14 additions & 0 deletions kasa/tests/test_childdevice.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import inspect
import sys
from datetime import datetime, timezone

import pytest
from freezegun.api import FrozenDateTimeFactory

from kasa import Device
from kasa.device_type import DeviceType
Expand Down Expand Up @@ -120,3 +122,15 @@ async def test_parent_property(dev: Device):
assert dev.parent is None
for child in dev.children:
assert child.parent == dev


@has_children_smart
async def test_child_time(dev: Device, freezer: FrozenDateTimeFactory):
"""Test a child device gets the time from it's parent module."""
if not dev.children:
pytest.skip(f"Device {dev} fixture does not have any children")

fallback_time = datetime.now(timezone.utc).astimezone().replace(microsecond=0)
assert dev.parent is None
for child in dev.children:
assert child.time != fallback_time