mirror of
https://github.com/python-kasa/python-kasa.git
synced 2026-10-07 13:23:55 +00:00
Fall back to UTC for iot time when the device clock is not set (#1768)
A new or factory-reset iot device can report a clock far in the past, e.g. year 2000, which causes a failure in timezone autodetection logic as the difference can be in years. This causes `update()` to fail, so every cli command fails, including `wifi scan` and `wifi join`, which are the commands needed to provision the device. To fix this, this changes the logic fall back to UTC if the device clock is not set, detected by checking if the timedelta is too large. Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -513,6 +513,40 @@ async def test_time_post_update_uses_offset_when_index_missing_unit(
|
||||
assert inst.timezone.utcoffset(now) == timedelta(0)
|
||||
|
||||
|
||||
@device_iot
|
||||
async def test_time_post_update_unsynced_clock_uses_utc(
|
||||
dev: Device, mocker: MockerFixture
|
||||
):
|
||||
"""Fall back to UTC when the device clock is not set.
|
||||
|
||||
An unprovisioned device can report e.g. year 2000, which is no valid UTC offset
|
||||
away from the host time, so the offset-based guess must not be attempted.
|
||||
"""
|
||||
from zoneinfo import ZoneInfoNotFoundError
|
||||
|
||||
proto = dev.protocol._transport.proto # type: ignore[attr-defined]
|
||||
for target in ("time", "smartlife.iot.common.timesetting"):
|
||||
if target in proto:
|
||||
proto[target]["get_time"] = {
|
||||
"year": 2000,
|
||||
"month": 1,
|
||||
"mday": 1,
|
||||
"hour": 2,
|
||||
"min": 45,
|
||||
"sec": 0,
|
||||
}
|
||||
# Force the offset-based path, as when the zone is not available on the host
|
||||
mocker.patch(
|
||||
"kasa.iot.modules.time.get_timezone",
|
||||
new=AsyncMock(side_effect=ZoneInfoNotFoundError("missing on host")),
|
||||
)
|
||||
|
||||
await dev.update()
|
||||
time_mod = dev.modules[Module.Time]
|
||||
assert time_mod.timezone is UTC
|
||||
assert time_mod.time.year == 2000
|
||||
|
||||
|
||||
async def test_time_get_time_exception_returns_none_unit(mocker: MockerFixture):
|
||||
"""Cover Time.get_time exception path (unit test of iot Time)."""
|
||||
from kasa.iot.modules.time import Time as TimeModule
|
||||
|
||||
Reference in New Issue
Block a user