mdeweerd / zha-toolkit

🧰 Zigbee Home Assistant Toolkit - service for "rare" Zigbee operations using ZHA on Home Assistant
GNU General Public License v3.0
171 stars 17 forks source link

Can't get data from API call #220

Open tunerooster opened 4 months ago

tunerooster commented 4 months ago

I am trying to use curl to read a zigbee attribute from a device.

From within Home Assistant, it works. I.e., this service request:

service: zha_toolkit.attr_read
data:
  ieee: switch.tz3210_j4pdtz9v_ts0001_switch
  endpoint: 1
  cluster: 0
  attribute: 3

Returns this response:

zha_toolkit_version: v1.1.8
zigpy_version: 0.60.4
zigpy_rf_version: 0.37.6
ieee_org: switch.tz3210_j4pdtz9v_ts0001_switch
ieee: a4:c1:38:09:a5:0d:19:c9
command: attr_read
command_data: null
start_time: "2024-02-17T01:39:31.705066+00:00"
errors: []
params:
  endpoint_id: 1
  cluster_id: 0
  attr_id: 3
  dir: 0
  tries: 1
  expect_reply: true
  args: []
  read_before_write: true
  read_after_write: true
attr_type: "0x20"
write_is_equal: false
result_read:
  - "3": 1
  - {}
success: true

However when I try to do the same thing via the Home Assistant API, as follows:

curl -X POST -H "$BEARERAUTH" -H "Content-Type: application/json" --data '{"ieee": "switch.tz3210_j4pdtz9v_ts0001_switch", "endpoint": 1, "cluster": 0, "attribute": 3}' http://homeassistant:8123/api/services/zha_toolkit/attr_read

It prints only:

[]

Which means the request succeeded, but there is no data in the response. I want to get a full json response with the same data as the Home Assistant interactive service request.

Any advice is appreciated!

mdeweerd commented 4 months ago

When used in automations, if you want a reply, you need to add expect_reply. I'ld expect that at best that is possible in the HA webservice as well.

If not, we would need to check the documentation how an integration can impact the web reply.

Other approaches I think of:

tunerooster commented 4 months ago

Thanks for your reply! My main reason for doing this is to determine if the zigbee decice is connected. The device occasionally drops out, and I want to determine (in a script) if that has happened, without changing the state of the device. When I query the device state directly, if replies "on" even if the device is powered off (unplugged). Also, reading from the cache would not likely serve my purpose for the same reason. I will look into the references you suggest...

Where do you put the "expect_reply" in the call to curl? Like this:

curl -H "$BEARERAUTH" -H "Content-Type: application/json" -d '{"ieee": "switch.tz3210_j4pdtz9v_ts0001_switch", "endpoint": 1, "cluster": 0, "attribute": 3, "expect_reply": "true"}' http://homeassistant:8123/api/services/zha_toolkit/attr_read

The above expect_reply did not work. Same "[]" response.

Do you know of any better ways to test the zigbee connection status? Thanks again!

mdeweerd commented 4 months ago

You could also look into the webhooks functionnality.

I made a mistake about expect_reply - that's to indicate that the service call should wait for a reply for the device (I did not check again).

The option is actually "response_variable" for use in scripts, so maybe you can set that in your request.

The functionality for the response data was announced here and implemented in zha-toolkit at about that period: https://www.home-assistant.io/blog/2023/07/05/release-20237/

mdeweerd commented 4 months ago

Here are two other approaches:

Read value, write to CSV file

See https://github.com/mdeweerd/zha-toolkit/blob/main/examples/script_read_basic_cluster.yaml for an example where a value is read and written to a CSV file that is put in a file accessible as a URL.

Update entity attributes, get entity attributes

As you can write values read directly to a state, you could update a state on a regular basis and read that state using the W.

Use zha_devices service from zha

See https://github.com/mdeweerd/zha-toolkit/blob/main/examples/script_use_zha_devices_response.yaml . You can use that information to help determine the state of your device and report it.

Use the ha_set_state service from zha-toolkit to update a state that you can read through the web service.

tunerooster commented 4 months ago

I tried this, as you suggested, but still no data:

curl -H "$BEARERAUTH" -H "Content-Type: application/json" -d '{"ieee": "switcurl -H "$BEARERAUTH" -H "Content-Type: application/json" -d '{"ieee": "switch.tz3210_j4pdtz9v_ts0001_switch", "endpoint": 1, "cluster": 0, "attribute": 3, "response_variable": "true"}' http://homeassistant:8123/api/services/zha_toolkit/attr_read
ch.tz3210_j4pdtz9v_ts0001_switch", "endpoint": 1, "cluster": 0, "attribute": 3, "response_variable": "true"}' http://homeassistant:8123/api/services/zha_toolkit/attr_read

[]

Is the curl call correct? What should the value of "response_variable" be?

mdeweerd commented 4 months ago

It is possible that the HA web interface does not return a value. The value for response_variable is a string here which is ok because the value is the name of the variable to set, however, "true" may not be an accepted name for a variable.

tunerooster commented 4 months ago

I can't afford to change the state of the device, as any change will trigger/activate the device. (It is a fingerbot which interprets any on/off/toggle of its state to be a "click" and a button is pushed which turns on/off another device.)

Are you saying to use a HA script to call the service and output the data using csvout: "{{ csv }}"? Is "csvout" a zha_toolkit attribute? I'm not quite understanding, even looking at the script_read_basic_cluster.yaml script.

The response_variable sounds the most promising, but I can't see how to use it, even after reading the reference you provided.

I very much appreciate your help!

mdeweerd commented 4 months ago

I can't afford to change the state of the device, ...

That's not what I am suggesting - you can create another entity/state with the value that you want to observe.

Are you saying to use a HA script to call the service and output the data using csvout: "{{ csv }}"? Is "csvout" a zha_toolkit attribute?

'csvout' is a parameter to several zha-toolkit services - including the read attribute - it will write the information to the indicated CSV file. "{{ csv }}" is a template indicating to Home Assistant to return the value of 'csv' which is an input/parameter to the example script.

The response_variable sounds the most promising, but I can't see how to use it, even after reading the reference you provided.

Maybe the response is not available in a simple curl request - I guess it is available if you are using a websocket (which means that you have a continuous connection). I shared the python script I developed for another project that could help with that - but it seems to be above your skillset.

tunerooster commented 4 months ago

Thanks for your explanation. I will keep trying. But yes, it's challenging for me. Best regards!

On Sat, Feb 17, 2024, 4:57 AM MDW @.***> wrote:

I can't afford to change the state of the device, ...

That's not what I am suggesting - you can create another entity/state with the value that you want to observe.

Are you saying to use a HA script to call the service and output the data using csvout: "{{ csv }}"? Is "csvout" a zha_toolkit attribute?

'csvout' is a parameter to several zha-toolkit services - including the read attribute - it will write the information to the indicated CSV file. "{{ csv }}" is a template indicating to Home Assistant to return the value of 'csv' which is an input/parameter to the example script.

The response_variable sounds the most promising, but I can't see how to use it, even after reading the reference you provided.

Maybe the response is not available in a simple curl request - I guess it is available if you are using a websocket (which means that you have a continuous connection). I shared the python script I developed for another project that could help with that - but it seems to be above your skillset.

— Reply to this email directly, view it on GitHub https://github.com/mdeweerd/zha-toolkit/issues/220#issuecomment-1949983935, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADFMVIVZDSQSLS5ETWRGU23YUCLMLAVCNFSM6AAAAABDM5FDMWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNBZHE4DGOJTGU . You are receiving this because you authored the thread.Message ID: @.***>