Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions apps/predbat/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import uuid
import traceback

from datetime import datetime
from component_base import ComponentBase

try:
Expand Down Expand Up @@ -412,7 +411,7 @@ def _inject_entities(self, status):
if status.timestamp > 0 and len(status.inverters) > 0:
primary_inv = next((inv for inv in status.inverters if inv.primary), status.inverters[0])
ts_suffix = primary_inv.serial[-6:].lower() if len(primary_inv.serial) > 6 else primary_inv.serial.lower()
dt = datetime.fromtimestamp(status.timestamp)
dt = datetime.datetime.fromtimestamp(status.timestamp)
self.set_state_wrapper(
Comment on lines 411 to 415
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The datetime-shadowing bug was in a runtime path (_publish_predbat_data/inject*), but the current gateway unit tests don’t exercise _inject_entities / _inject_inverter_entities (timestamp formatting) or _check_token_refresh (ISO timestamp parsing). Consider adding a small regression test that constructs a minimal GatewayMQTT instance and calls these methods with a sample GatewayStatus / expires_at string to ensure they don’t raise and produce expected values.

Copilot generated this review using guidance from repository custom instructions.
f"sensor.{self.prefix}_gateway_{ts_suffix}_inverter_time",
dt.strftime("%Y-%m-%d %H:%M:%S"),
Expand Down Expand Up @@ -508,9 +507,7 @@ def _inject_inverter_entities(self, inv, suffix):

# Inverter time (from GatewayStatus timestamp for clock drift detection)
if self._last_status and self._last_status.timestamp:
from datetime import datetime, timezone

dt = datetime.fromtimestamp(self._last_status.timestamp, tz=timezone.utc)
dt = datetime.datetime.fromtimestamp(self._last_status.timestamp, tz=datetime.timezone.utc)
self.set_state_wrapper(f"sensor.{pfx}_inverter_time", dt.strftime("%Y-%m-%d %H:%M:%S"))

# Battery scaling (depth of discharge) — from firmware pct, apps.yaml override, or 0.95 default
Expand Down Expand Up @@ -1066,7 +1063,7 @@ async def _check_token_refresh(self):
if isinstance(data["expires_at"], (int, float)):
self.mqtt_token_expires_at = float(data["expires_at"])
else:
dt = datetime.fromisoformat(data["expires_at"].replace("Z", "+00:00"))
dt = datetime.datetime.fromisoformat(data["expires_at"].replace("Z", "+00:00"))
self.mqtt_token_expires_at = dt.timestamp()
except (ValueError, AttributeError):
self.mqtt_token_expires_at = 0
Expand Down
Loading