zigpy-xbee
zigpy-xbee is a Python library that enables communication with XBee radio devices for the zigpy project. It acts as a hardware-specific radio library for zigpy, allowing users to integrate Digi XBee Zigbee modules into a Zigbee Home Automation setup. The library is currently at version 0.21.1 and requires Python 3.8 or newer. It is actively maintained as part of the broader zigpy ecosystem, with updates typically coinciding with changes in zigpy or Home Assistant's ZHA component.
Common errors
-
Failed to connect: 'serial.serialutil.SerialException: Could not open port /dev/ttyUSB0: [Errno 13] Permission denied:'
cause The current user does not have permission to access the serial port.fixAdd your user to the `dialout` group (or equivalent) and reboot: `sudo usermod -a -G dialout $USER` -
Failed to connect: 'serial.serialutil.SerialException: read failed: device reports readiness to read but returned no data (device disconnected or raw data error)'
cause The XBee device is not properly connected, its firmware is not in API mode, or the baud rate is incorrect.fixVerify USB cable connection and XBee orientation. Use Digi's XCTU to confirm the XBee is in API mode (ATAP2) and the baud rate (e.g., 57600/ATBD6) matches your `zigpy-xbee` configuration. Ensure the XBee is powered. -
Zigbee devices frequently disconnect or fail to respond, especially battery-powered end devices.
cause Weak Zigbee mesh network, Wi-Fi interference, or poor physical placement of the coordinator/routers.fixAdd more mains-powered Zigbee router devices to strengthen the mesh. Place the Zigbee coordinator on a USB extension cable away from computer/router. Adjust Wi-Fi channels to avoid overlap with Zigbee. Ensure devices are factory reset and paired in their final location, or close to the coordinator initially. -
AttributeError: module 'collections' has no attribute 'abc' (or similar during startup on Python 3.10+)
cause Older versions of a dependency (possibly `digi-xbee` or `zigpy` itself, which `zigpy-xbee` relies on) might be using deprecated `collections` module imports that were moved to `collections.abc` in Python 3.10.fixEnsure all `zigpy` related libraries, including `zigpy-xbee` and `zigpy`, are updated to their latest versions compatible with Python 3.10 or newer: `pip install --upgrade zigpy zigpy-xbee pyserial`.
Warnings
- breaking XBee radio firmware must be updated and configured for API mode (ATAP2) and a consistent baud rate (e.g., ATBD6 for 57600) using Digi's XCTU tool. Incorrect firmware or settings will prevent `zigpy-xbee` from communicating.
- gotcha USB 3.0 ports can cause 2.4GHz interference, negatively impacting Zigbee signal quality and reliability.
- gotcha Zigbee and 2.4GHz Wi-Fi operate on overlapping frequencies, leading to signal interference and an unreliable Zigbee network.
- deprecated There are ongoing discussions within the `zigpy/zha` ecosystem about potentially deprecating XBee and ZiGate radio support in future Home Assistant releases, which could affect `zigpy-xbee`'s primary use case.
Install
-
pip install zigpy-xbee
Imports
- ControllerApplication
from zigpy_xbee.zigbee.application import ControllerApplication
- XBee
import zigpy_xbee
Quickstart
import asyncio
import logging
import os
from zigpy_xbee.zigbee.application import ControllerApplication
logging.basicConfig(level=logging.INFO)
LOGGER = logging.getLogger(__name__)
async def main():
# Replace '/dev/ttyUSB0' with your XBee's serial port
# Ensure XBee is configured for API mode (ATAP2) and 57600 baud (ATBD6) using XCTU
serial_port = os.environ.get('ZB_SERIAL_PORT', '/dev/ttyUSB0')
baud_rate = 57600
# A persistent database file is highly recommended for zigpy applications
database_path = os.environ.get('ZB_DATABASE_PATH', 'zigbee.db')
try:
# Initialize the XBee ControllerApplication
application = ControllerApplication(
config={
'device': {
'path': serial_port,
'baudrate': baud_rate,
}
},
database_file=database_path
)
# Start the application
LOGGER.info(f"Starting zigpy-xbee on {serial_port}...")
await application.startup(auto_form=True)
LOGGER.info("zigpy-xbee application started successfully.")
# Example: Enable pairing mode for 120 seconds
LOGGER.info("Enabling pairing mode for 120 seconds...")
application.permit_joining(120)
# Keep the application running
while True:
await asyncio.sleep(3600) # Keep running for a long time or until interrupted
except Exception as e:
LOGGER.error(f"Failed to start zigpy-xbee: {e}")
finally:
if 'application' in locals() and application.state != 'dead':
LOGGER.info("Shutting down zigpy-xbee application.")
await application.shutdown()
if __name__ == '__main__':
asyncio.run(main())