diff --git a/custom_components/irrigation_unlimited/const.py b/custom_components/irrigation_unlimited/const.py index 2d23d46..d9f2099 100644 --- a/custom_components/irrigation_unlimited/const.py +++ b/custom_components/irrigation_unlimited/const.py @@ -122,6 +122,7 @@ CONF_USER = "user" CONF_TOGGLE = "toggle" CONF_EXTENDED_CONFIG = "extended_config" +CONF_READ_DELAY = "read_delay" # Defaults DEFAULT_NAME = DOMAIN diff --git a/custom_components/irrigation_unlimited/history.py b/custom_components/irrigation_unlimited/history.py index 5546158..0ab5f15 100644 --- a/custom_components/irrigation_unlimited/history.py +++ b/custom_components/irrigation_unlimited/history.py @@ -25,6 +25,7 @@ CONF_HISTORY, CONF_HISTORY_REFRESH, CONF_HISTORY_SPAN, + CONF_READ_DELAY, CONF_REFRESH_INTERVAL, CONF_SPAN, TIMELINE_ADJUSTMENT, @@ -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] = {} @@ -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: @@ -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: @@ -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) diff --git a/custom_components/irrigation_unlimited/schema.py b/custom_components/irrigation_unlimited/schema.py index 899430b..cdf3205 100644 --- a/custom_components/irrigation_unlimited/schema.py +++ b/custom_components/irrigation_unlimited/schema.py @@ -92,6 +92,7 @@ CONF_USER, CONF_TOGGLE, CONF_EXTENDED_CONFIG, + CONF_READ_DELAY, ) IU_ID = r"^[a-z0-9]+(_[a-z0-9]+)*$" @@ -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, } )