Skip to content

Commit

Permalink
Add some information about using the response data
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeweerd committed Jul 23, 2023
1 parent cad0426 commit 1e86aa5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,17 @@ the entity. You can switch back and forth!
There are several examples below for different commands. You can copy/paste
them to start from.

The services now provide the data as
[response data](https://www.home-assistant.io/blog/2023/07/05/release-20237/#services-can-now-respond)
\- they are still provided as event_data.\
Adding
`response_variable: VAR_NAME` to a `zha_toolkit` service call will make the
result of that call available under `VAR_NAME`.\
You can verify what the
service returns interactively. Example of an attribute read:

![Service Response Example](ServiceResponse.png)

Not all available commands are documented. The undocumented ones were in
the original repository.\
Some of these undocumented commands seem to be
Expand Down Expand Up @@ -1734,6 +1745,12 @@ tap_action:
See [script_use_zha_devices.yaml](examples/script_use_zha_devices.yaml) to
see how you can loop over the device list provided in the event data.

See
[script_use_zha_devices_response.yaml](examples/script_use_zha_devices_response.yaml)
shows how a new method available since Home Assistant 2023.7 that allows us
to avoid the complexity of event_data by using the response from the
zha-toolkit service.

### `register_services`: Reregister ZHA-Toolkit services

The services may have evolved after an update of the code and calling
Expand Down
34 changes: 34 additions & 0 deletions examples/script_use_zha_devices_response.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
alias: Loop over zha_devices, extract some device data. For HA>=2023.7
sequence:
- service: zha_toolkit.zha_devices
data:
response_variable: dev_data
- service: system_log.write
data:
logger: zha_devices
level: error
message: '{{ "Got device_data %s" % ( dev_data.devices ) }}'
- service: system_log.write
alias: List unavailable only
data:
logger: zha_devices
level: error
message: >
{% set ns = namespace(names=[]) %}
{% for item in dev_data.devices if not item.available %}
{% set ns.names = ns.names + [ "'%s'" % (item.name) ] %}
{% endfor %}
Items: {{ ns.names | join(', ') }}
- repeat:
for_each: '{{ dev_data.devices }}'
sequence:
- service: system_log.write
data:
logger: zha_devices
level: error
message: >-
{{ "Item '%s' Power: %s dBm Available: %s" % (
repeat.item.name, repeat.item.rssi, repeat.item.available
) }}
mode: single
Binary file added images/ServiceResponse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1e86aa5

Please sign in to comment.