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

Allow device triggers to specify extra fields, update ha-form with support for paper-time-input #3721

Closed

Conversation

emontnemery
Copy link
Collaborator

@emontnemery emontnemery commented Sep 15, 2019

Allow device triggers to specify extra fields which will then be shown in the UI using ha-form.
Some extra love is given to support for (duration) in device automation triggers

Screenshot:
image

Intended for backend PRs home-assistant/core#26658, home-assistant/core#27133 etc.

@emontnemery emontnemery changed the title Add support for for to device automation triggers WIP: Add support for for to device automation triggers Sep 15, 2019
@@ -49,6 +49,7 @@ export class PaperTimeInput extends PolymerElement {
display: none;
}
--paper-input-container-shared-input-style_-_-webkit-appearance: textfield;
--paper-input-container-invalid-color: red;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Maybe this styling should not be here?
Also, how to block saving the automation if the paper-time-input is invalid?

Copy link
Member

Choose a reason for hiding this comment

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

You could move it to ha-style.ts

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

OK, but how to add it such that it only applies to paper-input inside paper-time-input?

src/panels/config/js/trigger/device.tsx Outdated Show resolved Hide resolved
@bramkragten
Copy link
Member

Can we add a days field? For if you want more than 23 hours?

@emontnemery
Copy link
Collaborator Author

Can we add a days field? For if you want more than 23 hours?

OK, makes sense, will give it a try.
Backend only supports hh:mm:ss though, I guess OK to split hours in days + hours in frontend?

@bramkragten
Copy link
Member

Can we add a days field? For if you want more than 23 hours?

OK, makes sense, will give it a try.
Backend only supports hh:mm:ss though, I guess OK to split hours in days + hours in frontend?

Yeah, I think it's fine to make the hours field take more than 23 with an option too

@emontnemery
Copy link
Collaborator Author

emontnemery commented Sep 24, 2019

I added a days input, how to make the paper-input and paper-time-input on same row though?

image

Also, the maths became super ugly converting back and forth between strings and numbers; will clean that up tomorrow.

@bramkragten
Copy link
Member

Set display of the paper-input to inline-block and give it a proper width:

paper-input {
  display: inline-block;
  width: 50px;
}

@bramkragten
Copy link
Member

Btw, the backend does seem to support days:

    # If given, will trigger when condition has been for X time, can also use days and milliseconds.
    for:
      hours: 1
      minutes: 10
      seconds: 5

https://www.home-assistant.io/docs/automation/trigger/#numeric-state-trigger

@emontnemery
Copy link
Collaborator Author

@bramkragten Thanks for help with styling!

Days and hh:mm:ss is now on a single row, but it's quite ugly
image

How to achieve something like this:
image

@bramkragten
Copy link
Member

@bramkragten Thanks for help with styling!

Days and hh:mm:ss is now on a single row, but it's quite ugly
image

How to achieve something like this:
image

We might be better of creating a new element for picking time that has all the logic you want and includes days, that could also handle the hours -> days conversion and export it in the right format. We could reuse that element for all automation triggers. Trying to get this component to do 2 totally different thing will be messy. Or we should add the days to the current time picker...

@emontnemery
Copy link
Collaborator Author

emontnemery commented Sep 30, 2019

Or we should add the days to the current time picker...

I went down that route, I think it's quite OK:
image

src/components/paper-time-input.js Outdated Show resolved Hide resolved
Comment on lines 36 to 43
this.hass = hass;
if (!this.state.capabilities && trigger.domain) {
fetchDeviceTriggerCapabilities(this.hass, trigger).then(
(capabilities) => {
this.setState({ ...this.state, capabilities });
}
);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fetchDeviceTriggerCapabilities is called when device trigger is picked, but if we're editing an existing automation that won't happen. The purpose of this is just to load the trigger capabilities once.
OK to take care of it here, or should it be done elsewhere, for example in componentDidMount?

Copy link
Member

Choose a reason for hiding this comment

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

The render method should never have side effects. Not in Preact nor Lit. It is possible that this method can get called multiple times before the capabilities resolve, and you will do many requests.

Instead, do it inside componentDidUpdate. (similar to updated in Lit)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

OK, moved out of render but to componentDidMount, componentDidUpdate was not called after first render.

@emontnemery emontnemery changed the title WIP: Add support for for to device automation triggers Allow device triggers to specify extra fields Oct 3, 2019
@bramkragten bramkragten force-pushed the automation_device_action_for branch from b186b16 to 9ab126b Compare October 7, 2019 11:49
@emontnemery emontnemery changed the title Allow device triggers to specify extra fields Allow device triggers to specify extra fields, update ha-form with support for paper-time-input Oct 9, 2019
@balloob
Copy link
Member

balloob commented Oct 12, 2019

Can we drop days ? People can just do 72 hours if they want that.

@bramkragten
Copy link
Member

Already did

@github-actions github-actions bot locked and limited conversation to collaborators Jul 6, 2022
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.

4 participants