@@ -31,13 +31,13 @@ global and services can be called from any script file.
3131
3232Reloading the ``.py `` files is accomplished by calling the ``pyscript.reload `` service, which is the
3333one built-in service (so you can’t create your own service with that name). All function
34- definitions, services and triggers are re-created on ``reload ``. Any currently running functions
35- (ie, functions that have been triggered and are actively executing Python code or waiting inside
36- ``task.sleep() `` or ``task.wait_until() ``) are not stopped by `` reload `` - they continue to run
37- until they finish (return). You can terminate these running functions too on `` reload `` if you wish
38- by calling ``task.unique() `` in the script file preamble (ie, outside any function definition), so
39- it is executed on load and reload, which will terminate any running functions that have previously
40- called ``task.unique() `` with the same argument.
34+ definitions, services and triggers are re-created on ``reload ``, except for any active Jupyter
35+ sessions. Any currently running functions (ie, functions that have been triggered and are actively
36+ executing Python code or waiting inside ``task.sleep() `` or ``task.wait_until() ``) are not stopped
37+ by `` reload `` - they continue to run until they finish (return). You can terminate these running
38+ functions too on `` reload `` if you wish by calling ``task.unique() `` in the script file preamble
39+ (ie, outside any function definition), so it is executed on load and reload, which will terminate
40+ any running functions that have previously called ``task.unique() `` with the same argument.
4141
4242
4343State Variables
@@ -62,15 +62,17 @@ get the state attributes, you can use the built-in functions ``state.get()``, ``
6262and ``state.set() ``; see below.
6363
6464The function ``state.names(domain=None) `` returns a list of all state variable names (ie,
65- ``entity_id ``\ s) of a domain. If ``domain `` is not specified, it returns all HASS state variable
66- (entity) names.
65+ ``entity_id ``\ s) of a domain. If ``domain `` is not specified, it returns all HASS state
66+ variable (entity) names.
6767
6868Also, service names (which are called as functions) take priority over state variable names, so if a
6969component has a state variable name that collides with one of its services, you’ll need to use
7070``state.get(name) `` to access that state variable.
7171
7272Finally, state variables that don’t exist evaluate to ``None ``, rather than throwing an exception.
73- That makes it easy to test if a state variable has a valid value.
73+ That makes it easy to test if a state variable has a valid value. In contrast, accessing a state
74+ attribute using ``DOMAIN.name.attr `` will throw an exception if the attribute doesn't exist
75+ (``None `` could be a valid attribute value).
7476
7577Calling services
7678----------------
@@ -132,9 +134,11 @@ more state variables, and evaluates to ``True`` or ``False`` (or non-zero or zer
132134state variables mentioned in the expression change, the expression is evaluated and the trigger
133135occurs if it evaluates to ``True `` (or non-zero). For each state variable, eg: ``domain.name ``, the
134136prior value if also available to the expression as ``domain.name.old `` in case you want to condition
135- the trigger on the prior value too. Note that all state variables have string values. So you’ll have
136- to do comparisons against string values or cast the variable to an integer or float. These two
137- examples are essentially equivalent (note the use of single quotes inside the outer double quotes):
137+ the trigger on the prior value too.
138+
139+ Note that all state variables have string values. So you’ll have to do comparisons against string
140+ values or cast the variable to an integer or float. These two examples are essentially equivalent
141+ (note the use of single quotes inside the outer double quotes):
138142
139143.. code :: python
140144
@@ -148,6 +152,10 @@ although the second will give an exception if the variable string doesn’t repr
148152If you want numerical inequalities you should use the second form, since string lexicographic
149153ordering is not the same as numeric ordering.
150154
155+ You can also use state variable attributes in the trigger expression, with an indenfitier of the
156+ form ``DOMAIN.name.attr ``. Attributes maintain their original type, so there is no need to cast
157+ then to another type.
158+
151159If you specify ``@state_trigger("True") `` the state trigger will never occur. While that might seem
152160counter-intuitive, the reason is that the expression will never be evaluated - it takes underlying
153161state variables in the expression to change before the expression is ever evaluated. Since this
@@ -446,29 +454,37 @@ made available by ``pyscript``.
446454
447455Note that even though the function names contain a period, the left portion is not a class (e.g.,
448456``state `` is not a class, and in fact isn’t even defined). These are simply functions whose name
449- includes a period. This is one aspect where the interpreter behaves differently from real Python.
457+ includes a period. This is one aspect where the interpreter behaves slightly differently from real
458+ Python.
450459
451- .. _accessing-state-variables-1 :
460+ However, if you set a variable like ``state ``, ``log `` or ``task `` to some value, then the functions
461+ defined with that prefix will no longer be available, since the portion after the period will now be
462+ interpreted as a method or class function acting on that variable. That's the same behavior as
463+ Python - for example if you set ``bytes `` to some value, then the ``bytes.fromhex() `` class method
464+ is no longer available in the current scope.
452465
453- Accessing state variables
454- ^^^^^^^^^^^^^^^^^^^^^^^^^
466+ State variables
467+ ^^^^^^^^^^^^^^^
455468
456469State variables can be used and set just by using them as normal Python variables. However, there
457- could be cases where you want to dynamically generate the variable name (eg, in a loop). These
458- functions allow you to get and set a variable using its string name. The set function also allows
459- you to optionally set the attributes, which you can’t do if you are directly assigning to the
460- variable:
470+ could be cases where you want to dynamically generate the variable name (eg, in a function or loop
471+ where the state variable name is computed dynamically). These functions allow you to get and set a
472+ variable using its string name. The set function also allows you to optionally set the attributes,
473+ which you can’t do if you are directly assigning to the variable:
461474
462475``state.get(name) ``
463- returns the value of the state variable, or ``None `` if it doesn’t exist
476+ Returns the value of the state variable given its string ``name ``, or ``None `` if it doesn’t exist.
477+ If ``name `` is a string of the form ``DOMAIN.entity.attr `` then the attribute ``attr `` of the
478+ state variable ``DOMAIN.entity `` is returned; an exception is thrown if that attribute doesn't
479+ exist.
464480``state.get_attr(name) ``
465- returns a ``dict `` of attribute values for the state variable, or ``None ``
481+ Returns a ``dict `` of attribute values for the state variable, or ``None ``
466482 if it doesn’t exist
467483``state.names(domain=None) ``
468- returns a list of all state variable names (ie, ``entity_id ``\ s) of a
484+ Returns a list of all state variable names (ie, ``entity_id ``\ s) of a
469485 domain. If ``domain `` is not specified, it returns all HASS state variable (``entity_id ``) names.
470486``state.set(name, value, new_attributes=None, **kwargs) ``
471- sets the state variable to the given value, with the optional attributes. The optional 3rd
487+ Sets the state variable to the given value, with the optional attributes. The optional 3rd
472488 argument, ``new_attributes ``, should be a ``dict `` and it will overwrite all the existing
473489 attributes if specified. If instead attributes are specified using keyword arguments, then other
474490 attributes will not be affected. If no optional arguments are provided, just the state variable
@@ -493,8 +509,8 @@ Event Firing
493509``event.fire(event_type, **kwargs) ``
494510 sends an event with the given ``event_type `` string and the keyword parameters as the event data.
495511
496- Logging functions
497- ^^^^^^^^^^^^^^^^^
512+ Logging
513+ ^^^^^^^
498514
499515Five logging functions are provided, with increasing levels of severity:
500516
0 commit comments