From e5cb270ab026627e4fedfe25472c2b655ddd8f0a Mon Sep 17 00:00:00 2001 From: dewgenenny Date: Tue, 13 Apr 2021 10:49:37 +0200 Subject: [PATCH 1/3] Fix start bug and improve error handling --- miio/tests/test_walkingpad.py | 12 +++++++++++- miio/walkingpad.py | 18 ++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/miio/tests/test_walkingpad.py b/miio/tests/test_walkingpad.py index d1e094bcb..c77ff2fb0 100644 --- a/miio/tests/test_walkingpad.py +++ b/miio/tests/test_walkingpad.py @@ -1,3 +1,4 @@ +from datetime import timedelta from unittest import TestCase import pytest @@ -120,7 +121,9 @@ def test_status(self): assert self.state().step_count == self.device.start_state["step"] assert self.state().distance == self.device.start_state["dist"] assert self.state().sensitivity == self.device.start_state["sensitivity"] - assert self.state().walking_time == self.device.start_state["time"] + assert self.state().walking_time == timedelta( + seconds=self.device.start_state["time"] + ) def test_set_mode(self): def mode(): @@ -145,6 +148,7 @@ def test_set_speed(self): def speed(): return self.device.status().speed + self.device.on() self.device.set_speed(3.055) assert speed() == 3.055 @@ -157,10 +161,16 @@ def speed(): with pytest.raises(WalkingpadException): self.device.set_speed("blah") + with pytest.raises(WalkingpadException): + self.device.off() + self.device.set_speed(3.4) + def test_set_start_speed(self): def speed(): return self.device.status().start_speed + self.device.on() + self.device.set_start_speed(3.055) assert speed() == 3.055 diff --git a/miio/walkingpad.py b/miio/walkingpad.py index e5470c61d..a549e08e4 100644 --- a/miio/walkingpad.py +++ b/miio/walkingpad.py @@ -41,7 +41,7 @@ class WalkingpadStatus(DeviceStatus): 'sp': 3.0, 'start_speed': 3.0, 'step': 180, - 'time': 121} + 'walking_time': 121} """ def __init__(self, data: Dict[str, Any]) -> None: @@ -60,7 +60,7 @@ def is_on(self) -> bool: @property def walking_time(self) -> timedelta: """Current walking duration in seconds.""" - return int(self.data["time"]) + return timedelta(seconds=int(self.data["time"])) @property def speed(self) -> float: @@ -133,7 +133,7 @@ def status(self) -> WalkingpadStatus: default_output=format_output( "", "Mode: {result.mode.name}\n" - "Time: {result.walking_time}\n" + "Walking time: {result.walking_time}\n" "Steps: {result.step_count}\n" "Speed: {result.speed}\n" "Distance: {result.distance}\n" @@ -180,7 +180,7 @@ def start(self): # In case the treadmill is not already turned on, turn it on. if not self.status().is_on: - self.on(self) + self.on() return self.send("set_state", ["run"]) @@ -208,6 +208,10 @@ def set_mode(self, mode: OperationMode): def set_speed(self, speed: float): """Set speed.""" + # In case the treadmill is not already turned on, throw an exception. + if not self.status().is_on: + raise WalkingpadException("Cannot set the speed, device is turned off") + if not isinstance(speed, float): raise WalkingpadException("Invalid speed: %s" % speed) @@ -223,6 +227,12 @@ def set_speed(self, speed: float): def set_start_speed(self, speed: float): """Set start speed.""" + # In case the treadmill is not already turned on, throw an exception. + if not self.status().is_on: + raise WalkingpadException( + "Cannot set the start speed, device is turned off" + ) + if not isinstance(speed, float): raise WalkingpadException("Invalid start speed: %s" % speed) From 4b0f04b7fe74f4737edfc2102e42293fc3944468 Mon Sep 17 00:00:00 2001 From: dewgenenny Date: Tue, 13 Apr 2021 11:12:14 +0200 Subject: [PATCH 2/3] Update test for set_start_speed --- miio/tests/test_walkingpad.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/miio/tests/test_walkingpad.py b/miio/tests/test_walkingpad.py index c77ff2fb0..df5c05197 100644 --- a/miio/tests/test_walkingpad.py +++ b/miio/tests/test_walkingpad.py @@ -183,6 +183,10 @@ def speed(): with pytest.raises(WalkingpadException): self.device.set_start_speed("blah") + with pytest.raises(WalkingpadException): + self.device.off() + self.device.set_start_speed(3.4) + def test_set_sensitivity(self): def sensitivity(): return self.device.status().sensitivity From 4aaba897bdaeba4801d4d033800bb5cbad295898 Mon Sep 17 00:00:00 2001 From: dewgenenny Date: Fri, 16 Apr 2021 11:09:44 +0200 Subject: [PATCH 3/3] Correct intialisation pydoc content --- miio/walkingpad.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/miio/walkingpad.py b/miio/walkingpad.py index a549e08e4..d9bb877c5 100644 --- a/miio/walkingpad.py +++ b/miio/walkingpad.py @@ -41,7 +41,7 @@ class WalkingpadStatus(DeviceStatus): 'sp': 3.0, 'start_speed': 3.0, 'step': 180, - 'walking_time': 121} + 'time': 121} """ def __init__(self, data: Dict[str, Any]) -> None: