Skip to content

Commit

Permalink
Add next_schedule to vacuum timers (#712)
Browse files Browse the repository at this point in the history
* Add next_schedule to Timer

* Add poetry.lock

* Update poetry.lock

* Move self.timezone() out of loop

* Update miio/vacuumcontainers.py

Co-authored-by: Teemu R. <[email protected]>

* Update miio/vacuumcontainers.py

Co-authored-by: Teemu R. <[email protected]>

Co-authored-by: Teemu R. <[email protected]>
  • Loading branch information
MarBra and rytilahti authored May 30, 2020
1 parent 82f2b4d commit f9c396b
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 54 deletions.
3 changes: 2 additions & 1 deletion miio/vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,9 @@ def find(self):
def timer(self) -> List[Timer]:
"""Return a list of timers."""
timers = list()
timezone = self.timezone()
for rec in self.send("get_timer", [""]):
timers.append(Timer(rec))
timers.append(Timer(rec, timezone=timezone))

return timers

Expand Down
14 changes: 13 additions & 1 deletion miio/vacuumcontainers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from enum import IntEnum
from typing import Any, Dict, List

from croniter import croniter
from pytz import timezone

from .utils import pretty_seconds, pretty_time


Expand Down Expand Up @@ -401,12 +404,13 @@ class Timer:
The timers are accessed using an integer ID, which is based on the unix
timestamp of the creation time."""

def __init__(self, data: List[Any]) -> None:
def __init__(self, data: List[Any], timezone: str) -> None:
# id / timestamp, enabled, ['<cron string>', ['command', 'params']
# [['1488667794112', 'off', ['49 22 * * 6', ['start_clean', '']]],
# ['1488667777661', 'off', ['49 21 * * 3,4,5,6', ['start_clean', '']]
# ],
self.data = data
self.timezone = timezone

@property
def id(self) -> int:
Expand Down Expand Up @@ -434,6 +438,14 @@ def action(self) -> str:
Note, this seems to be always 'start'."""
return str(self.data[2][1])

@property
def next_schedule(self) -> datetime:
"""Next schedule for the timer."""
local_tz = timezone(self.timezone)
cron = croniter(self.cron, start_time=local_tz.localize(datetime.now()))

return cron.get_next(ret_type=datetime)

def __repr__(self) -> str:
return "<Timer %s: %s - enabled: %s - cron: %s>" % (
self.id,
Expand Down
Loading

0 comments on commit f9c396b

Please sign in to comment.