Skip to content

Commit

Permalink
fix some error cases
Browse files Browse the repository at this point in the history
  • Loading branch information
mampfes committed Jan 7, 2024
1 parent 87aba4f commit d8b67d5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
16 changes: 7 additions & 9 deletions custom_components/epex_spot_sensor/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,17 +247,16 @@ def _update_state_for_intermittent(
):
marketdata = get_marketdata_from_sensor_attrs(self._sensor_attributes)

(intervals, total_price) = calc_intervals_for_intermittent(
intervals = calc_intervals_for_intermittent(
marketdata=marketdata,
earliest_start=earliest_start,
latest_end=latest_end,
duration=self._duration,
most_expensive=self._price_mode == PriceModes.MOST_EXPENSIVE.value,
)

if len(intervals) == 0:
self._state = None
self._intervals = []
if intervals is None:
return

self._state = is_now_in_intervals(now, intervals)

Expand All @@ -267,14 +266,16 @@ def _update_state_for_intermittent(
# do calculation only if latest_end is limited to 24h from earliest_start,
# --> avoid calculation if latest_end includes all available marketdata
latest_end += timedelta(days=1)
(intervals2, _) = calc_intervals_for_intermittent(
intervals2 = calc_intervals_for_intermittent(
marketdata=marketdata,
earliest_start=earliest_start,
latest_end=latest_end,
duration=self._duration,
most_expensive=self._price_mode == PriceModes.MOST_EXPENSIVE.value,
)
intervals = [*intervals, *intervals2]

if intervals2 is not None:
intervals = [*intervals, *intervals2]

self._intervals = [
{
Expand All @@ -292,9 +293,6 @@ def _update_state_for_contigous(
):
marketdata = get_marketdata_from_sensor_attrs(self._sensor_attributes)

if len(marketdata) == 0:
return

result = calc_interval_for_contiguous(
marketdata,
earliest_start=earliest_start,
Expand Down
6 changes: 6 additions & 0 deletions custom_components/epex_spot_sensor/contiguous_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ def calc_interval_for_contiguous(
duration: timedelta,
most_expensive: bool = True,
):
if len(marketdata) == 0:
return None

if marketdata[-1].end_time < latest_end:
return None

start_times = _calc_start_times(
marketdata,
earliest_start=earliest_start,
Expand Down
10 changes: 7 additions & 3 deletions custom_components/epex_spot_sensor/intermittent_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ def calc_intervals_for_intermittent(
most_expensive: bool = False,
):
"""Calculate price for given start time and duration."""
if len(marketdata) == 0:
return None

if marketdata[-1].end_time < latest_end:
return None

# filter intervals which fit to start- and end-time (including overlapping)
marketdata = [
e
Expand All @@ -57,7 +63,6 @@ def calc_intervals_for_intermittent(

active_time: timedelta = timedelta(seconds=0)
intervals = []
total_price: float = None

for count, mp in enumerate(marketdata):
interval_start_time = (
Expand Down Expand Up @@ -87,12 +92,11 @@ def calc_intervals_for_intermittent(
)

active_time += active_duration_in_this_segment
total_price = price if total_price is None else total_price + price

if active_time == duration:
break

return (intervals, total_price)
return intervals


def is_now_in_intervals(now: datetime, intervals):
Expand Down

0 comments on commit d8b67d5

Please sign in to comment.