Skip to content

Add service entity context#71558

Merged
balloob merged 3 commits intodevfrom
service-entity-context
May 9, 2022
Merged

Add service entity context#71558
balloob merged 3 commits intodevfrom
service-entity-context

Conversation

@balloob
Copy link
Copy Markdown
Member

@balloob balloob commented May 9, 2022

Breaking change

Proposed change

This is a proposal.

I was talking with @marcelveldt about a specific media browser use case he has with Music Assistant. His integration maintains a queue per media player, and if a song is picked in the media browser from his integration's media source he wants to add it to the queue of the entity for which it was picked.

Media sources do not have any context about who is resolving media. This PR will change that by adding a context var with the current entity ID to the function that calls entity services.

Any function called as a result from an entity service can now detect the entity.

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

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

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

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:

@balloob balloob requested a review from a team as a code owner May 9, 2022 05:41
@probot-home-assistant probot-home-assistant bot added core new-feature small-pr PRs with less than 30 lines. labels May 9, 2022
"""Test we set the current entity context var."""

async def mock_service(entity, call):
assert entity.entity_id == service.current_entity.get()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we wrap the current_entity.get call in a helper like we do for async_get_current_platform?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I did that. However I made it so it doesn't raise because methods that are using it can be re-used outside of entity service calls; like media browsing can be run from a WS too (when resolving for the browser)

@balloob balloob force-pushed the service-entity-context branch from 6ccbc84 to 9d41509 Compare May 9, 2022 17:45
@marcelveldt
Copy link
Copy Markdown
Member

Just tested this branch with Music Assistant and it works perfectly fine. No more need for ugly workarounds to determine which player requested the media. Much appreciated!

@balloob balloob merged commit 64636a4 into dev May 9, 2022
@balloob balloob deleted the service-entity-context branch May 9, 2022 21:45
@github-actions github-actions bot locked and limited conversation to collaborators May 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants