Async Python client library for OPNsense
Project description
aiopnsense
aiopnsense is an async Python client library for OPNsense.
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
- OPNsense API reference: https://docs.opnsense.org/development/api.html
- OPNsense API usage guide: https://docs.opnsense.org/development/how-tos/api.html
- Creating and maintaining API keys: https://docs.opnsense.org/manual/how-tos/user-local.html#creating-and-maintaining-api-keys
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.
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,
)
try:
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')}")
finally:
await client.async_close()
asyncio.run(main())
Check firmware or control a service
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,
opts={"verify_ssl": True},
)
try:
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}")
finally:
await client.async_close()
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aiopnsense-1.0.3.tar.gz.
File metadata
- Download URL: aiopnsense-1.0.3.tar.gz
- Upload date:
- Size: 67.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
67f5c019e820d085376e43be41dcf3173b9c7b18c0c456538bc31bae3307c1df
|
|
| MD5 |
e4aa86712ffadf616c5cc980d68ddfca
|
|
| BLAKE2b-256 |
1c35d63e1dd2a8b33df9105ad7e03a4df27d1604563e80ff1ca29f37b432ae4d
|
Provenance
The following attestation bundles were made for aiopnsense-1.0.3.tar.gz:
Publisher:
release.yml on Snuffy2/aiopnsense
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aiopnsense-1.0.3.tar.gz -
Subject digest:
67f5c019e820d085376e43be41dcf3173b9c7b18c0c456538bc31bae3307c1df - Sigstore transparency entry: 1186820418
- Sigstore integration time:
-
Permalink:
Snuffy2/aiopnsense@dbdb36359de4656dbf4d759dcc50e7fa97b2808e -
Branch / Tag:
refs/tags/v1.0.3 - Owner: https://github.com/Snuffy2
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@dbdb36359de4656dbf4d759dcc50e7fa97b2808e -
Trigger Event:
release
-
Statement type:
File details
Details for the file aiopnsense-1.0.3-py3-none-any.whl.
File metadata
- Download URL: aiopnsense-1.0.3-py3-none-any.whl
- Upload date:
- Size: 44.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
769950f94c20d2a427e3bd4f03bc2807270836460181d5d1770490b89bace0f5
|
|
| MD5 |
f19af65c6d5610ab8bd8231b55073dbe
|
|
| BLAKE2b-256 |
6dd90b77c633ecd1daf0b78276c8eaf3d48f56358449a9c12114c6c297f38395
|
Provenance
The following attestation bundles were made for aiopnsense-1.0.3-py3-none-any.whl:
Publisher:
release.yml on Snuffy2/aiopnsense
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aiopnsense-1.0.3-py3-none-any.whl -
Subject digest:
769950f94c20d2a427e3bd4f03bc2807270836460181d5d1770490b89bace0f5 - Sigstore transparency entry: 1186820444
- Sigstore integration time:
-
Permalink:
Snuffy2/aiopnsense@dbdb36359de4656dbf4d759dcc50e7fa97b2808e -
Branch / Tag:
refs/tags/v1.0.3 - Owner: https://github.com/Snuffy2
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@dbdb36359de4656dbf4d759dcc50e7fa97b2808e -
Trigger Event:
release
-
Statement type: