evsemaster 1.2.2__tar.gz → 1.2.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: evsemaster
3
- Version: 1.2.2
3
+ Version: 1.2.3
4
4
  Summary: Python implementation of the EVSEMaster App
5
5
  License: MIT
6
6
  Author: Rafaël Schridi
@@ -269,24 +269,35 @@ class SimpleEVSEProtocol:
269
269
 
270
270
  def _datetime_to_shanghai_epoch(self, dt: datetime) -> int:
271
271
  """
272
- The EVSE handles time weirdly, time interpretation is always in Asia/Shanghai timezone.
273
- So any time object you give it it will convert to Asia/Shanghai.
274
- So we always convert to Asia/Shanghai here.
272
+ The EVSE handles time weirdly,
273
+ It expects the time to be whatever the requested time would be in Asia/Shanghai timezone.
274
+ While setting the time we can just swap the timezone to Asia/Shanghai and get the epoch from that.
275
275
  """
276
276
  shanghai_tz = zoneinfo.ZoneInfo("Asia/Shanghai")
277
- if dt.tzinfo is None:
278
- dt = dt.replace(tzinfo=shanghai_tz)
279
- else:
280
- dt = dt.astimezone(shanghai_tz)
277
+ dt = dt.replace(tzinfo=shanghai_tz)
281
278
  return int(dt.timestamp())
282
279
 
283
280
  def _shanghai_epoch_to_datetime(self, epoch: int) -> datetime:
284
281
  """
285
- Similarly we receive the time as epoch seconds but always in Asia/Shanghai timezone.
282
+ This EVSE returns timestamps in the weirdest way possible.
283
+ The timestamps it gives you back are TZ converted to our TZ but the time is all wrong
284
+ Because it gives the time it gives back is the time our epoch would happen IN SHANGHAI
285
+ So we need to calculate the offset between Shanghai and our local timezone
286
+ and apply that to the naive datetime we get from the epoch.
286
287
  """
287
288
  shanghai_tz = zoneinfo.ZoneInfo("Asia/Shanghai")
288
- dt = datetime.fromtimestamp(epoch).replace(tzinfo=shanghai_tz)
289
- return dt
289
+ local_tz = datetime.now().astimezone().tzinfo
290
+
291
+ now = datetime.now()
292
+ shanghai_now = now.replace(tzinfo=shanghai_tz)
293
+ local_now = now.replace(tzinfo=local_tz)
294
+
295
+ # Calculate the offset between Shanghai and local timezone, from their pov
296
+ offset = shanghai_now.utcoffset() - local_now.utcoffset()
297
+
298
+ naive_dt = datetime.fromtimestamp(epoch)
299
+ corrected_dt = naive_dt + offset
300
+ return corrected_dt.replace(tzinfo=local_tz)
290
301
 
291
302
  async def set_nickname(self, nickname: str) -> bool:
292
303
  """Set the EVSE nickname."""
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "evsemaster"
3
- version = "1.2.2"
3
+ version = "1.2.3"
4
4
  description = "Python implementation of the EVSEMaster App"
5
5
  authors = [
6
6
  "Rafaël Schridi <rafael@schridi.nl>"
File without changes
File without changes