Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Sonos alarms and favorites updating #55529

Merged
merged 3 commits into from Sep 28, 2021

Conversation

jjlawren
Copy link
Contributor

@jjlawren jjlawren commented Sep 1, 2021

Proposed change

Refactors the alarm and favorites handling in Sonos to utilize various update identifiers to avoid race conditions.

Each Sonos device keeps track of alarms and favorites locally and may not always be perfectly in sync when we query them as updates propagate between the devices. This ensures we're only processing the latest known update and will ignore stale data.

This also bumps soco as it's a dependency for this change (SoCo/SoCo#851). Other soco changes should be non-breaking: https://github.com/SoCo/SoCo/milestone/21?closed=1. This also fixes #52950 via dependency upgrade.

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)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • 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
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

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.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 馃 Silver
  • 馃 Gold
  • 馃弳 Platinum

To help with the load of incoming pull requests:

@probot-home-assistant
Copy link

Hey there @cgtobi, 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!
(message by CodeOwnersMention)

@project-bot project-bot bot added this to Needs review in Dev Sep 1, 2021
@project-bot project-bot bot moved this from Needs review to By Code Owner in Dev Sep 1, 2021
Copy link
Contributor

@AaronDavidSchneider AaronDavidSchneider left a comment

Choose a reason for hiding this comment

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

Looks good! Clean and nice code.
Seems to work as well

self.hass, SONOS_CREATE_ALARM, speaker, [alarm.alarm_id]
)
updated = await self.hass.async_add_executor_job(
self.update_cache, soco, update_id
Copy link
Member

Choose a reason for hiding this comment

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

Can this no longer throw (OSError, SoCoException)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it's still possible. Will catch during the update.

for alarm_id, alarm in self.alarms.alarms.items():
if alarm_id in self.created_alarm_ids:
continue
speaker = self.hass.data[DATA_SONOS].discovered.get(alarm.zone.uid)
Copy link
Member

Choose a reason for hiding this comment

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

alarms.py:            speaker = self.hass.data[DATA_SONOS].discovered.get(alarm.zone.uid)
household_coordinator.py:        discovered = self.hass.data[DATA_SONOS].discovered
speaker.py:        missing_zone = self.hass.data[DATA_SONOS].discovered[uid].zone_name
speaker.py:                speaker = self.hass.data[DATA_SONOS].discovered.get(uid)
speaker.py:                slave = self.hass.data[DATA_SONOS].discovered.get(slave_uid)
speaker.py:        speakers = self.hass.data[DATA_SONOS].discovered.values()
switch.py:            self.speaker = self.hass.data[DATA_SONOS].discovered.get(

In a future PR we should hide this implementation detail from the class

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, absolutely. Need to think about how to best clean this up.

Copy link
Member

@bdraco bdraco left a comment

Choose a reason for hiding this comment

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

Testing:

Added a new alarm in app, verified it showed up
Added a new alarm in app, verified it showed up
Enabled/disabled alarm 1 from hass
Enabled/disabled alarm 2 from hass
Adjusted alarm 1 from hass service
Waited for it to go off
Disabled alarm 1
Reenabled alarm 1
Deleted alarm 1
Deleted alarm 2
verified entities cleared

馃憤

Dev automation moved this from By Code Owner to Reviewer approved Sep 27, 2021
Copy link
Member

@bdraco bdraco left a comment

Choose a reason for hiding this comment

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

Lgtm

Copy link
Member

@frenck frenck left a comment

Choose a reason for hiding this comment

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

Looks good!

Tested locally in a similar fashion as described above by bdraco.

Thanks, @jjlawren 馃憤

@frenck frenck merged commit 0d6aa89 into home-assistant:dev Sep 28, 2021
Dev automation moved this from Reviewer approved to Done Sep 28, 2021
@jjlawren jjlawren deleted the sonos_version_alarms_favorites branch September 28, 2021 12:11
@github-actions github-actions bot locked and limited conversation to collaborators Sep 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Dev
  
Done
Development

Successfully merging this pull request may close these issues.

Sonos Alarm switch changes immediatly after click Sonos Warning - DATA STRUCTURE UPGRADE FAIL
6 participants