Async Python client for the Victron Energy VRM API and MQTT client for VRM
Project description
Victron Energy VRM API Client
An async Python client for the Victron Energy VRM API. This client is designed to be modular, easy to use, and compatible with Home Assistant.
Features
- Fully async API client using
httpx - Modular design organized by API categories
- Comprehensive error handling with custom exceptions
- Automatic token management and refresh
- Retry mechanism for failed requests
- Type hints and Pydantic models for data validation
- Compatible with Home Assistant
Installation
pip install victron-vrm
Usage
Demo Script
The package includes a demo script that uses the Victron Energy VRM API demo account to demonstrate the basic functionality of the library. You can run it without needing to provide any credentials:
python examples/demo.py
This script will:
- Get a demo token using the
/auth/loginAsDemoendpoint - Get all sites available in the demo account using the users module
- Get user information
- Get alarms for the site using the installations module
- Get tags for the site
- Get statistics for the site
- Get timezone for the site
Basic Usage
import asyncio
from victron_vrm import VictronVRMClient
async def main():
# Create the client with username and password
async with VictronVRMClient(
username="your_username",
password="your_password",
client_id="your_client_id",
) as client:
# Get all sites using the users module
sites = await client.users.list_sites()
print(f"Found {len(sites)} sites")
# Get a specific site
if sites:
site = sites[0]
# Get alarms for the site using the installations module
alarms = await client.installations.get_alarms(site.id)
print(f"Found {len(alarms.alarms)} alarms for site {site.name}")
# Alternatively, create the client with a token
async with VictronVRMClient(
token="your_token",
token_type="Bearer", # or "Token" for access tokens
) as client:
# Get all sites
sites = await client.users.list_sites()
print(f"Found {len(sites)} sites")
if __name__ == "__main__":
asyncio.run(main())
Using with Home Assistant
The client is designed to work with Home Assistant's async architecture. You can provide an existing httpx.AsyncClient session and use either username/password or token authentication:
import httpx
from victron_vrm import VictronVRMClient
async def setup_victron_vrm(hass, config):
# Use Home Assistant's existing httpx session
session = httpx.AsyncClient()
# Option 1: Using username and password
if "username" in config and "password" in config and "client_id" in config:
client = VictronVRMClient(
username=config["username"],
password=config["password"],
client_id=config["client_id"],
client_session=session,
)
# Option 2: Using a token
elif "token" in config:
client = VictronVRMClient(
token=config["token"],
token_type=config.get("token_type", "Bearer"),
client_session=session,
)
else:
raise ValueError("Either username/password/client_id or token must be provided")
# Get data from the API using the modular approach
sites = await client.users.list_sites()
# Get alarms for the first site
if sites:
site = sites[0]
alarms = await client.installations.get_alarms(site.id)
# Do something with the data
# ...
return True
API Reference
Client Initialization
VictronVRMClient(
username: Optional[str] = None,
password: Optional[str] = None,
client_id: Optional[str] = None,
token: Optional[str] = None,
token_type: str = "Bearer",
client_session: Optional[httpx.AsyncClient] = None,
request_timeout: int = 10,
max_retries: int = 3,
retry_delay: int = 1,
)
Either username, password, and client_id OR token must be provided. The token_type can be either "Bearer" (for JWT tokens) or "Token" (for access tokens).
Available Modules
The client is designed with a modular approach, organizing API endpoints into logical modules:
Users Module
Access via client.users:
get_me() -> User: Get the current useradd_site(installation_identifier: str) -> dict[str, str]: Add a new sitelist_sites(extended: bool = False, site_id: int | None = None) -> list[Site]: List all sitesget_site(site_id: int, extended: bool = False) -> Site | None: Get a specific sitecreate_access_token(name: str, expiry: Optional[int | datetime] = None) -> str: Create an access tokenlist_access_tokens() -> list[AccessToken]: List all access tokensrevoke_access_token(token_id: int | AccessToken) -> bool: Revoke an access tokenget_site_id_from_identifier(installation_identifier: str) -> Optional[int]: Get site ID from identifiersearch(query: str) -> list[dict[str, Any]]: Search for users
Installations Module
Access via client.installations:
get_alarms(site_id: int | Site) -> Alarms: Get alarms for a siteadd_alarm(site_id: int | Site, alarm: AlarmSettings) -> None: Add an alarm to a sitedelete_alarm(site_id: int | Site, alarm: AlarmSettings) -> None: Delete an alarm from a siteupdate_alarm(site_id: int | Site, alarm: AlarmSettings) -> AlarmSettings: Update an alarmclear_alarm(site_id: int | Site, alarm: AlarmSettings | int) -> None: Clear an alarmget_tags(site_id: int | Site) -> list[str]: Get tags for a siteadd_tag(site_id: int | Site, tag: str) -> None: Add a tag to a sitedelete_tag(site_id: int | Site, tag: str) -> None: Delete a tag from a sitestats(site_id: int | Site, ...) -> dict: Get statistics for a siteget_python_timezone(site_id: int | Site) -> Any: Get the Python timezone for a site
Error Handling
The client provides custom exceptions for different error scenarios:
from victron_vrm.exceptions import (
VictronVRMError,
AuthenticationError,
AuthorizationError,
NotFoundError,
RateLimitError,
ServerError,
ClientError,
ConnectionError,
TimeoutError,
ParseError,
)
async def example_with_error_handling():
try:
async with VictronVRMClient(...) as client:
sites = await client.get_sites()
except AuthenticationError:
print("Authentication failed")
except ConnectionError:
print("Connection error")
except VictronVRMError as err:
print(f"API error: {err}")
License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Development
To set up the development environment:
# Clone the repository
git clone https://github.com/KSoft-Si/victron-vrm.git
cd victron-vrm
# Install the package in development mode with test dependencies
pip install -e ".[test,dev]"
# Alternatively, if you have uv installed
uv pip install -e ".[test,dev]"
Testing
The project includes a test suite that uses the Victron Energy VRM API demo account. To run the tests:
# Run all tests
pytest
# Run with coverage report
pytest --cov=victron_vrm
# Run a specific test
pytest tests/test_client.py::test_get_sites
See the tests/README.md file for more information about the test suite.
CI/CD Workflow
This project uses GitHub Actions for continuous integration and deployment:
- Automated Testing: All pull requests and pushes to all branches are automatically tested against Python 3.11, 3.12, and 3.13.
- Automatic Versioning: When code is pushed to the main branch, the version is automatically incremented (patch version), a new tag is created, and a GitHub release is generated.
- Package Publishing: When code is pushed to the main branch, the package is automatically built and published to PyPI.
The workflow uses uv instead of pip for faster and more reliable dependency management.
Automatic Releases
The project is configured with automatic versioning and releases:
- When you push changes to the main branch, a GitHub Actions workflow automatically:
- Increments the patch version in
pyproject.toml(e.g., 0.1.0 → 0.1.1) - Commits the version change
- Creates a new tag (e.g., v0.1.1)
- Creates a GitHub release with automatically generated release notes
- Builds and publishes the package to PyPI
- Increments the patch version in
This means you don't need to manually update versions or create tags for routine updates.
Manual Releases
For major or minor version updates, you can still create releases manually:
- Update the version in
pyproject.toml(e.g., change 0.1.0 to 0.2.0 or 1.0.0) - Commit the changes:
git commit -am "Bump version to X.Y.Z" - Create a new tag:
git tag vX.Y.Z - Push the changes and tag:
git push && git push --tags
The GitHub Actions workflow will automatically build and publish the new version to PyPI.
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 victron_vrm-0.1.11.tar.gz.
File metadata
- Download URL: victron_vrm-0.1.11.tar.gz
- Upload date:
- Size: 129.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac0c0fd482977641456c257ad8502df1d11ad61247a1224e87b308fbdc67cff4
|
|
| MD5 |
b237fcfb102a9eb1bfbe5b4854ceb30e
|
|
| BLAKE2b-256 |
355bf037f390551bcbe923d1987be2d914fef6cf916508863b3bbeb063ad9f3c
|
Provenance
The following attestation bundles were made for victron_vrm-0.1.11.tar.gz:
Publisher:
ci-cd.yml on KSoft-Si/vrm-client
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
victron_vrm-0.1.11.tar.gz -
Subject digest:
ac0c0fd482977641456c257ad8502df1d11ad61247a1224e87b308fbdc67cff4 - Sigstore transparency entry: 833312290
- Sigstore integration time:
-
Permalink:
KSoft-Si/vrm-client@9a7b0bdb18de70dc14d298bd5d25f4dde8c40bd7 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/KSoft-Si
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci-cd.yml@9a7b0bdb18de70dc14d298bd5d25f4dde8c40bd7 -
Trigger Event:
push
-
Statement type:
File details
Details for the file victron_vrm-0.1.11-py3-none-any.whl.
File metadata
- Download URL: victron_vrm-0.1.11-py3-none-any.whl
- Upload date:
- Size: 29.0 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 |
b4b4fd187e68c1ef2bc3eb204d3c0971c770e372364f7b51998d7751405d4187
|
|
| MD5 |
9310a468ca95fa5be28bad11e89df012
|
|
| BLAKE2b-256 |
7cc20a906b88754fa9e1d564387cdcd3157ad8be4bc56d7d9a76305b60c44b84
|
Provenance
The following attestation bundles were made for victron_vrm-0.1.11-py3-none-any.whl:
Publisher:
ci-cd.yml on KSoft-Si/vrm-client
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
victron_vrm-0.1.11-py3-none-any.whl -
Subject digest:
b4b4fd187e68c1ef2bc3eb204d3c0971c770e372364f7b51998d7751405d4187 - Sigstore transparency entry: 833312291
- Sigstore integration time:
-
Permalink:
KSoft-Si/vrm-client@9a7b0bdb18de70dc14d298bd5d25f4dde8c40bd7 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/KSoft-Si
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci-cd.yml@9a7b0bdb18de70dc14d298bd5d25f4dde8c40bd7 -
Trigger Event:
push
-
Statement type: