Skip to content

Commit

Permalink
Merge pull request #44 from milobella/41-support-play_media-action
Browse files Browse the repository at this point in the history
feat(#41): Support play_media action + migrate to new action format
  • Loading branch information
celian-garcia authored Oct 11, 2022
2 parents 9d00feb + 939355c commit 97d7df6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
6 changes: 5 additions & 1 deletion milo/cast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ def play(self, name: str):

def pause(self, name: str):
if name in self.casts:
self.casts[name].media_controller.play()
self.casts[name].media_controller.pause()

def play_media(self, name: str, url: str):
if name in self.casts:
self.casts[name].media_controller.play_media(url)

def names(self):
return self.casts.keys()
Expand Down
44 changes: 38 additions & 6 deletions milo/milobella.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,45 @@ def milobella_request(self, question: str) -> (str, bool):
response = milobella_response.json()
self._current_context = response['context'] if 'context' in response else None
if 'actions' in response:
# Execute all actions in the response
# TODO: refactor
for action in response['actions']:
for param in response['params']:
if param['key'] == 'instrument':
if action == 'play':
self._cast.play(param['value'])
if action == 'pause':
self._cast.pause(param['value'])
if action['identifier'] == "play":
instrument_name = next((p['value'] for p in action['params'] if p['key'] == 'instrument'), None)
instrument_kind = next((p['value'] for p in action['params'] if p['key'] == 'kind'), 'chromecast')
if instrument_name is None:
print('instrument name not provided')
continue
if instrument_kind is not 'chromecast':
print(f'unsupported instrument kind {instrument_kind}')
continue
self._cast.play(instrument_name)

elif action['identifier'] == "pause":
instrument_name = next((p['value'] for p in action['params'] if p['key'] == 'instrument'), None)
instrument_kind = next((p['value'] for p in action['params'] if p['key'] == 'kind'), 'chromecast')
if instrument_name is None:
print('instrument name not provided')
continue
if instrument_kind is not 'chromecast':
print(f'unsupported instrument kind {instrument_kind}')
continue
self._cast.pause(instrument_name)

elif action['identifier'] == "play_media":
instrument_name = next((p['value'] for p in action['params'] if p['key'] == 'instrument'), None)
instrument_kind = next((p['value'] for p in action['params'] if p['key'] == 'kind'), 'chromecast')
url = next((p['value'] for p in action['params'] if p['key'] == 'url'), None)
if instrument_name is None:
print('instrument name not provided')
continue
if instrument_kind is not 'chromecast':
print(f'unsupported instrument kind {instrument_kind}')
continue
if url is None:
print('url not provided')
continue
self._cast.play_media(instrument_name, url)

print(response)
return response["vocal"], response['auto_reprompt'] if 'auto_reprompt' in response else False

0 comments on commit 97d7df6

Please sign in to comment.