Skip to content

Conversation

@ptarjan
Copy link
Contributor

@ptarjan ptarjan commented Dec 12, 2025

Handle the case where async_offline() is called before async_subscribe() has been invoked, which would leave _subscription_lock as None. This can happen during shutdown when a speaker was discovered but no activity was detected yet. Instead of asserting, create the lock if it doesn't exist, matching the existing pattern in async_subscribe().

Failed in: https://github.com/home-assistant/core/actions/runs/20151776328/job/57846433236

Breaking change

Proposed change

Root cause: In SonosSpeaker:

  • _subscription_lock is initialized as None in init (line 133)
  • The lock is only created when async_subscribe() is called (lines 374-375)
  • async_offline() asserts the lock is not None
  • During shutdown, async_offline() is called on all discovered speakers, including those that never had async_subscribe() called (e.g., speakers discovered but with no activity detected yet)
  • Fix (homeassistant/components/sonos/speaker.py:691-695): Instead of asserting, create the lock if it doesn't exist - matching the existing pattern in async_subscribe():

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

@home-assistant
Copy link

Hey there @jjlawren, @PeteRager, mind taking a look at this pull request as it has been labeled with an integration (sonos) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of sonos can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign sonos Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@ptarjan ptarjan force-pushed the claude/fix-sensor-registration-01QTT5DDWafcHuCxLTEU1hcn branch from 8fff783 to 45ae553 Compare December 12, 2025 03:47
Copy link
Contributor

@PeteRager PeteRager left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests should not access internals

@joostlek joostlek marked this pull request as draft December 12, 2025 12:56
@ptarjan ptarjan force-pushed the claude/fix-sensor-registration-01QTT5DDWafcHuCxLTEU1hcn branch 3 times, most recently from 0fbc440 to f601acd Compare December 12, 2025 16:15
Handle the case where async_offline() is called before async_subscribe()
has been invoked, which would leave _subscription_lock as None. This can
happen during shutdown when a speaker was discovered but no activity was
detected yet. Instead of asserting, create the lock if it doesn't exist,
matching the existing pattern in async_subscribe().
Tests the fix for the AssertionError when async_offline() is called
on a speaker where _subscription_lock is None (e.g., when a speaker
is discovered but no activity was detected before shutdown).
@ptarjan ptarjan force-pushed the claude/fix-sensor-registration-01QTT5DDWafcHuCxLTEU1hcn branch from f601acd to 5a464bc Compare December 12, 2025 17:58
@ptarjan ptarjan marked this pull request as ready for review December 12, 2025 18:02
Ensure all background tasks complete before checking results in the
async_offline_without_subscription_lock test.
Patching async_setup ensures the subscription lock is never created,
since async_subscribe (which creates the lock) is called from async_setup.
Instead of patching the integration, configure the SoCo mock's play_mode
property to raise SoCoException. This causes setup() to fail early
(before scheduling async_setup), leaving the speaker in discovered
without _subscription_lock being created.
Copy link
Contributor

@PeteRager PeteRager left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@joostlek joostlek merged commit 8971f75 into home-assistant:dev Dec 18, 2025
36 checks passed
@joostlek joostlek added this to the 2025.12.4 milestone Dec 18, 2025
@bramkragten bramkragten mentioned this pull request Dec 19, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Dec 20, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants