zigpy-xbee

0.21.1 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize and start a `zigpy-xbee` controller application. It configures the serial port and baud rate, then starts the Zigbee network, optionally forming a new one if it doesn't exist. It also includes an example of enabling pairing mode for new devices to join. Ensure your XBee radio is pre-configured for API mode and the correct baud rate using Digi's XCTU tool.

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())

view raw JSON →