Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added slightly expanded AC180 support #109

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
10 changes: 10 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ This tool provides an MQTT interface to Bluetti power stations. State will be
published to the ``bluetti/state/[DEVICE NAME]/[PROPERTY]`` topic, and commands
can be sent to the ``bluetti/command/[DEVICE NAME]/[PROPERTY]`` topic.

Notes On This Fork
------------

Most of this repository is unchanged. It has been expanded to include support for AC180 and AC2A power stations.

To install this version of bluetti_mqtt
1) clone repository to RPi
2) From inside the repository directory run `pip install .` It's generally a good idea to do this in a virtual environment.


Installation
------------

Expand Down
8 changes: 6 additions & 2 deletions bluetti_mqtt/bluetooth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
from typing import Set
from bleak import BleakScanner
from bleak.backends.device import BLEDevice
from bluetti_mqtt.core import BluettiDevice, AC200M, AC300, AC500, AC60, EP500, EP500P, EP600, EB3A
from bluetti_mqtt.core import BluettiDevice, AC200M, AC300, AC500, AC180, AC60, EP500, EP500P, EP600, EB3A, AC2A
from .client import BluetoothClient
from .exc import BadConnectionError, ModbusError, ParseError
from .manager import MultiDeviceManager


DEVICE_NAME_RE = re.compile(r'^(AC200M|AC300|AC500|AC60|EP500P|EP500|EP600|EB3A)(\d+)$')
DEVICE_NAME_RE = re.compile(r'^(AC200M|AC300|AC500|AC60|AC180|EP500P|EP500|EP600|EB3A|AC2A)(\d+)$')


async def scan_devices():
Expand All @@ -33,6 +33,8 @@ def build_device(address: str, name: str):
return AC500(address, match[2])
if match[1] == 'AC60':
return AC60(address, match[2])
if match[1] == 'AC180':
return AC180(address, match[2])
if match[1] == 'EP500':
return EP500(address, match[2])
if match[1] == 'EP500P':
Expand All @@ -41,6 +43,8 @@ def build_device(address: str, name: str):
return EP600(address, match[2])
if match[1] == 'EB3A':
return EB3A(address, match[2])
if match[1] == 'AC2A':
return AC2A(address, match[2])


