Skip to main content

Async Python client library for OPNsense

Project description

aiopnsense

PyPI Downloads GitHub Release GitHub Release Date GitHub Activity Coverage Documentation License

aiopnsense is an async Python client library for OPNsense.

Documentation: https://aiopnsense.readthedocs.io

Requires OPNsense Firmware 26.1.1+

What this library does

aiopnsense wraps supported OPNsense REST endpoints behind a single async client, OPNsenseClient. It is designed for applications that need to query router state or trigger supported OPNsense actions without manually building HTTP requests.

The client currently includes helpers for:

  • system information, notices, certificates, CARP, Wake-on-LAN, reboot, and interface reloads
  • firmware version checks, update status, and upgrade actions
  • interface, gateway, CPU, memory, filesystem, and temperature telemetry
  • DHCP lease and ARP table access
  • firewall rules, NAT rules, alias toggling, and state killing
  • service status lookup and service start/stop/restart operations
  • Unbound blocklist management
  • OpenVPN and WireGuard status plus VPN instance toggling
  • vnStat metrics, captive portal vouchers, and speed test data

Installation

python -m venv .venv
source .venv/bin/activate
pip install aiopnsense

OPNsense documentation

In practice, use the generated OPNsense API key as the username and the generated secret as the password when constructing OPNsenseClient.

Simple usage

The client expects an existing aiohttp.ClientSession. Most applications create one session for the lifetime of the integration or service and reuse it for all requests.

Call await client.validate() when you want an explicit startup check before reusing a long-lived client. If you prefer context-manager lifecycle handling, async with client: validates on entry and calls async_close() on exit.

Read system state and telemetry

import asyncio
import aiohttp
from aiopnsense import OPNsenseClient

async def main() -> None:
    async with aiohttp.ClientSession() as session:
        client = OPNsenseClient(
            url="https://opnsense.example.com",
            username="YOUR_API_KEY",
            password="YOUR_API_SECRET",
            session=session,
        )
        await client.validate()

        system_info = await client.get_system_info()
        telemetry = await client.get_telemetry()

        print(f"Firewall name: {system_info.get('name')}")
        print(f"CPU telemetry: {telemetry.get('cpu')}")
        print(f"Filesystem telemetry: {telemetry.get('filesystems')}")

        await client.async_close()


asyncio.run(main())

Check firmware or control a service with async with

import asyncio
import aiohttp
from aiopnsense import OPNsenseClient

async def main() -> None:
    async with aiohttp.ClientSession() as session:
        async with OPNsenseClient(
            url="https://opnsense.example.com",
            username="YOUR_API_KEY",
            password="YOUR_API_SECRET",
            session=session,
            opts={"verify_ssl": True},
        ) as client:
            firmware = await client.get_firmware_update_info()
            services = await client.get_services()

            print(f"Current firmware: {firmware.get('product', {}).get('product_version')}")
            print(f"Available services: {[service.get('name') for service in services[:5]]}")

            restarted = await client.restart_service_if_running("unbound")
            print(f"Restarted unbound: {restarted}")

asyncio.run(main())

If the OPNsense router uses a private CA or self-signed certificate, pass opts={"verify_ssl": False}.

Development

Install the package in editable mode with test dependencies and run:

python -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
pip install --group dev -e .
pytest
prek run --all-files

Origin and Purpose

aiopnsense was initially extracted from the hass-opnsense integration. It is primarily for use as an external dependency by Home Assistant for its OPNsense Integration.

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

aiopnsense-1.0.6.tar.gz (90.3 kB view details)

Uploaded Source

Built Distribution

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

aiopnsense-1.0.6-py3-none-any.whl (54.6 kB view details)

Uploaded Python 3

File details

Details for the file aiopnsense-1.0.6.tar.gz.

File metadata

  • Download URL: aiopnsense-1.0.6.tar.gz
  • Upload date:
  • Size: 90.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for aiopnsense-1.0.6.tar.gz
Algorithm Hash digest
SHA256 2a3fae088258d5cc64ecab0575a221ac111d1b1a2b6a63b36ed3390a3573e0fe
MD5 63951178ac2319bd7e034af52a920130
BLAKE2b-256 97d2ae06f38a04b3fd8b939cd968f2dca65941df3ea63bc4a05978c47c7d82e5

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiopnsense-1.0.6.tar.gz:

Publisher: release.yml on Snuffy2/aiopnsense

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

File details

Details for the file aiopnsense-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: aiopnsense-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 54.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for aiopnsense-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 92ea1c505fe91491aa88f4df0fa1f8b94325473240ce47c943db7e105a1fde1c
MD5 e01664d61f0f008b5b7ab85bde3272e3
BLAKE2b-256 849125a9e116e94013c09e65deda8b64e8c03e64e1664539490594a889ffd920

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiopnsense-1.0.6-py3-none-any.whl:

Publisher: release.yml on Snuffy2/aiopnsense

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