Skip to content

Commit

Permalink
Add remustering
Browse files Browse the repository at this point in the history
  • Loading branch information
rgc99 committed Dec 9, 2024
1 parent dd0410c commit dbc4bd3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
24 changes: 22 additions & 2 deletions custom_components/irrigation_unlimited/irrigation_unlimited.py
Original file line number Diff line number Diff line change
Expand Up @@ -5945,6 +5945,7 @@ def __init__(
self._fixed_clock = False
self._show_log = False
self._finalised = False
self.schedule_immediately = False

@property
def is_fixed(self) -> bool:
Expand Down Expand Up @@ -5994,6 +5995,10 @@ def next_awakening(self, atime: datetime) -> datetime:
if self._fixed_clock:
return atime + self.track_interval()

if self.schedule_immediately:
self.schedule_immediately = False
return atime

# Handle testing
if self._coordinator.tester.is_testing:
next_stime = self._coordinator.next_awakening()
Expand Down Expand Up @@ -6142,6 +6147,7 @@ def __init__(self, hass: HomeAssistant) -> None:
self._history = IUHistory(self._hass, self.service_history)
self._restored_from_configuration: bool = False
self._finalised = False
self._muster_status: IURQStatus = IURQStatus.NONE

@property
def hass(self) -> HomeAssistant:
Expand Down Expand Up @@ -6223,6 +6229,11 @@ def rename_entities(self) -> bool:
"""Indicate if entity renaming is allowed"""
return self._rename_entities

@property
def muster_required(self) -> bool:
"""Return status that muster is pending"""
return self._muster_required

def _is_setup(self) -> bool:
"""Wait for sensors to be setup"""
all_setup: bool = self._hass.is_running and self._component is not None
Expand Down Expand Up @@ -6387,9 +6398,11 @@ def poll(self, vtime: datetime, force: bool = False) -> None:
it is the actual time"""
wtime: datetime = wash_dt(vtime)
if (wtime != self._last_muster) or self._muster_required or force:
if not self.muster(wtime, force).is_empty():
self.check_run(wtime)
self._muster_required = False
self._muster_status |= self.muster(wtime, force)
if not self._muster_status.is_empty() and not self._muster_required:
self.check_run(wtime)
self._muster_status = IURQStatus.NONE
self._last_muster = wtime
self.update_sensor(vtime)

Expand Down Expand Up @@ -6457,6 +6470,13 @@ def _replay_last_timer(self, atime: datetime) -> None:
self.timer(tick)
self._clock.rearm(atime)

def remuster(self) -> None:
"""Schedule a muster immediately. This is used during a muster to flag
another pass is required. This occurs when schedules or other parameters
are modified within a muster"""
self._muster_required = True
self._clock.schedule_immediately = True

def next_awakening(self) -> datetime:
"""Return the next event time"""
dates: list[datetime] = [utc_eot()]
Expand Down
5 changes: 4 additions & 1 deletion tests/iu_test_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,10 @@ async def run_until(self, stop_at: datetime | str) -> None:
while self._coordinator.tester.is_testing and self._current_time <= astop_at:
# Get next scheduled clock tick
next_awakening = self._coordinator.clock.next_awakening(self._current_time)
assert next_awakening != self._current_time, "Infinite loop detected"
assert (
self._coordinator.muster_required
or next_awakening != self._current_time
), "Infinite loop detected"
self._coordinator.clock.test_ticker_update(next_awakening)
await self._hass.async_block_till_done()
self._coordinator.tester.ticker = min(next_awakening, astop_at)
Expand Down

0 comments on commit dbc4bd3

Please sign in to comment.