Repository navigation
Close the Chromecast connection once Spotify has taken over - #81
Merged
Merged
Conversation
async_build_from_type opened a new pychromecast connection for every service call that targeted a Google Cast entity and never closed it. pychromecast's socket client retries forever unless it is stopped, so each call left a thread reconnecting to the device for the life of the process. A speaker that received a few plays a day collected dozens of connections until it refused new ones (#80). MediaPlayer gains release(), a no-op by default. Chromecast.release() stops the socket client without joining it: disconnect(timeout=0) raises TimeoutError from its join while the thread is still winding down, which would turn a successful play into an error. async_build_from_type releases the player on any failure, including cancellation. play_media and play_custom_context, which every play service goes through, now start playback through async_play_and_release, which releases the player once playback and extras are applied, and also when they fail. The connection stays open until Spotify has the playback, so the receiver app is never left without a sender before then. Verified live against a Nest Audio: on 6.6.4, Home Assistant's connections to the device went 1, 2, 3, 4 over three play_media calls; with this change they stayed at 1 (Home Assistant's own cast entity), and Spotify kept playing after each release, including a cold launch. Fixes #80 Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Oct 27, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #80
Problem
Every play service call that targets a Google Cast entity builds a new pychromecast connection in
async_build_from_type, and nothing ever closes it. pychromecast's socket client retries forever until it is stopped, so each call leaves a thread reconnecting to the device for as long as Home Assistant runs. After enough calls the device refuses new connections and every later cast to it fails until Home Assistant restarts.Fix
MediaPlayer.release(), a no-op by default.Chromecast.release()callssocket_client.disconnect(), which stops the thread without waiting for it.Chromecast.disconnect(timeout=0)cannot be used: pychromecast 14.0.10 joins the thread and raisesTimeoutErrorwhile it is still winding down, which would turn a successful play into an error.async_build_from_typereleases the player on any failure during connect or launch, cancellation included.play_mediaandplay_custom_contextstart playback through a newasync_play_and_releasehelper that releases the player after playback and extras are applied, and when they fail. Every other play service goes throughplay_media.The connection is closed only after Spotify has the playback, so the receiver app is never left without a sender while it is still starting up.
Testing
main. TheChromecast.release()test runs pychromecast's real socket thread against a closed port and checks that it stops. Full suite: 853 passed; pylint 10.00.spotcast.play_mediacall:The remaining connection is Home Assistant's own cast entity. Spotify was still playing on the device about 25 s after each release.
play_custom_contextandplay_liked_songswere checked the same way.🤖 Generated with Claude Code