From a22d685916733161e7541703a9d049b505a74908 Mon Sep 17 00:00:00 2001 From: dewgenenny Date: Fri, 16 Apr 2021 14:39:50 +0200 Subject: [PATCH] Fix start bug and improve error handling in walkingpad integration (#1017) * Fix start bug and improve error handling * Update test for set_start_speed * Correct intialisation pydoc content --- miio/tests/test_walkingpad.py | 16 +++++++++++++++- miio/walkingpad.py | 16 +++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/miio/tests/test_walkingpad.py b/miio/tests/test_walkingpad.py index d1e094bcb..df5c05197 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 @@ -173,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 diff --git a/miio/walkingpad.py b/miio/walkingpad.py index e5470c61d..d9bb877c5 100644 --- a/miio/walkingpad.py +++ b/miio/walkingpad.py @@ -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)