Skip to content

Commit

Permalink
added repeat option (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
rileymd88 authored and fondberg committed Oct 19, 2019
1 parent 70a587a commit 74d9bcf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ where
- `device_name` is the friendly name of the Chromecast
- `uri` is the spotify uri, supports all uris including track (limit to one track)
- `random_song` optional parameter that starts the playback at a random position in the playlist
- `repeat` optional parameter that repeats the playlist/track

optionally you can specify the `entity_id` of an existing home assistant chromecast mediaplayer like:
```
Expand Down
13 changes: 10 additions & 3 deletions custom_components/spotcast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@
CONF_SPOTIFY_ACCOUNT = 'account'
CONF_TRANSFER_PLAYBACK = 'transfer_playback'
CONF_RANDOM = 'random_song'
CONF_REPEAT = 'repeat'

SERVICE_START_COMMAND_SCHEMA = vol.Schema({
vol.Optional(CONF_DEVICE_NAME): cv.string,
vol.Optional(CONF_ENTITY_ID): cv.string,
vol.Optional(CONF_SPOTIFY_URI): cv.string,
vol.Optional(CONF_SPOTIFY_ACCOUNT): cv.string,
vol.Optional(CONF_TRANSFER_PLAYBACK): cv.boolean,
vol.Optional(CONF_RANDOM): cv.boolean
vol.Optional(CONF_RANDOM): cv.boolean,
vol.Optional(CONF_REPEAT): cv.string
})

ACCOUNTS_SCHEMA = vol.Schema({
Expand Down Expand Up @@ -86,7 +88,7 @@ def get_spotify_token(username, password):
expires = data[1] - int(time.time())
return access_token, expires

def play(client, spotify_device_id, uri, random_song):
def play(client, spotify_device_id, uri, random_song, repeat):
# import spotipy
# import http.client as http_client
# spotipy.trace = True
Expand All @@ -112,6 +114,10 @@ def play(client, spotify_device_id, uri, random_song):

_LOGGER.debug('Playing context uri using context_uri for uri: "%s" (random_song: %s)', uri, random_song)
client.start_playback(**kwargs)
if repeat:
_LOGGER.debug('Turning repeat on')
time.sleep(5)
client.repeat(state=repeat, device_id=spotify_device_id)

def transfer_pb(client, spotify_device_id):
_LOGGER.debug('Transfering playback')
Expand All @@ -126,6 +132,7 @@ def start_casting(call):

uri = call.data.get(CONF_SPOTIFY_URI)
random_song = call.data.get(CONF_RANDOM, False)
repeat = call.data.get(CONF_REPEAT)

# Get device name from tiehr device_name or entity_id
device_name = None
Expand Down Expand Up @@ -196,7 +203,7 @@ def start_casting(call):
if transfer_playback == True:
transfer_pb(client, spotify_device_id)
else:
play(client, spotify_device_id, uri, random_song)
play(client, spotify_device_id, uri, random_song, repeat)

hass.services.register(DOMAIN, 'start', start_casting,
schema=SERVICE_START_COMMAND_SCHEMA)
Expand Down
3 changes: 2 additions & 1 deletion custom_components/spotcast/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ start:
uri: {description: Supported Spotify URI as string (if omitted will try to transfer current playback)., example: "spotify:playlist:37i9dQZF1DX3yvAYDslnv8"}
account: {description: Optionally starts Spotify using an alternative account specified in config., example: "my_wifes"}
transfer_playback: {description: Transfer playback if something is playing on the account otherwise play specified URI., example: "true"}
random_song: {description: Starts the playback at a random position in the playlist., example: "true"}
random_song: {description: Starts the playback at a random position in the playlist., example: "true"}
repeat: {description: "Set repeat mode for playback. Available options are track, context and off", example: "track"}

0 comments on commit 74d9bcf

Please sign in to comment.