Skip to content

Commit 89416e4

Browse files
Refactor timestamp handling and improve JSON serialization
1 parent 291019a commit 89416e4

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

wattwise.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,8 @@ def get_consumption_forecast(self):
368368
timestamp = datetime.datetime.fromisoformat(timestamp_str)
369369
else:
370370
timestamp = timestamp_str
371-
timestamp = timestamp.astimezone(
372-
tzlocal.get_localzone()
373-
) # Convert to local time
374-
hour = timestamp.hour
371+
timestamp = timestamp.astimezone(tzlocal.get_localzone())
372+
slot = timestamp.hour * (60 // self.STEP_MINUTES) + (timestamp.minute // self.STEP_MINUTES)
375373
value_str = state.get("state", 0)
376374
if is_float(value_str):
377375
value = float(value_str)
@@ -424,6 +422,7 @@ def save_consumption_history(self, history_data):
424422
history_data (list): List of historical consumption data.
425423
"""
426424
try:
425+
427426
def make_json_serializable(obj):
428427
if isinstance(obj, dict):
429428
return {k: make_json_serializable(v) for k, v in obj.items()}
@@ -432,9 +431,10 @@ def make_json_serializable(obj):
432431
elif isinstance(obj, datetime.datetime):
433432
return obj.isoformat()
434433
else:
435-
return obj
434+
return obj
435+
436436
cleaned_data = make_json_serializable(history_data)
437-
437+
438438
with open(self.CONSUMPTION_HISTORY_FILE, "w") as f:
439439
json.dump(cleaned_data, f)
440440
filepath = os.path.abspath(self.CONSUMPTION_HISTORY_FILE)
@@ -471,7 +471,7 @@ def get_history_data(self, entity_id, start_time, end_time):
471471
interval_data = self.get_history(
472472
entity_id=entity_id,
473473
start_time=current_time_naive,
474-
end_time=next_hour_naive,
474+
end_time=next_time_naive,
475475
)
476476
if interval_data:
477477
history_data.extend(interval_data[0])

0 commit comments

Comments
 (0)