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

WiFi Speaker support improved #291

Merged
merged 2 commits into from
Apr 1, 2018
Merged
Changes from all 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
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 @@ -108,6 +124,14 @@ def track_next(self):
"""Move to next track."""
return self.send("next_track")

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

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"])