Skip to content

Commit

Permalink
fix problem with empty history
Browse files Browse the repository at this point in the history
  • Loading branch information
fettlaus committed Apr 8, 2021
1 parent 3244224 commit f1af89d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 10 deletions.
48 changes: 48 additions & 0 deletions miio/tests/test_vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,51 @@ def test_history_dict(self):
assert self.device.clean_history().total_duration == datetime.timedelta(
days=2, seconds=1345
)

def test_history_details(self):
with patch.object(
self.device,
"send",
return_value=[[1488347071, 1488347123, 16, 0, 0, 0]],
):
assert self.device.clean_details(
123123, return_list=False
).duration == datetime.timedelta(seconds=16)

def test_history_details_dict(self):
with patch.object(
self.device,
"send",
return_value=[
{
"begin": 1616757243,
"end": 1616758193,
"duration": 950,
"area": 10852500,
"error": 0,
"complete": 1,
"start_type": 2,
"clean_type": 1,
"finish_reason": 52,
"dust_collection_status": 0,
}
],
):
assert self.device.clean_details(
123123, return_list=False
).duration == datetime.timedelta(seconds=950)

def test_history_empty(self):
with patch.object(
self.device,
"send",
return_value={
"clean_time": 174145,
"clean_area": 2410150000,
"clean_count": 82,
"dust_collection_count": 5,
},
):
assert self.device.clean_history().total_duration == datetime.timedelta(
days=2, seconds=1345
)
33 changes: 23 additions & 10 deletions miio/vacuumcontainers.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,10 @@ def __init__(self, data: Union[List[Any], Dict[str, Any]]) -> None:
data["clean_time"],
data["clean_area"],
data["clean_count"],
data["records"],
[],
]
if "records" in data:
self.data[3] = data["records"]
else:
self.data = data

Expand Down Expand Up @@ -226,48 +228,59 @@ def ids(self) -> List[int]:
class CleaningDetails(DeviceStatus):
"""Contains details about a specific cleaning run."""

def __init__(self, data: List[Any]) -> None:
def __init__(self, data: Union[List[Any], Dict[str, Any]]) -> None:
# start, end, duration, area, unk, complete
# { "result": [ [ 1488347071, 1488347123, 16, 0, 0, 0 ] ], "id": 1 }
self.data = data
# newer models return a dict
if type(data) is list:
self.data = {
"begin": data[0],
"end": data[1],
"duration": data[2],
"area": data[3],
"error": data[4],
"complete": data[5],
}
else:
self.data = data

@property
def start(self) -> datetime:
"""When cleaning was started."""
return pretty_time(self.data[0])
return pretty_time(self.data["begin"])

@property
def end(self) -> datetime:
"""When cleaning was finished."""
return pretty_time(self.data[1])
return pretty_time(self.data["end"])

@property
def duration(self) -> timedelta:
"""Total duration of the cleaning run."""
return pretty_seconds(self.data[2])
return pretty_seconds(self.data["duration"])

@property
def area(self) -> float:
"""Total cleaned area."""
return pretty_area(self.data[3])
return pretty_area(self.data["area"])

@property
def error_code(self) -> int:
"""Error code."""
return int(self.data[4])
return int(self.data["error"])

@property
def error(self) -> str:
"""Error state of this cleaning run."""
return error_codes[self.data[4]]
return error_codes[self.data["error"]]

@property
def complete(self) -> bool:
"""Return True if the cleaning run was complete (e.g. without errors).
see also :func:`error`.
"""
return bool(self.data[5] == 1)
return bool(self.data["complete"] == 1)


class ConsumableStatus(DeviceStatus):
Expand Down

0 comments on commit f1af89d

Please sign in to comment.