Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement adding, deleting and updating the timer #78

Merged
merged 4 commits into from
Sep 16, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,22 @@ Timer #6, id 1488667697356 (ts: 2017-03-04 23:48:17.355999)
At 08:48 on the 5th of March
```

Adding a new timer
```
$ mirobo timer add --cron '* * * * *'
```

Activating/deactivating an existing timer,
use `mirobo timer` to get the required id.
```
$ mirobo timer update <id> [--off|--on]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You missed to update this line.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both are fixed now, thanks!

```

Deleting a timer
```
$ mirobo timer delete <id>
```

### Cleaning history

```
Expand Down
28 changes: 21 additions & 7 deletions mirobo/vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import math
import time
from typing import List
import enum

from .vacuumcontainers import (VacuumStatus, ConsumableStatus,
CleaningSummary, CleaningDetails, Timer)
Expand All @@ -14,6 +15,10 @@ class VacuumException(DeviceException):
pass


class TimerState(enum.Enum):
On = "on"
Off = "off"

class Vacuum(Device):
"""Main class representing the vacuum."""

Expand Down Expand Up @@ -139,13 +144,22 @@ def timer(self) -> List[Timer]:

return timers

def set_timer(self, details):
# how to create timers/change values?
# ['ts', 'on'] to enable
raise NotImplementedError()
# return self.send(
# "set_timer", [["ts", ["cron_line", ["start_clean", ""]]]])
# return self.send("upd_timer", ["ts", "on"])
def add_timer(self, cron: str, command: str, parameters: str):
"""Add a timer."""
import time
ts = int(round(time.time() * 1000))
return self.send("set_timer", [
[str(ts), [cron, [command, parameters]]]
])

def delete_timer(self, timer_id: int):
"""Delete a timer."""
return self.send("del_timer", [str(timer_id)])

def update_timer(self, timer_id: int, mode: TimerState):
if mode != TimerState.On and mode != TimerState.Off:
raise DeviceException("Only 'On' or 'Off' are allowed")
return self.send("upd_timer", [str(timer_id), mode.value])

def dnd_status(self):
"""Returns do-not-disturb status."""
Expand Down
68 changes: 49 additions & 19 deletions mirobo/vacuum_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,27 +294,57 @@ def fanspeed(vac: mirobo.Vacuum, speed):
click.echo("Current fan speed: %s" % vac.fan_speed())


@cli.command()
@click.argument('timer', required=False, default=None)
@cli.group(invoke_without_command=True)
@pass_dev
@click.pass_context
def timer(ctx, vac: mirobo.Vacuum):
"""List and modify existing timers."""
if ctx.invoked_subcommand is not None:
return
timers = vac.timer()
click.echo("Timezone: %s\n" % vac.timezone())
for idx, timer in enumerate(timers):
color = "green" if timer.enabled else "yellow"
click.echo(click.style("Timer #%s, id %s (ts: %s)" % (
idx, timer.id, timer.ts), bold=True, fg=color))
click.echo(" %s" % timer.cron)
min, hr, x, y, days = timer.cron.split(' ')
cron = "%s %s %s %s %s" % (min, hr, x, y, days)
click.echo(" %s" % pretty_cron.prettify_cron(cron))


@timer.command()
@click.option('--cron')
@click.option('--command', default='', required=False)
@click.option('--params', default='', required=False)
@pass_dev
def add(vac: mirobo.Vacuum, cron, command, params):
"""Add a timer."""
click.echo(vac.add_timer(cron, command, params))


@timer.command()
@click.argument('timer_id', type=int, required=True)
@pass_dev
def delete(vac: mirobo.Vacuum, timer_id):
"""Delete a timer."""
click.echo(vac.delete_timer(timer_id))


@timer.command()
@click.argument('timer_id', type=int, required=True)
@click.option('--enable', is_flag=True)
@click.option('--disable', is_flag=True)
@pass_dev
def timer(vac: mirobo.Vacuum, timer):
"""Schedule vacuuming, times in GMT."""
if timer:
raise NotImplementedError()
# vac.set_timer(x)
def update(vac: mirobo.Vacuum, timer_id, enable, disable):
"""Enable/disable a timer."""
from mirobo.vacuum import TimerState
if enable and not disable:
vac.update_timer(timer_id, TimerState.On)
elif disable and not enable:
vac.update_timer(timer_id, TimerState.Off)
else:
timers = vac.timer()
for idx, timer in enumerate(timers):
color = "green" if timer.enabled else "yellow"
# Note ts == ID for changes
click.echo(click.style("Timer #%s, id %s (ts: %s)" % (
idx, timer.id, timer.ts), bold=True, fg=color))
print(" %s" % timer.cron)
min, hr, x, y, days = timer.cron.split(' ')
# hr is in gmt+8 (chinese time), TODO convert to local
hr = (int(hr) - 8) % 24
cron = "%s %s %s %s %s" % (min, hr, x, y, days)
click.echo(" %s" % pretty_cron.prettify_cron(cron))
click.echo("Only 'on' and 'off' are valid for timer")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this one.



@cli.command()
Expand Down