Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix start bug and improve error handling in walkingpad integration #1017

Merged
merged 3 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 14 additions & 4 deletions miio/walkingpad.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class WalkingpadStatus(DeviceStatus):
'sp': 3.0,
'start_speed': 3.0,
'step': 180,
'time': 121}
'walking_time': 121}
dewgenenny marked this conversation as resolved.
Show resolved Hide resolved
"""

def __init__(self, data: Dict[str, Any]) -> None:
Expand All @@ -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