Skip to content

Commit

Permalink
Work around DST issue
Browse files Browse the repository at this point in the history
  • Loading branch information
wlcrs committed Mar 31, 2024
1 parent 99682ca commit c12585b
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/huawei_solar/registers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from enum import IntEnum
from functools import partial
from inspect import isclass
import time
import typing as t

from pymodbus.payload import BinaryPayloadBuilder, BinaryPayloadDecoder
Expand Down Expand Up @@ -241,8 +242,19 @@ def decode(self, decoder: BinaryPayloadDecoder, inverter: "AsyncHuaweiSolar"):
if value is None:
return None

if inverter.time_zone:
value = value - 60 * inverter.time_zone

# if DST is in effect, we need to shift another hour. However, the inverter
# does not expose a register to check that.
# Workaround: check the local time on the machine running the library if DST
# is in effect there. We assume that both inverter and client are in the same time zone.

if time.localtime(value).tm_isdst:
value = value - 60 * 60

try:
return datetime.fromtimestamp(value - 60 * inverter.time_zone, timezone.utc)
return datetime.fromtimestamp(value, timezone.utc)
except OverflowError as err:
raise DecodeError(f"Received invalid timestamp {value}") from err

Expand Down

0 comments on commit c12585b

Please sign in to comment.