-
-
Notifications
You must be signed in to change notification settings - Fork 37.1k
Add context to event trigger #40932
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 context to event trigger #40932
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
|
|
||
| CONF_EVENT_TYPE = "event_type" | ||
| CONF_EVENT_DATA = "event_data" | ||
| CONF_EVENT_CONTEXT = "context" | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
@@ -19,36 +20,42 @@ | |
| vol.Required(CONF_PLATFORM): "event", | ||
| vol.Required(CONF_EVENT_TYPE): cv.string, | ||
| vol.Optional(CONF_EVENT_DATA): dict, | ||
| vol.Optional(CONF_EVENT_CONTEXT): dict, | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| def _populate_schema(config, config_parameter): | ||
| if config_parameter not in config: | ||
| return None | ||
|
|
||
| return vol.Schema( | ||
| {vol.Required(key): value for key, value in config[config_parameter].items()}, | ||
| extra=vol.ALLOW_EXTRA, | ||
| ) | ||
|
|
||
|
|
||
| async def async_attach_trigger( | ||
| hass, config, action, automation_info, *, platform_type="event" | ||
| ): | ||
| """Listen for events based on configuration.""" | ||
| event_type = config.get(CONF_EVENT_TYPE) | ||
| event_data_schema = None | ||
| if config.get(CONF_EVENT_DATA): | ||
| event_data_schema = vol.Schema( | ||
| { | ||
| vol.Required(key): value | ||
| for key, value in config.get(CONF_EVENT_DATA).items() | ||
| }, | ||
| extra=vol.ALLOW_EXTRA, | ||
| ) | ||
| event_data_schema = _populate_schema(config, CONF_EVENT_DATA) | ||
| event_context_schema = _populate_schema(config, CONF_EVENT_CONTEXT) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just realized that matching straight up on context like this is going to be annoying if one wants to match multiple users.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. How about something like: if isinstance(value, list):
vol.Required(key): vol.In(value)
else:
vol.Required(key): valuewhen building the schema? This can also be used for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Downside is that this could be a breaking change if the user intended to match a list.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed it here: To ensure backwards compatibility I only added this for context, not data, even though I think it could be a nice touch for data as well. |
||
|
|
||
| @callback | ||
| def handle_event(event): | ||
| """Listen for events and calls the action when data matches.""" | ||
| if event_data_schema: | ||
| # Check that the event data matches the configured | ||
| try: | ||
| # Check that the event data and context match the configured | ||
| # schema if one was provided | ||
| try: | ||
| if event_data_schema: | ||
| event_data_schema(event.data) | ||
| except vol.Invalid: | ||
| # If event data doesn't match requested schema, skip event | ||
| return | ||
| if event_context_schema: | ||
| event_context_schema(event.context.as_dict()) | ||
| except vol.Invalid: | ||
| # If event doesn't match, skip event | ||
| return | ||
|
|
||
| hass.async_run_job( | ||
| action, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
The existing behavior was changed slightly with this check. The old check only set an event data schema if the config had a populated event data dict. The current check sets a schema as long as the event data key is in the config. It doesn't check for a truthy event data dict.
Uh oh!
There was an error while loading. Please reload this page.
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.
Good catch. I'm not sure this makes any practical difference (since the schema will be empty), but I'll fix this together with the multiple user selection below.
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.
https://github.com/home-assistant/core/pull/41036/files#diff-aa600ac67a083c2d308272918338761fR36-R37