Skip to main content

Async Python client for the UniFi Access local API with WebSocket event support

Project description

py-unifi-access

CI License: MIT Python 3.12+

Async Python client for the UniFi Access local API with WebSocket event support. Designed for Home Assistant Core integrations.

Features

  • Async REST client (aiohttp) for doors, lock rules, emergency status
  • WebSocket with auto-reconnect and exponential backoff
  • Typed Pydantic v2 models for all API responses and 13 WebSocket event types
  • Stateless design for Home Assistant's DataUpdateCoordinator pattern

Installation

pip install py-unifi-access

Usage

import aiohttp
from unifi_access_api import UnifiAccessApiClient

async with aiohttp.ClientSession() as session:
    client = UnifiAccessApiClient("192.168.1.1", "your-api-token", session)

    # Authenticate
    await client.authenticate()

    # Get all doors (keyed by ID for quick lookup)
    doors = {d.id: d for d in await client.get_doors()}
    for door in doors.values():
        print(f"{door.name}: {door.door_position_status}")

    # Unlock a specific door by ID
    await client.unlock_door("your-door-id")

    # WebSocket for real-time events
    ws = client.start_websocket({
        "access.data.device.update": lambda msg: print(msg),
    })

Capturing WebSocket Messages

If you encounter parsing errors or unexpected behaviour with WebSocket events, capturing the raw message stream helps narrow down the issue. The CLI (included in the cli extra) writes two files to the current directory:

File Content
events_<datetime>_raw.jsonl All raw events, written before parsing
events_<datetime>_parsed.jsonl Successfully parsed events only
# 1. Create and activate a virtual environment
python3 -m venv .venv
source .venv/bin/activate

# 2. Install the package with CLI dependencies
pip install "py-unifi-access[cli]"

# 3. Record events (60 seconds, for example)
unifi-access -H 192.168.1.1 -t <TOKEN> --no-verify-ssl listen -d 60

Trigger the action you want to inspect (e.g. an access request) during the capture window, then review the events_*_raw.jsonl file before sharing it when reporting an issue. Raw payloads may contain sensitive data such as tokens or identifiers (for example MAC addresses and device/user names), so please redact secrets/identifiers first or share the file privately if needed.

Development

python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"
pre-commit install
pytest tests/ --cov

Releases

Merges to main now trigger an automatic release when the version in pyproject.toml changes. The workflow will:

  • run CI
  • build the package
  • publish it to PyPI
  • create and push a matching Git tag
  • create a GitHub release with generated notes

To make PyPI publishing work, configure a PyPI trusted publisher for this GitHub repository and environment:

  • repository: imhotep/py-unifi-access
  • workflow file: ci.yml
  • environment: pypi

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_unifi_access-1.3.0.tar.gz (34.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_unifi_access-1.3.0-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

Details for the file py_unifi_access-1.3.0.tar.gz.

File metadata

  • Download URL: py_unifi_access-1.3.0.tar.gz
  • Upload date:
  • Size: 34.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for py_unifi_access-1.3.0.tar.gz
Algorithm Hash digest
SHA256 cc3671af23f2669c43d515e17125a86569f8145cf928d6dc8fe99eddaf74de44
MD5 2798baa4669b6311b50e9fb17fb0297c
BLAKE2b-256 a12bf878c06188b0f21435e7524023f83db1fb37062ed1b5786f3e9b16632db2

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_unifi_access-1.3.0.tar.gz:

Publisher: ci.yml on imhotep/py-unifi-access

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py_unifi_access-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: py_unifi_access-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 22.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for py_unifi_access-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 95070b7423beb8c525b6b681fad57e0b995f4889a2ffa3cbb084c8fcdb81cc74
MD5 01a24c9df24cf70a15636b80039c9331
BLAKE2b-256 ea967d52aef8e573481967b4a4826bf61f288a5ba71708d2331c5e0a0d2356e5

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_unifi_access-1.3.0-py3-none-any.whl:

Publisher: ci.yml on imhotep/py-unifi-access

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page