Skip to content

Commit

Permalink
Merge pull request #84 from mampfes/helper_sensor
Browse files Browse the repository at this point in the history
Helper sensor
  • Loading branch information
mampfes authored Jan 7, 2024
2 parents dfd74cf + 5bacbf4 commit 0561b0a
Show file tree
Hide file tree
Showing 12 changed files with 888 additions and 18 deletions.
37 changes: 19 additions & 18 deletions custom_components/epex_spot/sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from statistics import median

import homeassistant.util.dt as dt_util
from homeassistant.components.sensor import (
SensorEntity,
SensorEntityDescription,
Expand Down Expand Up @@ -74,8 +75,8 @@ def native_value(self) -> StateType:
def extra_state_attributes(self):
data = [
{
ATTR_START_TIME: e.start_time.isoformat(),
ATTR_END_TIME: e.end_time.isoformat(),
ATTR_START_TIME: dt_util.as_local(e.start_time).isoformat(),
ATTR_END_TIME: dt_util.as_local(e.end_time).isoformat(),
self._localized.attr_name_per_mwh: e.price_eur_per_mwh,
self._localized.attr_name_per_kwh: to_ct_per_kwh(e.price_eur_per_mwh),
}
Expand Down Expand Up @@ -111,8 +112,8 @@ def native_value(self) -> StateType:
def extra_state_attributes(self):
data = [
{
ATTR_START_TIME: e.start_time.isoformat(),
ATTR_END_TIME: e.end_time.isoformat(),
ATTR_START_TIME: dt_util.as_local(e.start_time).isoformat(),
ATTR_END_TIME: dt_util.as_local(e.end_time).isoformat(),
self._localized.attr_name_per_kwh: self._source.to_net_price(
e.price_eur_per_mwh
),
Expand Down Expand Up @@ -145,8 +146,8 @@ def native_value(self) -> StateType:
def extra_state_attributes(self):
data = [
{
ATTR_START_TIME: e.start_time.isoformat(),
ATTR_END_TIME: e.end_time.isoformat(),
ATTR_START_TIME: dt_util.as_local(e.start_time).isoformat(),
ATTR_END_TIME: dt_util.as_local(e.end_time).isoformat(),
"buy_volume_mwh": e.buy_volume_mwh,
}
for e in self._source.marketdata
Expand Down Expand Up @@ -177,8 +178,8 @@ def native_value(self) -> StateType:
def extra_state_attributes(self):
data = [
{
ATTR_START_TIME: e.start_time.isoformat(),
ATTR_END_TIME: e.end_time.isoformat(),
ATTR_START_TIME: dt_util.as_local(e.start_time).isoformat(),
ATTR_END_TIME: dt_util.as_local(e.end_time).isoformat(),
"sell_volume_mwh": e.sell_volume_mwh,
}
for e in self._source.marketdata
Expand Down Expand Up @@ -209,8 +210,8 @@ def native_value(self) -> StateType:
def extra_state_attributes(self):
data = [
{
ATTR_START_TIME: e.start_time.isoformat(),
ATTR_END_TIME: e.end_time.isoformat(),
ATTR_START_TIME: dt_util.as_local(e.start_time).isoformat(),
ATTR_END_TIME: dt_util.as_local(e.end_time).isoformat(),
"volume_mwh": e.volume_mwh,
}
for e in self._source.marketdata
Expand Down Expand Up @@ -246,8 +247,8 @@ def extra_state_attributes(self):
]
data = [
{
ATTR_START_TIME: e.start_time.isoformat(),
ATTR_END_TIME: e.end_time.isoformat(),
ATTR_START_TIME: dt_util.as_local(e.start_time).isoformat(),
ATTR_END_TIME: dt_util.as_local(e.end_time).isoformat(),
"rank": sorted_prices.index(e.price_eur_per_mwh),
}
for e in self._source.sorted_marketdata_today
Expand Down Expand Up @@ -283,8 +284,8 @@ def extra_state_attributes(self):
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(),
ATTR_START_TIME: dt_util.as_local(e.start_time).isoformat(),
ATTR_END_TIME: dt_util.as_local(e.end_time).isoformat(),
"quantile": (e.price_eur_per_mwh - min_price) / (max_price - min_price),
}
for e in self._source.sorted_marketdata_today
Expand Down Expand Up @@ -317,8 +318,8 @@ def native_value(self) -> StateType:
def extra_state_attributes(self):
min = self._source.sorted_marketdata_today[0]
return {
ATTR_START_TIME: min.start_time.isoformat(),
ATTR_END_TIME: min.end_time.isoformat(),
ATTR_START_TIME: dt_util.as_local(min.start_time).isoformat(),
ATTR_END_TIME: dt_util.as_local(min.end_time).isoformat(),
self._localized.attr_name_per_kwh: to_ct_per_kwh(self.native_value),
}

Expand Down Expand Up @@ -347,8 +348,8 @@ def native_value(self) -> StateType:
def extra_state_attributes(self):
max = self._source.sorted_marketdata_today[-1]
return {
ATTR_START_TIME: max.start_time.isoformat(),
ATTR_END_TIME: max.end_time.isoformat(),
ATTR_START_TIME: dt_util.as_local(max.start_time).isoformat(),
ATTR_END_TIME: dt_util.as_local(max.end_time).isoformat(),
self._localized.attr_name_per_kwh: to_ct_per_kwh(self.native_value),
}

Expand Down
27 changes: 27 additions & 0 deletions custom_components/epex_spot_sensor/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""The EPEX Spot Sensor component."""
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up component from a config entry."""
await hass.config_entries.async_forward_entry_setups(
entry, (Platform.BINARY_SENSOR,)
)

entry.async_on_unload(entry.add_update_listener(config_entry_update_listener))

return True


async def config_entry_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Update listener, called when the config entry options are changed."""
await hass.config_entries.async_reload(entry.entry_id)


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
return await hass.config_entries.async_unload_platforms(
entry, (Platform.BINARY_SENSOR,)
)
Loading

0 comments on commit 0561b0a

Please sign in to comment.