Skip to content

Commit

Permalink
Fix hour bug and valid_tomorrow
Browse files Browse the repository at this point in the history
  • Loading branch information
Hellowlol committed Nov 11, 2022
1 parent 6ac27fc commit ecce460
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions custom_components/nordpool/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def __init__(
self._off_peak_1 = None
self._off_peak_2 = None
self._peak = None
self._additional_costs_value = None

_LOGGER.debug("Template %s", str(ad_template))
# Check incase the sensor was setup using config flow.
Expand Down Expand Up @@ -238,6 +239,10 @@ def device_info(self):
def state(self) -> float:
return self.current_price

@property
def additional_costs(self):
return self._additional_costs_value

@property
def low_price(self) -> bool:
"""Check if the price is lower then avg depending on settings"""
Expand Down Expand Up @@ -267,7 +272,7 @@ def _calc_price(self, value=None, fake_dt=None) -> float:

def faker():
def inner(*args, **kwargs):
return fake_dt
return fake_dt or dt_utils.now()

return pass_context(inner)

Expand All @@ -277,15 +282,15 @@ def inner(*args, **kwargs):
template_value = self._ad_template.async_render(
now=faker(), current_price=price
)
# _LOGGER.debug("Template value are %s", template_value)
self._additional_costs_value = template_value

price += template_value
else:
price = value / _PRICE_IN[self._price_type] * (float(1 + self._vat))
template_value = self._ad_template.async_render(
now=faker(), current_price=price
)
# _LOGGER.debug("Template value are %s", template_value)
self._additional_costs_value = template_value
price += template_value

# Convert price to cents if specified by the user.
Expand All @@ -311,7 +316,6 @@ def _update(self, data) -> None:
self._off_peak_2 = self._calc_price(data.get("Off-peak 2"))
self._peak = self._calc_price(data.get("Peak"))


@property
def current_price(self) -> float:
res = self._calc_price()
Expand Down Expand Up @@ -385,6 +389,7 @@ def extra_state_attributes(self) -> dict:
"tomorrow_valid": self.tomorrow_valid,
"raw_today": self.raw_today,
"raw_tomorrow": self.raw_tomorrow,
"additional_costs_current_hour": self.additional_costs,
}

def _add_raw(self, data):
Expand All @@ -408,8 +413,9 @@ def raw_tomorrow(self):

@property
def tomorrow_valid(self):
"""Verify that we have the values for tomorrow."""
# this should be checked a better way
return len(self.tomorrow) >= 23
return len([i for i in self.tomorrow if i not in (None, float("inf"))]) >= 23

async def _update_current_price(self) -> None:
"""update the current price (price this hour)"""
Expand All @@ -434,14 +440,19 @@ async def check_stuff(self) -> None:
self._last_tick = dt_utils.now()

if self._data_today is None:
_LOGGER.debug("NordpoolSensor _data_today is none, trying to fetch it. %s", self.name)
_LOGGER.debug(
"NordpoolSensor _data_today is none, trying to fetch it. %s", self.name
)
today = await self._api.today(self._area, self._currency)
if today:
self._data_today = today
self._update(today)

if self._data_tomorrow is None:
_LOGGER.debug("NordpoolSensor _data_tomorrow is none, trying to fetch it. %s", self.name)
_LOGGER.debug(
"NordpoolSensor _data_tomorrow is none, trying to fetch it. %s",
self.name,
)
tomorrow = await self._api.tomorrow(self._area, self._currency)
if tomorrow:
self._data_tomorrow = tomorrow
Expand Down Expand Up @@ -476,6 +487,4 @@ async def async_added_to_hass(self):
await super().async_added_to_hass()
_LOGGER.debug("called async_added_to_hass %s", self.name)
async_dispatcher_connect(self._api._hass, EVENT_NEW_DATA, self.check_stuff)

await self.check_stuff()

0 comments on commit ecce460

Please sign in to comment.