Skip to content

Commit f32abc2

Browse files
committed
added docs for new virtual methods on entity_ids feature; #64
1 parent 634fc1d commit f32abc2

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

docs/new_features.rst

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ The new features since 0.32 in master include:
2020
from @swazrgb and @dlashua (#48).
2121
- The ``hass`` object is available in all pyscript global contexts if the ``hass_is_global`` configuration parameter
2222
is true (default false). This allows access to HASS internals that might not be otherwise exposed by pyscript.
23-
Use with caution. PR #51.
23+
Use with caution (#51).
2424
- Improvements to UI config flow, including allows parameters to be updated, and the UI reload now works the same
2525
as the ``pyscript.reload`` service call, submitted by @raman325 (#53)
26-
- State variables now support virtual attributes last_changed and last_updated for the UTC time when state values
27-
or any attribute was last changed.
26+
- State variables now support virtual attributes ``last_changed`` and ``last_updated`` for the UTC time when state
27+
values or any attribute was last changed.
2828
- ``@state_trigger`` and ``task.wait_until`` now have an optional ``state_hold`` duration in seconds that requires
2929
the state trigger to remain true for that period of time. The trigger occurs after that time elapses. If the state
3030
trigger changes to false before the time expires, the process of waiting for a new trigger starts over.
@@ -38,6 +38,13 @@ The new features since 0.32 in master include:
3838
- Required Python packages can be specified in ``requirements.txt`` files at the top-level pyscript
3939
directory, and each module's or app's directory. Those files are read and any missing packages are
4040
installed on HASS startup and pyscript reload. Contributed by @raman325 (#66, #68, #69).
41+
- State variable attributes can be set by direct assignment, eg: ``DOMAIN.name.attr = value``. A
42+
equivalent new function ``state.setattr()`` allows a specific attribute to be set.
43+
- The ``state.get_attr()`` function has been renamed ``state.getattr()``. The old function is
44+
still available and will be removed in some future release.
45+
- Entities ``DOMAIN.ENTITY`` now support a virtual method ``SERVICE`` (eg, ``DOMAIN.ENTITY.SERVICE()``)
46+
that calls a service ``DOMAIN.SERVICE`` for any service that has an ``entity_id`` parameter.
47+
Proposed @dlashua (#64).
4148

4249
The bug fixes since 0.32 in master include:
4350

docs/reference.rst

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,39 @@ an attribute that doesn't exist will throw a ``AttributeError`` exception. One e
136136
to this that in a ``@state_trigger`` expression, undefined state variables will evaluate to
137137
``None`` instead of throwing an exception.
138138

139-
Two virtual attribute values are available when you use a variable directly as ``DOMAIN.entity.attr``
140-
or call ``state.get("DOMAIN.entity.attr")``:
139+
State variables also support virtual methods that are service calls with that ``entity_id``.
140+
For any state variable ``DOMAIN.ENTITY``, any services registered by ``DOMAIN``, eg:
141+
``DOMAIN.SERVICE``, that have an ``entity_id`` parameter can be called as a method
142+
``DOMAIN.ENTITY.SERVICE()``. Each of these statements are equivalent:
143+
144+
.. code:: python
145+
146+
service.call("DOMAIN", "SERVICE", entity_id="DOMAIN.ENTITY", other_param=123)
147+
DOMAIN.SERVICE(entity_id="DOMAIN.ENTITY", other_param=123)
148+
DOMAIN.ENTITY.SERVICE(other_param=123)
149+
150+
In the case the service has only one other parameter in addition to ``entity_id``, a further
151+
shorthand is that the method can be called with just a positional, rather than keyword parameter.
152+
So if the service only takes two parameters, ``entity_id`` and ``other_param``, this additional
153+
form is equivalent to each of the above statements:
154+
155+
.. code:: python
156+
157+
DOMAIN.ENTITY.SERVICE(123)
158+
159+
Here's an example using ``input_number``, assuming it has been configured to create an entity
160+
``input_number.test``. These statements all do the same thing (and the last one works because
161+
``set_value`` only takes on other parameter):
162+
163+
.. code:: python
164+
165+
service.call("input_number", "set_value", entity_id="input_number.test", value=13)
166+
input_number.set_value(entity_id="input_number.test", value=13)
167+
input_number.test.set_value(value=13)
168+
input_number.test.set_value(13)
169+
170+
Two additional virtual attribute values are available when you use a variable directly as
171+
``DOMAIN.entity.attr`` or call ``state.get("DOMAIN.entity.attr")``:
141172

142173
- ``last_changed`` is the last UTC time the state value was changed (not the attributes)
143174
- ``last_updated`` is the last UTC time the state entity was updated

0 commit comments

Comments
 (0)