@@ -4,7 +4,7 @@ Reference
4
4
Configuration
5
5
-------------
6
6
7
- Pyscript has one optional configuration variable that allows any Python package to be imported
7
+ Pyscript has one optional configuration setting that allows any Python package to be imported
8
8
if set, eg:
9
9
10
10
.. code :: yaml
@@ -28,13 +28,12 @@ For example, applications ``my_app1`` and ``my_app2`` would be configured as:
28
28
my_app2 :
29
29
# any settings for my_app2 go here
30
30
31
- As explained below, this configuration parameter structure of ``apps `` with entries for each
32
- application by name is used to determine which application scripts are autoloaded. That's the
33
- only configuration structure that pyscript checks - any other parameters can be added and
34
- used as you like.
31
+ As explained below, the use of ``apps `` with entries for each application by name below,
32
+ is used to determine which application scripts are autoloaded. That's the only configuration
33
+ structure that pyscript checks - any other parameters can be added and used as you like.
35
34
36
35
At startup, pyscript loads the following files. It also unloads and reloads these files when
37
- the ``pyscript.reload `` service is called, and the ``yaml `` configuration is reloaded too:
36
+ the ``pyscript.reload `` service is called, which also reloads the ``yaml `` configuration.
38
37
39
38
``<config>/pyscript/*.py ``
40
39
all files with a ``.py `` suffix are autoloaded
@@ -110,8 +109,8 @@ Also, service names (which are called as functions) take priority over state var
110
109
component has a state variable name that collides with one of its services, you’ll need to use
111
110
``state.get(name) `` to access that state variable.
112
111
113
- Accessing state variables don't exist will throw a ``NameError `` exception, and accessing an
114
- attribute that doesn't exist will throw a ``AttributeError `` exception.
112
+ Accessing state variables that don't exist will throw a ``NameError `` exception, and accessing
113
+ an attribute that doesn't exist will throw a ``AttributeError `` exception.
115
114
116
115
Calling services
117
116
----------------
@@ -171,11 +170,11 @@ function.
171
170
``@state_trigger `` takes a single string ``str_expr `` that contains any expression based on one or
172
171
more state variables, and evaluates to ``True `` or ``False `` (or non-zero or zero). Whenever the
173
172
state variables mentioned in the expression change, the expression is evaluated and the trigger
174
- occurs if it evaluates to ``True `` (or non-zero). For each state variable, eg: ``domain.name ``, the
175
- prior value if also available to the expression as ``domain.name.old `` in case you want to condition
176
- the trigger on the prior value too.
173
+ occurs if it evaluates to ``True `` (or non-zero). For each state variable, eg: ``domain.name ``,
174
+ the prior value is also available to the expression as ``domain.name.old `` in case you want to
175
+ condition the trigger on the prior value too.
177
176
178
- Note that all state variables have string values. So you’ll have to do comparisons against string
177
+ All state variables in HASS have string values. So you’ll have to do comparisons against string
179
178
values or cast the variable to an integer or float. These two examples are essentially equivalent
180
179
(note the use of single quotes inside the outer double quotes):
181
180
@@ -187,19 +186,19 @@ values or cast the variable to an integer or float. These two examples are essen
187
186
188
187
@state_trigger (" int(domain.light_level) == 255 or int(domain.light2_level) == 0" )
189
188
190
- although the second will give an exception if the variable string doesn’t represent a valid integer.
189
+ although the second will throw an exception if the variable string doesn’t represent a valid integer.
191
190
If you want numerical inequalities you should use the second form, since string lexicographic
192
191
ordering is not the same as numeric ordering.
193
192
194
- You can also use state variable attributes in the trigger expression, with an indenfitier of the
193
+ You can also use state variable attributes in the trigger expression, with an idenfitier of the
195
194
form ``DOMAIN.name.attr ``. Attributes maintain their original type, so there is no need to cast
196
195
then to another type.
197
196
198
197
If you specify ``@state_trigger("True") `` the state trigger will never occur. While that might seem
199
198
counter-intuitive, the reason is that the expression will never be evaluated - it takes underlying
200
- state variables in the expression to change before the expression is ever evaluated. Since this
201
- expression has no state variables, it will never be evaluated. You can achieve a state trigger on
202
- any value change with a decorator of the form:
199
+ state variables in the expression to change before the expression is evaluated. Since this
200
+ expression has no state variables, it will never be evaluated. You can achieve a state trigger
201
+ on any value change with a decorator of the form:
203
202
204
203
.. code :: python
205
204
@@ -210,8 +209,8 @@ Because of operator short-circuiting, the expression evaluates to ``True`` witho
210
209
value of ``domain.light_level ``. So the result is a trigger whenever the state variable changes to
211
210
any value. This idea can extend to multiple variables just by stringing them together.
212
211
213
- Note that if a state variable is set to the same value, HA doesn’t generate a state change event, so
214
- the ``@state_trigger `` condition will not be checked. It is only evaluated each time a state
212
+ Note that if a state variable is set to the same value, HASS doesn’t generate a state change event,
213
+ so the ``@state_trigger `` condition will not be checked. It is only evaluated each time a state
215
214
variable changes to a new value.
216
215
217
216
When the trigger occurs and the function is executed (meaning any active checks passed too), keyword
@@ -250,6 +249,13 @@ and all those values will simply get passed in into kwargs as a ``dict``. That
250
249
form to use if you have multiple decorators, since each one passes different variables into the
251
250
function (although all of them set ``trigger_type ``).
252
251
252
+ Inside ``str_expr ``, undefined state variables, undefined state attributes, and undefined
253
+ ``.old `` variables evaluate to ``None ``, rather than throwing an exception. The ``.old `` variable will
254
+ be ``None `` the first time the state variable is set (since it has no prior value), and when the
255
+ ``str_expr `` is being evaluated because a different state variable changed (only the state variable
256
+ change that caused ``str_expr `` to be evaluated gets its prior value in ``.old ``; any other ``.old ``
257
+ variables will be ``None `` for that evaluation).
258
+
253
259
@time_trigger
254
260
^^^^^^^^^^^^^
255
261
@@ -412,6 +418,14 @@ When any trigger occurs (whether time, state or event), the ``@state_active`` ex
412
418
evaluated. If it evaluates to ``False `` (or zero), the trigger is ignored and the trigger function
413
419
is not called.
414
420
421
+ If the trigger was caused by ``@state_trigger ``, the prior value of the state variable that
422
+ caused the trigger is available to ``str_expr `` with a ``.old `` suffix.
423
+
424
+ Inside the ``str_expr ``, undefined state variables, undefined state attributes, and undefined
425
+ ``.old `` variables evaluate to `None `, rather than throwing an exception. Any ``.old `` variable
426
+ will be ``None `` if the trigger is not a state trigger or if a different state variable change
427
+ cause the state trigger.
428
+
415
429
@time_active
416
430
^^^^^^^^^^^^
417
431
@@ -910,7 +924,7 @@ Trigger Closures
910
924
Pyscript supports trigger functions that are defined as closures, ie: functions defined inside
911
925
another function. This allows you to easily create many similar trigger functions that might
912
926
differ only in a couple of parameters (eg, a common function in different rooms or for each
913
- media set up ). The trigger will be stopped when the function is no longer referenced in
927
+ media setup ). The trigger will be stopped when the function is no longer referenced in
914
928
any scope. Typically the closure function is returned, and the return value is assigned
915
929
to a variable. If that variable is re-assigned or deleted, the trigger function will be
916
930
destroyed.
0 commit comments