Skip to content

Commit

Permalink
Adort static as done if duration eq. spent-hours
Browse files Browse the repository at this point in the history
  • Loading branch information
dala318 committed Dec 19, 2024
1 parent b26cc0d commit 229a56f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
28 changes: 26 additions & 2 deletions custom_components/nordpool_planner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,14 @@ def update(self):

# initialize local variables
now = dt_util.now()
duration = dt.timedelta(hours=self._duration - 1)

if self._is_static and self.low_hours is not None:
if self.low_hours >= self._duration:
_LOGGER.debug("No need to update, quota of hours fulfilled")
self.set_done_for_now()
duration = dt.timedelta(hours=max(0, self._duration - self.low_hours) - 1)
else:
duration = dt.timedelta(hours=self._duration - 1)

# Initiate states and variables for Moving planner
if self._is_moving:
Expand Down Expand Up @@ -471,6 +478,7 @@ def update(self):
if not prices_group.valid:
continue
# TODO: Should not end up here, why?
# Does end up here now when (duration - hours_low) return zero
prices_groups.append(prices_group)

if len(prices_groups) == 0:
Expand Down Expand Up @@ -559,9 +567,25 @@ def set_highest_cost_state(self, prices_group: NordpoolPricesGroup) -> None:
self._prices_entity.current_price_attr / prices_group.average
)
else:
self.low_cost_state.now_cost_rate = STATE_UNAVAILABLE
self.high_cost_state.now_cost_rate = STATE_UNAVAILABLE
_LOGGER.debug("Wrote highest cost state: %s", self.high_cost_state)

def set_done_for_now(self) -> None:
"""Set output state to off."""
now_hour = dt_util.now().replace(minute=0, second=0, microsecond=0)
start_hour = now_hour.replace(hour=self._start_time)
if start_hour < now_hour():
start_hour += dt.timedelta(days=1)
self.low_cost_state.starts_at = start_hour
self.low_cost_state.cost_at = STATE_UNAVAILABLE
self.low_cost_state.now_cost_rate = STATE_UNAVAILABLE
self.high_cost_state.starts_at = start_hour
self.high_cost_state.cost_at = STATE_UNAVAILABLE
self.high_cost_state.now_cost_rate = STATE_UNAVAILABLE
_LOGGER.debug("Setting output states to unavailable")
for listener in self._output_listeners.values():
listener.update_callback()

def set_unavailable(self) -> None:
"""Set output state to unavailable."""
self.low_cost_state.starts_at = STATE_UNAVAILABLE
Expand Down
3 changes: 2 additions & 1 deletion custom_components/nordpool_planner/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,10 @@ async def async_added_to_hass(self) -> None:
if (
(last_state := await self.async_get_last_state()) is not None
and last_state.state not in (STATE_UNKNOWN, STATE_UNAVAILABLE)
and last_state.state.isdigit()
# and (extra_data := await self.async_get_last_sensor_data()) is not None
):
self._planner.low_hours = last_state.state
self._planner.low_hours = int(last_state.state)
else:
self._planner.low_hours = 0

Expand Down

0 comments on commit 229a56f

Please sign in to comment.