Skip to content

Commit

Permalink
Fix start bug and improve error handling in walkingpad integration (#…
Browse files Browse the repository at this point in the history
…1017)

* Fix start bug and improve error handling

* Update test for set_start_speed

* Correct intialisation pydoc content
  • Loading branch information
dewgenenny authored Apr 16, 2021
1 parent 4e7c2df commit 56bfbda
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
16 changes: 15 additions & 1 deletion miio/tests/test_walkingpad.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import timedelta
from unittest import TestCase

import pytest
Expand Down Expand Up @@ -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():
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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
Expand Down
16 changes: 13 additions & 3 deletions miio/walkingpad.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"])

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down

0 comments on commit 56bfbda

Please sign in to comment.