Skip to content

Commit

Permalink
Add status entity
Browse files Browse the repository at this point in the history
  • Loading branch information
dala318 committed Dec 20, 2024
1 parent f63bba1 commit db9f023
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
15 changes: 15 additions & 0 deletions custom_components/nordpool_planner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
CONF_TYPE_STATIC,
CONF_USED_HOURS_LOW_ENTITY,
DOMAIN,
PlannerStates,
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -226,6 +227,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

# Output states
self.low_cost_state = NordpoolPlannerState()
Expand Down Expand Up @@ -253,6 +255,11 @@ def price_now(self) -> str:
"""Current price from source sensor."""
return self._prices_entity.current_price_attr

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

@property
def _duration(self) -> int:
"""Get duration parameter."""
Expand Down Expand Up @@ -408,18 +415,22 @@ def update(self):
return

if not self._prices_entity.valid:
self._state = PlannerStates.Warning
_LOGGER.warning("Aborting update since no valid Nordpool data")
return

if not self._duration:
self._state = PlannerStates.Warning
_LOGGER.warning("Aborting update since no valid Duration")
return

if self._is_moving and not self._search_length:
self._state = PlannerStates.Warning
_LOGGER.warning("Aborting update since no valid Search length")
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")
return

Expand All @@ -430,6 +441,7 @@ def update(self):
if self.low_hours >= self._duration:
_LOGGER.debug("No need to update, quota of hours fulfilled")
self.set_done_for_now()
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
# duration = dt.timedelta(hours=1)
Expand Down Expand Up @@ -544,6 +556,7 @@ 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 @@ -573,6 +586,7 @@ 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 @@ -589,6 +603,7 @@ 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
14 changes: 14 additions & 0 deletions custom_components/nordpool_planner/const.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
"""Common constants for integration."""

from enum import Enum

DOMAIN = "nordpool_planner"


class PlannerStates(Enum):
"""Standard numeric identifiers for planner states."""

Ok = 0
Idle = 1
Warning = 2
Error = 3
Unknown = 4


CONF_TYPE = "type"
CONF_TYPE_MOVING = "moving"
CONF_TYPE_STATIC = "static"
CONF_TYPE_LIST = [CONF_TYPE_MOVING, CONF_TYPE_STATIC]
CONF_PRICES_ENTITY = "prices_entity"
CONF_LOW_COST_ENTITY = "low_cost_entity"
CONF_HEALTH_ENTITY = "health_entity"
CONF_HIGH_COST_ENTITY = "high_cost_entity"
CONF_STARTS_AT_ENTITY = "starts_at_entity"
CONF_DURATION_ENTITY = "duration_entity"
Expand Down
27 changes: 27 additions & 0 deletions custom_components/nordpool_planner/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

from . import NordpoolPlanner, NordpoolPlannerEntity
from .const import (
CONF_HEALTH_ENTITY,
CONF_HIGH_COST_ENTITY,
CONF_LOW_COST_ENTITY,
CONF_STARTS_AT_ENTITY,
CONF_USED_HOURS_LOW_ENTITY,
DOMAIN,
PlannerStates,
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -48,6 +50,13 @@
entity_category=EntityCategory.DIAGNOSTIC,
)

HEALTH_ENTITY_DESCRIPTION = SensorEntityDescription(
key=CONF_HEALTH_ENTITY,
device_class=SensorDeviceClass.ENUM,
entity_category=EntityCategory.DIAGNOSTIC,
options=[e.name for e in PlannerStates],
)


async def async_setup_entry(
hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities
Expand Down Expand Up @@ -82,6 +91,15 @@ async def async_setup_entry(
)
)

# if config_entry.data.get(CONF_HEALTH_ENTITY):
if True:
entities.append(
NordpoolPlannerHealthSensor(
planner,
entity_description=HEALTH_ENTITY_DESCRIPTION,
)
)

async_add_entities(entities)
return True

Expand Down Expand Up @@ -185,3 +203,12 @@ async def async_added_to_hass(self) -> None:
def native_value(self):
"""Output state."""
return self._planner.low_hours


class NordpoolPlannerHealthSensor(NordpoolPlannerSensor, RestoreSensor):
"""Start at specific sensor."""

@property
def native_value(self):
"""Output state."""
return self._planner.planner_state.name

0 comments on commit db9f023

Please sign in to comment.