Skip to content

Commit

Permalink
WiFi Speaker support improved
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi committed Apr 1, 2018
1 parent dc43ed0 commit 1e04799
Showing 1 changed file with 44 additions and 20 deletions.
64 changes: 44 additions & 20 deletions miio/wifispeaker.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import enum
import logging
import warnings

Expand All @@ -6,16 +7,38 @@
_LOGGER = logging.getLogger(__name__)


class PlayState(enum.Enum):
Playing = "PLAYING"
Stopped = "STOPPED"
Paused = "PAUSED_PLAYBACK"
NoMedia = "NO_MEDIA_PRESENT"
Transitioning = "TRANSITIONING"


class TransportChannel(enum.Enum):
Playlist = "PLAYLIST"
OneTime = "ONETIME"
Auxiliary = "AUX"
Bluetooth = "BT"
Radio = "RADIO"
Air = "AIR"
Qplay = "QPLAY"


class WifiSpeakerStatus:
"""Container of a speaker state.
This contains information such as the name of the device,
and what is currently being played by it."""
def __init__(self, data):
# {"DeviceName": "Mi Internet Speaker", "channel_title\": "XXX",
# "current_state": "PLAYING", "hardware_version": "S602",
# "play_mode": "REPEAT_ALL", "track_artist": "XXX",
# "track_duration": "00:04:58", "track_title": "XXX",
# "transport_channel": "PLAYLIST"}
"""
Example response of a xiaomi.wifispeaker.v2:
{"DeviceName": "Mi Internet Speaker", "channel_title\": "XXX",
"current_state": "PLAYING", "hardware_version": "S602",
"play_mode": "REPEAT_ALL", "track_artist": "XXX",
"track_duration": "00:04:58", "track_title": "XXX",
"transport_channel": "PLAYLIST"}
"""
self.data = data

@property
Expand All @@ -29,15 +52,9 @@ def channel(self) -> str:
return self.data["channel_title"]

@property
def state(self) -> str:
def state(self) -> PlayState:
"""State of the device, e.g. PLAYING."""
# note: this can be enumized when all values are known
class PlayState:
Playing = "PLAYING"
Stopped = "STOPPED"
Paused = "PAUSED_PLAYBACK"

return self.data["current_state"]
return PlayState(self.data["current_state"])

@property
def hardware_version(self) -> str:
Expand Down Expand Up @@ -65,14 +82,9 @@ def track_duration(self) -> str:
return self.data["track_duration"]

@property
def transport_channel(self) -> str:
def transport_channel(self) -> TransportChannel:
"""Transport channel, e.g. PLAYLIST"""
# note: this can be enumized when all values are known
class TransportChannel:
Playlist = "PLAYLIST"
OneTime = "ONETIME"

return self.data["transport_channel"]
return TransportChannel(self.data["transport_channel"])


class WifiSpeaker(Device):
Expand All @@ -92,6 +104,10 @@ def power(self):
# is this a toggle?
return self.send("power")

def toggle(self):
"""Toggle play."""
return self.send("toggle")

def volume_up(self, amount: int = 5):
"""Set volume up."""
return self.send("vol_up", [amount])
Expand All @@ -111,3 +127,11 @@ def track_next(self):
def track_position(self):
"""Return current track position."""
return self.send("get_prop", ["rel_time"])

def volume(self):
"""Speaker volume."""
return self.send("get_prop", ["volume"])

def next_channel(self):
"""Change transport channel."""
return self.send(["next_channel"])

0 comments on commit 1e04799

Please sign in to comment.