Skip to content

Commit

Permalink
Tidy up RunQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
rgc99 committed Mar 16, 2022
1 parent 0cb52f1 commit 9b81880
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions custom_components/irrigation_unlimited/irrigation_unlimited.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Irrigation Unlimited Coordinator and sub classes"""
# pylint: disable=too-many-lines
import typing
import weakref
from datetime import datetime, time, timedelta
from types import MappingProxyType
Expand Down Expand Up @@ -261,11 +260,6 @@ def __eq__(self, other) -> bool:
def __hash__(self) -> int:
return self._uid

@property
def uid(self) -> str:
"""Return our unique id"""
return self._uid

@property
def index(self) -> int:
"""Return position within siblings"""
Expand Down Expand Up @@ -782,8 +776,7 @@ def as_dict(self) -> OrderedDict:
return result


# class IURunQueue(list[IURun]): # python 3.9
class IURunQueue(typing.List[IURun]):
class IURunQueue(List[IURun]):
"""Irrigation Unlimited class to hold the upcoming runs"""

# pylint: disable=too-many-public-methods
Expand Down Expand Up @@ -904,28 +897,28 @@ def clear(self, stime: datetime) -> bool:
self._sorted = True
return modified

def find_last_index(self, uid: int) -> int:
def find_last_index(self, schedule: IUSchedule) -> int:
"""Return the index of the run that finishes last in the queue.
This routine does not require the list to be sorted."""
result: int = None
last_time: datetime = None
for i, run in enumerate(self):
if run.schedule is not None and run.schedule.uid == uid:
if run.schedule is not None and run.schedule == schedule:
if last_time is None or run.end_time > last_time:
last_time = run.end_time
result = i
return result

def find_last_run(self, uid: int) -> IURun:
"""Find the last run for the matching uid"""
i = self.find_last_index(uid)
def find_last_run(self, schedule: IUSchedule) -> IURun:
"""Find the last run for the matching schedule"""
i = self.find_last_index(schedule)
if i is not None:
return self[i]
return None

def find_last_date(self, uid: int) -> datetime:
"""Find the last time in the queue for the supplied uid"""
run = self.find_last_run(uid)
def find_last_date(self, schedule: IUSchedule) -> datetime:
"""Find the last time in the queue for the supplied schedule"""
run = self.find_last_run(schedule)
if run is not None:
return run.end_time
return None
Expand Down Expand Up @@ -1120,7 +1113,7 @@ def merge_one(

# See if schedule already exists in run queue. If so get
# the finish time of the last entry.
next_time = self.find_last_date(schedule.uid)
next_time = self.find_last_date(schedule)
if next_time is not None:
next_time += granularity_time()
else:
Expand Down Expand Up @@ -2633,7 +2626,7 @@ def init_run_time(
total_duration: timedelta,
) -> datetime:
if schedule is not None:
next_time = zone.runs.find_last_date(schedule.uid)
next_time = zone.runs.find_last_date(schedule)
if next_time is not None:
next_time += granularity_time()
else:
Expand Down

0 comments on commit 9b81880

Please sign in to comment.