Skip to main content

Python library for UniFi network topology discovery and SVG rendering

Project description

unifi-topology

CI PyPI PyPI - Downloads Python License: MIT

Python library for UniFi network topology discovery and SVG diagram rendering.

Extracted from unifi-network-maps to provide a clean library API for programmatic use, including the Home Assistant integration.

Installation

pip install unifi-topology

Quick Start

from unifi_topology import (
    Config,
    SvgOptions,
    build_node_type_map,
    build_topology,
    extract_wan_info,
    fetch_devices,
    fetch_networks,
    normalize_devices,
    render_svg,
    resolve_svg_themes,
)

# Connect to UniFi controller
config = Config.from_env()
api_devices = fetch_devices(config)
networks = fetch_networks(config)

# Build topology model
devices = normalize_devices(api_devices)
node_types = build_node_type_map(devices)
gateways = [name for name, node_type in node_types.items() if node_type == "gateway"]
topology = build_topology(
    devices,
    include_ports=True,
    only_unifi=False,
    gateways=gateways,
)
gateway = next((device for device in devices if node_types.get(device.name) == "gateway"), None)
wan_info = extract_wan_info(gateway) if gateway else None

# Render SVG
theme = resolve_svg_themes(theme_name="unifi")
options = SvgOptions()
svg = render_svg(
    topology.tree_edges or topology.raw_edges,
    node_types=node_types,
    theme=theme,
    options=options,
    wan_info=wan_info,
)

API Overview

Adapters

  • Config -- Configuration from environment variables (UNIFI_URL, UNIFI_USER/UNIFI_PASS or UNIFI_API_KEY, etc.)
  • fetch_devices(config) -- Fetch device list from UniFi controller
  • fetch_clients(config) -- Fetch active clients
  • fetch_networks(config) -- Fetch network/VLAN configuration
  • fetch_firewall_zones(config) -- Fetch firewall zone definitions
  • fetch_firewall_policies(config) -- Fetch zone-based firewall policies
  • fetch_firewall_groups(config) -- Fetch firewall address/port groups
  • resolve_hostnames(ips, dns_server) -- Reverse DNS resolution

Model

  • normalize_devices(raw) -- Convert raw API data to Device objects
  • normalize_firewall_zones(raw) -- Convert raw zone data to FirewallZone objects
  • normalize_firewall_policies(raw) -- Convert raw policy data to FirewallPolicy objects
  • normalize_firewall_groups(raw) -- Convert raw group data to FirewallGroup objects
  • build_node_type_map(devices, clients=None, ...) -- Classify node names for rendering
  • build_topology(devices, *, include_ports, only_unifi, gateways) -- Build topology graph (TopologyResult with raw_edges and tree_edges)
  • build_device_inventory(devices) -- Build device info table (list[DeviceInfo])
  • extract_wan_info(device, ...) -- Extract WAN upstream info for a gateway device

Rendering

  • render_svg(edges, *, node_types, theme, options, ...) -- Orthogonal SVG diagram
  • render_svg_isometric(edges, *, node_types, theme, options, ...) -- Isometric 3D-style SVG
  • render_dual(edges, *, node_types, theme, options, ...) -- Physical + VLAN grouped SVG output
  • resolve_svg_themes(theme_name, theme_file) -- Load built-in or custom SVG theme

Built-in themes: unifi, unifi-dark, minimal, minimal-dark, classic, classic-dark

Development

python -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"

make lint        # ruff check
make format      # ruff format
make typecheck   # pyright
make test        # pytest
make ci          # all checks

Contributing

See CONTRIBUTING.md for development setup and guidelines.

License

MIT -- see LICENSE. Third-party licenses in LICENSES.md.

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

unifi_topology-2.2.2.tar.gz (402.0 kB view details)

Uploaded Source

Built Distribution

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

unifi_topology-2.2.2-py3-none-any.whl (418.4 kB view details)

Uploaded Python 3

File details

Details for the file unifi_topology-2.2.2.tar.gz.

File metadata

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

File hashes

Hashes for unifi_topology-2.2.2.tar.gz
Algorithm Hash digest
SHA256 1460c5542189ea359a8aa9f3fe3afa8727afc1370e02bd3fde41a8d9c6237061
MD5 1ede5377787fa7a799f3b7c82faadc9c
BLAKE2b-256 36b15f5f4193a810a38ddd8cd5540d5ad7967259da00f116c44b5cc259bef289

See more details on using hashes here.

Provenance

The following attestation bundles were made for unifi_topology-2.2.2.tar.gz:

Publisher: publish.yml on merlijntishauser/unifi-topology

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

File details

Details for the file unifi_topology-2.2.2-py3-none-any.whl.

File metadata

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

File hashes

Hashes for unifi_topology-2.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0a126c182dfc9fd6203d2c3ba8642c89cc6927e4cc7e4226f5b7f6455e778ab7
MD5 f676277940a0da1a282766672c04f9a6
BLAKE2b-256 ebb65f8714fbee7aa797ccefd4e121b26280dde007a8ea67c3820f16b9cf1c04

See more details on using hashes here.

Provenance

The following attestation bundles were made for unifi_topology-2.2.2-py3-none-any.whl:

Publisher: publish.yml on merlijntishauser/unifi-topology

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