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

Add filters to climate and light service descriptions #86162

Merged
merged 8 commits into from
Mar 16, 2023

Conversation

emontnemery
Copy link
Contributor

@emontnemery emontnemery commented Jan 18, 2023

Proposed change

Add filters to climate and light service descriptions

This PR makes it possible for service descriptions to add filters both to a service and a service parameters based on the capabilities of the service call target:

  • entity target selector has a new configuration parameter supported_features
    • supported_features filter lists supported features needed for the service field, the target entity must support at least one of the supported features
  • service fields has a new parameter filter
    • A service field filter can be either supported_features or attributes
      • A supported_features filter lists supported features needed for the service field, the target entity must support at least one of the supported features
      • An attribute filter names an attribute and lists possible attribute states needed for the service field

climate and light are chosen as targets because they both have services which should be hidden depending on the target, or where service call parameters should be hidden depending on the target.

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:

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.

To help with the load of incoming pull requests:

@home-assistant
Copy link

Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (climate) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of climate can trigger bot actions by commenting:

  • @home-assistant close Closes the issue.
  • @home-assistant rename Awesome new title Change the title of the issue.
  • @home-assistant reopen Reopen the issue.
  • @home-assistant unassign climate Removes the current integration label and assignees on the issue, add the integration domain after the command.

@home-assistant
Copy link

Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (light) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of light can trigger bot actions by commenting:

  • @home-assistant close Closes the issue.
  • @home-assistant rename Awesome new title Change the title of the issue.
  • @home-assistant reopen Reopen the issue.
  • @home-assistant unassign light Removes the current integration label and assignees on the issue, add the integration domain after the command.

@emontnemery emontnemery force-pushed the filter_service_descriptions_mvp branch from 083221f to 44d235f Compare March 1, 2023 10:36
script/hassfest/services.py Outdated Show resolved Hide resolved
.yamllint Show resolved Hide resolved
homeassistant/components/climate/services.yaml Outdated Show resolved Hide resolved
script/hassfest/services.py Outdated Show resolved Hide resolved
@emontnemery emontnemery marked this pull request as ready for review March 14, 2023 16:57
if isinstance(supported_feature, int):
return supported_feature

known_entity_features = {
Copy link
Member

Choose a reason for hiding this comment

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

should we generate this in a function decorated with @cache ?

"WaterHeaterEntityFeature": WaterHeaterEntityFeature,
}

_, enum, feature = supported_feature.split(".", 2)
Copy link
Member

Choose a reason for hiding this comment

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

you should catch if it's only has a single or no .

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Voluptuous actually catches this and generates an acceptable error message without catching the ValueError which is raised if the tuple unpacking fails:

voluptuous.error.MultipleInvalid: not a valid value @ data['filter'][0]['supported_features'][0]

Do you think it adds value to add a more specific validation error, maybe something like:

voluptuous.error.MultipleInvalid: Invalid supported feature 'blah', expected <domain>.<enum>.<member> @ data['filter'][0]['supported_features'][0]

@@ -87,6 +152,10 @@ def serialize(self) -> dict[str, dict[str, _T]]:
vol.Optional("domain"): vol.All(cv.ensure_list, [str]),
# Device class of the entity
vol.Optional("device_class"): vol.All(cv.ensure_list, [str]),
# Features supported by the entity
vol.Optional("supported_features"): [
vol.All(vol.Any(int, str), _validate_supported_feature)
Copy link
Member

Choose a reason for hiding this comment

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

let's not support int. It's difficult to read

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is required to be able to instantiate a selector from an already validated configuration where the enum strings have been resolved to their integer values.
We do that in tests to test serialization of configuration, I'm not sure if it's actually needed though?

@emontnemery emontnemery merged commit 9384ec1 into dev Mar 16, 2023
@emontnemery emontnemery deleted the filter_service_descriptions_mvp branch March 16, 2023 14:59
@github-actions github-actions bot locked and limited conversation to collaborators Mar 17, 2023
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.

3 participants