Skip to content

Commit

Permalink
Add unified cli support to WiFi Speaker
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi committed Apr 4, 2018
1 parent 9e247a4 commit c7939cd
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
6 changes: 3 additions & 3 deletions miio/tests/test_airconditioningcompanion.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def send_configuration_known_aircondition():
'010000000001072700', # best guess
Power.On,
OperationMode.Auto,
22.5,
22,
FanSpeed.Low,
SwingMode.On,
Led.Off)
Expand All @@ -146,7 +146,7 @@ def send_configuration_known_aircondition_turn_off():
'010000000001072700', # best guess
Power.Off,
OperationMode.Auto,
22.5,
22,
FanSpeed.Low,
SwingMode.On,
Led.Off)
Expand All @@ -156,7 +156,7 @@ def send_configuration_unknown_aircondition():
'010507950000257301',
Power.On,
OperationMode.Auto,
22.5,
22,
FanSpeed.Low,
SwingMode.On,
Led.Off)
Expand Down
69 changes: 69 additions & 0 deletions miio/wifispeaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import logging
import warnings

import click

from .click_common import command, format_output
from .device import Device

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -86,6 +89,32 @@ def transport_channel(self) -> TransportChannel:
"""Transport channel, e.g. PLAYLIST"""
return TransportChannel(self.data["transport_channel"])

def __repr__(self) -> str:
s = "<WifiSpeakerStatus " \
"device_name=%s, " \
"channel=%s, " \
"state=%s, " \
"play_mode=%s, " \
"track_artist=%s, " \
"track_title=%s, " \
"track_duration=%s, " \
"transport_channel=%s, " \
"hardware_version=%s>" % \
(self.device_name,
self.channel,
self.state,
self.play_mode,
self.track_artist,
self.track_title,
self.track_duration,
self.transport_channel,
self.hardware_version)

return s

def __json__(self):
return self.data


class WifiSpeaker(Device):
"""Device class for Xiaomi Smart Wifi Speaker."""
Expand All @@ -95,39 +124,79 @@ def __init__(self, *args, **kwargs):
"`play_mode` and `transport_channel`.", stacklevel=2)
super().__init__(*args, **kwargs)

@command(
default_output=format_output(
"",
"Device name: {result.device_name}\n"
"Channel: {result.channel}\n"
"State: {result.state}\n"
"Play mode: {result.play_mode}\n"
"Track artist: {result.track_artist}\n"
"Track title: {result.track_title}\n"
"Track duration: {result.track_duration}\n"
"Transport channel: {result.transport_channel}\n"
"Hardware version: {result.hardware_version}\n"
)
)
def status(self) -> WifiSpeakerStatus:
"""Return device status."""
return WifiSpeakerStatus(self.send("get_prop", ["umi"]))

@command(
default_output=format_output("Powering on"),
)
def power(self):
"""Toggle power on and off."""
# is this a toggle?
return self.send("power")

@command(
default_output=format_output("Toggling play"),
)
def toggle(self):
"""Toggle play."""
return self.send("toggle")

@command(
click.argument("amount", type=int),
default_output=format_output("Increasing volume by {amount} percent")
)
def volume_up(self, amount: int = 5):
"""Set volume up."""
return self.send("vol_up", [amount])

@command(
click.argument("amount", type=int),
default_output=format_output("Decreasing volume by {amount} percent")
)
def volume_down(self, amount: int = 5):
"""Set volume down."""
return self.send("vol_down", [amount])

@command(
default_output=format_output("Playing previous track"),
)
def track_previous(self):
"""Move to previous track."""
return self.send("previous_track")

@command(
default_output=format_output("Playing next track"),
)
def track_next(self):
"""Move to next track."""
return self.send("next_track")

@command(
default_output=format_output("Switching to the next transport channel"),
)
def channel_next(self):
"""Change transport channel."""
return self.send("next_channel")

@command(
default_output=format_output("Track position: {result.rel_time}"),
)
def track_position(self):
"""Return current track position."""
return self.send("get_prop", ["rel_time"])
Expand Down

0 comments on commit c7939cd

Please sign in to comment.