From 9efc4453aa97d1f8b8b2126fc7a4660066fbc422 Mon Sep 17 00:00:00 2001 From: Teemu Rytilahti Date: Wed, 17 Oct 2018 18:51:18 +0200 Subject: [PATCH 1/2] Add last_clean_details to return information from the last clean This involves also refactoring clean_details() to return the results unwrapped. For the time being the default behavior will remain the same as it was (wrapped), but a deprecation warning will be raised. I'm expecting that we can flip the toggle after a while, and remove the warning at that point, too. --- miio/vacuum.py | 33 +++++++++++++++++++++++++++------ miio/vacuum_cli.py | 18 +++++++++--------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/miio/vacuum.py b/miio/vacuum.py index 6a7e9eced..eb942b849 100644 --- a/miio/vacuum.py +++ b/miio/vacuum.py @@ -6,7 +6,7 @@ import os import pathlib import time -from typing import List +from typing import List, Optional, Union import click import pytz @@ -184,17 +184,38 @@ def clean_history(self) -> CleaningSummary: """Return generic cleaning history.""" return CleaningSummary(self.send("get_clean_summary")) + @command() + def last_clean_details(self) -> CleaningDetails: + """Return details from the last cleaning.""" + last_clean_id = self.clean_history().ids.pop() + return self.clean_details(last_clean_id, return_list=False) + @command( click.argument("id_", type=int, metavar="ID"), + click.argument("return_list", type=bool, default=True) ) - def clean_details(self, id_: int) -> List[CleaningDetails]: + def clean_details(self, id_: int, return_list=True) -> Union[ + List[CleaningDetails], + Optional[CleaningDetails]]: """Return details about specific cleaning.""" details = self.send("get_clean_record", [id_]) - res = list() - for rec in details: - res.append(CleaningDetails(rec)) - + if not details: + _LOGGER.warning("No cleaning record found for id %s" % id_) + return None + + if len(details) > 1: + _LOGGER.warning("Got multiple clean details, returning the first") + + res = CleaningDetails(details.pop()) + if return_list: + _LOGGER.warning("This method will be returning the details " + "without wrapping them into a list in the " + "near future. The current behavior can be " + "kept by passing return_list=True and this " + "warning will be removed when the default gets " + "changed.") + return [res] return res @command() diff --git a/miio/vacuum_cli.py b/miio/vacuum_cli.py index 29f313a34..87e6647f5 100644 --- a/miio/vacuum_cli.py +++ b/miio/vacuum_cli.py @@ -418,15 +418,15 @@ def cleaning_history(vac: miio.Vacuum): res.total_area)) click.echo() for idx, id_ in enumerate(res.ids): - for e in vac.clean_details(id_): - color = "green" if e.complete else "yellow" - click.echo(click.style( - "Clean #%s: %s-%s (complete: %s, error: %s)" % ( - idx, e.start, e.end, e.complete, e.error), - bold=True, fg=color)) - click.echo(" Area cleaned: %s m²" % e.area) - click.echo(" Duration: (%s)" % e.duration) - click.echo() + details = vac.clean_details(id_, return_list=False) + color = "green" if details.complete else "yellow" + click.echo(click.style( + "Clean #%s: %s-%s (complete: %s, error: %s)" % ( + idx, details.start, details.end, details.complete, details.error), + bold=True, fg=color)) + click.echo(" Area cleaned: %s m²" % details.area) + click.echo(" Duration: (%s)" % details.duration) + click.echo() @cli.command() From 6b792bc481780342bf253d81f2e414966dfe2915 Mon Sep 17 00:00:00 2001 From: Teemu Rytilahti Date: Wed, 17 Oct 2018 19:20:14 +0200 Subject: [PATCH 2/2] try to fix the indent, return the full list instead of a single entry when return_list=True --- miio/vacuum.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/miio/vacuum.py b/miio/vacuum.py index eb942b849..cc63bd442 100644 --- a/miio/vacuum.py +++ b/miio/vacuum.py @@ -192,11 +192,11 @@ def last_clean_details(self) -> CleaningDetails: @command( click.argument("id_", type=int, metavar="ID"), - click.argument("return_list", type=bool, default=True) + click.argument("return_list", type=bool, default=False) ) def clean_details(self, id_: int, return_list=True) -> Union[ - List[CleaningDetails], - Optional[CleaningDetails]]: + List[CleaningDetails], + Optional[CleaningDetails]]: """Return details about specific cleaning.""" details = self.send("get_clean_record", [id_]) @@ -204,10 +204,6 @@ def clean_details(self, id_: int, return_list=True) -> Union[ _LOGGER.warning("No cleaning record found for id %s" % id_) return None - if len(details) > 1: - _LOGGER.warning("Got multiple clean details, returning the first") - - res = CleaningDetails(details.pop()) if return_list: _LOGGER.warning("This method will be returning the details " "without wrapping them into a list in the " @@ -215,7 +211,12 @@ def clean_details(self, id_: int, return_list=True) -> Union[ "kept by passing return_list=True and this " "warning will be removed when the default gets " "changed.") - return [res] + return [CleaningDetails(entry) for entry in details] + + if len(details) > 1: + _LOGGER.warning("Got multiple clean details, returning the first") + + res = CleaningDetails(details.pop()) return res @command()