-
-
Notifications
You must be signed in to change notification settings - Fork 36.3k
Add subscribe preview feature endpoint to labs #157976
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
Merged
+312
−54
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| """Helper functions for the Home Assistant Labs integration.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Callable | ||
|
|
||
| from homeassistant.const import EVENT_LABS_UPDATED | ||
| from homeassistant.core import Event, HomeAssistant, callback | ||
|
|
||
| from .const import LABS_DATA | ||
| from .models import EventLabsUpdatedData | ||
|
|
||
|
|
||
| @callback | ||
| def async_is_preview_feature_enabled( | ||
| hass: HomeAssistant, domain: str, preview_feature: str | ||
| ) -> bool: | ||
| """Check if a lab preview feature is enabled. | ||
|
|
||
| Args: | ||
| hass: HomeAssistant instance | ||
| domain: Integration domain | ||
| preview_feature: Preview feature name | ||
|
|
||
| Returns: | ||
| True if the preview feature is enabled, False otherwise | ||
| """ | ||
| if LABS_DATA not in hass.data: | ||
| return False | ||
|
|
||
| labs_data = hass.data[LABS_DATA] | ||
| return (domain, preview_feature) in labs_data.data.preview_feature_status | ||
|
|
||
|
|
||
| @callback | ||
| def async_listen( | ||
| hass: HomeAssistant, | ||
| domain: str, | ||
| preview_feature: str, | ||
| listener: Callable[[], None], | ||
| ) -> Callable[[], None]: | ||
| """Listen for changes to a specific preview feature. | ||
|
|
||
| Args: | ||
| hass: HomeAssistant instance | ||
| domain: Integration domain | ||
| preview_feature: Preview feature name | ||
| listener: Callback to invoke when the preview feature is toggled | ||
|
|
||
| Returns: | ||
| Callable to unsubscribe from the listener | ||
| """ | ||
|
|
||
| @callback | ||
| def _async_feature_updated(event: Event[EventLabsUpdatedData]) -> None: | ||
| """Handle labs feature update event.""" | ||
| if ( | ||
| event.data["domain"] == domain | ||
| and event.data["preview_feature"] == preview_feature | ||
| ): | ||
| listener() | ||
|
|
||
| return hass.bus.async_listen(EVENT_LABS_UPDATED, _async_feature_updated) |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I look at this code, I think that
async_is_preview_feature_enabledshould also have a check whether the lab feature exists at all and if it does not exist, we should throw an exception. This way we save people the trouble of dealing with typos and wondering why their code isn’t working. A similar issue that you’re trying to guard against on the frontend also shows up in Python code. Since this is a new API, it might be worth considering before more people start using it and we end up too scared to introduce changes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess that would in another PR, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that would make sense as a separate PR and discussion. My comment here was just a general observation while looking at how the current API would be used to implement the feature you’re adding. This missing check stood out as a piece that might be worth addressing separately.