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:
Ahmed Hassan
2026-10-04 18:52:54 +05:00
committed by GitHub
parent b2a15afa7e
commit 9030179aad
2 changed files with 40 additions and 1 deletions

View File

@@ -35,7 +35,7 @@ class Time(IotModule, TimeInterface):
If the configured zone is not available on this host, compute the device's
current UTC offset and choose a best-match available zone, preferring DST-
observing candidates when the original index implies DST. As a last resort,
use a fixed-offset timezone.
use a fixed-offset timezone. If the device clock is not set, use UTC.
"""
if res := self.data.get("get_timezone"):
idx = res.get("index")
@@ -58,6 +58,11 @@ class Time(IotModule, TimeInterface):
now_utc = datetime.now(UTC)
delta = device_local - now_utc.replace(tzinfo=None)
rounded = timedelta(seconds=60 * round(delta.total_seconds() / 60))
# UTC offsets range from -12h to +14h, anything beyond that means the
# device clock is not set (e.g. before provisioning).
if abs(rounded) > timedelta(hours=14):
self._timezone = UTC
return
dst_expected = None
if res := self.data.get("get_timezone"):