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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pyblu library in bluesound #117257

Merged
merged 8 commits into from
Jul 21, 2024

Conversation

LouisChrist
Copy link
Contributor

Breaking change

Sources behave slightly different. Radio station providers(e.g. TuneIn, Radio Paradise) are not part of source_list anymore as they are not selectable as source. Presets which play a specific radio station can be created in the BluOS app and are displayed as sources in Home Assistant. The currently selected source tries to match those presets and falls back to the radio station provider(e.g. TuneIn, Radio Paradise).

Proposed change

Adds pypi library for all device calls: pyblu

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:

Requirement for other PR: PR: Add config_flow to bluesound

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
  • 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.

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

To help with the load of incoming pull requests:

@home-assistant
Copy link

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

Code owner commands

Code owners of bluesound 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 bluesound 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.

@LouisChrist LouisChrist changed the title Integrate pypi libraray: pyblu Integrate pypi library: pyblu May 11, 2024
Comment on lines +330 to +343
with suppress(TimeoutError):
await self.async_update_sync_status()
await self.async_update_presets()
await self.async_update_captures()
Copy link
Member

Choose a reason for hiding this comment

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

Why do we suppress these now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

send_bluesound_command(which i replaced by the library) was called with raise_timeout=False which suppressed TimeoutError. I pulled that logic one level up and put it here.

Comment on lines 384 to 381
if self._sync_status is None:
return None
Copy link
Member

Choose a reason for hiding this comment

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

When is sync status None?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

SyncStatus is one endpoint of the device. It starts with None and might be None if the device is not reachable.

Copy link
Member

Choose a reason for hiding this comment

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

If the device is not reachable, I think we should raise an issue like PlatformNotReady I think

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed the return None to raise PlatformNotReady.

Copy link
Member

Choose a reason for hiding this comment

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

No I mean, in the async_setup or async_setup_platform. If we can't fetch the state, that means the device doesn't respond and we can't get data. Right? In that case we should raise platform during setup

Copy link
Contributor Author

@LouisChrist LouisChrist May 12, 2024

Choose a reason for hiding this comment

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

Ah ok. I can do that.

There is one caveat: This integrations supports multiple devices. Throwing a PlatformNotReady would result in none of those working if one of the devices is not reachable.

Copy link
Member

Choose a reason for hiding this comment

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

Right, let me ask some others how they did this back in the day

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Any updates on this?

And as some background information: Whatever we decide here will probably be removed in my other MR, which introduces a config flow and removes the yaml configuration.

Copy link
Member

Choose a reason for hiding this comment

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

I did ask someone but got no reply, and I forgot to follow up as I was busy with my thesis, but let me ask someone else instead.


async def async_media_stop(self) -> None:
"""Send stop command."""
if self.is_grouped and not self.is_master:
return

return await self.send_bluesound_command("Pause")
async with timeout(BluesoundPlayer.COMMAND_TIMEOUT):
Copy link
Member

Choose a reason for hiding this comment

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

Why don't we put this timeout in the library instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The old code did not use the timeout functionality of aiohttp and used async with timeout(...) instead and i kept it that way.

Copy link
Member

Choose a reason for hiding this comment

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

So it essentially does the same?

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 should be equivalent. I had a quick look at aiohttp and it seems to use asyncio.timeout_at internally.

Copy link
Member

Choose a reason for hiding this comment

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

Honestly, if we can already make the code cleaner (by moving connection specific things to the library, like timeouts), go for it

Copy link
Member

Choose a reason for hiding this comment

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

It’s always better to put the time out as far down in the code as possible so the least amount of has to worry about a cancellation.

In this case, it’s much much better for it to be done in aiohttp

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I moved the timeouts into the library. Most of the explicit timeouts have been replaced by a default timeout for all requests.

@MartinHjelmare MartinHjelmare changed the title Integrate pypi library: pyblu Use pyblu library in bluesound May 12, 2024
@joostlek
Copy link
Member

joostlek commented Jul 5, 2024

@thrawnarn Are you able to test this change?

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.

Oh hi there @LouisChrist 👋

There is now a merge conflict on this PR, could you take a look?

I've drafted the PR for that reason. Please un-draft it once the CI passes and it is ready for review again by clicking the "Ready for review" button.

Thanks! 👍

../Frenck

Learn more about our pull request process.

@frenck frenck marked this pull request as draft July 7, 2024 15:09
@LouisChrist LouisChrist marked this pull request as ready for review July 12, 2024 16:30
@joostlek
Copy link
Member

Oh one final thing:

Sources behave slightly different. Radio station providers(e.g. TuneIn, Radio Paradise) are not part of source_list anymore as they are not selectable as source. Presets which play a specific radio station can be created in the BluOS app and are displayed as sources in Home Assistant. The currently selected source tries to match those presets and falls back to the radio station provider(e.g. TuneIn, Radio Paradise).

How come this difference?

@LouisChrist
Copy link
Contributor Author

LouisChrist commented Jul 19, 2024

How come this difference?

The source_list contained those radio station providers before (e.g. TuneIn). But selecting them just stopped the music and did not start playing anything. I could not fix them, therefore i removed them from the list.

The problem is the underlying API. There is no consistent concept of sources. I have these "services" on my device:

  • Captures - physical sources - e.g. HDMI
  • Spotify - special service for spotify
  • TuneIn - radio service
  • RadioParadise - radio service
  • Presets - presets which can be created by the user

Captures can be selected and they play music. Spotify works in a similar way. Presets have a different API endpoint, but can also be selected.
Radio services on the other hand can not be selected on their own. A specific station has to be selected.

The API endpoints for selecting and listing those services are inconsistent in what they return. And the same endpoint returns different things for different services.

To summarize: It's a big mess. And this is the best solution i could find.

@joostlek joostlek merged commit f629364 into home-assistant:dev Jul 21, 2024
39 of 40 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jul 22, 2024
@LouisChrist LouisChrist deleted the bluesound_pypi_library branch August 3, 2024 09:44
Copy link
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

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

Please address the comments in a new PR. Thanks!

return
await self.async_set_volume_level(current_vol + 0.01)
if self.volume_level is None:
return None
Copy link
Member

Choose a reason for hiding this comment

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

This method shouldn't return anything.


new_volume = self.volume_level + 0.01
new_volume = min(1, new_volume)
return await self.async_set_volume_level(new_volume)
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

@home-assistant home-assistant unlocked this conversation Aug 28, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Aug 29, 2024
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