Skip to content

Commit 3ede6f7

Browse files
committed
fix chromecast and music sync
1 parent 05d4529 commit 3ede6f7

File tree

5 files changed

+56
-10
lines changed

5 files changed

+56
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ music_assistant/config.json
88
music_assistant/testrun.sh
99
build/
1010
dist/
11+
venv/

.vscode/launch.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Python: Module",
9+
"type": "python",
10+
"request": "launch",
11+
"module": "music_assistant"
12+
},
13+
{
14+
"name": "Python: Huidige bestand",
15+
"type": "python",
16+
"request": "launch",
17+
"program": "${file}",
18+
"console": "integratedTerminal"
19+
},
20+
{
21+
"name": "Python: Attach using Process Id",
22+
"type": "python",
23+
"request": "attach",
24+
"processId": "${command:pickProcess}"
25+
}
26+
]
27+
}

music_assistant/metadata.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import re
55

66
import aiohttp
7+
import json
78
from asyncio_throttle import Throttler
89
from music_assistant.cache import use_cache
910
from music_assistant.utils import LOGGER, compare_strings, get_compare_string
@@ -250,7 +251,7 @@ async def get_data(self, endpoint, params={}):
250251
) as response:
251252
try:
252253
result = await response.json()
253-
except aiohttp.client_exceptions.ContentTypeError:
254+
except (aiohttp.client_exceptions.ContentTypeError, json.decoder.JSONDecodeError):
254255
LOGGER.error("Failed to retrieve %s", endpoint)
255256
text_result = await response.text()
256257
LOGGER.debug(text_result)

music_assistant/models/musicprovider.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async def artist(
4646
item_id = await self.mass.db.get_database_id(
4747
provider, prov_item_id, MediaType.Artist
4848
)
49-
if item_id is not None:
49+
if item_id is None:
5050
# artist not yet in local database so fetch details
5151
cache_key = f"{self.prov_id}.get_artist.{prov_item_id}"
5252
artist_details = await cached(
@@ -172,7 +172,7 @@ async def album(
172172
item_id = await self.mass.db.get_database_id(
173173
provider, prov_item_id, MediaType.Album
174174
)
175-
if not item_id:
175+
if item_id is None:
176176
# album not yet in local database so fetch details
177177
if not album_details:
178178
cache_key = f"{self.prov_id}.get_album.{prov_item_id}"
@@ -216,7 +216,7 @@ async def track(
216216
item_id = await self.mass.db.get_database_id(
217217
provider, prov_item_id, MediaType.Track
218218
)
219-
if not item_id:
219+
if item_id is None:
220220
# track not yet in local database so fetch details
221221
if not track_details:
222222
cache_key = f"{self.prov_id}.get_track.{prov_item_id}"
@@ -275,7 +275,7 @@ async def playlist(self, prov_playlist_id, provider=None) -> Playlist:
275275
db_id = await self.mass.db.get_database_id(
276276
provider, prov_playlist_id, MediaType.Playlist
277277
)
278-
if not db_id:
278+
if db_id is None:
279279
# item not yet in local database so fetch and store details
280280
item_details = await self.get_playlist(prov_playlist_id)
281281
db_id = await self.mass.db.add_playlist(item_details)
@@ -288,7 +288,7 @@ async def radio(self, prov_radio_id, provider=None) -> Radio:
288288
db_id = await self.mass.db.get_database_id(
289289
provider, prov_radio_id, MediaType.Radio
290290
)
291-
if not db_id:
291+
if db_id is None:
292292
# item not yet in local database so fetch and store details
293293
item_details = await self.get_radio(prov_radio_id)
294294
db_id = await self.mass.db.add_radio(item_details)

music_assistant/playerproviders/chromecast.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
CONNECTION_STATUS_CONNECTED,
2121
CONNECTION_STATUS_DISCONNECTED,
2222
)
23+
import zeroconf
2324

2425
PROV_ID = "chromecast"
2526
PROV_NAME = "Chromecast"
@@ -278,10 +279,15 @@ def run_chromecast_discovery(self):
278279
# cleanup cast object
279280
del player.cc
280281
self.mass.run_task(self.remove_player(player.player_id))
282+
281283
# search for available chromecasts
282-
from pychromecast.discovery import start_discovery, stop_discovery
283284

284-
def discovered_callback(name):
285+
def list_devices():
286+
LOGGER.debug("Currently known cast devices:")
287+
for uuid, service in listener.services.items():
288+
LOGGER.debug(" {} {}".format(uuid, service))
289+
290+
def add_callback(name):
285291
"""Called when zeroconf has discovered a (new) chromecast."""
286292
discovery_info = listener.services[name]
287293
ip_address, port, uuid, model_name, friendly_name = discovery_info
@@ -290,9 +296,20 @@ def discovered_callback(name):
290296
self.__chromecast_discovered(player_id, discovery_info)
291297
self.__update_group_players()
292298

293-
listener, browser = start_discovery(discovered_callback)
299+
def remove_callback(uuid, name, service):
300+
LOGGER.debug("Lost mDNS service for cast device {} {}".format(uuid, service))
301+
list_devices()
302+
303+
def update_callback(uuid, name):
304+
LOGGER.debug("Updated mDNS service for cast device {}".format(uuid))
305+
list_devices()
306+
307+
listener = pychromecast.CastListener(add_callback, remove_callback, update_callback)
308+
zconf = zeroconf.Zeroconf()
309+
browser = pychromecast.discovery.start_discovery(listener, zconf)
310+
294311
time.sleep(30) # run discovery for 30 seconds
295-
stop_discovery(browser)
312+
pychromecast.stop_discovery(browser)
296313
LOGGER.debug("Chromecast discovery completed...")
297314
self._discovery_running = False
298315

0 commit comments

Comments
 (0)