custom-mygrid

mygrid-modbus

A standalone async Python device library for MyGrid ModuleTwo batteries, implementing manufacturer register map version 2. Requires Python 3.12+.

The library uses modbus-connection component models. Applications supply a ModbusUnit, choose the transport backend, and own the connection lifetime. No application framework is required. Install it from PyPI; the source is on GitHub.

Documentation

Read the Python library guide and Home Assistant setup guide on the MyGrid documentation website.

This README and the protocol reference are authoritative for Python usage, device communication and protocol decoding. Device prerequisites and library validation belong here too. Edit the source in this repository; the website publishes reviewed, commit-pinned snapshots and may lag this branch. Home Assistant installation, entities, Energy dashboard configuration and automation examples belong in ha-mygrid-modbus. Link to those guides instead of maintaining another copy. See CONTRIBUTING.md for documentation changes.

Installation

python -m pip install 'mygrid-modbus[tmodbus]'

From a checkout of this repository: python -m pip install '.[tmodbus]'.

Install the base package without extras when the consuming application supplies its own unit. The optional [pymodbus] extra selects the other supported backend.

Query a battery

The helper is read-only; it never sends power or restart commands. After the pip installation above, run:

mygrid-query 192.168.1.20 --port 502
mygrid-query 192.168.1.20 --port 502 --raw

From a checkout of this repository with uv installed:

uv sync --frozen
uv run script/query.py 192.168.1.20 --port 502
uv run script/query.py 192.168.1.20 --port 502 --raw

--raw includes an address-keyed register dump, suitable for replay in tests with mock_modbus_unit.load_raw(payload['registers']). The helper uses modbus_connection.cli_helper for connection arguments and backend selection. Only the documented TCP socket transport is offered. Unit ID is fixed to 1.

Device API

import asyncio

from modbus_connection import ModbusTcpParams
from modbus_connection.tmodbus import ModbusConnection
from mygrid_modbus import MyGridDevice


async def main() -> None:
    connection = ModbusConnection(ModbusTcpParams(host="192.168.1.20", port=502))
    try:
        device = MyGridDevice(connection.for_unit(1))
        report = await device.async_update()
        if "battery" in report.updated:
            print(device.battery.stored_energy)  # typed component attribute
            print(device.snapshot)  # immutable, validated readings
        if report.failed:
            print(report.failed)
        # Explicit commands only, if this application is the power controller:
        # accepted = await device.async_set_power(100)
        # await device.async_set_power(0)
        # await device.async_restart()
        raw = await device.async_read_raw()
        print(raw)
    finally:
        await connection.close()


asyncio.run(main())
API Contract
MyGridDevice(unit) Device composed of a typed Battery component
async_update() Returns UpdateReport; one pooled read of holding registers 0–17
battery Component with declarative signed/scaled/low-word-first fields
snapshot Last fully validated BatteryData; raises before the first good read
async_read() Convenience validated snapshot read; raises on failure
async_set_power(watts) Fresh limits, one validated write, confirmed readback
async_restart() One function-06 write of value 1 to register 9
async_read_raw() Fresh raw words grouped by Modbus space, replayable by the mock

Power is in W, energy in Wh. Positive power charges; negative power discharges. A setpoint is a signed I16 value in 100 W increments and must fall within fresh device limits. async_set_power(watts) returns the readback snapshot; compare its setpoint with the requested watts because the device may clamp the request if limits change. A matching readback confirms acceptance, not actual power delivery.

Status 0 means normal, 1 fault and 2 telemetry stale. The immutable snapshot suppresses SOC, measured power and faults when status is 2; other valid fields remain available. Raw dumps preserve exactly what the device returned. When a poll fails, inspect its report before treating cached component values or the last snapshot as current. An unsupported map or invalid SOC/status fails the component instead of publishing corrupt measurements.

MyGridError, UnsupportedMapError and InvalidSetpointError extend the neutral ModbusError hierarchy. Transport connection/timeouts retain their typed backend-neutral exceptions. Raw diagnostics raise the same errors. Operations have a 12-second budget after this device’s lock is acquired; the device requests a minimum 3-second request timeout through its unit. The owner resolves timing requirements across consumers.

The library never closes or disconnects the supplied unit, even on a failed command or cancellation. Both supported backends disable write retransmission. The device retains no desired setpoint and never replays commands. A failed confirmation can mean the write was applied; read the battery before manually deciding to issue another command.

Use async_set_power() for live-limit validation. Component field metadata describes writable fields and static validators, while the device method handles fresh limits and readback. All other registers are read-only except restart. The component constrains reads to the documented range and uses low-word-first U32 fields, including high fault bits.

Device prerequisites

Link the battery to your MyGrid account and select Home Assistant as the control source in the manufacturer’s app, then check whether Modbus TCP on port 502 is available. On the replacement development battery, the owner reported that it started updating once linked to the account; enabling Home Assistant control again (normally used via MQTT) then made Modbus TCP available. Both read-only live tests passed on 2026-11-09 with register map 2. See the validation record.

The earlier connection refusal affected the previous, now-replaced battery. It no longer establishes that a separate IT activation is required. If port 502 remains closed after account linking and selecting Home Assistant control, contact MyGrid support with the battery’s hostname and MAC address.

Use the same LAN and the Auto leader for a cluster. The battery accepts one TCP client at a time, so share a connection between local consumers. Choose one writer: sharing the socket does not arbitrate competing power strategies.

There is no TLS/authentication, serial transport, firmware-update API, hardware identity, follower enumeration or documented disconnection watchdog in this map. Keep port 502 on the trusted LAN. Closing the connection does not request idle. Cluster SOC, limits and setpoints have documented aggregate meanings; energy aggregation is not established by the manual. See protocol details.

Development and release

uv sync --frozen
uv run ruff check .
uv run ruff format --check .
uv run ty check .
uv run pytest --cov=mygrid_modbus
uv build
uv run twine check dist/*
uv run python scripts/check_artifacts.py

Tests use the supplied mock_modbus_unit/mock_modbus_connection pytest fixtures for models, failures and replayed dumps. Independent raw-frame TCP tests exercise both real backends, clamping, lost acknowledgments and connection sharing. CI covers Python 3.12–3.14 and built-wheel installation, including base imports without backend extras.

Live smoke tests

With a ModuleTwo that has Modbus TCP enabled, check that it still speaks register map 2:

MYGRID_HOST=192.168.0.15 uv run pytest -m live

They are read-only (no setpoint or restart is ever sent), skipped without MYGRID_HOST, and excluded from ordinary runs. A failure is classified as DEVICE UNAVAILABLE (cannot connect or no answer), DEVICE INCOMPATIBLE (unusable data) or DEVICE USES AN UNSUPPORTED REGISTER MAP. Both live tests passed against a ModuleTwo on 2026-11-09, and a separate read-only query returned map 2. See the validation record for readings, activation history and remaining verification limits.

Release instructions describe tag-based versions, trusted publishing and the required pypi environment. Publishing is not performed by local validation. The Apache-2.0 license covers original code; the manufacturer manual is included unmodified as docs/mygrid-modbus-tcp-manual-en.md and remains MyGrid’s. Read-only hardware smoke validation has passed; write behavior, activation persistence and offline operation remain unverified on hardware.