Simplified communication with home batteries
Project description
HomeBattery
Overview
This project provides a set of Python modules to interact with various inverters and battery management systems. The modules allow for reading and writing parameters such as power setpoints, battery state of charge, and grid voltage.
Features
-
Support for multiple inverter brands:
-
Victron
-
Huawei
-
Deye (via Modbus and Solarman data logger)
-
Sessy
-
SMA
-
-
Communication via Modbus TCP
-
Reading and writing inverter and battery parameters
-
Asynchronous API support for Sessy battery system
-
Comprehensive register mapping for each inverter
Tested Devices
The following devices have been used to test the implementations in this project:
- Victron Inverter: Victron MultiPlus-II
- Huawei SUN2000 Inverter: Huawei SUN2000-6KTL-M1
- Deye Inverter: Deye 12K-SG04LP3-EU
- Sessy Battery System: Sessy
- SMA Inverter: Sunny island 8.0H-13
Installation
For PyPi installation please run:
python3 -m pip install HomeBattery
For local installation please run this command:
python3 -m pip install .
Usage
Example usage for interacting with a Victron inverter:
Warning: Using this library to interact with your inverters and battery systems may result in the loss of settings or configurations. Ensure you have backed up all important settings before proceeding. Use this library at your own risk.
from HomeBattery import VictronInverter
# Initialize the Victron inverter
victron_inverter = VictronInverter(ip="192.168.1.100", port=502)
# Read active power
active_power = victron_inverter.get_active_power()
print(f"Active Power: {active_power} W")
# Read battery state of charge (SOC)
battery_soc = victron_inverter.get_battery_soc()
print(f"Battery SOC: {battery_soc} %")
# Read grid voltage
grid_voltage = victron_inverter.get_phase_voltage(phase=1)
print(f"Grid Voltage: {grid_voltage} V")
# Set power setpoint
victron_inverter.set_power_setpoint(1500)
print("Power setpoint set to 1500 W on phase 1")
Modules
1. modbus.py
Contains utility classes for interacting with Modbus devices:
-
ModbusDevice: Base class for Modbus communication. This class provides the foundational methods and properties required to communicate with Modbus devices. It handles the setup of communication parameters, sending and receiving Modbus requests, and processing responses.
-
Register: Data class that encapsulates the properties and behaviors of a Modbus register, providing methods to decode and build payloads based on the register's data type and other attributes.
-
AccessType and DataType: Enums for defining register properties.
AccessTypespecifies the read/write access for a register, whileDataTypeDefines the type of data stored in the register.
2. battery.py
Defines battery management classes:
-
Battery: Base battery class
-
ModbusBattery: Extends Battery to support Modbus communication
3. victron.py
Handles communication with Victron inverters:
-
Inherits from
ModbusBattery -
Supports reading active power, battery SOC, and grid voltage
-
Allows setting power and current setpoints
-
Implements comprehensive register mapping
4. sun2000.py
Handles communication with Sun2000 inverters:
-
Inherits from
ModbusBattery -
Supports reading active power, battery SOC, and grid voltage
-
Allows setting power and current setpoints
-
Implements comprehensive register mapping
5. deye_modbus.py
Handles communication with Deye inverters via Modbus:
-
Inherits from
ModbusBattery -
Supports reading active power, battery SOC, and grid voltage
-
Allows setting power and current setpoints
-
Implements comprehensive register mapping
6. deye_solarman.py
Handles communication with Deye inverters via Solarman data logger:
-
Inherits from
Battery -
Uses
pysolarmanv5for communication -
Similar functionality to
deye_modbus.pybut using the Solarman API
7. sessy.py
Handles communication with Sessy Battery System:
-
Uses
aiohttpfor asynchronous API communication with the Sessy API -
Supports reading active power, battery SOC, and grid voltage
-
Allows setting power and current setpoints
8. sma.py
Handles communication with SMA inverters:
-
Inherits from
ModbusBattery -
Supports reading active power, battery SOC, and grid voltage
-
Allows setting power and current setpoints
-
Implements comprehensive register mapping
Implementing a New Battery
To implement a new battery, follow these steps:
-
Define the Battery Class: Create a new class that inherits from the
BatteryorModbusBatteryclass, depending on the communication protocol used by the new battery system. -
Initialize the Battery: Implement the
__init__method to initialize the battery-specific parameters and call the superclass constructor. -
Register Mapping: If using Modbus communication, define the register mapping for the new battery system. You can use an Enum class to map the register addresses with their respective parameters, or you can use a dictionary or any other suitable data structure that fits your needs.
-
Implement Required Methods: Define methods to read and write battery parameters such as active power, state of charge (SOC), and grid voltage. Use the provided templates as a guide.
Example Battery Implementation
from battery import Battery
class MyNewBattery(Battery):
def __init__(
self,
capacity: int = None,
max_charge_power: int = None,
max_discharge_power: int = None,
soc: int = None
):
super().__init__(capacity, max_charge_power, max_discharge_power, soc)
# Initialize battery-specific parameters here
def get_active_power_total(self):
# Implement logic to get active power total
pass
def get_battery_soc(self):
# Implement logic to get battery SOC
pass
def get_phase_voltage(self):
# Implement logic to get grid voltage
pass
def set_power_setpoint(self, setpoint: int):
# Implement logic to set power setpoint
pass
Example Modbus Battery Implementation
from enum import Enum
from battery import ModbusBattery
from modbus import Register, AccessType, DataType
class MyNewModbusBattery(ModbusBattery):
def __init__(
self,
ip: str,
port: int = 502,
timeout: int = 3,
capacity: int = None,
max_charge_power: int = None,
max_discharge_power: int = None,
soc: int = None
):
super().__init__(ip, port, timeout, capacity, max_charge_power, max_discharge_power, soc)
def get_active_power(self):
return self.read_holding_register(self.NewBatteryRegisters.active_power_total.value)
def get_battery_soc(self):
return self.read_holding_register(self.NewBatteryRegisters.battery_soc.value)
def get_phase_voltage(self):
return self.read_holding_register(self.NewBatteryRegisters.grid_voltage.value)
def set_power_setpoint(self, setpoint: int):
self.write_register(self.NewBatteryRegisters.power_setpoint.value, setpoint)
class NewBatteryRegisters(Enum):
grid_voltage = Register(30000, "grid_voltage", DataType.UINT16)
battery_soc = Register(30001, "battery_soc", DataType.UINT16, gain=0.01)
active_power_total = Register(30002, "active_power_total", DataType.UINT16, gain=10)
power_setpoint = Register(30003, "power_setpoint", DataType.INT16, access_type=AccessType.READ_WRITE)
References
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
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 homebattery-0.0.13.tar.gz.
File metadata
- Download URL: homebattery-0.0.13.tar.gz
- Upload date:
- Size: 65.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f29473570a1a4efe551f3b0fb8402fc5ca101320291c8aa0db423eb2a915dadc
|
|
| MD5 |
9f09e9583f4e28a3d33644a84d9e05e9
|
|
| BLAKE2b-256 |
88c348aed8937f970a9ea39748fc028fc10370f45e3b282608458a1b02c432ed
|
Provenance
The following attestation bundles were made for homebattery-0.0.13.tar.gz:
Publisher:
workflow.yml on ElaadNL/HomeBattery
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
homebattery-0.0.13.tar.gz -
Subject digest:
f29473570a1a4efe551f3b0fb8402fc5ca101320291c8aa0db423eb2a915dadc - Sigstore transparency entry: 499499619
- Sigstore integration time:
-
Permalink:
ElaadNL/HomeBattery@6a1166700917cc6457b6a0dbc7b8514fb1013d02 -
Branch / Tag:
refs/tags/0.0.13 - Owner: https://github.com/ElaadNL
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@6a1166700917cc6457b6a0dbc7b8514fb1013d02 -
Trigger Event:
release
-
Statement type:
File details
Details for the file homebattery-0.0.13-py3-none-any.whl.
File metadata
- Download URL: homebattery-0.0.13-py3-none-any.whl
- Upload date:
- Size: 60.2 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 |
72991b7a4365b8322a76186e73fcfa5c6c06d5d116aac2c33df86201273cfc9a
|
|
| MD5 |
401792dd51b8cc2f615a6a068417006c
|
|
| BLAKE2b-256 |
b7f269e3e7fa668b58b46fe265ac282bf6a89b796589d482330988558283ba6a
|
Provenance
The following attestation bundles were made for homebattery-0.0.13-py3-none-any.whl:
Publisher:
workflow.yml on ElaadNL/HomeBattery
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
homebattery-0.0.13-py3-none-any.whl -
Subject digest:
72991b7a4365b8322a76186e73fcfa5c6c06d5d116aac2c33df86201273cfc9a - Sigstore transparency entry: 499499622
- Sigstore integration time:
-
Permalink:
ElaadNL/HomeBattery@6a1166700917cc6457b6a0dbc7b8514fb1013d02 -
Branch / Tag:
refs/tags/0.0.13 - Owner: https://github.com/ElaadNL
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@6a1166700917cc6457b6a0dbc7b8514fb1013d02 -
Trigger Event:
release
-
Statement type: