zigpy / zigpy-znp

TI CC2531, CC13x2, CC26x2 radio support for Zigpy and ZHA
GNU General Public License v3.0
145 stars 40 forks source link

a synopsis or template on how to use the zigpy-znp library #108

Closed pitchoun38 closed 2 years ago

pitchoun38 commented 2 years ago

The zigpy-znp library looks very nice and I wonder if this could be use as a standalone library imported in a python3 code, without the all zigpy stack.

I got some hints from the interesting zigpy-cli project , and so far I'm able to open the communication with the Zigbee usb key. However, I'm struggling on how to send zigbee raw command and how to receive them.

For receiving messages, I guess that I need to create a listener, but not sure on which API it should rely on.

For sending messages, that should I use the request(), broadcast() or mrequest() depending of unicast, briadcast or multi-cast ? is those API standard for each zigpy- ?

puddly commented 2 years ago

Central documentation is unfortunately lacking at the moment so here are some links:

Sending messages is usually done through zigpy itself, since it takes care of constructing the ZCL/ZDO header and eventually calling ControllerApplication.request:

await device.endpoints[1].on_off.toggle()
pitchoun38 commented 2 years ago

This looks very interesting and especially that one #709 thanks.

what is not clear for me is related to zipy- versus zigby stack. In other terms, can I develop a python application relying only on the zigpy- ?

What do you mean by "Received messages should be passed to handle_message." in #709 ? Do you mean we have to use add_listener() ? In that case the all zigpy stack mecanism came, correct ?

puddly commented 2 years ago

In other terms, can I develop a python application relying only on the zigpy- ?

Yes. Zigpy on its own just handles ZCL and ZDO, it doesn't talk to any radios and expects a ControllerApplication subclass to implement this. You can use any library's ControllerApplication in the same way (bellows.zigbee.application.ControllerApplication, zigpy_deconz.zigbee.application.ControllerApplication, etc.).

zigpy-znp is dependent on zigpy and internally uses its structures, like zigpy.device.Device, so it's not possible to use standalone unless you want to directly talk to the Texas Instruments radio with zigpy_znp.api.ZNP and handle everything yourself. Each radio library has a different internal interface for communicating with its Zigbee chip, which is why ControllerApplication exists.

What do you mean by "Received messages should be passed to handle_message."

That linked document is explaining how to write a radio library to talk to a new USB coordinator, like zigpy-znp. It describes the internals. Zigpy implements handle_message but you can subclass ControllerApplication and replace all of this.

In that case the all zigpy stack mecanism came, correct ?

What portion of zigpy are you trying to avoid? If you configure it to use its SQLite database, it will store device NWK and IEEE addresses and when performing initialization, persist the node descriptor, endpoint descriptors, and model/manufacturer attribute values. That's about it. Binding, setting up attribute reporting, and actual device control is up to your application.

If you can describe what you're trying to do, I can maybe come up with a better example application that shows how to use zigpy.

As an example of a small, standalone command line tool that uses ControllerApplication, take a look at https://github.com/zigpy/zigpy-znp/blob/dev/zigpy_znp/tools/energy_scan.py

Hedda commented 2 years ago

@pitchoun38 Is questions related to Domoticz-Zigate implementation of zigpy-znp and zigate-znp radio libraries discussed here:

https://github.com/zigpy/zigpy/discussions/865

At least it sounds as pipiche38 what to achieve the same thing in his Zigate plugin for Domoticz because that already have its own ZDO and ZCL layers which are working so believe he for now only want to partly use zigpy radio libraries for hardware abstraction.

You might also be interested in the somewhat related discussions here:

https://github.com/zigpy/zigpy/issues/842

https://github.com/zigpy/zigpy/pull/848

https://github.com/zigpy/zigpy/issues/557

As well as be aware of zigpy-cli here:

https://github.com/zigpy/zigpy-cli

https://github.com/zigpy/zigpy-cli/pulls

https://github.com/zigpy/zigpy-cli/issues

pitchoun38 commented 2 years ago

Thanks for the help