Skip to content
Merged
Show file tree
Hide file tree
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
29 changes: 15 additions & 14 deletions edge_mining/adapters/domain/energy/monitors/home_assistant_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,6 @@ def get_current_energy_state(self) -> Optional[EnergyStateSnapshot]:
if self.logger:
self.logger.debug("Fetching current energy state from Home Assistant...")
now = Timestamp(datetime.now())
has_critical_error = False

# --- Production ---
if self.entity_production:
Expand Down Expand Up @@ -374,33 +373,35 @@ def get_current_energy_state(self) -> Optional[EnergyStateSnapshot]:
else:
grid_watts = None
if self.entity_grid:
has_critical_error = True # Grid is usually important
if self.logger:
self.logger.warning(
f"Could not retrieve grid value (Entity: {self.entity_grid}). "
"Continuing without grid data."
)

# Battery: We want positive for CHARGING, negative for DISCHARGING
if battery_power_raw is not None:
battery_power = battery_power_raw if self.battery_positive_charge else -battery_power_raw
else:
battery_power = None
# Only critical if battery SOC is also configured
if self.entity_battery_soc and self.entity_battery_power:
has_critical_error = True
if self.logger:
self.logger.warning(
f"Could not retrieve battery power value "
f"(Entity: {self.entity_battery_power}). "
"Continuing without battery data."
)

# Check if essential values are missing
if production_watts is None and self.entity_production:
if self.logger:
self.logger.error(f"Missing critical value: Production (Entity: {self.entity_production})")
has_critical_error = True
self.logger.warning(
f"Could not retrieve production value (Entity: {self.entity_production}). "
"Defaulting to 0W."
)
if consumption_watts is None and self.entity_consumption:
if self.logger:
self.logger.error(f"Missing critical value: House Consumption (Entity: {self.entity_consumption})")
has_critical_error = True

if has_critical_error:
if self.logger:
self.logger.error(
"Failed to retrieve one or more critical energy values from Home Assistant. Cannot create snapshot."
)
return None

reading_timestamp = now

Expand Down
35 changes: 15 additions & 20 deletions edge_mining/adapters/domain/energy/monitors/home_assistant_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@ def get_current_energy_state(self) -> Optional[EnergyStateSnapshot]:

now = datetime.now(timezone.utc)
snapshot_time = Timestamp(now.astimezone()) # Convert to local timezone for snapshot
has_critical_error = False
is_stale = False

# Get the latest values and check if they are stale
Expand All @@ -359,39 +358,35 @@ def get_current_energy_state(self) -> Optional[EnergyStateSnapshot]:
# Check if critical data is missing (never received)
if self.topics_map.get("solar_production") and production is None:
if self.logger:
self.logger.error(
f"Missing critical value: Solar Production (Topic: {self.topics_map['solar_production']})"
self.logger.warning(
f"Could not retrieve Solar Production (Topic: {self.topics_map['solar_production']}). "
"Defaulting to 0W."
)
has_critical_error = True
if self.topics_map.get("house_consumption") and consumption is None:
if self.logger:
self.logger.error(
f"Missing critical value: House Consumption (Topic: {self.topics_map['house_consumption']})"
)
has_critical_error = True
if self.topics_map.get("grid_power") and grid_power is None:
if self.logger:
self.logger.error(f"Missing critical value: Grid Power (Topic: {self.topics_map['grid_power']})")
has_critical_error = True
# Battery is critical only if both SOC and Power are required and missing
self.logger.warning(
f"Could not retrieve Grid Power (Topic: {self.topics_map['grid_power']}). "
"Continuing without grid data."
)
# Battery: log warning if configured but missing
if self.topics_map.get("battery_soc") and self.topics_map.get("battery_power"):
if battery_soc is None:
if self.logger:
self.logger.error(f"Missing critical value: Battery SOC (Topic: {self.topics_map['battery_soc']})")
has_critical_error = True
self.logger.warning(
f"Could not retrieve Battery SOC (Topic: {self.topics_map['battery_soc']}). "
"Continuing without battery data."
)
if battery_power is None:
if self.logger:
self.logger.error(
f"Missing critical value: Battery Power (Topic: {self.topics_map['battery_power']})"
self.logger.warning(
f"Could not retrieve Battery Power (Topic: {self.topics_map['battery_power']}). "
"Continuing without battery data."
)
has_critical_error = True

if has_critical_error:
if self.logger:
self.logger.error(
"One or more critical energy values were never received via MQTT. Cannot create snapshot."
)
return None

if is_stale:
if self.logger:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ def get_forecast(self) -> Optional[Forecast]:
"""Fetches the energy production forecast."""
if self.logger:
self.logger.debug("Fetching forecast energy state from Home Assistant...")
has_critical_error = False

# --- Actual Power h ---
if self.entity_forecast_power_actual_h:
Expand Down Expand Up @@ -433,30 +432,22 @@ def get_forecast(self) -> Optional[Forecast]:
else:
energy_remaining_today = None

# Check if essential values are missing
# Check if essential values are missing - log warnings but continue with partial data
if energy_today is None and self.entity_forecast_energy_today:
if self.logger:
self.logger.error(
f"Missing critical value: Solar Production (Entity: {self.entity_forecast_energy_today})"
self.logger.warning(
f"Could not retrieve forecast energy today (Entity: {self.entity_forecast_energy_today}). "
"Continuing with partial forecast data."
)
has_critical_error = True
if energy_tomorrow is None and self.entity_forecast_energy_tomorrow:
if self.logger:
self.logger.error(
f"Missing critical value: Solar Production (Entity: {self.entity_forecast_energy_tomorrow})"
self.logger.warning(
f"Could not retrieve forecast energy tomorrow (Entity: {self.entity_forecast_energy_tomorrow}). "
"Continuing with partial forecast data."
)
has_critical_error = True

# Add here other checks for critical values as needed

if has_critical_error:
if self.logger:
self.logger.error(
"Failed to retrieve one or more critical energy values "
"from Home Assistant. Cannot create forecast data."
)
return None

now = Timestamp(datetime.now())
actual_hour = datetime.now().replace(minute=0, second=0, microsecond=0)
end_of_today = datetime.combine(now, time.max)
Expand Down