Skip to content

fix: datetime import shadow breaks gateway price data#3696

Merged
springfall2008 merged 1 commit intomainfrom
fix/gateway-datetime-shadow
Mar 29, 2026
Merged

fix: datetime import shadow breaks gateway price data#3696
springfall2008 merged 1 commit intomainfrom
fix/gateway-datetime-shadow

Conversation

@mgazza
Copy link
Copy Markdown
Collaborator

@mgazza mgazza commented Mar 29, 2026

Summary

  • gateway.py has both import datetime (line 10) and from datetime import datetime (line 18)
  • The second import shadows the first, so datetime becomes the class not the module
  • All datetime.datetime.*, datetime.timezone.*, and datetime.timedelta() calls fail with: type object 'datetime.datetime' has no attribute 'datetime'
  • This crashes _publish_predbat_data() every 30 seconds, preventing price/timeline data from reaching gateway devices

Fix

  • Remove the shadowing from datetime import datetime import
  • Use datetime.datetime.* consistently (4 call sites fixed)

Test plan

  • ruff + black pass
  • Verify price data publishes to gateway device after fix

🤖 Generated with Claude Code

`from datetime import datetime` on line 18 shadows `import datetime`
on line 10, causing all `datetime.datetime.*`, `datetime.timezone.*`
and `datetime.timedelta()` calls to fail with:
  "type object 'datetime.datetime' has no attribute 'datetime'"

This breaks `_publish_predbat_data()` which prevents price/timeline
data from reaching gateway devices.

Fix: remove the shadowing import and use `datetime.datetime.*`
consistently throughout.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a crash in the GatewayMQTT component caused by datetime module/class shadowing, which prevented gateway devices from receiving published price/timeline data.

Changes:

  • Removed from datetime import datetime that shadowed the datetime module.
  • Updated affected call sites to use datetime.datetime.* / datetime.timezone.* explicitly.
  • Removed an inner datetime/timezone import and replaced it with module-qualified usage.

Comment on lines 411 to 415
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(
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.
@springfall2008 springfall2008 merged commit b9cf48b into main Mar 29, 2026
5 checks passed
@springfall2008 springfall2008 deleted the fix/gateway-datetime-shadow branch March 29, 2026 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants