Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finalize state-sensor #78

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 33 additions & 17 deletions custom_components/nordpool_planner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None:
# Local state variables
self._last_update = None
self.low_hours = None
self._state = PlannerStates.Unknown
self._planner_status = NordpoolPlannerStatus()

# Output states
self.low_cost_state = NordpoolPlannerState()
Expand Down Expand Up @@ -267,9 +267,9 @@ def price_now(self) -> str:
return self._prices_entity.current_price_attr

@property
def planner_state(self) -> PlannerStates:
"""Current price from source sensor."""
return self._state
def planner_status(self) -> NordpoolPlannerStatus:
"""Current planner status."""
return self._planner_status

@property
def _duration(self) -> int:
Expand Down Expand Up @@ -421,37 +421,52 @@ def update(self):
_LOGGER.debug("Updating planner")

# Update inputs
if not self._prices_entity.update(self._hass):
if not self._prices_entity.update(self._hass) and not self._prices_entity.valid:
self.set_unavailable()
return

if not self._prices_entity.valid:
self._state = PlannerStates.Warning
_LOGGER.warning("Aborting update since no valid Nordpool data")
self._planner_status.status = PlannerStates.Error
self._planner_status.running_text = "No valid Price data"
return

if not self._duration:
self._state = PlannerStates.Warning
_LOGGER.warning("Aborting update since no valid Duration")
self._planner_status.status = PlannerStates.Error
self._planner_status.running_text = "No valid Duration data"
return

if self._is_moving and not self._search_length:
self._state = PlannerStates.Warning
_LOGGER.warning("Aborting update since no valid Search length")
self._planner_status.status = PlannerStates.Error
self._planner_status.running_text = "No valid Search-Length data"
return

if self._is_static and not (self._start_time and self._end_time):
self._state = PlannerStates.Warning
_LOGGER.warning("Aborting update since no valid end time")
_LOGGER.warning("Aborting update since no valid Start or end time")
self._planner_status.status = PlannerStates.Error
self._planner_status.running_text = "No valid Start-Time or End-Time"
return

# If come this far no running error texts relevant (for now...)
self._planner_status.status = PlannerStates.Ok
self._planner_status.running_text = "ok"
self._planner_status.config_text = "ok"

if self._is_moving and self._search_length < self._duration:
self._planner_status.status = PlannerStates.Warning
self._planner_status.config_text = "Duration is Lager than Search-Length"

# if self._is_static and (self._end_time - self._start_time) < self._duration:
# self._planner_status.status = PlannerStates.Warning
# self._planner_status.config_text = "Duration is Lager than Search-Window"

# initialize local variables
now = dt_util.now()

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()
self._planner_status.status = PlannerStates.Idle
self._planner_status.running_text = "Quota of hours fulfilled"
return
duration = dt.timedelta(hours=max(0, self._duration - self.low_hours) - 1)
# TODO: Need to fix this so that the duration amount of hours are found in range for static
Expand Down Expand Up @@ -488,6 +503,8 @@ def update(self):
# Invalid planner type
else:
_LOGGER.warning("Aborting update since unknown planner type")
self._planner_status.status = PlannerStates.Error
self._planner_status.config_text = "Bad planner type"
return

prices_groups: list[NordpoolPricesGroup] = []
Expand All @@ -512,6 +529,8 @@ def update(self):
end_time,
duration,
)
self._planner_status.status = PlannerStates.Warning
self._planner_status.running_text = "No prices in active range"
return

_LOGGER.debug(
Expand Down Expand Up @@ -567,7 +586,6 @@ def update(self):
if end_time.hour == now.hour:
self.low_hours = 0
self._last_update = now
self._state = PlannerStates.Ok
for listener in self._output_listeners.values():
listener.update_callback()

Expand Down Expand Up @@ -597,7 +615,6 @@ def set_highest_cost_state(self, prices_group: NordpoolPricesGroup) -> None:

def set_done_for_now(self) -> None:
"""Set output state to off."""
self._state = PlannerStates.Idle
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():
Expand All @@ -614,7 +631,6 @@ def set_done_for_now(self) -> None:

def set_unavailable(self) -> None:
"""Set output state to unavailable."""
self._state = PlannerStates.Error
self.low_cost_state.starts_at = STATE_UNAVAILABLE
self.low_cost_state.cost_at = STATE_UNAVAILABLE
self.low_cost_state.now_cost_rate = STATE_UNAVAILABLE
Expand Down
17 changes: 8 additions & 9 deletions custom_components/nordpool_planner/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,12 @@ class NordpoolPlannerHealthSensor(NordpoolPlannerSensor, RestoreSensor):
@property
def native_value(self):
"""Output state."""
return self._planner.planner_state.name
return self._planner.planner_status.status.name

# @property
# def extra_state_attributes(self):
# """Extra state attributes."""
# state_attributes = {
# "running_state": STATE_UNKNOWN,
# "config_state": STATE_UNKNOWN,
# }
# return state_attributes
@property
def extra_state_attributes(self):
"""Extra state attributes."""
return {
"running_state": self._planner.planner_status.running_text,
"config_state": self._planner.planner_status.config_text,
}
Loading