Skip to content

Commit

Permalink
add rank+quantile values for whole day to extra-attributes
Browse files Browse the repository at this point in the history
fix #74
  • Loading branch information
mampfes committed Jan 4, 2024
1 parent 90c89f1 commit 3c79270
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions custom_components/epex_spot/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,7 @@ def extra_state_attributes(self):
for e in self._source.marketdata
]

self._attr_extra_state_attributes = {
ATTR_DATA: data,
}
return {ATTR_DATA: data}


class EpexSpotSellVolumeSensorEntity(EpexSpotEntity, SensorEntity):
Expand Down Expand Up @@ -186,9 +184,7 @@ def extra_state_attributes(self):
for e in self._source.marketdata
]

self._attr_extra_state_attributes = {
ATTR_DATA: data,
}
return {ATTR_DATA: data}


class EpexSpotVolumeSensorEntity(EpexSpotEntity, SensorEntity):
Expand Down Expand Up @@ -220,9 +216,7 @@ def extra_state_attributes(self):
for e in self._source.marketdata
]

self._attr_extra_state_attributes = {
ATTR_DATA: data,
}
return {ATTR_DATA: data}


class EpexSpotRankSensorEntity(EpexSpotEntity, SensorEntity):
Expand All @@ -245,6 +239,22 @@ def native_value(self) -> StateType:
e.price_eur_per_mwh for e in self._source.sorted_marketdata_today
].index(self._source.marketdata_now.price_eur_per_mwh)

@property
def extra_state_attributes(self):
sorted_prices = [
e.price_eur_per_mwh for e in self._source.sorted_marketdata_today
]
data = [
{
ATTR_START_TIME: e.start_time.isoformat(),
ATTR_END_TIME: e.end_time.isoformat(),
"rank": sorted_prices.index(e.price_eur_per_mwh),
}
for e in self._source.sorted_marketdata_today
]

return {ATTR_DATA: data}


class EpexSpotQuantileSensorEntity(EpexSpotEntity, SensorEntity):
"""Home Assistant sensor containing all EPEX spot data."""
Expand All @@ -267,6 +277,21 @@ def native_value(self) -> StateType:
max_price = self._source.sorted_marketdata_today[-1].price_eur_per_mwh
return (current_price - min_price) / (max_price - min_price)

@property
def extra_state_attributes(self):
min_price = self._source.sorted_marketdata_today[0].price_eur_per_mwh
max_price = self._source.sorted_marketdata_today[-1].price_eur_per_mwh
data = [
{
ATTR_START_TIME: e.start_time.isoformat(),
ATTR_END_TIME: e.end_time.isoformat(),
"quantile": (e.price_eur_per_mwh - min_price) / (max_price - min_price),
}
for e in self._source.sorted_marketdata_today
]

return {ATTR_DATA: data}


class EpexSpotLowestPriceSensorEntity(EpexSpotEntity, SensorEntity):
"""Home Assistant sensor containing all EPEX spot data."""
Expand Down

0 comments on commit 3c79270

Please sign in to comment.