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

Feature Request: cleaner @state_trigger('True or ... ') #44

Closed
dlashua opened this issue Oct 15, 2020 · 6 comments
Closed

Feature Request: cleaner @state_trigger('True or ... ') #44

dlashua opened this issue Oct 15, 2020 · 6 comments

Comments

@dlashua
Copy link
Contributor

dlashua commented Oct 15, 2020

This syntax is a bit hacky looking and hard for new users to follow:

@state_trigger('True or binary_sensor.dark')

There's no reason to make the above syntax NOT work, but think a convenience decorator for this use pattern might be beneficial. Something like this:

@state_change_trigger('binary_sensor.dark')

@state_change_trigger([
  'binary_sensor.dark',
  'binary_sensor.motion'
])

Though, naming things is hard. There's probably something better than state_change_trigger.

@craigbarratt
Copy link
Member

Yes, the current suggested approach isn't very intuitive.

However, the last example (putting the variables in a list) should work with @state_trigger, although the quotes would go outside the list. The reason it works is that a non-empty list evaluates to non-zero:

@state_trigger("[binary_sensor.dark, binary_sensor.motion]")

Do you agree it's sufficient to document this method as the way to get a trigger on any change?

For a single variable, it almost works correctly without using a list, except for the case where the variable is an empty string (since non-empty string are non-zero).

@dlashua
Copy link
Contributor Author

dlashua commented Oct 15, 2020

Documenting it this way is certainly make the code more readable.

It still means that if you already have a python list of entities to watch, you have to do this:

@state_trigger("[" + ", ".join(my_list_of_entities) +"]")

In which case, this is still cleaner to me

@state_trigger('True ' + " or ".join(my_list_of_entities))

But isn't nearly as good as:

@state_trigger(my_list_of_entities)

@swazrgb
Copy link
Contributor

swazrgb commented Oct 16, 2020

I notice that almost all of my state triggers follow the True or ... pattern, so a more convenient syntax for this would be great. The suggestion to accept a list of entities would be fantastic.

@craigbarratt
Copy link
Member

Ok, that's two votes for

@state_trigger(my_list_of_entities)

so let me look at doing that. I was also thinking about using a list of strings for multiple expressions, that are ored together (ie, anyone being true will trigger the function).

It would be possible to make both forms work. A single string argument state variable (ie, no expression and no list) could be detected and treated as trigger on any change. And a list of strings, each of which could be an expression or a state variable name meaning any change, would be logically ored together.

@craigbarratt
Copy link
Member

craigbarratt commented Oct 17, 2020

Ok, @state_trigger now allows multiple arguments of strings or lists of strings. Any string can be just a state variable name, which means trigger on any change. Commit 59c12f9. The old form still works, but I removed it from the docs.

task.wait_until()'s state_trigger argument also has the same extensions, although it can only be a single string or list of strings.

@dlashua
Copy link
Contributor Author

dlashua commented Oct 17, 2020

I tested @state_trigger() and task.wait_until() with lists, strings, combinations of lists and strings, as well as the old "True or ..." functionality. Everything worked as expected.

THANKS!

@dlashua dlashua closed this as completed Oct 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants