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

Support shorthand templates in condition actions #61177

Merged
merged 3 commits into from Dec 21, 2021

Conversation

emontnemery
Copy link
Contributor

@emontnemery emontnemery commented Dec 7, 2021

Proposed change

Support shorthand templates in condition actions

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

@emontnemery emontnemery requested a review from a team as a code owner December 7, 2021 16:33
@project-bot project-bot bot added this to Needs review in Dev Dec 7, 2021

def dynamic_template_condition_action(config: dict) -> dict:
"""Validate and convert a shorthand template condition to a template condition."""
schema = vol.Schema(
Copy link
Member

Choose a reason for hiding this comment

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

Define this outside of this function.

@@ -1207,6 +1207,40 @@ def STATE_CONDITION_SCHEMA(value: Any) -> dict: # pylint: disable=invalid-name
)
)


def dynamic_template_condition_action(config: dict) -> dict:
Copy link
Member

Choose a reason for hiding this comment

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

Alternative:

dynamic_template_condition_action = vol.All(
  vol.Schema(…),
  lambda config: {
    **config,
    CONF_VALUE_TEMPLATE: config[CONF_CONDITION],
    CONF_CONDITION: "template"
  }
)

"zone": ZONE_CONDITION_SCHEMA,
},
),
dynamic_template_condition_action,
Copy link
Member

Choose a reason for hiding this comment

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

Based on the broken test this breaks the validation message.

Dev automation moved this from Needs review to Reviewer approved Dec 7, 2021
Comment on lines +882 to +884
if default_schema:
with contextlib.suppress(vol.Invalid):
return cast(Dict[Hashable, Any], default_schema(value))
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 will hide error messages from the default validator, not sure if that's acceptable?

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.

Thanks 👍

@emontnemery emontnemery merged commit e2fca2e into dev Dec 21, 2021
Dev automation moved this from Reviewer approved to Done Dec 21, 2021
@emontnemery emontnemery deleted the condition_action_shorthand_template branch December 21, 2021 11:19
@github-actions github-actions bot locked and limited conversation to collaborators Dec 22, 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.

None yet

4 participants