# How to discover/pair ZHA compliant Zigbee devices with Xbee python library

**URL:** <https://forums.digi.com/t/how-to-discover-pair-zha-compliant-zigbee-devices-with-xbee-python-library/23267>\
**Category:** Python\
**Tags:** library, python, xbee3\
**Created:** [October 28, 2022, 8:50am UTC](https://forums.digi.com/t/how-to-discover-pair-zha-compliant-zigbee-devices-with-xbee-python-library/23267 "2022-10-28T08:50:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahsan69](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@ahsan69](https://forums.digi.com/u/ahsan69)\
**Post date:** [October 28, 2022, 8:50am UTC](https://forums.digi.com/t/how-to-discover-pair-zha-compliant-zigbee-devices-with-xbee-python-library/23267/1 "2022-10-28T08:50:23Z")

</div>

I have Xbee3USB which is setup as Coordinator (AP: 2, CE: 1, AO: 3/E, ZS: 2 with XCTU tool). In XCTU network mode, if “Scan” is pressed and ZHA compliant endpoint devices(i.e IKEA Tradfri light, Plug, Remote Control) are in paring mode then all the end devices are discovered and connected.

But how to do it with xbee python library([XBee Python Library — Digi XBee Python library 1.5.0 documentation](https://xbplib.readthedocs.io/en/latest/))? I have setup the environment in Windows and Python is the latest (3.11).

Any guideline how to discover zigbee endpoints and control the remote devices?

## The following code discovers only the Coordinator MAC address connected to the USB port:

```
# Instantiate a local XBee object.
xbee = XBeeDevice("COM1", 9600)

xbee.open()

# Define the device discovered callback.
def callback(remote):
    print(remote)

# Get the XBee network object from the local XBee.
xnet = xbee.get_network()

# Add the device discovered callback.
xnet.add_device_discovered_callback(callback)

xnet.set_deep_discovery_options(deep_mode=NeighborDiscoveryMode.FLOOD,
                                del_not_discovered_nodes_in_last_scan=False)

# Configure the discovery timeout, in SECONDS.
xnet.set_deep_discovery_timeouts(node_timeout=25, time_bw_requests=10,
                                time_bw_scans=20)

# Start the discovery process.
xnet.start_discovery_process(deep=True, n_deep_scans=1)

while xnet.is_discovery_running():
    time.sleep(0.5)

if xbee is not None and xbee.is_open():
    xbee.close()

```

* * *

## I have also tried to run: xbee.execute\_command(“ND”) and xbee.execute\_command(“AS”). But no success.

---

<div class="post-metadata">

**Author:** ![mvut](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.digi.com/mvut/32/31_2.png) [@mvut](https://forums.digi.com/u/mvut)\
**Post date:** [October 28, 2022, 12:02pm UTC](https://forums.digi.com/t/how-to-discover-pair-zha-compliant-zigbee-devices-with-xbee-python-library/23267/2 "2022-10-28T12:02:53Z")

</div>

You are going to need to do a mixture of [https://xbplib.readthedocs.io/en/latest/user\_doc/discovering\_the\_xbee\_network.html?highlight=Zigbee%20device%20object%20discovery#configure-the-standard-discovery-process](https://xbplib.readthedocs.io/en/latest/user_doc/discovering_the_xbee_network.html?highlight=Zigbee%20device%20object%20discovery#configure-the-standard-discovery-process) and [https://xbplib.readthedocs.io/en/latest/\_modules/digi/xbee/models/zdo.html?highlight=Zigbee%20device%20object%20discovery](https://xbplib.readthedocs.io/en/latest/_modules/digi/xbee/models/zdo.html?highlight=Zigbee%20device%20object%20discovery)

---

<div class="post-metadata">

**Author:** ![ahsan69](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@ahsan69](https://forums.digi.com/u/ahsan69)\
**Post date:** [October 31, 2022, 9:04am UTC](https://forums.digi.com/t/how-to-discover-pair-zha-compliant-zigbee-devices-with-xbee-python-library/23267/3 "2022-10-31T09:04:13Z")

</div>

Thanks for your help again. With the following steps, I am able to discover the devices.  
Step1:

- With XCTU - I needed changes only (AP:2, ZS:2, CE:1) from default settings. I kept AO as the default value: 0.
- Also, I used the same discovery timing settings as in XCTU network working mode (25s timeout, 3s between Req and 5 sec between Scan). It was 30s timeout there. But the xbee python module complains if it more than 25s. I have not studied yet in the code. But it is OK.
- Increased number of scanning cycles for some extended time.  
Step2: Run the python code and put the device in pairing mode

* * *

## import datetime import random import string import time import sys from digi.xbee.devices import XBeeDevice from digi.xbee.devices import ZigBeeDevice from digi.xbee.exception import TimeoutException from digi.xbee.util import utils from digi.xbee.models.mode import NeighborDiscoveryMode def main(): # Instantiate a local XBee object. xbee = ZigBeeDevice(“COM12”, 9600) # Define the device discovered callback. def dcallback(remote): print(remote) try: xbee.open() # Get the XBee network object from the local XBee. xnet = xbee.get\_network() xnet.clear() # Add the device discovered callback. xnet.add\_device\_discovered\_callback(dcallback) xnet.set\_deep\_discovery\_options(deep\_mode=NeighborDiscoveryMode.CASCADE, del\_not\_discovered\_nodes\_in\_last\_scan=False) # Configure the discovery timeout, in SECONDS. xnet.set\_deep\_discovery\_timeouts(node\_timeout=25, time\_bw\_requests=3, time\_bw\_scans=5) # Start the discovery process. xnet.start\_discovery\_process(deep=True, n\_deep\_scans=5) while xnet.is\_discovery\_running(): time.sleep(0.5) nodes = xnet.get\_devices() print(nodes) finally: if xbee is not None and xbee.is\_open(): xbee.close() if **name** == ‘ **main** ’: main()

Result: MAC addresses of discovered Zigbee devices are printed.
