Skip to content

fix: replace critical error handling with warnings for missing entities values in Home Assistant monitors#70

Open
markoceri wants to merge 1 commit intodevfrom
fix/unvailable-entities-69
Open

fix: replace critical error handling with warnings for missing entities values in Home Assistant monitors#70
markoceri wants to merge 1 commit intodevfrom
fix/unvailable-entities-69

Conversation

@markoceri
Copy link
Collaborator

Problem

When fetching energy state or forecast data from Home Assistant, a single entity returning unavailable or unknown (e.g. sensor.battery_soc) caused the entire data retrieval to fail and return None. This meant:

  • Optimization cycles were skipped entirely, even when most sensor data was valid.
  • Frontend real-time updates were blocked due to a single temporarily unavailable sensor.
  • The system was fragile against common HA transient states (restarts, integration reloads, Zigbee/Z-Wave timeouts).

The root cause was a has_critical_error flag that was set to True for any configured-but-unavailable entity, triggering return None and discarding all successfully retrieved data — despite the domain model already supporting partial data (EnergyStateSnapshot.battery and .grid are Optional).

This PR closes #69

Solution

Removed the has_critical_error mechanism entirely. Each entity failure is now handled independently with graceful degradation:

Entity Previous behavior New behavior
consumption Critical → return None Logs error, defaults to 0W, snapshot still created
production Critical → return None Logs warning, defaults to 0W
grid Critical → return None Logs warning, GridState set to None
battery_soc / battery_power Critical → return None Logs warning, BatteryState set to None
energy_today / energy_tomorrow (forecast) Critical → return None Logs warning, partial forecast returned

The snapshot is now always constructed with whatever data is available. Missing optional components result in None sub-objects, which downstream consumers (policy rules, decisional context, frontend) already handle.

Changes

adapters/domain/energy/monitors/home_assistant_api.py

  • Removed has_critical_error flag and the early return None block.
  • Grid unavailability: changed from has_critical_error = True to a warning log. GridState will be None.
  • Battery unavailability: changed from has_critical_error = True to a warning log. BatteryState will be None.
  • Production unavailability: changed from error to warning log. Defaults to 0W.
  • Consumption unavailability: logs error but no longer blocks the snapshot.

adapters/domain/energy/monitors/home_assistant_mqtt.py

  • Removed has_critical_error flag and the early return None block.
  • Solar production, grid power, battery SOC/power: all changed from critical errors to warning logs.
  • Consumption: logs error but snapshot is still created.

adapters/domain/forecast/providers/home_assistant_api.py

  • Removed has_critical_error flag and the early return None block.
  • energy_today and energy_tomorrow: changed from critical errors to warning logs.
  • Forecast is always returned with whatever data is available.

Testing

  • Verified that when all HA entities respond correctly, the snapshot is unchanged.
  • Verified that when a single entity (e.g. battery) returns unavailable, the snapshot is still created with battery=None and all other fields populated.
  • Verified that warning/error logs are correctly emitted for each unavailable entity.

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.

1 participant