Skip to main content

aiomarstek

PyPI Python CI License: MIT

Async client library for local Marstek device communication.

aiomarstek contains the UDP protocol, command builder, discovery cache, polling pause handling, and high-level status helpers used by the Home Assistant Marstek integration. hold_glitches runs inside get_device_status, so every consumer gets the same protection: a 0 % SOC reading right after a high level, and a single dip of a lifetime energy counter, are held at their previous value until they persist over a few polls (then accepted as real). Counters that do persist are passed on, and the sensors expose them as TOTAL_INCREASING, for which Home Assistant treats a lower reading as a meter reset.

Installation

python -m pip install aiomarstek

Example

import asyncio

from aiomarstek import MarstekUDPClient


async def main() -> None:
    client = MarstekUDPClient()
    await client.async_setup()
    try:
        devices = await client.discover_devices()
        print(devices)
    finally:
        await client.async_cleanup()


asyncio.run(main())

Units

Numeric status fields are normalized, so a consumer does not have to know which interface reported a value. Every energy counter is in Wh, although the reply scales differ per interface and per field:

Reply Counters Scale
ES.GetStatus total_pv_energy 0.01 kWh
ES.GetStatus grid and load totals Wh
Bat.GetStatus bat_total_*_energy 0.1 kWh
Bat.GetStatus backup_*_energy 0.001 kWh (i.e. Wh)
EM.GetStatus CT input_energy / output_energy 0.1 Wh
Energy.GetData day, month and total charge/discharge 0.01 kWh

FIELD_UNITS in aiomarstek/models.py is the single source of truth: it records the reply property name, the documented scale and the normalized unit of each scaled field, and the parsers take their factor from it. The scales follow the Marstek Device Local API Rev 4.0 field tables, cross-checked against a VenusE 3.0. Two points where the document alone is misleading:

  • EM.GetStatus.input_energy / output_energy still print [Wh] (*0.1) in Rev 4.0. They are read as 0.1 Wh, which reproduces the measured 230.68 kWh for a raw 2306800. ES.GetMode repeats the CT fields with the corrected [0.1 Wh] scale, but only documents them as valid in Auto and AI mode, so the library ignores that copy: EM.GetStatus is the single source of the CT fields (ct_state, phase/total power and the two energy counters).
  • The Energy.GetData counters are grid-side energy, not battery throughput: total_charge equals ES.GetStatus.total_grid_input_energy / 10 at the same instant. The battery-side totals are battery_total_input_energy / battery_total_output_energy from Bat.GetStatus.

Unscaled fields keep the unit the device reports: power in W, voltage in V, current in A, battery_soc / dod / battery_soh in %, battery_temperature in °C and the battery capacities in Wh.

Security considerations

The protocol is unauthenticated: build_command emits only id, method and params, and responses carry no token or signature either. The trust boundary is therefore the network — any host that can reach a device's UDP port can send it any command, and any host can answer a broadcast discovery request.

What the library does mitigate: with verify_source_ip=True (the default) a unicast response whose source address does not match the device the request was sent to is dropped, so another host cannot forge a status reply or fake a write success. That says nothing about whether the device itself validates the commands it receives — assume it does not until Marstek documents otherwise.

  • Never expose the device UDP port (default 30000) to untrusted networks or the internet; keep devices on a segment that only trusted hosts can reach.
  • The client binds 0.0.0.0:<local_port> (default 30000) for replies; restrict inbound UDP on that port to the device addresses.
  • Discovery accepts responses from any host, so treat discovery results as untrusted input.
  • Debug logging records full request/response JSON and peer addresses; disable it after troubleshooting and scrub network identifiers before sharing logs.

Development

python -m pip install -e ".[test]"
python -m pytest
python -m ruff check .
python -m build

Metadata

Release files for aiomarstek 0.2.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aiomarstek 0.2.5
File Size Uploaded Provenance Actions
aiomarstek-0.2.5.tar.gz 59.6 kB With provenance Details for aiomarstek-0.2.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for aiomarstek 0.2.5
File Interpreter ABI Platform Actions
aiomarstek-0.2.5-py3-none-any.whl Python 3 none any Details for aiomarstek-0.2.5-py3-none-any.whl

Total release size: 95.9 kB

Release files / aiomarstek-0.2.5.tar.gz

Download URL aiomarstek-0.2.5.tar.gz
Size 59.6 kB
Tags Source
SHA-256 checksum
How to use checksums
0d98cd402a496c426ec13fd0cec733a1d630eb223fc79de7226cc3c9cface657
BLAKE2b-256 checksum
How to use checksums
fe4bf63ae95afb6f185b434e0234887daa57ce458eeeb791320c54a841532733
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.

Transparency log

Release files / aiomarstek-0.2.5-py3-none-any.whl

Download URL aiomarstek-0.2.5-py3-none-any.whl
Size 36.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
55c6a3b2345b039c1111e17f682bede2d0493ce30bac0bd250052410f96f2ed9
BLAKE2b-256 checksum
How to use checksums
eddb5590502b2b1a1da0c8a60bbdf8431cb6939754a1b367a27a42a675391d3a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.

Transparency log

Security for aiomarstek 0.2.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for aiomarstek-0.2.5.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page