From f461ed01678bb5726ff75ce5906350b96ddfa133 Mon Sep 17 00:00:00 2001 From: Teemu Rytilahti Date: Sat, 7 Oct 2017 20:22:28 +0200 Subject: [PATCH] wrap dnd status inside an object --- miio/vacuum.py | 4 ++-- miio/vacuum_cli.py | 8 +++----- miio/vacuumcontainers.py | 25 ++++++++++++++++++++++++- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/miio/vacuum.py b/miio/vacuum.py index 240f00d81..bd6843596 100644 --- a/miio/vacuum.py +++ b/miio/vacuum.py @@ -4,7 +4,7 @@ from typing import List import enum -from .vacuumcontainers import (VacuumStatus, ConsumableStatus, +from .vacuumcontainers import (VacuumStatus, ConsumableStatus, DNDStatus, CleaningSummary, CleaningDetails, Timer) from .device import Device, DeviceException @@ -165,7 +165,7 @@ def dnd_status(self): """Returns do-not-disturb status.""" # {'result': [{'enabled': 1, 'start_minute': 0, 'end_minute': 0, # 'start_hour': 22, 'end_hour': 8}], 'id': 1} - return self.send("get_dnd_timer") + return DNDStatus(self.send("get_dnd_timer")[0]) def set_dnd(self, start_hr: int, start_min: int, end_hr: int, end_min: int): diff --git a/miio/vacuum_cli.py b/miio/vacuum_cli.py index e8d5b4500..e623e1f4f 100644 --- a/miio/vacuum_cli.py +++ b/miio/vacuum_cli.py @@ -275,11 +275,9 @@ def dnd(vac: miio.Vacuum, cmd: str, end_hr, end_min)) click.echo(vac.set_dnd(start_hr, start_min, end_hr, end_min)) else: - x = vac.dnd_status()[0] - click.echo("DND %02i:%02i to %02i:%02i (enabled: %s)" % ( - x['start_hour'], x['start_minute'], - x['end_hour'], x['end_minute'], - x['enabled'])) + x = vac.dnd_status() + click.echo(click.style("Between %s and %s (enabled: %s)" % ( + x.start, x.end, x.enabled), bold=x.enabled)) @cli.command() diff --git a/miio/vacuumcontainers.py b/miio/vacuumcontainers.py index ab7f457a9..9fb085225 100644 --- a/miio/vacuumcontainers.py +++ b/miio/vacuumcontainers.py @@ -1,5 +1,5 @@ # -*- coding: UTF-8 -*# -from datetime import datetime, timedelta +from datetime import datetime, timedelta, time from typing import Any, Dict, List import warnings import functools @@ -325,6 +325,29 @@ def __repr__(self) -> str: return "" % ( # noqa: E501 self.main_brush, self.side_brush, self.filter, self.sensor_dirty) +class DNDStatus: + def __init__(self, data: Dict[str, Any]): + self.data = data + + @property + def enabled(self) -> bool: + return bool(self.data["enabled"]) + + @property + def start(self) -> time: + return time(hour=self.data["start_hour"], minute=self.data["start_minute"]) + + @property + def end(self) -> time: + return time(hour=self.data["end_hour"], minute=self.data["end_minute"]) + + def __repr__(self): + return "" % ( + self.enabled, + self.start, + self.end) + + class Timer: def __init__(self, data: List[Any]) -> None: