Skip to content

Commit

Permalink
Provide detailed forecast in sensor attribute
Browse files Browse the repository at this point in the history
Fixes #1

Signed-off-by: rany <ranygh@riseup.net>
  • Loading branch information
rany2 committed Jun 9, 2024
1 parent 0d901b8 commit e96c43e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
5 changes: 4 additions & 1 deletion custom_components/open_meteo_solar_forecast/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@
CONF_AZIMUTH = "azimuth"
CONF_MODULES_POWER = "modules_power"
CONF_INVERTER_POWER = "inverter_power"
CONF_EFFICIENCY_FACTOR = "efficiency_factor"
CONF_EFFICIENCY_FACTOR = "efficiency_factor"

ATTR_WATTS = "watts"
ATTR_WH_PERIOD = "wh_period"
13 changes: 13 additions & 0 deletions custom_components/open_meteo_solar_forecast/recorder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Integration platform for recorder."""

from __future__ import annotations

from homeassistant.core import HomeAssistant, callback

from .const import ATTR_WATTS, ATTR_WH_PERIOD


@callback
def exclude_attributes(hass: HomeAssistant) -> set[str]:
"""Exclude potentially large attributes from being recorded in the database."""
return {ATTR_WATTS, ATTR_WH_PERIOD}
40 changes: 39 additions & 1 deletion custom_components/open_meteo_solar_forecast/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

from open_meteo_solar_forecast.models import Estimate

from .const import DOMAIN
from .const import ATTR_WATTS, ATTR_WH_PERIOD, DOMAIN
from .coordinator import OpenMeteoSolarForecastDataUpdateCoordinator


Expand Down Expand Up @@ -290,3 +290,41 @@ def native_value(self) -> datetime | StateType:
state = self.entity_description.state(self.coordinator.data)

return state

@property
def extra_state_attributes(self) -> dict[str, Any] | None:
"""Return the state attributes."""
if self.entity_description.key.startswith(
"energy_production_d"
) or self.entity_description.key in (
"energy_production_today",
"energy_production_tomorrow",
):
target_date = self.coordinator.data.now().date()
if self.entity_description.key == "energy_production_tomorrow":
target_date += timedelta(days=1)
elif self.entity_description.key.startswith("energy_production_d"):
target_date += timedelta(
days=int(self.entity_description.key[len("energy_production_d") :])
)
elif self.entity_description.key == "energy_production_today":
pass # target_date is already set to today
else:
raise ValueError(
f"Unexpected key {self.entity_description.key} for extra_state_attributes"
)

return {
ATTR_WATTS: {
watt_datetime.isoformat(): watt_value
for watt_datetime, watt_value in self.coordinator.data.watts.items()
if watt_datetime.date() == target_date
},
ATTR_WH_PERIOD: {
wh_datetime.isoformat(): wh_value
for wh_datetime, wh_value in self.coordinator.data.wh_period.items()
if wh_datetime.date() == target_date
},
}

return None

0 comments on commit e96c43e

Please sign in to comment.