zigpy library

1.2.2 · active · verified Mon Apr 13

A low-level Python library implementing a full Zigbee stack, allowing communication with Zigbee devices through various hardware adapters. It is currently at version 1.2.2 and maintains a frequent release cadence, often with new minor or patch versions every 1-3 months, primarily focusing on bug fixes, new quirk support, and dependency updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize and start a `zigpy` application. It attempts to connect to a Zigbee coordinator specified by `ZIGBEE_DEVICE_PATH` and `ZIGBEE_DEVICE_BAUDRATE` environment variables (defaulting to '/dev/ttyUSB0' and 115200). Note that this example requires a physical Zigbee coordinator and a corresponding `zigpy` adapter library (e.g., `zigpy-znp`) to run successfully. Without a device, it will raise an exception.

import asyncio
import logging
import os
from zigpy.application import ControllerApplication
from zigpy.config import CONF_DATABASE_PATH, CONF_DEVICE, CONF_DEVICE_PATH, CONF_DEVICE_BAUDRATE

logging.basicConfig(level=logging.INFO)

async def run_zigbee_app():
    # In a real scenario, the device path would be your Zigbee coordinator's path
    # (e.g., '/dev/ttyUSB0' on Linux, 'COM3' on Windows) and its baudrate.
    # For this quickstart without a physical device, it will likely fail to connect.
    # Replace with valid device path and baudrate for successful execution with hardware.
    config = {
        CONF_DATABASE_PATH: "zigbee_quickstart.db",
        CONF_DEVICE: {
            CONF_DEVICE_PATH: os.environ.get("ZIGBEE_DEVICE_PATH", "/dev/ttyUSB0"),
            CONF_DEVICE_BAUDRATE: int(os.environ.get("ZIGBEE_DEVICE_BAUDRATE", "115200")),
        }
    }

    print(f"Attempting to start zigpy application with device: {config[CONF_DEVICE][CONF_DEVICE_PATH]}")
    app = None # Initialize app to None
    try:
        # Initialize the Zigbee controller application
        # auto_form=True will form a new network if one doesn't exist
        # auto_backup=True will manage database backups
        app = await ControllerApplication.new(
            config=config, auto_form=True, auto_backup=True
        )
        print("Zigbee application initialized. Starting up...")
        await app.startup(auto_form=True) # Start the network connection

        print("Zigbee application started. Press Ctrl+C to stop.")
        print("In a real application, you'd have event listeners, device discovery, etc. here.")
        # Keep the application running for a short period or until interrupted
        await asyncio.sleep(60) 
        
    except Exception as e:
        logging.error(f"Failed to start zigpy application: {e}")
        print("\nError: Ensure your Zigbee coordinator is connected and path/baudrate are correct.")
        print("You may need to install an adapter library (e.g., 'pip install zigpy-znp') for your hardware.")
    finally:
        if app and app.state.started:
            print("\nShutting down Zigbee application...")
            await app.shutdown()
            print("Application shut down.")

if __name__ == "__main__":
    asyncio.run(run_zigbee_app())

view raw JSON →