@@ -117,10 +117,11 @@ you need their numeric value.
117
117
State variables have attributes that can be accessed by adding the name of the attribute, as in
118
118
``DOMAIN.name.attr ``. The attribute names and their meaning depend on the component that sets them,
119
119
so you will need to look at the State tab in the Developer Tools to see the available attributes.
120
+ You can set an attribute directly by assigning ``DOMAIN.name.attr = value ``.
120
121
121
122
In cases where you need to compute the name of the state variable dynamically, or you need to set or
122
- get the state attributes, you can use the built-in functions ``state.get() ``, ``state.get_attr () ``
123
- and ``state.set () ``; see below .
123
+ get all the state attributes, you can use the built-in functions ``state.get() ``, ``state.getattr () ``,
124
+ `` state.set() `` and ``state.setattr () ``; see ` State Functions < #state-variable-functions >`__ .
124
125
125
126
The function ``state.names(domain=None) `` returns a list of all state variable names (ie,
126
127
``entity_id ``\ s) of a domain. If ``domain `` is not specified, it returns all HASS state
@@ -142,7 +143,7 @@ or call ``state.get("DOMAIN.entity.attr")``:
142
143
- ``last_updated `` is the last UTC time the state entity was updated
143
144
144
145
Note that these two values take precedence over any entity attributes that have the same name. If an
145
- entity has attributes with those names and you need to access them, use ``state.get_attr (name) ``.
146
+ entity has attributes with those names and you need to access them, use ``state.getattr (name) ``.
146
147
If you need to compute how many seconds ago the ``binary_sensor.test1 `` state changed, you could
147
148
do this:
148
149
@@ -624,9 +625,10 @@ which you can’t do if you are directly assigning to the variable:
624
625
is thrown if the name doesn't exist. If ``name `` is a string of the form ``DOMAIN.entity.attr ``
625
626
then the attribute ``attr `` of the state variable ``DOMAIN.entity `` is returned; an
626
627
``AttributeError `` exception is thrown if that attribute doesn't exist.
627
- ``state.get_attr(name) ``
628
- Returns a ``dict `` of attribute values for the state variable, or ``None ``
629
- if it doesn’t exist
628
+ ``state.getattr(name) ``
629
+ Returns a ``dict `` of attribute values for the state variable, or ``None `` if it doesn’t exist.
630
+ In pyscript versions 0.32 and earlier, this function was ``state.get_attr() ``. That deprecated
631
+ name is still supported, but will be removed in a future version.
630
632
``state.names(domain=None) ``
631
633
Returns a list of all state variable names (ie, ``entity_id ``\ s) of a
632
634
domain. If ``domain `` is not specified, it returns all HASS state variable (``entity_id ``) names.
@@ -637,13 +639,17 @@ which you can’t do if you are directly assigning to the variable:
637
639
are preserved across HASS restarts. This only applies to entities in the ``pyscript ``
638
640
domain (ie, name starts with ``pyscript. ``). See `this section <#persistent-state >`__ for
639
641
more information
640
- ``state.set(name, value, new_attributes=None, **kwargs) ``
642
+ ``state.set(name, value=None , new_attributes=None, **kwargs) ``
641
643
Sets the state variable to the given value, with the optional attributes. The optional 3rd
642
644
argument, ``new_attributes ``, should be a ``dict `` and it will overwrite all the existing
643
- attributes if specified. If instead attributes are specified using keyword arguments, then other
644
- attributes will not be affected. If no optional arguments are provided, just the state variable
645
- value is set and the attributes are not changed. To clear the attributes, set
646
- ``new_attributes={} ``.
645
+ attributes if specified. If instead attributes are specified using keyword arguments, then
646
+ just those attributes will be set and other attributes will not be affected. If no optional
647
+ arguments are provided, just the state variable value is set and the attributes are not changed.
648
+ If no value is provided, just the state attributes are set and the value isn't changed.
649
+ To clear all the attributes, set ``new_attributes={} ``.
650
+ ``state.setattr(name, value) ``
651
+ Sets a state variable attribute to the given value. The ``name `` should fully specify the
652
+ state variable and attribute as a string in the form ``DOMAIN.entity.attr ``.
647
653
648
654
Note that in HASS, all state variable values are coerced into strings. For example, if a state
649
655
variable has a numeric value, you might want to convert it to a numeric type (eg, using ``int() ``
0 commit comments