async def check_addresses(addresses: Set[str]):
Expand Down
2 changes: 2 additions & 0 deletions bluetti_mqtt/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from .devices.ep500p import EP500P
from .devices.ep600 import EP600
from .devices.eb3a import EB3A
from .devices.ac180 import AC180
from .devices.ac2a import AC2A
from .commands import (
DeviceCommand,
ReadHoldingRegisters,
Expand Down
66 changes: 66 additions & 0 deletions bluetti_mqtt/core/devices/ac180.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from typing import List
from ..commands import ReadHoldingRegisters
from .bluetti_device import BluettiDevice
from .struct import DeviceStruct

class AC180(BluettiDevice):
def __init__(self, address: str, sn: str):
self.struct = DeviceStruct()

#Core
# self.struct.add_swap_string_field('device_type', 110, 6)
# self.struct.add_sn_field('serial_number', 116)
self.struct.add_swap_string_field('device_type', 1101, 6)
self.struct.add_sn_field('serial_number', 1107)

#Battery Data
self.struct.add_swap_string_field('battery_type', 6101, 6)
self.struct.add_sn_field('battery_serial_number', 6107)
self.struct.add_version_field('bcu_version', 6175)
self.struct.add_uint_field('total_battery_percent', 102)

#Power IO
self.struct.add_uint_field('output_mode',123) #32 when both loads off, 40 when AC is on, 48 when DC is on, 56 when both on
self.struct.add_uint_field('dc_output_power', 140)
self.struct.add_uint_field('ac_output_power', 142)
self.struct.add_uint_field('dc_input_power', 144)
self.struct.add_uint_field('ac_input_power', 146) #this is a guess because I didn't have a PV module handy to test

#History
# self.struct.add_decimal_field('power_generation', 154, 1) # Total power generated since last reset (kwh)
self.struct.add_decimal_field('power_generation', 1202, 1) # Total power generated since last reset (kwh)

# this is usefule for investigating the available data
# registers = {0:21,100:67,700:6,720:49,1100:51,1200:90,1300:31,1400:48,1500:30,2000:67,2200:29,3000:27,6000:31,6100:100,6300:52,7000:5}
# for k in registers:
# for v in range(registers[k]):
# self.struct.add_uint_field('testI' + str(v+k), v+k)

super().__init__(address, 'AC180', sn)

@property
def polling_commands(self) -> List[ReadHoldingRegisters]:
return [
ReadHoldingRegisters(100, 62),
]

@property
def logging_commands(self) -> List[ReadHoldingRegisters]:
return [
ReadHoldingRegisters(0,21),
ReadHoldingRegisters(100, 67),
ReadHoldingRegisters(700,6),
ReadHoldingRegisters(720,49),
ReadHoldingRegisters(1100, 51),
ReadHoldingRegisters(1200, 90),
ReadHoldingRegisters(1300, 31),
ReadHoldingRegisters(1400, 48),
ReadHoldingRegisters(1500, 30),
ReadHoldingRegisters(2000, 67),
ReadHoldingRegisters(2200, 29),
ReadHoldingRegisters(3000, 27),
ReadHoldingRegisters(6000, 31),
ReadHoldingRegisters(6100, 100),
ReadHoldingRegisters(6300, 52),
ReadHoldingRegisters(7000,5)
]
66 changes: 66 additions & 0 deletions bluetti_mqtt/core/devices/ac2a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from typing import List
from ..commands import ReadHoldingRegisters
from .bluetti_device import BluettiDevice
from .struct import DeviceStruct

class AC2A(BluettiDevice):
def __init__(self, address: str, sn: str):
self.struct = DeviceStruct()

#Core
# self.struct.add_swap_string_field('device_type', 110, 6)
# self.struct.add_sn_field('serial_number', 116)
self.struct.add_swap_string_field('device_type', 1101, 6)
self.struct.add_sn_field('serial_number', 1107)

#Battery Data
self.struct.add_swap_string_field('battery_type', 6101, 6)
self.struct.add_sn_field('battery_serial_number', 6107)
self.struct.add_version_field('bcu_version', 6175)
self.struct.add_uint_field('total_battery_percent', 102)

#Power IO
self.struct.add_uint_field('output_mode',123) #32 when both loads off, 40 when AC is on, 48 when DC is on, 56 when both on
self.struct.add_uint_field('dc_output_power', 140)
self.struct.add_uint_field('ac_output_power', 142)
self.struct.add_uint_field('dc_input_power', 144)
self.struct.add_uint_field('ac_input_power', 146) #this is a guess because I didn't have a PV module handy to test

#History
# self.struct.add_decimal_field('power_generation', 154, 1) # Total power generated since last reset (kwh)
self.struct.add_decimal_field('power_generation', 1202, 1) # Total power generated since last reset (kwh)

# this is usefule for investigating the available data
# registers = {0:21,100:67,700:6,720:49,1100:51,1200:90,1300:31,1400:48,1500:30,2000:67,2200:29,3000:27,6000:31,6100:100,6300:52,7000:5}
# for k in registers:
# for v in range(registers[k]):
# self.struct.add_uint_field('testI' + str(v+k), v+k)

super().__init__(address, 'AC2A', sn)

@property
def polling_commands(self) -> List[ReadHoldingRegisters]:
return [
ReadHoldingRegisters(100, 62),
]

@property
def logging_commands(self) -> List[ReadHoldingRegisters]:
return [
ReadHoldingRegisters(0,21),
ReadHoldingRegisters(100, 62),
ReadHoldingRegisters(700,6),
ReadHoldingRegisters(720,49),
ReadHoldingRegisters(1100, 51),
ReadHoldingRegisters(1200, 90),
ReadHoldingRegisters(1300, 31),
ReadHoldingRegisters(1400, 48),
ReadHoldingRegisters(1500, 30),
ReadHoldingRegisters(2000, 67),
ReadHoldingRegisters(2200, 29),
ReadHoldingRegisters(3000, 27),
ReadHoldingRegisters(6000, 31),
ReadHoldingRegisters(6100, 100),
ReadHoldingRegisters(6300, 52),
ReadHoldingRegisters(7000,5)
]
Empty file.
59 changes: 59 additions & 0 deletions build/lib/bluetti_mqtt/bluetooth/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import logging
import re
from typing import Set
from bleak import BleakScanner
from bleak.backends.device import BLEDevice
from bluetti_mqtt.core import BluettiDevice, AC200M, AC300, AC500, AC180, AC60, EP500, EP500P, EP600, EB3A, AC2A
from .client import BluetoothClient
from .exc import BadConnectionError, ModbusError, ParseError
from .manager import MultiDeviceManager


DEVICE_NAME_RE = re.compile(r'^(AC200M|AC300|AC500|AC60|AC180|EP500P|EP500|EP600|EB3A|AC2A)(\d+)$')


async def scan_devices():
print('Scanning....')
devices = await BleakScanner.discover()
if len(devices) == 0:
print('0 devices found - something probably went wrong')
else:
bluetti_devices = [d for d in devices if d.name and DEVICE_NAME_RE.match(d.name)]
for d in bluetti_devices:
print(f'Found {d.name}: address {d.address}')


def build_device(address: str, name: str):
match = DEVICE_NAME_RE.match(name)
if match[1] == 'AC200M':
return AC200M(address, match[2])
if match[1] == 'AC300':
return AC300(address, match[2])
if match[1] == 'AC500':
return AC500(address, match[2])
if match[1] == 'AC60':
return AC60(address, match[2])
if match[1] == 'AC180':
return AC180(address, match[2])
if match[1] == 'EP500':
return EP500(address, match[2])
if match[1] == 'EP500P':
return EP500P(address, match[2])
if match[1] == 'EP600':
return EP600(address, match[2])
if match[1] == 'EB3A':
return EB3A(address, match[2])
if match[1] == 'AC2A':
return AC2A(address, match[2])


async def check_addresses(addresses: Set[str]):
logging.debug(f'Checking we can connect: {addresses}')
devices = await BleakScanner.discover()
filtered = [d for d in devices if d.address in addresses]
logging.debug(f'Found devices: {filtered}')

if len(filtered) != len(addresses):
return []

return [build_device(d.address, d.name) for d in filtered]
Loading