diff --git a/README.md b/README.md index 6cc6ecd53..2ef6e4a0e 100644 --- a/README.md +++ b/README.md @@ -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 [--enable|--disable] +``` + +Deleting a timer +``` +$ mirobo timer delete +``` + ### Cleaning history ``` diff --git a/mirobo/vacuum.py b/mirobo/vacuum.py index 862dd7aa8..240f00d81 100644 --- a/mirobo/vacuum.py +++ b/mirobo/vacuum.py @@ -2,6 +2,7 @@ import math import time from typing import List +import enum from .vacuumcontainers import (VacuumStatus, ConsumableStatus, CleaningSummary, CleaningDetails, Timer) @@ -14,6 +15,10 @@ class VacuumException(DeviceException): pass +class TimerState(enum.Enum): + On = "on" + Off = "off" + class Vacuum(Device): """Main class representing the vacuum.""" @@ -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.""" diff --git a/mirobo/vacuum_cli.py b/mirobo/vacuum_cli.py index 1665a7958..87b52f5c3 100644 --- a/mirobo/vacuum_cli.py +++ b/mirobo/vacuum_cli.py @@ -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("You need to specify either --enable or --disable") @cli.command()