Add context to event trigger#7182
Conversation
src/data/automation.ts
Outdated
| platform: "event"; | ||
| event_type: string; | ||
| event_data: any; | ||
| context: any; |
There was a problem hiding this comment.
This is not true. Right now context is always context_id: string, user_id?: string
| .defaultValue=${event_data} | ||
| @value-changed=${this._dataChanged} | ||
| ></ha-yaml-editor> | ||
| <ha-yaml-editor |
There was a problem hiding this comment.
We shouldn't just put up a YAML editor. Instead we know that context ID can only have a user that is relevant for matching, so we should show a drop down where the user can pick users to match.
|
@OnFreund Any progress? |
|
@zsarnett I think I understand how to show a user drop down using |
640ca9a to
840b2f3
Compare
|
Thanks @bramkragten, |
src/data/automation.ts
Outdated
| platform: "event"; | ||
| event_type: string; | ||
| event_data: any; | ||
| context: ContextConstraint; |
There was a problem hiding this comment.
it's optional, I'll change this.
| "ui.panel.config.automation.editor.triggers.type.event.context_user_pick" | ||
| )} | ||
| .hass=${this.hass} | ||
| .value=${this._wrapUsersInArray(context)} |
There was a problem hiding this comment.
the user_id constraint can contain either a single user id or an array of them. This normalizes it to an array. Is there a better way to do this?
| "ui.panel.config.automation.editor.triggers.type.event.context_user_pick" | ||
| )} | ||
| .hass=${this.hass} | ||
| .value=${this._wrapUsersInArray(context)} |
There was a problem hiding this comment.
| .value=${this._wrapUsersInArray(context)} | |
| .value=${this._wrapUsersInArray(context?.user_id)} |
| .hass=${this.hass} | ||
| .value=${this._wrapUsersInArray(context)} | ||
| .users=${this._users} | ||
| @value-changed=${this._valueChanged} |
There was a problem hiding this comment.
We can't use the default _valueChanged handler, as this value needs to be merged in the context object first.
| .hass=${this.hass} | ||
| .value=${this._wrapUsersInArray(context)} | ||
| .users=${this._users} | ||
| @value-changed=${this._valueChanged} |
There was a problem hiding this comment.
| @value-changed=${this._valueChanged} | |
| @value-changed=${this._usersChanged} |
private _usersChanged(ev) {
ev.stopPropagation();
fireEvent(this, "value-changed", {
value: { ...this.trigger, context: {...this.trigger.context, user_id: ev.detail.value} },
});
}There was a problem hiding this comment.
Thanks, that did the trick!
src/data/automation.ts
Outdated
| event_data: any; | ||
| context: ContextConstraint; |
There was a problem hiding this comment.
| event_data: any; | |
| context: ContextConstraint; | |
| event_data?: any; | |
| context?: ContextConstraint; |
And you should guard for context being undefined.
|
|
||
| public static get defaultConfig() { | ||
| return { event_type: "", event_data: {} }; | ||
| return { event_type: "", event_data: {}, context: { user_id: [] } }; |
There was a problem hiding this comment.
No need to add this
| return { event_type: "", event_data: {}, context: { user_id: [] } }; | |
| return { event_type: "" }; |
| const empty: string[] = []; | ||
| return empty.concat(context.user_id || []); |
There was a problem hiding this comment.
| const empty: string[] = []; | |
| return empty.concat(context.user_id || []); | |
| if (typeof user_id === "string") { | |
| return [user_id]; | |
| } | |
| return user_id; |
| `; | ||
| } | ||
|
|
||
| private _wrapUsersInArray(context): string[] { |
There was a problem hiding this comment.
| private _wrapUsersInArray(context): string[] { | |
| private _wrapUsersInArray(user_id: string | string[] | undefined): string[] { |
| } | ||
|
|
||
| protected firstUpdated(): void { | ||
| if (!this._users) { |
There was a problem hiding this comment.
It can't be defined here, so you can skip this check.
| private async _addUser(event: PolymerChangedEvent<string>) { | ||
| event.stopPropagation(); | ||
| const toAdd = event.detail.value; | ||
| (event.currentTarget as any).value = ""; |
There was a problem hiding this comment.
@bramkragten this doesn't work, because ha-user-picker is doing this:
const newValue = ev.detail.item.dataset.userId;
if (newValue !== this._value) {And changing the value doesn't change the selected item. What's the best way to resolve this?
There was a problem hiding this comment.
That's actually weird, because it's comparing to ev.detail.item.dataset.userId, yet setting to ev.detail.value:
private _userChanged(ev) {
const newValue = ev.detail.item.dataset.userId;
if (newValue !== this._value) {
this.value = ev.detail.value;
setTimeout(() => {
fireEvent(this, "value-changed", { value: newValue });
fireEvent(this, "change");
}, 0);
}
}There was a problem hiding this comment.
Yeah, that doesn't seem correct. I think newValue should be ev.detail.value?
There was a problem hiding this comment.
This is really strange. So ev.detail.value doesn't even exist (which means that this.value = ev.detail.value; is probably wrong), and ev.newValue is always undefined.
It seems like ev.detail.item.dataset.userId is indeed the right way to grab the currently selected user, but it's unclear how to programmatically select a user...
There was a problem hiding this comment.
This is a red herring - this method on ha-user-picker isn't even called by setting value to "", and it's unclear that value even has any meaning (I thought it was called, because it was called twice when picking a user, so I assumed one of those was a result of setting value to "").
I'm completely stumped 🤷♂️
There was a problem hiding this comment.
I'll put this PR on my todo list :-)
There was a problem hiding this comment.
Any chance we can get it in time for the beta? The core feature is already merged
|
If a user removes all users, should the |
|
Thanks @bramkragten!
It should be removed |
|
@OnFreund can you review/test this? |
|
Looks great, and working. The only thing that's slightly off is that when you add a user, the drop down only contains users who haven't been selected, yet if you open it again after adding a user, it will show all users. |
|
Not seeing that? How can I reproduce that? |
|
|
@OnFreund can you try again? |
|
Perfect! |
Proposed change
Add automation editor support to context in event triggers.
The backend is implemented in home-assistant/core#40932.
Type of change
Additional information
Checklist
If user exposed functionality or configuration variables are added/changed: