Skip to content

Commit

Permalink
Add paramater to delay reading history after known event
Browse files Browse the repository at this point in the history
  • Loading branch information
rgc99 committed Oct 7, 2024
1 parent 191c022 commit 456277a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions custom_components/irrigation_unlimited/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
CONF_USER = "user"
CONF_TOGGLE = "toggle"
CONF_EXTENDED_CONFIG = "extended_config"
CONF_READ_DELAY = "read_delay"

# Defaults
DEFAULT_NAME = DOMAIN
Expand Down
11 changes: 7 additions & 4 deletions custom_components/irrigation_unlimited/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
CONF_HISTORY,
CONF_HISTORY_REFRESH,
CONF_HISTORY_SPAN,
CONF_READ_DELAY,
CONF_REFRESH_INTERVAL,
CONF_SPAN,
TIMELINE_ADJUSTMENT,
Expand Down Expand Up @@ -69,6 +70,7 @@ def __init__(self, hass: HomeAssistant, callback: Callable[[set[str]], None]):
self._history_span = timedelta(days=7)
self._refresh_interval = timedelta(seconds=120)
self._enabled = True
self._read_delay = timedelta(0)
# Private variables
self._history_last: datetime = None
self._cache: dict[str, Any] = {}
Expand All @@ -89,8 +91,8 @@ def _remove_refresh(self) -> None:

def _get_next_refresh_event(self, utc_time: datetime, force: bool) -> datetime:
"""Calculate the next event time."""
if self._history_last is None or force or not self._fixed_clock:
return utc_time
if self._history_last is None or force:
return utc_time + self._read_delay
return utc_time + self._refresh_interval

def _schedule_refresh(self, force: bool) -> None:
Expand All @@ -107,8 +109,7 @@ async def _async_handle_refresh_event(self, utc_time: datetime) -> None:
"""Handle history event."""
# pylint: disable=unused-argument
self._refresh_remove = None
if self._fixed_clock:
self._schedule_refresh(False)
self._schedule_refresh(False) # Perpetual worker
await self._async_update_history(self._stime)

def _initialise(self) -> bool:
Expand Down Expand Up @@ -252,6 +253,8 @@ def load(self, config: OrderedDict, fixed_clock: bool) -> "IUHistory":
self._enabled = hist_conf.get(CONF_ENABLED, self._enabled)
span_days = hist_conf.get(CONF_SPAN, span_days)
refresh_seconds = hist_conf.get(CONF_REFRESH_INTERVAL, refresh_seconds)
if (read_delay := hist_conf.get(CONF_READ_DELAY)) is not None:
self._read_delay = timedelta(seconds=read_delay)

if span_days is not None:
self._history_span = timedelta(days=span_days)
Expand Down
2 changes: 2 additions & 0 deletions custom_components/irrigation_unlimited/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
CONF_USER,
CONF_TOGGLE,
CONF_EXTENDED_CONFIG,
CONF_READ_DELAY,
)

IU_ID = r"^[a-z0-9]+(_[a-z0-9]+)*$"
Expand Down Expand Up @@ -315,6 +316,7 @@ def _parse_dd_mmm(value: str) -> date | None:
vol.Optional(CONF_ENABLED): cv.boolean,
vol.Optional(CONF_REFRESH_INTERVAL): cv.positive_int,
vol.Optional(CONF_SPAN): cv.positive_int,
vol.Optional(CONF_READ_DELAY): cv.positive_int,
}
)

Expand Down

0 comments on commit 456277a

Please sign in to comment.