Skip to content

Commit

Permalink
fix chromecast and music sync
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelveldt committed Jul 4, 2020
1 parent 05d4529 commit 3ede6f7
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ music_assistant/config.json
music_assistant/testrun.sh
build/
dist/
venv/
27 changes: 27 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "music_assistant"
},
{
"name": "Python: Huidige bestand",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "Python: Attach using Process Id",
"type": "python",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
3 changes: 2 additions & 1 deletion music_assistant/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re

import aiohttp
import json
from asyncio_throttle import Throttler
from music_assistant.cache import use_cache
from music_assistant.utils import LOGGER, compare_strings, get_compare_string
Expand Down Expand Up @@ -250,7 +251,7 @@ async def get_data(self, endpoint, params={}):
) as response:
try:
result = await response.json()
except aiohttp.client_exceptions.ContentTypeError:
except (aiohttp.client_exceptions.ContentTypeError, json.decoder.JSONDecodeError):
LOGGER.error("Failed to retrieve %s", endpoint)
text_result = await response.text()
LOGGER.debug(text_result)
Expand Down
10 changes: 5 additions & 5 deletions music_assistant/models/musicprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async def artist(
item_id = await self.mass.db.get_database_id(
provider, prov_item_id, MediaType.Artist
)
if item_id is not None:
if item_id is None:
# artist not yet in local database so fetch details
cache_key = f"{self.prov_id}.get_artist.{prov_item_id}"
artist_details = await cached(
Expand Down Expand Up @@ -172,7 +172,7 @@ async def album(
item_id = await self.mass.db.get_database_id(
provider, prov_item_id, MediaType.Album
)
if not item_id:
if item_id is None:
# album not yet in local database so fetch details
if not album_details:
cache_key = f"{self.prov_id}.get_album.{prov_item_id}"
Expand Down Expand Up @@ -216,7 +216,7 @@ async def track(
item_id = await self.mass.db.get_database_id(
provider, prov_item_id, MediaType.Track
)
if not item_id:
if item_id is None:
# track not yet in local database so fetch details
if not track_details:
cache_key = f"{self.prov_id}.get_track.{prov_item_id}"
Expand Down Expand Up @@ -275,7 +275,7 @@ async def playlist(self, prov_playlist_id, provider=None) -> Playlist:
db_id = await self.mass.db.get_database_id(
provider, prov_playlist_id, MediaType.Playlist
)
if not db_id:
if db_id is None:
# item not yet in local database so fetch and store details
item_details = await self.get_playlist(prov_playlist_id)
db_id = await self.mass.db.add_playlist(item_details)
Expand All @@ -288,7 +288,7 @@ async def radio(self, prov_radio_id, provider=None) -> Radio:
db_id = await self.mass.db.get_database_id(
provider, prov_radio_id, MediaType.Radio
)
if not db_id:
if db_id is None:
# item not yet in local database so fetch and store details
item_details = await self.get_radio(prov_radio_id)
db_id = await self.mass.db.add_radio(item_details)
Expand Down
25 changes: 21 additions & 4 deletions music_assistant/playerproviders/chromecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
CONNECTION_STATUS_CONNECTED,
CONNECTION_STATUS_DISCONNECTED,
)
import zeroconf

PROV_ID = "chromecast"
PROV_NAME = "Chromecast"
Expand Down Expand Up @@ -278,10 +279,15 @@ def run_chromecast_discovery(self):
# cleanup cast object
del player.cc
self.mass.run_task(self.remove_player(player.player_id))

# search for available chromecasts
from pychromecast.discovery import start_discovery, stop_discovery

def discovered_callback(name):
def list_devices():
LOGGER.debug("Currently known cast devices:")
for uuid, service in listener.services.items():
LOGGER.debug(" {} {}".format(uuid, service))

def add_callback(name):
"""Called when zeroconf has discovered a (new) chromecast."""
discovery_info = listener.services[name]
ip_address, port, uuid, model_name, friendly_name = discovery_info
Expand All @@ -290,9 +296,20 @@ def discovered_callback(name):
self.__chromecast_discovered(player_id, discovery_info)
self.__update_group_players()

listener, browser = start_discovery(discovered_callback)
def remove_callback(uuid, name, service):
LOGGER.debug("Lost mDNS service for cast device {} {}".format(uuid, service))
list_devices()

def update_callback(uuid, name):
LOGGER.debug("Updated mDNS service for cast device {}".format(uuid))
list_devices()

listener = pychromecast.CastListener(add_callback, remove_callback, update_callback)
zconf = zeroconf.Zeroconf()
browser = pychromecast.discovery.start_discovery(listener, zconf)

time.sleep(30) # run discovery for 30 seconds
stop_discovery(browser)
pychromecast.stop_discovery(browser)
LOGGER.debug("Chromecast discovery completed...")
self._discovery_running = False

Expand Down

0 comments on commit 3ede6f7

Please sign in to comment.