Compare commits
204
Commits
0.0.9
...
encryption
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
773a9b2205 | ||
|
|
998362a5e6 | ||
|
|
206d22c50b | ||
|
|
e2ac4cf8f5 | ||
|
|
ba98204ab2 | ||
|
|
8912a0bd08 | ||
|
|
db0147e7c1 | ||
|
|
c5f985b5f6 | ||
|
|
63f798ca5a | ||
|
|
483ca4049d | ||
|
|
de1ca78c90 | ||
|
|
a72f42eb4a | ||
|
|
6ce8f24477 | ||
|
|
28cd715327 | ||
|
|
49c1409656 | ||
|
|
d61459c011 | ||
|
|
edf5e87720 | ||
|
|
742964b78b | ||
|
|
21e89e738d | ||
|
|
52fd1932fa | ||
|
|
3e54283faf | ||
|
|
9be8aa47ac | ||
|
|
8967f3379c | ||
|
|
6f9afedfdb | ||
|
|
fe23dd00cd | ||
|
|
566761dba7 | ||
|
|
33cb6e6fbd | ||
|
|
5823136280 | ||
|
|
7b6f845edb | ||
|
|
1fbcf83b1b | ||
|
|
da2017a24b | ||
|
|
83f0db267c | ||
|
|
149633f9c8 | ||
|
|
9ab7cefda7 | ||
|
|
52853d6d3e | ||
|
|
c96f551024 | ||
|
|
75209ee1aa | ||
|
|
7396616113 | ||
|
|
a7f7ff1843 | ||
|
|
cc755b3003 | ||
|
|
e5f690ef3b | ||
|
|
fa9bcf63ca | ||
|
|
211f1971c5 | ||
|
|
a1e9c180b3 | ||
|
|
0e5c1a0b44 | ||
|
|
0f33a31ee5 | ||
|
|
23551ae829 | ||
|
|
a54b36a90b | ||
|
|
f85661345c | ||
|
|
3d3401c942 | ||
|
|
f50232ee89 | ||
|
|
5922362772 | ||
|
|
6478825503 | ||
|
|
3169ee182e | ||
|
|
ece1fc4129 | ||
|
|
4d54e048c5 | ||
|
|
09a9112279 | ||
|
|
b37276ec52 | ||
|
|
4f88792371 | ||
|
|
7da392d85a | ||
|
|
03ea349cbf | ||
|
|
5a762266c0 | ||
|
|
12a59346e9 | ||
|
|
206099ab64 | ||
|
|
6be2abaaf2 | ||
|
|
c3bfad0ea6 | ||
|
|
b548528085 | ||
|
|
6e433e3551 | ||
|
|
b34d0a3039 | ||
|
|
2922c5cb90 | ||
|
|
9af3d67ec6 | ||
|
|
221da9a38d | ||
|
|
f5430216a8 | ||
|
|
591c86baf5 | ||
|
|
97154bb027 | ||
|
|
b2672ec302 | ||
|
|
f5b8e09fb4 | ||
|
|
c8d75d1899 | ||
|
|
efaa9d0fdb | ||
|
|
9315650ddc | ||
|
|
9df2073585 | ||
|
|
2c6df77078 | ||
|
|
e584d32037 | ||
|
|
5ac8d8d7ae | ||
|
|
0cd5ae10fb | ||
|
|
a93b80796a | ||
|
|
a7967e74dd | ||
|
|
5bd98d6de3 | ||
|
|
2a4c3e943c | ||
|
|
dcfebb0f84 | ||
|
|
44dbd2b965 | ||
|
|
89928e2d9c | ||
|
|
dc3aaf92e1 | ||
|
|
95b1dafeb1 | ||
|
|
15a890fb7d | ||
|
|
542f244a28 | ||
|
|
eb701de865 | ||
|
|
f8ed8141ac | ||
|
|
bf86fe172b | ||
|
|
babdd1755e | ||
|
|
7596f8cee1 | ||
|
|
98d2a0b73e | ||
|
|
c499fbcd9b | ||
|
|
ec996d5728 | ||
|
|
ec657ad59b | ||
|
|
df8375016c | ||
|
|
8901c11afc | ||
|
|
da5443172b | ||
|
|
85e50498f6 | ||
|
|
5fa1c8631f | ||
|
|
9cba466228 | ||
|
|
cc42617bdb | ||
|
|
4d27f108fa | ||
|
|
6412630c85 | ||
|
|
7f576df84b | ||
|
|
8bc8ba4a4b | ||
|
|
213ee82e63 | ||
|
|
c08099ca4f | ||
|
|
3f51dd829e | ||
|
|
64fbcbd08b | ||
|
|
c59881de7f | ||
|
|
842f133cd4 | ||
|
|
a50deaae37 | ||
|
|
9b7fd82591 | ||
|
|
e40e056e56 | ||
|
|
38e1fa4ecd | ||
|
|
564a971d7c | ||
|
|
caac9330b4 | ||
|
|
d8999c6470 | ||
|
|
c45994a9a6 | ||
|
|
18825f9062 | ||
|
|
8409221675 | ||
|
|
2abcbbb392 | ||
|
|
71bf9b1952 | ||
|
|
4008dc844e | ||
|
|
e27c28b5c8 | ||
|
|
d8c61e1ebe | ||
|
|
03b11a69d5 | ||
|
|
3a709f9bc5 | ||
|
|
e395c400b5 | ||
|
|
3d65d8b00f | ||
|
|
a183e8b02f | ||
|
|
432d46d0f1 | ||
|
|
928ff2bf5d | ||
|
|
d9d4860d44 | ||
|
|
5fcd57b517 | ||
|
|
4f92e9147e | ||
|
|
037ce83891 | ||
|
|
f7b4d8dc37 | ||
|
|
01cb7f35a5 | ||
|
|
cac64b40bd | ||
|
|
4231b34bb8 | ||
|
|
f6f19b27e1 | ||
|
|
0943ecf187 | ||
|
|
58f2ec50c4 | ||
|
|
ad536235bb | ||
|
|
ba6611b084 | ||
|
|
3e4b8601e8 | ||
|
|
ebbcea3c4c | ||
|
|
9ef9d38a6e | ||
|
|
463f02b09b | ||
|
|
8eb09a32f0 | ||
|
|
e8b9db9a42 | ||
|
|
9c7e5968bb | ||
|
|
0420511d90 | ||
|
|
dcc6131d02 | ||
|
|
11716084c9 | ||
|
|
aa1c3ef425 | ||
|
|
a8931e09c7 | ||
|
|
66a7de9051 | ||
|
|
528378ed9f | ||
|
|
b7c162aaef | ||
|
|
dda0d475f4 | ||
|
|
98f004dad1 | ||
|
|
515a06dc3a | ||
|
|
14f2219b94 | ||
|
|
4710a8183f | ||
|
|
186d73ace1 | ||
|
|
7c211cde6f | ||
|
|
4f99b7d746 | ||
|
|
70d7403aec | ||
|
|
33fe91c2d2 | ||
|
|
7904a07e7f | ||
|
|
18e48092c0 | ||
|
|
792da76a04 | ||
|
|
107ac9f0c8 | ||
|
|
3d8cf4b98f | ||
|
|
c3afdcfe89 | ||
|
|
d38c30011c | ||
|
|
32cfc34c37 | ||
|
|
7c25cbbe27 | ||
|
|
57d0969701 | ||
|
|
b8a73e0ab9 | ||
|
|
39c75b0f75 | ||
|
|
3dc1cff551 | ||
|
|
296c9cbfd3 | ||
|
|
0cadbb9931 | ||
|
|
3a662964b8 | ||
|
|
43be1e1dbe | ||
|
|
e0c7ada147 | ||
|
|
cf43c55cb8 | ||
|
|
a5ced35d32 | ||
|
|
15216e1268 | ||
|
|
ed10127a90 |
No files matched your search
@@ -0,0 +1,3 @@
|
|||||||
|
*/__pycache__/
|
||||||
|
*.pyc
|
||||||
|
.DS_Store
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
github: Patrick762
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
name: Bug Report
|
||||||
|
description: File a bug report
|
||||||
|
title: "[Bug]: "
|
||||||
|
labels: ["bug"]
|
||||||
|
assignees:
|
||||||
|
- octocat
|
||||||
|
body:
|
||||||
|
- type: textarea
|
||||||
|
id: what-happened
|
||||||
|
attributes:
|
||||||
|
label: What happened?
|
||||||
|
description: Also tell us, what did you expect to happen?
|
||||||
|
placeholder: Tell us what you see!
|
||||||
|
value: "An exception appeared"
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
id: version
|
||||||
|
attributes:
|
||||||
|
label: What version of our software are you running?
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
id: device
|
||||||
|
attributes:
|
||||||
|
label: What device are you seeing the problem on?
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: dropdown
|
||||||
|
id: bluetooth
|
||||||
|
attributes:
|
||||||
|
label: What bluetooth connection are you using?
|
||||||
|
multiple: false
|
||||||
|
options:
|
||||||
|
- USB dongle
|
||||||
|
- ESPHome bluetooth proxy
|
||||||
|
- Internal bluetooth adapter
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: dropdown
|
||||||
|
id: options-changed
|
||||||
|
attributes:
|
||||||
|
label: Have you changed the integration settings?
|
||||||
|
multiple: false
|
||||||
|
options:
|
||||||
|
- No
|
||||||
|
- Yes (Please write down the changed settings in the next step)
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
id: options-details
|
||||||
|
attributes:
|
||||||
|
label: Integration settings (if you changed them)
|
||||||
|
description: Please use one line per option
|
||||||
|
render: text
|
||||||
|
- type: textarea
|
||||||
|
id: logs
|
||||||
|
attributes:
|
||||||
|
label: Relevant log output
|
||||||
|
description: Please copy and paste any relevant log output. This will be automatically formatted into code, so no need for backticks.
|
||||||
|
render: shell
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
name: Close inactive issues
|
||||||
|
on:
|
||||||
|
schedule:
|
||||||
|
- cron: "30 1 * * *"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
close-issues:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
issues: write
|
||||||
|
pull-requests: write
|
||||||
|
steps:
|
||||||
|
- uses: actions/stale@v5
|
||||||
|
with:
|
||||||
|
days-before-issue-stale: 30
|
||||||
|
days-before-issue-close: 14
|
||||||
|
stale-issue-label: "stale"
|
||||||
|
stale-issue-message: "This issue is stale because it has been open for 30 days with no activity."
|
||||||
|
close-issue-message: "This issue was closed because it has been inactive for 14 days since being marked as stale."
|
||||||
|
days-before-pr-stale: -1
|
||||||
|
days-before-pr-close: -1
|
||||||
|
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
name: Unittests
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
pull_request:
|
||||||
|
# Allows you to run this workflow manually from the Actions tab
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Set up Python
|
||||||
|
uses: actions/setup-python@v3
|
||||||
|
with:
|
||||||
|
python-version: '3.x'
|
||||||
|
- name: Install dependencies
|
||||||
|
run: |
|
||||||
|
python -m pip install --upgrade pip
|
||||||
|
pip install -r requirements.txt
|
||||||
|
- name: Run unittests
|
||||||
|
run: python3 -m unittest discover -s tests -p "*_test.py"
|
||||||
@@ -1,2 +1,3 @@
|
|||||||
*/__pycache__/
|
*/__pycache__/
|
||||||
*.pyc
|
*.pyc
|
||||||
|
.DS_Store
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
FROM python:3
|
||||||
|
|
||||||
|
COPY requirements.txt .
|
||||||
|
RUN pip install -r requirements.txt
|
||||||
|
|
||||||
|
COPY tests ./tests
|
||||||
|
COPY custom_components ./custom_components
|
||||||
|
|
||||||
|
RUN python3 -m unittest discover -s tests -p "*_test.py"
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2024 Patrick762
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
@@ -1,29 +1,36 @@
|
|||||||
# hassio-bluetti-bt
|
# hassio-bluetti-bt
|
||||||
[](https://github.com/hacs/integration)
|
[](https://github.com/hacs/integration)
|
||||||
[](https://github.com/Patrick762/hassio-bluetti-bt/actions/workflows/hassfest_validation.yml)
|
[](https://github.com/Patrick762/hassio-bluetti-bt/actions/workflows/hassfest_validation.yml)
|
||||||
[](https://github.com/Patrick762/hassio-bluetti-bt/actions/workflows/HACS.yml)
|
[](https://github.com/Patrick762/hassio-bluetti-bt/actions/workflows/HACS.yml)
|
||||||
|
|
||||||
[](https://my.home-assistant.io/redirect/hacs_repository/?repository=hassio-bluetti-bt&owner=Patrick762)
|
|
||||||
|
|
||||||
Bluetti Integration for Home Assistant
|
Bluetti Integration for Home Assistant
|
||||||
|
|
||||||
Based on [bluetti_mqtt](https://github.com/warhammerkid/bluetti_mqtt).
|
## Disclaimer
|
||||||
|
This integration is provided without any warranty or support by Bluetti (unfortunately). I do not take responsibility for any problems it may cause in all cases. Use it at your own risk.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
To install this integration, you first need [HACS](https://hacs.xyz/) installed.
|
||||||
|
After the installation, you can use this button to install the integration:
|
||||||
|
|
||||||
|
[](https://my.home-assistant.io/redirect/hacs_repository/?owner=Patrick762&repository=hassio-bluetti-bt&category=integration)
|
||||||
|
|
||||||
### Supported devices:
|
### Supported devices:
|
||||||
(based on [supported devices of bluetti_mqtt](https://github.com/warhammerkid/bluetti_mqtt/tree/main/bluetti_mqtt/core/devices))
|
|
||||||
|
|
||||||
- AC60
|
- AC60
|
||||||
|
- AC70 (basic data)
|
||||||
|
- AC70P (untested)
|
||||||
|
- AC180 (basic data)
|
||||||
|
- AC180P (tested)
|
||||||
|
- AC200L (untested)
|
||||||
- AC200M
|
- AC200M
|
||||||
- AC300 (tested)
|
- AC300 (tested)
|
||||||
- AC500
|
- AC500 (tested)
|
||||||
- EB3A
|
- EB3A
|
||||||
- EP500
|
- EP500
|
||||||
- EP500P
|
- EP500P
|
||||||
- EP600 (tested)
|
- EP600 (tested)
|
||||||
|
- EP760 (basic data)
|
||||||
### Available sensors:
|
- EP800 (basic data)
|
||||||
All sensors which are available in bluetti_mqtt (Based on [this file](https://github.com/warhammerkid/bluetti_mqtt/blob/main/bluetti_mqtt/mqtt_client.py))
|
|
||||||
excluding battery pack fields.
|
|
||||||
|
|
||||||
### Available controls:
|
### Available controls:
|
||||||
If enabled in the Integration options (you need to reload the integration if you change this option):
|
If enabled in the Integration options (you need to reload the integration if you change this option):
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import re
|
import re
|
||||||
import logging
|
import logging
|
||||||
|
from typing import List
|
||||||
|
|
||||||
from homeassistant.components import bluetooth
|
from homeassistant.components import bluetooth
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
@@ -12,19 +13,37 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
|
|
||||||
from .const import DATA_COORDINATOR, DOMAIN, MANUFACTURER, CONF_USE_CONTROLS, DATA_POLLING_RUNNING
|
from .const import (
|
||||||
|
CONF_ENCRYPTION,
|
||||||
|
CONF_MAX_RETRIES,
|
||||||
|
CONF_PERSISTENT_CONN,
|
||||||
|
CONF_POLLING_INTERVAL,
|
||||||
|
CONF_POLLING_TIMEOUT,
|
||||||
|
CONF_USE_CONTROLS,
|
||||||
|
DATA_COORDINATOR,
|
||||||
|
DATA_POLLING_RUNNING,
|
||||||
|
DOMAIN,
|
||||||
|
MANUFACTURER,
|
||||||
|
)
|
||||||
from .coordinator import PollingCoordinator
|
from .coordinator import PollingCoordinator
|
||||||
|
|
||||||
PLATFORMS: [Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
PLATFORMS: List[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Set up Bluetti Powerstation from a config entry."""
|
"""Set up Bluetti Powerstation from a config entry."""
|
||||||
|
|
||||||
|
_LOGGER.debug("Init Bluetti BT Integration for device %s", entry.data.get(CONF_NAME))
|
||||||
|
|
||||||
address = entry.data.get(CONF_ADDRESS)
|
address = entry.data.get(CONF_ADDRESS)
|
||||||
device_name = entry.data.get(CONF_NAME)
|
device_name = entry.data.get(CONF_NAME)
|
||||||
use_controls = entry.data.get(CONF_USE_CONTROLS)
|
use_controls = entry.data.get(CONF_USE_CONTROLS)
|
||||||
|
polling_interval = entry.data.get(CONF_POLLING_INTERVAL, 60)
|
||||||
|
persistent_conn = entry.data.get(CONF_PERSISTENT_CONN, False)
|
||||||
|
polling_timeout = entry.data.get(CONF_POLLING_TIMEOUT, 120)
|
||||||
|
max_retries = entry.data.get(CONF_MAX_RETRIES, 5)
|
||||||
|
use_encryption = entry.data.get(CONF_ENCRYPTION, False)
|
||||||
|
|
||||||
if address is None:
|
if address is None:
|
||||||
return False
|
return False
|
||||||
@@ -38,10 +57,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
hass.data[DOMAIN][entry.entry_id].setdefault(DATA_POLLING_RUNNING, False)
|
hass.data[DOMAIN][entry.entry_id].setdefault(DATA_POLLING_RUNNING, False)
|
||||||
|
|
||||||
# Create coordinator for polling
|
# Create coordinator for polling
|
||||||
coordinator = PollingCoordinator(hass, address, device_name)
|
_LOGGER.debug("Creating coordinator")
|
||||||
|
coordinator = PollingCoordinator(hass, address, device_name, polling_interval, persistent_conn, polling_timeout, max_retries, use_encryption)
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
hass.data[DOMAIN][entry.entry_id].setdefault(DATA_COORDINATOR, coordinator)
|
hass.data[DOMAIN][entry.entry_id].setdefault(DATA_COORDINATOR, coordinator)
|
||||||
|
|
||||||
|
_LOGGER.debug("Creating entities")
|
||||||
platforms: list = PLATFORMS
|
platforms: list = PLATFORMS
|
||||||
if use_controls is True:
|
if use_controls is True:
|
||||||
_LOGGER.warning("You are using controls with this integration at your own risk!")
|
_LOGGER.warning("You are using controls with this integration at your own risk!")
|
||||||
@@ -50,6 +71,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
# Setup platforms
|
# Setup platforms
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, platforms)
|
await hass.config_entries.async_forward_entry_setups(entry, platforms)
|
||||||
|
|
||||||
|
_LOGGER.debug("Setup done")
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
@@ -69,23 +92,3 @@ def get_unique_id(name: str, sensor_type: str | None = None):
|
|||||||
if sensor_type is not None:
|
if sensor_type is not None:
|
||||||
return f"{sensor_type}.{res}"
|
return f"{sensor_type}.{res}"
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
def get_type_by_bt_name(bt_name: str):
|
|
||||||
"""Get the device type."""
|
|
||||||
dev_type = "Unknown"
|
|
||||||
if bt_name.startswith("AC200M"):
|
|
||||||
dev_type = "AC200M"
|
|
||||||
elif bt_name.startswith("AC300"):
|
|
||||||
dev_type = "AC300"
|
|
||||||
elif bt_name.startswith("AC500"):
|
|
||||||
dev_type = "AC500"
|
|
||||||
elif bt_name.startswith("AC60"):
|
|
||||||
dev_type = "AC60"
|
|
||||||
elif bt_name.startswith("EB3A"):
|
|
||||||
dev_type = "EB3A"
|
|
||||||
elif bt_name.startswith("EP500"):
|
|
||||||
dev_type = "EP500"
|
|
||||||
elif bt_name.startswith("EP600"):
|
|
||||||
dev_type = "EP600"
|
|
||||||
return dev_type
|
|
||||||
@@ -18,16 +18,13 @@ from homeassistant.helpers.update_coordinator import (
|
|||||||
CoordinatorEntity,
|
CoordinatorEntity,
|
||||||
)
|
)
|
||||||
|
|
||||||
from bluetti_mqtt.bluetooth import build_device
|
from .bluetti_bt_lib.field_attributes import FIELD_ATTRIBUTES, FieldType
|
||||||
from bluetti_mqtt.mqtt_client import (
|
from .bluetti_bt_lib.utils.device_builder import build_device
|
||||||
NORMAL_DEVICE_FIELDS,
|
|
||||||
DC_INPUT_FIELDS,
|
|
||||||
MqttFieldType,
|
|
||||||
)
|
|
||||||
|
|
||||||
from . import device_info as dev_info, get_unique_id
|
from . import device_info as dev_info, get_unique_id
|
||||||
from .const import DATA_COORDINATOR, DOMAIN, ADDITIONAL_DEVICE_FIELDS
|
from .const import DATA_COORDINATOR, DOMAIN, CONF_USE_CONTROLS
|
||||||
from .coordinator import PollingCoordinator, DummyDevice
|
from .coordinator import PollingCoordinator
|
||||||
|
from .utils import unique_id_loggable
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -35,31 +32,28 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Setup sensor entities."""
|
"""Setup binary_sensor entities."""
|
||||||
|
|
||||||
device_name = entry.data.get(CONF_NAME)
|
device_name = entry.data.get(CONF_NAME)
|
||||||
address = entry.data.get(CONF_ADDRESS)
|
address = entry.data.get(CONF_ADDRESS)
|
||||||
|
use_controls = entry.data.get(CONF_USE_CONTROLS, False)
|
||||||
if address is None:
|
if address is None:
|
||||||
_LOGGER.error("Device has no address")
|
_LOGGER.error("Device has no address")
|
||||||
|
|
||||||
# Generate device info
|
# Generate device info
|
||||||
_LOGGER.info("Creating sensors for device with address %s", address)
|
_LOGGER.info("Creating binary_sensors for device with address %s", address)
|
||||||
device_info = dev_info(entry)
|
device_info = dev_info(entry)
|
||||||
|
|
||||||
# Add sensors according to device_info
|
# Add sensors according to device_info
|
||||||
bluetti_device = build_device(address, device_name)
|
bluetti_device = build_device(address, device_name)
|
||||||
bluetti_device = DummyDevice(bluetti_device)
|
|
||||||
|
|
||||||
sensors_to_add = []
|
sensors_to_add = []
|
||||||
all_fields = NORMAL_DEVICE_FIELDS
|
all_fields = FIELD_ATTRIBUTES
|
||||||
all_fields.update(DC_INPUT_FIELDS)
|
|
||||||
all_fields.update(ADDITIONAL_DEVICE_FIELDS)
|
|
||||||
for field_key, field_config in all_fields.items():
|
for field_key, field_config in all_fields.items():
|
||||||
if bluetti_device.has_field(field_key):
|
if bluetti_device.has_field(field_key):
|
||||||
if field_config.type == MqttFieldType.BOOL:
|
if field_config.type == FieldType.BOOL:
|
||||||
category = None
|
|
||||||
if field_config.setter is True:
|
if field_config.setter is True:
|
||||||
category = EntityCategory.DIAGNOSTIC
|
continue
|
||||||
|
|
||||||
sensors_to_add.append(
|
sensors_to_add.append(
|
||||||
BluettiBinarySensor(
|
BluettiBinarySensor(
|
||||||
@@ -67,8 +61,7 @@ async def async_setup_entry(
|
|||||||
device_info,
|
device_info,
|
||||||
address,
|
address,
|
||||||
field_key,
|
field_key,
|
||||||
field_config.home_assistant_extra.get(CONF_NAME, ""),
|
field_config.name,
|
||||||
category=category,
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -85,46 +78,83 @@ class BluettiBinarySensor(CoordinatorEntity, BinarySensorEntity):
|
|||||||
address,
|
address,
|
||||||
response_key: str,
|
response_key: str,
|
||||||
name: str,
|
name: str,
|
||||||
category: EntityCategory | None = None,
|
|
||||||
):
|
):
|
||||||
"""Init battery entity."""
|
"""Init battery entity."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
|
|
||||||
|
self._attr_has_entity_name = True
|
||||||
e_name = f"{device_info.get('name')} {name}"
|
e_name = f"{device_info.get('name')} {name}"
|
||||||
self._address = address
|
self._address = address
|
||||||
self._response_key = response_key
|
self._response_key = response_key
|
||||||
|
self._unavailable_counter = 5
|
||||||
|
|
||||||
self._attr_device_info = device_info
|
self._attr_device_info = device_info
|
||||||
self._attr_name = e_name
|
self._attr_name = name
|
||||||
self._attr_available = False
|
self._attr_available = False
|
||||||
self._attr_unique_id = get_unique_id(e_name)
|
self._attr_unique_id = get_unique_id(e_name)
|
||||||
self._attr_entity_category = category
|
|
||||||
|
@property
|
||||||
|
def available(self) -> bool:
|
||||||
|
"""Return if entity is available."""
|
||||||
|
return self._attr_available
|
||||||
|
|
||||||
|
def _set_available(self):
|
||||||
|
"""Set sensor as available."""
|
||||||
|
self._attr_available = True
|
||||||
|
self._unavailable_counter = 0
|
||||||
|
self._attr_extra_state_attributes = {}
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
def _set_unavailable(self, cause: str = "Unknown"):
|
||||||
|
"""Set sensor as unavailable."""
|
||||||
|
self._unavailable_counter += 1
|
||||||
|
|
||||||
|
self._attr_extra_state_attributes = {
|
||||||
|
"unavailable_counter": self._unavailable_counter,
|
||||||
|
"unavailable_cause": cause,
|
||||||
|
}
|
||||||
|
|
||||||
|
if self._unavailable_counter >= 5:
|
||||||
|
self._attr_available = False
|
||||||
|
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _handle_coordinator_update(self) -> None:
|
def _handle_coordinator_update(self) -> None:
|
||||||
"""Handle updated data from the coordinator."""
|
"""Handle updated data from the coordinator."""
|
||||||
_LOGGER.debug("Updating state of %s", self._attr_unique_id)
|
|
||||||
if not isinstance(self.coordinator.data, dict):
|
if self.coordinator.reader.persistent_conn and not self.coordinator.reader.client.is_connected:
|
||||||
_LOGGER.error(
|
return
|
||||||
"Invalid data from coordinator (sensor.%s)", self._attr_unique_id
|
|
||||||
|
if self.coordinator.data is None:
|
||||||
|
_LOGGER.debug(
|
||||||
|
"Data from coordinator is None",
|
||||||
)
|
)
|
||||||
self._attr_available = False
|
self._set_unavailable("Data is None")
|
||||||
|
return
|
||||||
|
|
||||||
|
_LOGGER.debug("Updating state of %s", unique_id_loggable(self._attr_unique_id))
|
||||||
|
if not isinstance(self.coordinator.data, dict):
|
||||||
|
_LOGGER.debug(
|
||||||
|
"Invalid data from coordinator (binary_sensor.%s)", unique_id_loggable(self._attr_unique_id)
|
||||||
|
)
|
||||||
|
self._set_unavailable("Invalid data")
|
||||||
return
|
return
|
||||||
|
|
||||||
response_data = self.coordinator.data.get(self._response_key)
|
response_data = self.coordinator.data.get(self._response_key)
|
||||||
if response_data is None:
|
if response_data is None:
|
||||||
self._attr_available = False
|
self._set_unavailable("No data")
|
||||||
return
|
return
|
||||||
|
|
||||||
if not isinstance(response_data, bool):
|
if not isinstance(response_data, bool):
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"Invalid response data type from coordinator (sensor.%s): %s",
|
"Invalid response data type from coordinator (binary_sensor.%s): %s",
|
||||||
self._attr_unique_id,
|
unique_id_loggable(self._attr_unique_id),
|
||||||
response_data,
|
response_data,
|
||||||
)
|
)
|
||||||
self._attr_available = False
|
self._set_unavailable("Invalid data type")
|
||||||
return
|
return
|
||||||
|
|
||||||
self._attr_available = True
|
self._set_available()
|
||||||
self._attr_is_on = self.coordinator.data[self._response_key] is True
|
self._attr_is_on = self.coordinator.data[self._response_key] is True
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
from .const import *
|
||||||
|
from .exceptions import *
|
||||||
|
from .utils.commands import ReadHoldingRegisters
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
"""Bluetti device."""
|
||||||
|
|
||||||
|
# Reduced copy of https://github.com/warhammerkid/bluetti_mqtt/blob/main/bluetti_mqtt/core/devices/bluetti_device.py
|
||||||
|
|
||||||
|
from typing import Any, List
|
||||||
|
|
||||||
|
from ..utils.commands import ReadHoldingRegisters, WriteSingleRegister
|
||||||
|
from ..utils.struct import BoolField, DeviceStruct, EnumField
|
||||||
|
|
||||||
|
|
||||||
|
class BluettiDevice:
|
||||||
|
struct: DeviceStruct
|
||||||
|
|
||||||
|
def __init__(self, address: str, type: str, sn: str):
|
||||||
|
self.address = address
|
||||||
|
self.type = type
|
||||||
|
self.sn = sn
|
||||||
|
|
||||||
|
def parse(self, address: int, data: bytes) -> dict:
|
||||||
|
return self.struct.parse(address, data)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def pack_num_max(self):
|
||||||
|
"""
|
||||||
|
A given device has a maximum number of battery packs, including the
|
||||||
|
internal battery if it has one. We can provide this information statically
|
||||||
|
so it's not necessary to poll the device.
|
||||||
|
"""
|
||||||
|
return 1
|
||||||
|
|
||||||
|
@property
|
||||||
|
def polling_commands(self) -> List[ReadHoldingRegisters]:
|
||||||
|
"""A given device has an optimal set of commands for polling"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
@property
|
||||||
|
def pack_polling_commands(self) -> List[ReadHoldingRegisters]:
|
||||||
|
"""A given device may have a set of commands for polling pack data"""
|
||||||
|
return []
|
||||||
|
|
||||||
|
@property
|
||||||
|
def writable_ranges(self) -> List[range]:
|
||||||
|
"""The address ranges that are writable"""
|
||||||
|
return []
|
||||||
|
|
||||||
|
@property
|
||||||
|
def pack_num_field(self) -> List[ReadHoldingRegisters]:
|
||||||
|
"""The address 'range' of the pack num result. Matches pack_num_result"""
|
||||||
|
return []
|
||||||
|
|
||||||
|
def has_field(self, field: str):
|
||||||
|
return any(f.name == field for f in self.struct.fields)
|
||||||
|
|
||||||
|
def has_field_setter(self, field: str):
|
||||||
|
matches = [f for f in self.struct.fields if f.name == field]
|
||||||
|
return any(any(f.address in r for r in self.writable_ranges) for f in matches)
|
||||||
|
|
||||||
|
def build_setter_command(self, field: str, value: Any):
|
||||||
|
matches = [f for f in self.struct.fields if f.name == field]
|
||||||
|
device_field = next(
|
||||||
|
f for f in matches if any(f.address in r for r in self.writable_ranges)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Convert value to an integer
|
||||||
|
if isinstance(device_field, EnumField):
|
||||||
|
value = device_field.enum[value].value
|
||||||
|
elif isinstance(device_field, BoolField):
|
||||||
|
value = 1 if value else 0
|
||||||
|
|
||||||
|
return WriteSingleRegister(device_field.address, value)
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
"""Base device definition for V1 Protocol devices."""
|
||||||
|
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from ..utils.commands import ReadHoldingRegisters
|
||||||
|
from ..utils.struct import DeviceStruct
|
||||||
|
from .BluettiDevice import BluettiDevice
|
||||||
|
|
||||||
|
|
||||||
|
class ProtocolV1Device(BluettiDevice):
|
||||||
|
def __init__(self, address: str, type: str, sn: str):
|
||||||
|
self.struct = DeviceStruct()
|
||||||
|
|
||||||
|
# Device info
|
||||||
|
self.struct.add_string_field("device_type", 10, 6)
|
||||||
|
self.struct.add_sn_field("serial_number", 17)
|
||||||
|
self.struct.add_version_field('arm_version', 23)
|
||||||
|
self.struct.add_version_field('dsp_version', 25)
|
||||||
|
|
||||||
|
# Power IO
|
||||||
|
self.struct.add_uint_field("dc_input_power", 36)
|
||||||
|
self.struct.add_uint_field("ac_input_power", 37)
|
||||||
|
self.struct.add_uint_field("ac_output_power", 38)
|
||||||
|
self.struct.add_uint_field("dc_output_power", 39)
|
||||||
|
|
||||||
|
# Power IO statistics
|
||||||
|
self.struct.add_decimal_field(
|
||||||
|
"power_generation", 41, 1
|
||||||
|
) # Total power generated since last reset (kwh)
|
||||||
|
|
||||||
|
# Battery
|
||||||
|
self.struct.add_uint_field("total_battery_percent", 43)
|
||||||
|
|
||||||
|
# Output state
|
||||||
|
self.struct.add_bool_field("ac_output_on", 48)
|
||||||
|
self.struct.add_bool_field("dc_output_on", 49)
|
||||||
|
|
||||||
|
# Pack selector
|
||||||
|
self.struct.add_uint_field("pack_num", 3006) # internal
|
||||||
|
|
||||||
|
# Output controls
|
||||||
|
self.struct.add_bool_field("ac_output_on_switch", 3007)
|
||||||
|
self.struct.add_bool_field("dc_output_on_switch", 3008)
|
||||||
|
|
||||||
|
super().__init__(address, type, sn)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def polling_commands(self) -> List[ReadHoldingRegisters]:
|
||||||
|
return [
|
||||||
|
ReadHoldingRegisters(10, 10),
|
||||||
|
ReadHoldingRegisters(36, 4),
|
||||||
|
ReadHoldingRegisters(41, 1),
|
||||||
|
ReadHoldingRegisters(43, 1),
|
||||||
|
ReadHoldingRegisters(48, 2),
|
||||||
|
ReadHoldingRegisters(3007, 2),
|
||||||
|
]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def writable_ranges(self) -> List[range]:
|
||||||
|
return [range(3006, 3009)]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def pack_num_field(self) -> List[ReadHoldingRegisters]:
|
||||||
|
return [
|
||||||
|
ReadHoldingRegisters(96, 1),
|
||||||
|
]
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
"""Base device definition for V2 Protocol devices."""
|
||||||
|
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from ..utils.commands import ReadHoldingRegisters
|
||||||
|
from ..utils.struct import DeviceStruct
|
||||||
|
from .BluettiDevice import BluettiDevice
|
||||||
|
|
||||||
|
|
||||||
|
class ProtocolV2Device(BluettiDevice):
|
||||||
|
def __init__(self, address: str, type: str, sn: str):
|
||||||
|
self.struct = DeviceStruct()
|
||||||
|
|
||||||
|
# Device info
|
||||||
|
self.struct.add_swap_string_field("device_type", 110, 6)
|
||||||
|
self.struct.add_sn_field("serial_number", 116)
|
||||||
|
|
||||||
|
# Power IO
|
||||||
|
# self.struct.add_uint_field('dc_input_power', ?)
|
||||||
|
# self.struct.add_uint_field('ac_input_power', ?)
|
||||||
|
# self.struct.add_uint_field('ac_output_power', ?)
|
||||||
|
# self.struct.add_uint_field('dc_output_power', ?)
|
||||||
|
|
||||||
|
# Power IO statistics
|
||||||
|
self.struct.add_decimal_field(
|
||||||
|
"power_generation", 154, 1
|
||||||
|
) # Total power generated since last reset (kwh)
|
||||||
|
|
||||||
|
# Battery
|
||||||
|
self.struct.add_uint_field("total_battery_percent", 102)
|
||||||
|
|
||||||
|
# Battery packs
|
||||||
|
# self.struct.add_uint_field('pack_num_max', ?) # internal
|
||||||
|
# self.struct.add_decimal_field('total_battery_voltage', ?, 1)
|
||||||
|
# self.struct.add_uint_field('pack_num', ?) # internal
|
||||||
|
# self.struct.add_decimal_field('pack_voltage', ?, 2) # Full pack voltage
|
||||||
|
# self.struct.add_uint_field('pack_battery_percent', ?)
|
||||||
|
|
||||||
|
# Output state
|
||||||
|
# self.struct.add_bool_field('ac_output_on', ?)
|
||||||
|
# self.struct.add_bool_field('dc_output_on', ?)
|
||||||
|
|
||||||
|
# Pack selector
|
||||||
|
# self.struct.add_uint_field('pack_num', ?) # internal
|
||||||
|
|
||||||
|
# Output controls
|
||||||
|
# self.struct.add_bool_field('ac_output_on_switch', ?)
|
||||||
|
# self.struct.add_bool_field('dc_output_on_switch', ?)
|
||||||
|
|
||||||
|
# Add battery fields here?
|
||||||
|
|
||||||
|
super().__init__(address, type, sn)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def polling_commands(self) -> List[ReadHoldingRegisters]:
|
||||||
|
return [
|
||||||
|
ReadHoldingRegisters(110, 6),
|
||||||
|
ReadHoldingRegisters(116, 4),
|
||||||
|
ReadHoldingRegisters(154, 1),
|
||||||
|
ReadHoldingRegisters(102, 1),
|
||||||
|
]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def writable_ranges(self) -> List[range]:
|
||||||
|
return []
|
||||||
|
|
||||||
|
@property
|
||||||
|
def pack_polling_commands(self) -> List[ReadHoldingRegisters]:
|
||||||
|
return []
|
||||||
@@ -0,0 +1,289 @@
|
|||||||
|
"""Device reader."""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
from typing import Any, Callable, List, cast
|
||||||
|
import async_timeout
|
||||||
|
from bleak import BleakClient, BleakError
|
||||||
|
|
||||||
|
from custom_components.bluetti_bt.bluetti_bt_lib.bluetooth.encryption import BluettiEncryption, Message, MessageType
|
||||||
|
|
||||||
|
from ..base_devices.BluettiDevice import BluettiDevice
|
||||||
|
from ..const import NOTIFY_UUID, RESPONSE_TIMEOUT, WRITE_UUID
|
||||||
|
from ..exceptions import BadConnectionError, ModbusError, ParseError
|
||||||
|
from ..utils.commands import ReadHoldingRegisters
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class DeviceReader:
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
bleak_client: BleakClient,
|
||||||
|
bluetti_device: BluettiDevice,
|
||||||
|
future_builder_method: Callable[[], asyncio.Future[Any]],
|
||||||
|
persistent_conn: bool = False,
|
||||||
|
polling_timeout: int = 45,
|
||||||
|
max_retries: int = 5,
|
||||||
|
encrypted: bool = False,
|
||||||
|
) -> None:
|
||||||
|
self.client = bleak_client
|
||||||
|
self.bluetti_device = bluetti_device
|
||||||
|
self.create_future = future_builder_method
|
||||||
|
self.persistent_conn = persistent_conn
|
||||||
|
self.polling_timeout = polling_timeout
|
||||||
|
self.max_retries = max_retries
|
||||||
|
self.encrypted = encrypted
|
||||||
|
|
||||||
|
self.has_notifier = False
|
||||||
|
self.notify_future: asyncio.Future[Any] | None = None
|
||||||
|
self.current_command = None
|
||||||
|
self.notify_response = bytearray()
|
||||||
|
|
||||||
|
# polling mutex to guard against switches
|
||||||
|
self.polling_lock = asyncio.Lock()
|
||||||
|
|
||||||
|
self.encryption = BluettiEncryption()
|
||||||
|
|
||||||
|
async def read_data(
|
||||||
|
self, filter_registers: List[ReadHoldingRegisters] | None = None
|
||||||
|
) -> dict | None:
|
||||||
|
_LOGGER.info("Reading data")
|
||||||
|
|
||||||
|
if self.bluetti_device is None:
|
||||||
|
_LOGGER.error("Device is None")
|
||||||
|
return None
|
||||||
|
|
||||||
|
polling_commands = self.bluetti_device.polling_commands
|
||||||
|
pack_commands = self.bluetti_device.pack_polling_commands
|
||||||
|
if filter_registers is not None:
|
||||||
|
polling_commands = filter_registers
|
||||||
|
pack_commands = []
|
||||||
|
_LOGGER.info("Polling commands: " + ",".join([f"{c.starting_address}-{c.starting_address + c.quantity - 1}" for c in polling_commands]))
|
||||||
|
_LOGGER.info("Pack comands: " + ",".join([f"{c.starting_address}-{c.starting_address + c.quantity - 1}" for c in pack_commands]))
|
||||||
|
|
||||||
|
parsed_data: dict = {}
|
||||||
|
|
||||||
|
async with self.polling_lock:
|
||||||
|
try:
|
||||||
|
async with async_timeout.timeout(self.polling_timeout):
|
||||||
|
# Reconnect if not connected
|
||||||
|
for attempt in range(1, self.max_retries + 1):
|
||||||
|
try:
|
||||||
|
if not self.client.is_connected:
|
||||||
|
await self.client.connect()
|
||||||
|
break
|
||||||
|
except Exception as e:
|
||||||
|
if attempt == self.max_retries:
|
||||||
|
raise e # pass exception on max_retries attempt
|
||||||
|
else:
|
||||||
|
_LOGGER.warning(
|
||||||
|
f"Connect unsucessful (attempt {attempt}): {e}. Retrying..."
|
||||||
|
)
|
||||||
|
await asyncio.sleep(2)
|
||||||
|
|
||||||
|
# Attach notifier if needed
|
||||||
|
if not self.has_notifier:
|
||||||
|
await self.client.start_notify(
|
||||||
|
NOTIFY_UUID, self._notification_handler
|
||||||
|
)
|
||||||
|
self.has_notifier = True
|
||||||
|
|
||||||
|
while self.encrypted and not self.encryption.is_ready_for_commands:
|
||||||
|
await asyncio.sleep(5)
|
||||||
|
_LOGGER.debug("Encryption handshake not finished yet")
|
||||||
|
|
||||||
|
# Execute polling commands
|
||||||
|
for command in polling_commands:
|
||||||
|
try:
|
||||||
|
body = command.parse_response(
|
||||||
|
await self._async_send_command(command)
|
||||||
|
)
|
||||||
|
_LOGGER.debug("Raw data: %s", body)
|
||||||
|
parsed = self.bluetti_device.parse(
|
||||||
|
command.starting_address, body
|
||||||
|
)
|
||||||
|
_LOGGER.debug("Parsed data: %s", parsed)
|
||||||
|
parsed_data.update(parsed)
|
||||||
|
except ParseError:
|
||||||
|
_LOGGER.warning("Got a parse exception")
|
||||||
|
|
||||||
|
# Execute pack polling commands
|
||||||
|
if len(pack_commands) > 0 and len(self.bluetti_device.pack_num_field) == 1:
|
||||||
|
_LOGGER.debug("Polling battery packs")
|
||||||
|
for pack in range(1, self.bluetti_device.pack_num_max + 1):
|
||||||
|
_LOGGER.debug("Setting pack_num to %i", pack)
|
||||||
|
|
||||||
|
# Set current pack number
|
||||||
|
command = self.bluetti_device.build_setter_command(
|
||||||
|
"pack_num", pack
|
||||||
|
)
|
||||||
|
body = command.parse_response(
|
||||||
|
await self._async_send_command(command)
|
||||||
|
)
|
||||||
|
_LOGGER.debug("Raw data set: %s", body)
|
||||||
|
|
||||||
|
# Check set pack_num
|
||||||
|
set_pack = int.from_bytes(body, byteorder='big')
|
||||||
|
if set_pack is not pack:
|
||||||
|
_LOGGER.warning("Pack polling failed (pack_num %i doesn't match expected %i)", set_pack, pack)
|
||||||
|
continue
|
||||||
|
|
||||||
|
if self.bluetti_device.pack_num_max > 1:
|
||||||
|
# We need to wait after switching packs
|
||||||
|
# for the data to be available
|
||||||
|
await asyncio.sleep(5)
|
||||||
|
|
||||||
|
for command in pack_commands:
|
||||||
|
# Request & parse result for each pack
|
||||||
|
try:
|
||||||
|
body = command.parse_response(
|
||||||
|
await self._async_send_command(command)
|
||||||
|
)
|
||||||
|
parsed = self.bluetti_device.parse(
|
||||||
|
command.starting_address, body
|
||||||
|
)
|
||||||
|
_LOGGER.debug("Parsed data: %s", parsed)
|
||||||
|
|
||||||
|
for key, value in parsed.items():
|
||||||
|
# Ignore likely unavailable pack data
|
||||||
|
if value != 0:
|
||||||
|
parsed_data.update({key + str(pack): value})
|
||||||
|
|
||||||
|
except ParseError:
|
||||||
|
_LOGGER.warning("Got a parse exception...")
|
||||||
|
|
||||||
|
except TimeoutError as err:
|
||||||
|
_LOGGER.error(f"Polling timed out ({self.polling_timeout}s). Trying again later", exc_info=err)
|
||||||
|
return None
|
||||||
|
except BleakError as err:
|
||||||
|
_LOGGER.error("Bleak error: %s", err)
|
||||||
|
return None
|
||||||
|
finally:
|
||||||
|
# Disconnect if connection not persistant
|
||||||
|
if not self.persistent_conn:
|
||||||
|
if self.has_notifier:
|
||||||
|
try:
|
||||||
|
await self.client.stop_notify(NOTIFY_UUID)
|
||||||
|
except:
|
||||||
|
# Ignore errors here
|
||||||
|
pass
|
||||||
|
self.has_notifier = False
|
||||||
|
await self.client.disconnect()
|
||||||
|
|
||||||
|
# Check if dict is empty
|
||||||
|
if not parsed_data:
|
||||||
|
return None
|
||||||
|
|
||||||
|
# Reset Encryption keys
|
||||||
|
self.encryption.reset()
|
||||||
|
|
||||||
|
return parsed_data
|
||||||
|
|
||||||
|
async def _async_send_command(self, command: ReadHoldingRegisters) -> bytes:
|
||||||
|
"""Send command and return response"""
|
||||||
|
try:
|
||||||
|
# Prepare to make request
|
||||||
|
self.current_command = command
|
||||||
|
self.notify_future = self.create_future()
|
||||||
|
self.notify_response = bytearray()
|
||||||
|
|
||||||
|
# Make request
|
||||||
|
_LOGGER.debug("Requesting %s", command)
|
||||||
|
|
||||||
|
command_bytes = bytes(command)
|
||||||
|
|
||||||
|
# Encrypt command
|
||||||
|
if self.encrypted is True:
|
||||||
|
if not self.encryption.is_ready_for_commands:
|
||||||
|
return bytes()
|
||||||
|
command_bytes = self.encryption.aes_encrypt(command_bytes, self.encryption.secure_aes_key, None)
|
||||||
|
|
||||||
|
await self.client.write_gatt_char(WRITE_UUID, command_bytes)
|
||||||
|
|
||||||
|
# Wait for response
|
||||||
|
res = await asyncio.wait_for(self.notify_future, timeout=RESPONSE_TIMEOUT)
|
||||||
|
|
||||||
|
# Process data
|
||||||
|
_LOGGER.debug("Got %s bytes", len(res))
|
||||||
|
return cast(bytes, res)
|
||||||
|
|
||||||
|
except TimeoutError:
|
||||||
|
_LOGGER.debug("Polling single command timed out")
|
||||||
|
except ModbusError as err:
|
||||||
|
_LOGGER.debug(
|
||||||
|
"Got an invalid request error for %s: %s",
|
||||||
|
command,
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
except (BadConnectionError, BleakError) as err:
|
||||||
|
# Ignore other errors
|
||||||
|
pass
|
||||||
|
|
||||||
|
# caught an exception, return empty bytes object
|
||||||
|
return bytes()
|
||||||
|
|
||||||
|
async def _notification_handler(self, _sender: int, data: bytearray):
|
||||||
|
"""Handle bt data."""
|
||||||
|
|
||||||
|
# Handle encrypted data
|
||||||
|
if self.encrypted is True:
|
||||||
|
message = Message(data)
|
||||||
|
|
||||||
|
# Handle key exchange
|
||||||
|
if message.is_pre_key_exchange:
|
||||||
|
message.verify_checksum()
|
||||||
|
|
||||||
|
if message.type == MessageType.CHALLENGE:
|
||||||
|
challenge_response = self.encryption.msg_challenge(message)
|
||||||
|
await self.client.write_gatt_char(WRITE_UUID, challenge_response)
|
||||||
|
return
|
||||||
|
|
||||||
|
if message.type == MessageType.CHALLENGE_ACCEPTED:
|
||||||
|
_LOGGER.debug("Challenge accepted")
|
||||||
|
return
|
||||||
|
|
||||||
|
if self.encryption.unsecure_aes_key is None:
|
||||||
|
_LOGGER.error("Received encrypted message before key initialization")
|
||||||
|
|
||||||
|
key, iv = self.encryption.getKeyIv()
|
||||||
|
decrypted = Message(self.encryption.aes_decrypt(message.buffer, key, iv))
|
||||||
|
|
||||||
|
if decrypted.is_pre_key_exchange:
|
||||||
|
decrypted.verify_checksum()
|
||||||
|
|
||||||
|
if decrypted.type == MessageType.PEER_PUBKEY:
|
||||||
|
peer_pubkey_response = self.encryption.msg_peer_pubkey(decrypted)
|
||||||
|
await self.client.write_gatt_char(WRITE_UUID, peer_pubkey_response)
|
||||||
|
return
|
||||||
|
|
||||||
|
if decrypted.type == MessageType.PUBKEY_ACCEPTED:
|
||||||
|
self.encryption.msg_key_accepted(decrypted)
|
||||||
|
return
|
||||||
|
|
||||||
|
# Handle as message
|
||||||
|
data = decrypted.buffer
|
||||||
|
|
||||||
|
# Ignore notifications we don't expect
|
||||||
|
if self.notify_future is None or self.notify_future.done():
|
||||||
|
_LOGGER.warning("Unexpected notification")
|
||||||
|
return
|
||||||
|
|
||||||
|
# If something went wrong, we might get weird data.
|
||||||
|
if data == b"AT+NAME?\r" or data == b"AT+ADV?\r":
|
||||||
|
err = BadConnectionError("Got AT+ notification")
|
||||||
|
self.notify_future.set_exception(err)
|
||||||
|
return
|
||||||
|
|
||||||
|
# Save data
|
||||||
|
self.notify_response.extend(data)
|
||||||
|
|
||||||
|
if len(self.notify_response) == self.current_command.response_size():
|
||||||
|
if self.current_command.is_valid_response(self.notify_response):
|
||||||
|
self.notify_future.set_result(self.notify_response)
|
||||||
|
else:
|
||||||
|
self.notify_future.set_exception(ParseError("Failed checksum"))
|
||||||
|
elif self.current_command.is_exception_response(self.notify_response):
|
||||||
|
# We got a MODBUS command exception
|
||||||
|
msg = f"MODBUS Exception {self.current_command}: {self.notify_response[2]}"
|
||||||
|
self.notify_future.set_exception(ModbusError(msg))
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
"""Device recognizer."""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
from typing import Any, Callable
|
||||||
|
from bleak import BleakClient
|
||||||
|
|
||||||
|
from custom_components.bluetti_bt.bluetti_bt_lib.utils.commands import (
|
||||||
|
ReadHoldingRegisters,
|
||||||
|
)
|
||||||
|
|
||||||
|
from ..base_devices.ProtocolV2Device import ProtocolV2Device
|
||||||
|
from ..bluetooth.device_reader import DeviceReader
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
async def recognize_device(
|
||||||
|
bleak_client: BleakClient,
|
||||||
|
future_builder_method: Callable[[], asyncio.Future[Any]],
|
||||||
|
) -> str:
|
||||||
|
|
||||||
|
# Since we don't know the type we use the base device
|
||||||
|
bluetti_device = ProtocolV2Device("Unknown", "Unknown", "Unknown")
|
||||||
|
|
||||||
|
# Create device builder
|
||||||
|
device_reader = DeviceReader(bleak_client, bluetti_device, future_builder_method)
|
||||||
|
|
||||||
|
# Retry a few times to get data
|
||||||
|
for _ in range(1, 50):
|
||||||
|
# We only need 6 registers to get the device type
|
||||||
|
data = await device_reader.read_data(
|
||||||
|
[
|
||||||
|
ReadHoldingRegisters(110, 6),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
if data is not None:
|
||||||
|
break
|
||||||
|
|
||||||
|
await asyncio.sleep(0.1)
|
||||||
|
|
||||||
|
if data is None:
|
||||||
|
# Should not happen
|
||||||
|
return "Unknown"
|
||||||
|
|
||||||
|
field_data = data.get("device_type")
|
||||||
|
|
||||||
|
if field_data is None:
|
||||||
|
# We have a problem
|
||||||
|
_LOGGER.error("No data in device type field_datas. Maybe it's a V1 device?")
|
||||||
|
return "Unknown"
|
||||||
|
|
||||||
|
if not isinstance(field_data, str):
|
||||||
|
# We have a problem
|
||||||
|
_LOGGER.error("Invalid data in device type field_datas")
|
||||||
|
return "Unknown"
|
||||||
|
|
||||||
|
return field_data
|
||||||
@@ -0,0 +1,289 @@
|
|||||||
|
from enum import Enum
|
||||||
|
import hashlib
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import pyasn1.codec.der.decoder as der_decoder
|
||||||
|
import pyasn1.codec.der.encoder as der_encoder
|
||||||
|
from pyasn1.type import univ
|
||||||
|
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
||||||
|
from cryptography.hazmat.primitives.asymmetric import ec
|
||||||
|
from cryptography.hazmat.primitives import hashes, serialization
|
||||||
|
from cryptography.exceptions import InvalidSignature
|
||||||
|
|
||||||
|
from custom_components.bluetti_bt.bluetti_bt_lib.const import LOCAL_AES_KEY, PRIVATE_KEY_L1, PUBLIC_KEY_K2, SECP_256R1_PUBLIC_PREFIX
|
||||||
|
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
KEX_MAGIC = b"**"
|
||||||
|
AES_BLOCK_SIZE = 16
|
||||||
|
|
||||||
|
def hexsum(s, sz: int):
|
||||||
|
checksum = sum(s)
|
||||||
|
as_hex = f"{checksum:0{sz*2}x}"
|
||||||
|
return bytes.fromhex(as_hex)
|
||||||
|
|
||||||
|
def hexxor(a: bytes, b: bytes) -> bytes | None:
|
||||||
|
if len(a) != len(b):
|
||||||
|
_LOGGER.error("Can only XOR two identical length byte strings")
|
||||||
|
return None
|
||||||
|
return bytes([x ^ y for x, y in zip(a, b)])
|
||||||
|
|
||||||
|
def raw_ecdsa_to_der(sig):
|
||||||
|
# <byte r[32]> <byte s[32]>
|
||||||
|
|
||||||
|
if len(sig) != 64:
|
||||||
|
raise ValueError("ecdsa signature is the wrong size")
|
||||||
|
|
||||||
|
seq = univ.SequenceOf()
|
||||||
|
seq.extend(
|
||||||
|
[
|
||||||
|
univ.Integer(int.from_bytes(sig[:32], "big")), # r
|
||||||
|
univ.Integer(int.from_bytes(sig[32:], "big")), # s
|
||||||
|
]
|
||||||
|
)
|
||||||
|
return der_encoder.encode(seq)
|
||||||
|
|
||||||
|
def der_to_raw_ecdsa(sig):
|
||||||
|
# 30 45 02 20 1956307e59448178b47c222e4e1e6c8ef7d707bc230e5a9fa77f919ec44e5f74
|
||||||
|
# | | | | |> byte r[0x20]
|
||||||
|
# | | | |---> Length
|
||||||
|
# | | |------> DER type (int)
|
||||||
|
# | |---------> Payload size
|
||||||
|
# |------------> DER type (sequence)
|
||||||
|
|
||||||
|
# 02 21 00cfad6e11abd5e803fb6874c3838bc968db1f3c070ae6b85db9d8ed8936a1eb5c
|
||||||
|
# | | |> byte s[0x21]
|
||||||
|
# | |---> Length
|
||||||
|
# |------> DER type (int)
|
||||||
|
|
||||||
|
seq, remainder = der_decoder.decode(sig)
|
||||||
|
if remainder:
|
||||||
|
raise ValueError("Found trailing data")
|
||||||
|
|
||||||
|
return b"".join([int.to_bytes(int(x), 0x20, "big") for x in seq])
|
||||||
|
|
||||||
|
def verify_and_extract_signed_data(message, signed_data_suffix: bytes | None):
|
||||||
|
# 64 bytes of data
|
||||||
|
# 64 bytes of signature
|
||||||
|
if len(message) != 128:
|
||||||
|
raise ValueError("Unexpected message length")
|
||||||
|
|
||||||
|
data = message[:64]
|
||||||
|
signature = message[64:]
|
||||||
|
signed_data = data.tobytes() + signed_data_suffix
|
||||||
|
der_signature = raw_ecdsa_to_der(signature)
|
||||||
|
try:
|
||||||
|
key_bytes = bytes.fromhex(PUBLIC_KEY_K2)
|
||||||
|
serialization.load_der_public_key(key_bytes).verify(
|
||||||
|
der_signature, signed_data, ec.ECDSA(hashes.SHA256())
|
||||||
|
)
|
||||||
|
_LOGGER.debug("Signature OK")
|
||||||
|
except InvalidSignature:
|
||||||
|
raise
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
|
def pubkey_from_bytes(data):
|
||||||
|
encoded_peer_pubkey = bytes.fromhex(SECP_256R1_PUBLIC_PREFIX) + data
|
||||||
|
return serialization.load_der_public_key(encoded_peer_pubkey)
|
||||||
|
|
||||||
|
def generate_keypair():
|
||||||
|
private = ec.generate_private_key(ec.SECP256R1())
|
||||||
|
return (private.public_key(), private)
|
||||||
|
|
||||||
|
def pubkey_to_bytes(pubkey):
|
||||||
|
out = pubkey.public_bytes(
|
||||||
|
encoding=serialization.Encoding.X962,
|
||||||
|
format=serialization.PublicFormat.UncompressedPoint,
|
||||||
|
)
|
||||||
|
|
||||||
|
if out[0] != 0x4 or len(out) != 65:
|
||||||
|
raise RuntimeError(
|
||||||
|
"First byte should be 04 for the uncompressed format, and total size 64"
|
||||||
|
)
|
||||||
|
|
||||||
|
return out[1:]
|
||||||
|
|
||||||
|
class MessageType(Enum):
|
||||||
|
CHALLENGE = 1
|
||||||
|
CHALLENGE_ACCEPTED = 3
|
||||||
|
PEER_PUBKEY = 4
|
||||||
|
PUBKEY_ACCEPTED = 6
|
||||||
|
|
||||||
|
class Message:
|
||||||
|
"""
|
||||||
|
Two types of messages. The first one is for communication that happens before we have
|
||||||
|
a proper symmetric key available. These are fully handled by this file, it's purely
|
||||||
|
to setup the encryption protocol.
|
||||||
|
|
||||||
|
2a2a .... 0000
|
||||||
|
| |> Body checksum
|
||||||
|
| |-----> Body
|
||||||
|
|----------> Magic value for pre-key-exchange
|
||||||
|
|
||||||
|
The second type is for anything after. This can either contain the type 1 message above, or
|
||||||
|
anything else -- at this point we're just wrapping the regular protocol, and we just forward
|
||||||
|
the packets dowm the regular processing path after decrypting them.
|
||||||
|
|
||||||
|
See aes_decrypt() for the format.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, buffer: bytes):
|
||||||
|
self.buffer = buffer
|
||||||
|
self.view = memoryview(self.buffer)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def header(self) -> memoryview:
|
||||||
|
return self.view[:2]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_pre_key_exchange(self) -> bool:
|
||||||
|
return self.header == KEX_MAGIC
|
||||||
|
|
||||||
|
@property
|
||||||
|
def checksum(self) -> memoryview:
|
||||||
|
return self.view[-2:]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def body(self) -> memoryview:
|
||||||
|
return self.view[len(self.header) : -len(self.checksum)]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def data(self) -> memoryview:
|
||||||
|
return self.body[2:]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def type(self) -> int:
|
||||||
|
return MessageType(self.body[0])
|
||||||
|
|
||||||
|
def verify_checksum(self):
|
||||||
|
message_checksum = self.checksum
|
||||||
|
computed_checksum = hexsum(self.body, len(message_checksum))
|
||||||
|
if computed_checksum != message_checksum:
|
||||||
|
_LOGGER.error("Checksum error!")
|
||||||
|
_LOGGER.debug("Checksum OK")
|
||||||
|
|
||||||
|
class BluettiEncryption:
|
||||||
|
# Derived exclusively from data sent over the network
|
||||||
|
# Used for the initial handshake
|
||||||
|
unsecure_aes_key: bytes | None = None
|
||||||
|
|
||||||
|
# Predictably derived from a seed sent by the peer
|
||||||
|
# This is the same for all the messages encrypted
|
||||||
|
# with that key during the connection
|
||||||
|
unsecure_aes_iv: bytes | None = None
|
||||||
|
|
||||||
|
# Proper key exchange gives us another key,
|
||||||
|
# that is used for the remainder of the connection
|
||||||
|
# IV is random per message
|
||||||
|
secure_aes_key: bytes | None = None
|
||||||
|
|
||||||
|
# Received through key exchange
|
||||||
|
# The signing key for the key exchange is well-known
|
||||||
|
peer_pubkey: bytes | None = None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_ready_for_commands(self) -> bool:
|
||||||
|
return self.secure_aes_key is not None and self.peer_pubkey is not None
|
||||||
|
|
||||||
|
def aes_decrypt(self, data: bytes, aes_key: bytes | None, iv: bytes | None):
|
||||||
|
data_len = (data[0] << 8) + data[1]
|
||||||
|
|
||||||
|
if iv is None:
|
||||||
|
iv = hashlib.md5(data[2:6]).digest()
|
||||||
|
encrypted = memoryview(data)[6:]
|
||||||
|
else:
|
||||||
|
encrypted = memoryview(data[2:])
|
||||||
|
|
||||||
|
if len(encrypted) % AES_BLOCK_SIZE != 0:
|
||||||
|
raise ValueError("Data not aligned on aes block size")
|
||||||
|
|
||||||
|
cipher = Cipher(algorithms.AES(aes_key), modes.CBC(iv))
|
||||||
|
decryptor = cipher.decryptor()
|
||||||
|
decrypted = decryptor.update(encrypted) + decryptor.finalize()
|
||||||
|
decrypted = decrypted[:data_len]
|
||||||
|
|
||||||
|
_LOGGER.debug(">PLAIN " + decrypted.hex())
|
||||||
|
return decrypted
|
||||||
|
|
||||||
|
|
||||||
|
def aes_encrypt(self, data: bytes, aes_key: bytes | None, iv: bytes | None):
|
||||||
|
message_header = int.to_bytes(len(data), 2, "big")
|
||||||
|
|
||||||
|
if iv is None:
|
||||||
|
iv_seed = os.urandom(4)
|
||||||
|
iv = hashlib.md5(iv_seed).digest()
|
||||||
|
message_header += iv_seed
|
||||||
|
|
||||||
|
padding = (AES_BLOCK_SIZE - len(data) % AES_BLOCK_SIZE) % AES_BLOCK_SIZE
|
||||||
|
data += bytes(padding)
|
||||||
|
|
||||||
|
cipher = Cipher(algorithms.AES(aes_key), modes.CBC(iv))
|
||||||
|
encryptor = cipher.encryptor()
|
||||||
|
encrypted = encryptor.update(data) + encryptor.finalize()
|
||||||
|
encrypted = message_header + encrypted
|
||||||
|
|
||||||
|
_LOGGER.debug("PLAIN> " + data.hex())
|
||||||
|
return encrypted
|
||||||
|
|
||||||
|
def msg_challenge(self, message: Message) -> bytes | None:
|
||||||
|
_LOGGER.debug("Received challenge")
|
||||||
|
|
||||||
|
if len(message.data) != 4:
|
||||||
|
_LOGGER.error("Unexpected message length")
|
||||||
|
return None
|
||||||
|
|
||||||
|
self.unsecure_aes_iv = hashlib.md5(message.data[::-1].tobytes()).digest()
|
||||||
|
static_key = bytes.fromhex(LOCAL_AES_KEY)
|
||||||
|
self.unsecure_aes_key = hexxor(self.unsecure_aes_iv, static_key)
|
||||||
|
|
||||||
|
_LOGGER.info("Unsecure iv " + self.unsecure_aes_iv.hex())
|
||||||
|
_LOGGER.info("Unsecure key " + self.unsecure_aes_key.hex())
|
||||||
|
|
||||||
|
body = bytes.fromhex("0204") + self.unsecure_aes_iv[8:12]
|
||||||
|
return b"".join([KEX_MAGIC, body, hexsum(body, 2)])
|
||||||
|
|
||||||
|
def msg_peer_pubkey(self, message: Message) -> bytes | None:
|
||||||
|
_LOGGER.debug("Received peer pubkey, checking signature")
|
||||||
|
data = verify_and_extract_signed_data(message.data, self.unsecure_aes_iv)
|
||||||
|
self.peer_pubkey = pubkey_from_bytes(data)
|
||||||
|
|
||||||
|
_LOGGER.debug("Generating a local keypair")
|
||||||
|
self.my_pubkey, self.my_privkey = generate_keypair()
|
||||||
|
my_pubkey_bytes = pubkey_to_bytes(self.my_pubkey)
|
||||||
|
|
||||||
|
_LOGGER.debug("Signing the local pubkey")
|
||||||
|
signing_secret = int.from_bytes(
|
||||||
|
bytes.fromhex(PRIVATE_KEY_L1), "big"
|
||||||
|
)
|
||||||
|
signing_key = ec.derive_private_key(signing_secret, ec.SECP256R1())
|
||||||
|
to_sign = my_pubkey_bytes + self.unsecure_aes_iv
|
||||||
|
signature = signing_key.sign(to_sign, ec.ECDSA(hashes.SHA256()))
|
||||||
|
raw_signature = der_to_raw_ecdsa(signature)
|
||||||
|
|
||||||
|
body = b"".join([bytes.fromhex("0580"), my_pubkey_bytes, raw_signature])
|
||||||
|
msg = b"".join([KEX_MAGIC, body, hexsum(body, 2)])
|
||||||
|
return self.aes_encrypt(msg, self.unsecure_aes_key, self.unsecure_aes_iv)
|
||||||
|
|
||||||
|
def msg_key_accepted(self, message: Message) -> None:
|
||||||
|
_LOGGER.debug("Received key exchange confirmation, calculating shared secret")
|
||||||
|
|
||||||
|
if len(message.data) != 1:
|
||||||
|
raise ValueError("Unexpected message length")
|
||||||
|
if message.data[0] != 0:
|
||||||
|
raise ValueError("Key acceptance response is not 0")
|
||||||
|
|
||||||
|
self.secure_aes_key = self.my_privkey.exchange(ec.ECDH(), self.peer_pubkey)
|
||||||
|
_LOGGER.info("Secure key " + self.secure_aes_key.hex())
|
||||||
|
|
||||||
|
def getKeyIv(self):
|
||||||
|
return (
|
||||||
|
(self.unsecure_aes_key, self.unsecure_aes_iv)
|
||||||
|
if self.secure_aes_key is None
|
||||||
|
else (self.secure_aes_key, None)
|
||||||
|
)
|
||||||
|
|
||||||
|
def reset(self):
|
||||||
|
self.peer_pubkey = None
|
||||||
|
self.secure_aes_key = None
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
"""Const definitions."""
|
||||||
|
|
||||||
|
RESPONSE_TIMEOUT = 5
|
||||||
|
WRITE_UUID = "0000ff02-0000-1000-8000-00805f9b34fb"
|
||||||
|
NOTIFY_UUID = "0000ff01-0000-1000-8000-00805f9b34fb"
|
||||||
|
DEVICE_NAME_UUID = "00002a00-0000-1000-8000-00805f9b34fb"
|
||||||
|
|
||||||
|
LOCAL_AES_KEY = "459FC535808941F17091E0993EE3E93D"
|
||||||
|
PRIVATE_KEY_L1 = "4F19A16E3E87BDD9BD24D3E5495B88041511943CBC8B969ADE9641D0F56AF337"
|
||||||
|
PUBLIC_KEY_K2 = "3059301306072a8648ce3d020106082a8648ce3d03010703420004A73ABF5D2232C8C1C72E68304343C272495E3A8FD6F30EA96DE2F4B3CE60B251EE21AC667CF8A71E18B46B664EAEFFE3C489F24F695B6411DB7E22CCC85A8594"
|
||||||
|
SECP_256R1_PUBLIC_PREFIX = "3059301306072a8648ce3d020106082a8648ce3d03010703420004"
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
"""AC180 fields."""
|
||||||
|
|
||||||
|
from ..base_devices.ProtocolV2Device import ProtocolV2Device
|
||||||
|
|
||||||
|
|
||||||
|
class AC180(ProtocolV2Device):
|
||||||
|
def __init__(self, address: str, sn: str):
|
||||||
|
super().__init__(address, "AC180", sn)
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
"""AC180P fields."""
|
||||||
|
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from ..field_enums import ChargingMode
|
||||||
|
from ..utils.commands import ReadHoldingRegisters
|
||||||
|
from ..base_devices.ProtocolV2Device import ProtocolV2Device
|
||||||
|
|
||||||
|
class AC180P(ProtocolV2Device):
|
||||||
|
def __init__(self, address: str, sn: str):
|
||||||
|
super().__init__(address, "AC180P", sn)
|
||||||
|
|
||||||
|
# Power IO
|
||||||
|
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)
|
||||||
|
|
||||||
|
# Input Details (1100 - 1300)
|
||||||
|
self.struct.add_decimal_field('ac_input_voltage', 1314, 1)
|
||||||
|
|
||||||
|
# Controls (2000)
|
||||||
|
self.struct.add_bool_field('ac_output_on_switch', 2011)
|
||||||
|
self.struct.add_bool_field('dc_output_on_switch', 2012)
|
||||||
|
self.struct.add_bool_field('silent_charging_on', 2020) # to be able to choose between normal and silent charging modes
|
||||||
|
self.struct.add_enum_field('charging_mode', 2020, ChargingMode) # can't be set from HA, custom switch needed
|
||||||
|
self.struct.add_bool_field('power_lifting_on', 2021)
|
||||||
|
|
||||||
|
# Controls (2200)
|
||||||
|
self.struct.add_bool_field('grid_enhancement_mode_on', 2225)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def polling_commands(self) -> List[ReadHoldingRegisters]:
|
||||||
|
return super().polling_commands + [
|
||||||
|
ReadHoldingRegisters(140, 1),
|
||||||
|
ReadHoldingRegisters(142, 1),
|
||||||
|
ReadHoldingRegisters(144, 1),
|
||||||
|
ReadHoldingRegisters(146, 1),
|
||||||
|
ReadHoldingRegisters(1314, 1),
|
||||||
|
ReadHoldingRegisters(2011, 1),
|
||||||
|
ReadHoldingRegisters(2012, 1),
|
||||||
|
ReadHoldingRegisters(2020, 1),
|
||||||
|
ReadHoldingRegisters(2021, 1),
|
||||||
|
ReadHoldingRegisters(2225, 1),
|
||||||
|
]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def writable_ranges(self) -> List[range]:
|
||||||
|
return super().writable_ranges + [
|
||||||
|
range(2000, 2022),
|
||||||
|
range(2200, 2226)
|
||||||
|
]
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
"""AC200L fields."""
|
||||||
|
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from ..utils.commands import ReadHoldingRegisters
|
||||||
|
from ..field_enums import AutoSleepMode, OutputMode
|
||||||
|
from ..base_devices.ProtocolV1Device import ProtocolV1Device
|
||||||
|
|
||||||
|
|
||||||
|
class AC200L(ProtocolV1Device):
|
||||||
|
def __init__(self, address: str, sn: str):
|
||||||
|
super().__init__(address, "AC200L", sn)
|
||||||
|
|
||||||
|
# Details
|
||||||
|
self.struct.add_enum_field("ac_output_mode", 70, OutputMode)
|
||||||
|
self.struct.add_decimal_field("internal_ac_voltage", 71, 1, multiplier=10)
|
||||||
|
self.struct.add_decimal_field("internal_current_one", 72, 1)
|
||||||
|
self.struct.add_uint_field("internal_power_one", 73)
|
||||||
|
self.struct.add_decimal_field("internal_ac_frequency", 74, 2, multiplier=10)
|
||||||
|
self.struct.add_uint_field("internal_dc_input_voltage", 86, multiplier=.1)
|
||||||
|
self.struct.add_decimal_field("internal_dc_input_power", 87, 1, multiplier=10)
|
||||||
|
self.struct.add_decimal_field("internal_dc_input_current", 88, 2)
|
||||||
|
|
||||||
|
# Battery packs
|
||||||
|
self.struct.add_uint_field("pack_num_max", 91) # internal
|
||||||
|
self.struct.add_decimal_field("total_battery_voltage", 92, 1, multiplier=.1)
|
||||||
|
self.struct.add_uint_field("pack_num_result", 96) # internal
|
||||||
|
self.struct.add_decimal_field("pack_voltage", 98, 2) # Full pack voltage
|
||||||
|
self.struct.add_uint_field("pack_battery_percent", 99)
|
||||||
|
self.struct.add_decimal_array_field("cell_voltages", 105, 16, 2) # internal
|
||||||
|
self.struct.add_version_field("pack_bms_version", 201)
|
||||||
|
|
||||||
|
# Controls
|
||||||
|
self.struct.add_bool_field("power_off", 3060)
|
||||||
|
self.struct.add_enum_field("auto_sleep_mode", 3061, AutoSleepMode)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def pack_num_max(self):
|
||||||
|
return 3
|
||||||
|
|
||||||
|
@property
|
||||||
|
def polling_commands(self) -> List[ReadHoldingRegisters]:
|
||||||
|
return self.struct.get_read_holding_registers(
|
||||||
|
filter=lambda address: address <= 90 or address >= 256 # pack_polling_commands
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def writable_ranges(self) -> List[range]:
|
||||||
|
return super().writable_ranges + [range(3060, 3062)]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def pack_polling_commands(self) -> List[ReadHoldingRegisters]:
|
||||||
|
return [
|
||||||
|
ReadHoldingRegisters(91, 37),
|
||||||
|
ReadHoldingRegisters(201, 2),
|
||||||
|
]
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
"""AC200M fields."""
|
||||||
|
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from ..utils.commands import ReadHoldingRegisters
|
||||||
|
from ..field_enums import AutoSleepMode, OutputMode
|
||||||
|
from ..base_devices.ProtocolV1Device import ProtocolV1Device
|
||||||
|
|
||||||
|
|
||||||
|
class AC200M(ProtocolV1Device):
|
||||||
|
def __init__(self, address: str, sn: str):
|
||||||
|
super().__init__(address, "AC200M", sn)
|
||||||
|
|
||||||
|
# Details
|
||||||
|
self.struct.add_enum_field("ac_output_mode", 70, OutputMode)
|
||||||
|
self.struct.add_decimal_field("internal_ac_voltage", 71, 1)
|
||||||
|
self.struct.add_decimal_field("internal_current_one", 72, 1)
|
||||||
|
self.struct.add_uint_field("internal_power_one", 73)
|
||||||
|
self.struct.add_decimal_field("internal_ac_frequency", 74, 2)
|
||||||
|
self.struct.add_uint_field("internal_dc_input_voltage", 86)
|
||||||
|
self.struct.add_decimal_field("internal_dc_input_power", 87, 1)
|
||||||
|
self.struct.add_decimal_field("internal_dc_input_current", 88, 2)
|
||||||
|
|
||||||
|
# Battery packs
|
||||||
|
self.struct.add_uint_field("pack_num_max", 91) # internal
|
||||||
|
self.struct.add_decimal_field("total_battery_voltage", 92, 1)
|
||||||
|
self.struct.add_uint_field("pack_num_result", 96) # internal
|
||||||
|
self.struct.add_decimal_field("pack_voltage", 98, 2) # Full pack voltage
|
||||||
|
self.struct.add_uint_field("pack_battery_percent", 99)
|
||||||
|
self.struct.add_decimal_array_field("cell_voltages", 105, 16, 2) # internal
|
||||||
|
self.struct.add_version_field("pack_bms_version", 201)
|
||||||
|
|
||||||
|
# Controls
|
||||||
|
self.struct.add_bool_field("power_off", 3060)
|
||||||
|
self.struct.add_enum_field("auto_sleep_mode", 3061, AutoSleepMode)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def pack_num_max(self):
|
||||||
|
return 3
|
||||||
|
|
||||||
|
@property
|
||||||
|
def polling_commands(self) -> List[ReadHoldingRegisters]:
|
||||||
|
return self.struct.get_read_holding_registers(
|
||||||
|
filter=lambda address: address <= 90 or address >= 256 # pack_polling_commands
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def writable_ranges(self) -> List[range]:
|
||||||
|
return super().writable_ranges + [range(3060, 3062)]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def pack_polling_commands(self) -> List[ReadHoldingRegisters]:
|
||||||
|
return [
|
||||||
|
ReadHoldingRegisters(91, 37),
|
||||||
|
ReadHoldingRegisters(201, 2),
|
||||||
|
]
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
"""AC300 fields."""
|
||||||
|
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from ..utils.commands import ReadHoldingRegisters
|
||||||
|
from ..field_enums import AutoSleepMode, OutputMode, SplitPhaseMachineType, UpsMode
|
||||||
|
from ..base_devices.ProtocolV1Device import ProtocolV1Device
|
||||||
|
|
||||||
|
|
||||||
|
class AC300(ProtocolV1Device):
|
||||||
|
def __init__(self, address: str, sn: str):
|
||||||
|
super().__init__(address, "AC300", sn)
|
||||||
|
|
||||||
|
# Details
|
||||||
|
self.struct.add_enum_field("ac_output_mode", 70, OutputMode)
|
||||||
|
self.struct.add_decimal_field("internal_ac_voltage", 71, 1)
|
||||||
|
self.struct.add_decimal_field("internal_current_one", 72, 1)
|
||||||
|
self.struct.add_int_field("internal_power_one", 73)
|
||||||
|
self.struct.add_decimal_field("internal_ac_frequency", 74, 2)
|
||||||
|
self.struct.add_decimal_field("internal_current_two", 75, 1)
|
||||||
|
self.struct.add_int_field("internal_power_two", 76)
|
||||||
|
self.struct.add_decimal_field("ac_input_voltage", 77, 1)
|
||||||
|
self.struct.add_decimal_field("internal_current_three", 78, 1, (0, 100))
|
||||||
|
self.struct.add_int_field("internal_power_three", 79)
|
||||||
|
self.struct.add_decimal_field("ac_input_frequency", 80, 2)
|
||||||
|
self.struct.add_uint_field("pv_input_voltage1", 86, multiplier=.1)
|
||||||
|
self.struct.add_decimal_field("pv_input_power1", 87, 1, multiplier=10)
|
||||||
|
self.struct.add_decimal_field("pv_input_current1", 88, 2, multiplier=10)
|
||||||
|
|
||||||
|
# Battery packs
|
||||||
|
self.struct.add_uint_field("pack_num_max", 91) # internal
|
||||||
|
self.struct.add_decimal_field("total_battery_voltage", 92, 1)
|
||||||
|
self.struct.add_uint_field("pack_num_result", 96) # internal
|
||||||
|
self.struct.add_decimal_field("pack_voltage", 98, 2) # Full pack voltage
|
||||||
|
self.struct.add_uint_field("pack_battery_percent", 99)
|
||||||
|
self.struct.add_decimal_array_field("cell_voltages", 105, 16, 2) # internal
|
||||||
|
self.struct.add_version_field("pack_bms_version", 201)
|
||||||
|
|
||||||
|
# Controls
|
||||||
|
self.struct.add_enum_field("ups_mode", 3001, UpsMode)
|
||||||
|
self.struct.add_bool_field("split_phase_on", 3004)
|
||||||
|
self.struct.add_enum_field(
|
||||||
|
"split_phase_machine_mode", 3005, SplitPhaseMachineType
|
||||||
|
)
|
||||||
|
self.struct.add_bool_field("grid_charge_on", 3011)
|
||||||
|
self.struct.add_bool_field("time_control_on", 3013)
|
||||||
|
self.struct.add_uint_field("battery_range_start", 3015)
|
||||||
|
self.struct.add_uint_field("battery_range_end", 3016)
|
||||||
|
self.struct.add_enum_field("auto_sleep_mode", 3061, AutoSleepMode)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def pack_num_max(self):
|
||||||
|
return 4
|
||||||
|
|
||||||
|
@property
|
||||||
|
def polling_commands(self) -> List[ReadHoldingRegisters]:
|
||||||
|
return self.struct.get_read_holding_registers(
|
||||||
|
filter=lambda address: address <= 90 or address >= 256 # pack_polling_commands
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def writable_ranges(self) -> List[range]:
|
||||||
|
return super().writable_ranges + [
|
||||||
|
range(3001, 3017),
|
||||||
|
range(3061, 3062),
|
||||||
|
]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def pack_polling_commands(self) -> List[ReadHoldingRegisters]:
|
||||||
|
return [
|
||||||
|
ReadHoldingRegisters(91, 37),
|
||||||
|
ReadHoldingRegisters(201, 2),
|
||||||
|
]
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
"""AC500 fields."""
|
||||||
|
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from ..utils.commands import ReadHoldingRegisters
|
||||||
|
from ..field_enums import AutoSleepMode, OutputMode, SplitPhaseMachineType, UpsMode
|
||||||
|
from ..base_devices.ProtocolV1Device import ProtocolV1Device
|
||||||
|
|
||||||
|
|
||||||
|
class AC500(ProtocolV1Device):
|
||||||
|
def __init__(self, address: str, sn: str):
|
||||||
|
super().__init__(address, "AC500", sn)
|
||||||
|
|
||||||
|
# Details
|
||||||
|
self.struct.add_enum_field("ac_output_mode", 70, OutputMode)
|
||||||
|
self.struct.add_decimal_field("internal_ac_voltage", 71, 1)
|
||||||
|
self.struct.add_decimal_field("internal_current_one", 72, 1)
|
||||||
|
self.struct.add_int_field("internal_power_one", 73)
|
||||||
|
self.struct.add_decimal_field("internal_ac_frequency", 74, 2)
|
||||||
|
self.struct.add_decimal_field("internal_current_two", 75, 1)
|
||||||
|
self.struct.add_int_field("internal_power_two", 76)
|
||||||
|
self.struct.add_decimal_field("ac_input_voltage", 77, 1)
|
||||||
|
self.struct.add_decimal_field("internal_current_three", 78, 1, (0, 100))
|
||||||
|
self.struct.add_int_field("internal_power_three", 79)
|
||||||
|
self.struct.add_decimal_field("ac_input_frequency", 80, 2)
|
||||||
|
self.struct.add_uint_field("pv_input_voltage1", 86, multiplier=.1)
|
||||||
|
self.struct.add_decimal_field("pv_input_power1", 87, 1, multiplier=10)
|
||||||
|
self.struct.add_decimal_field("pv_input_current1", 88, 2, multiplier=10)
|
||||||
|
|
||||||
|
# Battery packs
|
||||||
|
self.struct.add_uint_field("pack_num_max", 91) # internal
|
||||||
|
self.struct.add_decimal_field("total_battery_voltage", 92, 1)
|
||||||
|
self.struct.add_uint_field("pack_num_result", 96) # internal
|
||||||
|
self.struct.add_decimal_field("pack_voltage", 98, 2) # Full pack voltage
|
||||||
|
self.struct.add_uint_field("pack_battery_percent", 99)
|
||||||
|
self.struct.add_decimal_array_field("cell_voltages", 105, 16, 2) # internal
|
||||||
|
self.struct.add_version_field('pack_bms_version', 201)
|
||||||
|
|
||||||
|
# Controls
|
||||||
|
self.struct.add_enum_field("ups_mode", 3001, UpsMode)
|
||||||
|
self.struct.add_bool_field("split_phase_on", 3004)
|
||||||
|
self.struct.add_enum_field(
|
||||||
|
"split_phase_machine_mode", 3005, SplitPhaseMachineType
|
||||||
|
)
|
||||||
|
self.struct.add_bool_field("grid_charge_on", 3011)
|
||||||
|
self.struct.add_bool_field("time_control_on", 3013)
|
||||||
|
self.struct.add_uint_field("battery_range_start", 3015)
|
||||||
|
self.struct.add_uint_field("battery_range_end", 3016)
|
||||||
|
self.struct.add_enum_field("auto_sleep_mode", 3061, AutoSleepMode)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def pack_num_max(self):
|
||||||
|
return 6
|
||||||
|
|
||||||
|
@property
|
||||||
|
def polling_commands(self) -> List[ReadHoldingRegisters]:
|
||||||
|
return self.struct.get_read_holding_registers(
|
||||||
|
filter=lambda address: address <= 90 or address >= 256 # pack_polling_commands
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def writable_ranges(self) -> List[range]:
|
||||||
|
return super().writable_ranges + [
|
||||||
|
range(3001, 3017),
|
||||||
|
range(3061, 3062),
|
||||||
|
]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def pack_polling_commands(self) -> List[ReadHoldingRegisters]:
|
||||||
|
return [
|
||||||
|
ReadHoldingRegisters(91, 37),
|
||||||
|
ReadHoldingRegisters(201, 2),
|
||||||
|
]
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
"""AC60 fields."""
|
||||||
|
|
||||||
|
from ..base_devices.ProtocolV2Device import ProtocolV2Device
|
||||||
|
|
||||||
|
|
||||||
|
class AC60(ProtocolV2Device):
|
||||||
|
def __init__(self, address: str, sn: str):
|
||||||
|
super().__init__(address, "AC60", sn)
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
"""AC70 fields."""
|
||||||
|
|
||||||
|
from ..base_devices.ProtocolV2Device import ProtocolV2Device
|
||||||
|
|
||||||
|
|
||||||
|
class AC70(ProtocolV2Device):
|
||||||
|
def __init__(self, address: str, sn: str):
|
||||||
|
super().__init__(address, "AC70", sn)
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
"""AC70P fields."""
|
||||||
|
|
||||||
|
from ..base_devices.ProtocolV2Device import ProtocolV2Device
|
||||||
|
|
||||||
|
|
||||||
|
class AC70P(ProtocolV2Device):
|
||||||
|
def __init__(self, address: str, sn: str):
|
||||||
|
super().__init__(address, "AC70P", sn)
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
"""EB3A fields."""
|
||||||
|
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from ..utils.commands import ReadHoldingRegisters
|
||||||
|
from ..field_enums import ChargingMode, EcoShutdown, LedMode
|
||||||
|
from ..base_devices.ProtocolV1Device import ProtocolV1Device
|
||||||
|
|
||||||
|
|
||||||
|
class EB3A(ProtocolV1Device):
|
||||||
|
def __init__(self, address: str, sn: str):
|
||||||
|
super().__init__(address, "EB3A", sn)
|
||||||
|
|
||||||
|
# Details
|
||||||
|
self.struct.add_decimal_field("ac_input_voltage", 77, 1)
|
||||||
|
self.struct.add_uint_field("internal_dc_input_voltage", 86)
|
||||||
|
|
||||||
|
# Controls
|
||||||
|
self.struct.add_enum_field("led_mode", 3034, LedMode)
|
||||||
|
self.struct.add_bool_field("power_off", 3060)
|
||||||
|
self.struct.add_bool_field("eco_on", 3063)
|
||||||
|
self.struct.add_enum_field("eco_shutdown", 3064, EcoShutdown)
|
||||||
|
self.struct.add_enum_field("charging_mode", 3065, ChargingMode)
|
||||||
|
self.struct.add_bool_field("power_lifting_on", 3066)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def polling_commands(self) -> List[ReadHoldingRegisters]:
|
||||||
|
return self.struct.get_read_holding_registers()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def writable_ranges(self) -> List[range]:
|
||||||
|
return super().writable_ranges + [
|
||||||
|
range(3034, 3035),
|
||||||
|
range(3060, 3061),
|
||||||
|
range(3063, 3067),
|
||||||
|
]
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
"""EP500 fields."""
|
||||||
|
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from ..utils.commands import ReadHoldingRegisters
|
||||||
|
from ..field_enums import AutoSleepMode, OutputMode, SplitPhaseMachineType, UpsMode
|
||||||
|
from ..base_devices.ProtocolV1Device import ProtocolV1Device
|
||||||
|
|
||||||
|
|
||||||
|
class EP500(ProtocolV1Device):
|
||||||
|
def __init__(self, address: str, sn: str):
|
||||||
|
super().__init__(address, "EP500", sn)
|
||||||
|
|
||||||
|
# Details
|
||||||
|
self.struct.add_enum_field("ac_output_mode", 70, OutputMode)
|
||||||
|
self.struct.add_decimal_field("internal_ac_voltage", 71, 1)
|
||||||
|
self.struct.add_decimal_field("internal_current_one", 72, 1)
|
||||||
|
self.struct.add_uint_field("internal_power_one", 73)
|
||||||
|
self.struct.add_decimal_field("internal_ac_frequency", 74, 2)
|
||||||
|
self.struct.add_decimal_field("internal_current_two", 75, 1)
|
||||||
|
self.struct.add_uint_field("internal_power_two", 76)
|
||||||
|
self.struct.add_decimal_field("ac_input_voltage", 77, 1)
|
||||||
|
self.struct.add_decimal_field("internal_current_three", 78, 1, (0, 100))
|
||||||
|
self.struct.add_uint_field("internal_power_three", 79)
|
||||||
|
self.struct.add_decimal_field("ac_input_frequency", 80, 2)
|
||||||
|
self.struct.add_uint_field("internal_dc_input_voltage", 86)
|
||||||
|
self.struct.add_decimal_field("internal_dc_input_power", 87, 1)
|
||||||
|
self.struct.add_decimal_field("internal_dc_input_current", 88, 2)
|
||||||
|
|
||||||
|
# Battery packs
|
||||||
|
self.struct.add_uint_field("pack_num_max", 91) # internal
|
||||||
|
self.struct.add_decimal_field("total_battery_voltage", 92, 1)
|
||||||
|
self.struct.add_uint_field("pack_num_result", 96) # internal
|
||||||
|
self.struct.add_decimal_field("pack_voltage", 98, 2) # Full pack voltage
|
||||||
|
self.struct.add_uint_field("pack_battery_percent", 99)
|
||||||
|
self.struct.add_decimal_array_field("cell_voltages", 105, 16, 2) # internal
|
||||||
|
self.struct.add_version_field("pack_bms_version", 201)
|
||||||
|
|
||||||
|
# Controls
|
||||||
|
self.struct.add_enum_field("ups_mode", 3001, UpsMode)
|
||||||
|
self.struct.add_bool_field("split_phase_on", 3004)
|
||||||
|
self.struct.add_enum_field(
|
||||||
|
"split_phase_machine_mode", 3005, SplitPhaseMachineType
|
||||||
|
)
|
||||||
|
self.struct.add_bool_field("grid_charge_on", 3011)
|
||||||
|
self.struct.add_bool_field("time_control_on", 3013)
|
||||||
|
self.struct.add_uint_field("battery_range_start", 3015)
|
||||||
|
self.struct.add_uint_field("battery_range_end", 3016)
|
||||||
|
self.struct.add_enum_field("auto_sleep_mode", 3061, AutoSleepMode)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def polling_commands(self) -> List[ReadHoldingRegisters]:
|
||||||
|
return self.struct.get_read_holding_registers(
|
||||||
|
filter=lambda address: address <= 90 or address >= 256 # pack_polling_commands
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def writable_ranges(self) -> List[range]:
|
||||||
|
return super().writable_ranges + [
|
||||||
|
range(3001, 3017),
|
||||||
|
range(3061, 3062),
|
||||||
|
]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def pack_polling_commands(self) -> List[ReadHoldingRegisters]:
|
||||||
|
return [
|
||||||
|
ReadHoldingRegisters(91, 37),
|
||||||
|
ReadHoldingRegisters(201, 2),
|
||||||
|
]
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
"""EP500P fields."""
|
||||||
|
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from ..utils.commands import ReadHoldingRegisters
|
||||||
|
from ..field_enums import AutoSleepMode, OutputMode, SplitPhaseMachineType, UpsMode
|
||||||
|
from ..base_devices.ProtocolV1Device import ProtocolV1Device
|
||||||
|
|
||||||
|
|
||||||
|
class EP500P(ProtocolV1Device):
|
||||||
|
def __init__(self, address: str, sn: str):
|
||||||
|
super().__init__(address, "EP500P", sn)
|
||||||
|
|
||||||
|
# Details
|
||||||
|
self.struct.add_enum_field("ac_output_mode", 70, OutputMode)
|
||||||
|
self.struct.add_decimal_field("internal_ac_voltage", 71, 1)
|
||||||
|
self.struct.add_decimal_field("internal_current_one", 72, 1)
|
||||||
|
self.struct.add_uint_field("internal_power_one", 73)
|
||||||
|
self.struct.add_decimal_field("internal_ac_frequency", 74, 2)
|
||||||
|
self.struct.add_decimal_field("internal_current_two", 75, 1)
|
||||||
|
self.struct.add_uint_field("internal_power_two", 76)
|
||||||
|
self.struct.add_decimal_field("ac_input_voltage", 77, 1)
|
||||||
|
self.struct.add_decimal_field("internal_current_three", 78, 1, (0, 100))
|
||||||
|
self.struct.add_uint_field("internal_power_three", 79)
|
||||||
|
self.struct.add_decimal_field("ac_input_frequency", 80, 2)
|
||||||
|
self.struct.add_uint_field("internal_dc_input_voltage", 86)
|
||||||
|
self.struct.add_decimal_field("internal_dc_input_power", 87, 1)
|
||||||
|
self.struct.add_decimal_field("internal_dc_input_current", 88, 2)
|
||||||
|
|
||||||
|
# Battery packs
|
||||||
|
self.struct.add_uint_field("pack_num_max", 91) # internal
|
||||||
|
self.struct.add_decimal_field("total_battery_voltage", 92, 1)
|
||||||
|
self.struct.add_uint_field("pack_num_result", 96) # internal
|
||||||
|
self.struct.add_decimal_field("pack_voltage", 98, 2) # Full pack voltage
|
||||||
|
self.struct.add_uint_field("pack_battery_percent", 99)
|
||||||
|
self.struct.add_decimal_array_field("cell_voltages", 105, 16, 2) # internal
|
||||||
|
self.struct.add_version_field("pack_bms_version", 201)
|
||||||
|
|
||||||
|
# Controls
|
||||||
|
self.struct.add_enum_field("ups_mode", 3001, UpsMode)
|
||||||
|
self.struct.add_bool_field("split_phase_on", 3004)
|
||||||
|
self.struct.add_enum_field(
|
||||||
|
"split_phase_machine_mode", 3005, SplitPhaseMachineType
|
||||||
|
)
|
||||||
|
self.struct.add_bool_field("grid_charge_on", 3011)
|
||||||
|
self.struct.add_bool_field("time_control_on", 3013)
|
||||||
|
self.struct.add_uint_field("battery_range_start", 3015)
|
||||||
|
self.struct.add_uint_field("battery_range_end", 3016)
|
||||||
|
self.struct.add_enum_field("auto_sleep_mode", 3061, AutoSleepMode)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def polling_commands(self) -> List[ReadHoldingRegisters]:
|
||||||
|
return self.struct.get_read_holding_registers(
|
||||||
|
filter=lambda address: address <= 90 or address >= 256 # pack_polling_commands
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def writable_ranges(self) -> List[range]:
|
||||||
|
return super().writable_ranges + [
|
||||||
|
range(3001, 3017),
|
||||||
|
range(3061, 3062),
|
||||||
|
]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def pack_polling_commands(self) -> List[ReadHoldingRegisters]:
|
||||||
|
return [
|
||||||
|
ReadHoldingRegisters(91, 37),
|
||||||
|
ReadHoldingRegisters(201, 2),
|
||||||
|
]
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
"""EP600 fields."""
|
||||||
|
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from ..utils.commands import ReadHoldingRegisters
|
||||||
|
from ..base_devices.ProtocolV2Device import ProtocolV2Device
|
||||||
|
|
||||||
|
|
||||||
|
class EP600(ProtocolV2Device):
|
||||||
|
def __init__(self, address: str, sn: str):
|
||||||
|
super().__init__(address, "EP600", sn)
|
||||||
|
|
||||||
|
# Details
|
||||||
|
self.struct.add_uint_field("battery_range_start", 2022)
|
||||||
|
self.struct.add_uint_field("battery_range_end", 2023)
|
||||||
|
self.struct.add_uint_field("max_ac_input_power", 2213)
|
||||||
|
self.struct.add_uint_field("max_ac_input_current", 2214)
|
||||||
|
self.struct.add_uint_field("max_ac_output_power", 2215)
|
||||||
|
self.struct.add_uint_field("max_ac_output_current", 2216)
|
||||||
|
|
||||||
|
# DC Solar
|
||||||
|
self.struct.add_uint_field("pv_input_power1", 1212)
|
||||||
|
self.struct.add_decimal_field("pv_input_voltage1", 1213, 1)
|
||||||
|
self.struct.add_decimal_field("pv_input_current1", 1214, 1)
|
||||||
|
self.struct.add_uint_field("pv_input_power2", 1220)
|
||||||
|
self.struct.add_decimal_field("pv_input_voltage2", 1221, 1)
|
||||||
|
self.struct.add_decimal_field("pv_input_current2", 1222, 1)
|
||||||
|
|
||||||
|
# ADL400 Smart Meter for AC Solar
|
||||||
|
self.struct.add_uint_field("adl400_ac_input_power_phase1", 1228)
|
||||||
|
self.struct.add_uint_field("adl400_ac_input_power_phase2", 1236)
|
||||||
|
self.struct.add_uint_field("adl400_ac_input_power_phase3", 1244)
|
||||||
|
self.struct.add_decimal_field("adl400_ac_input_voltage_phase1", 1229, 1)
|
||||||
|
self.struct.add_decimal_field("adl400_ac_input_voltage_phase2", 1237, 1)
|
||||||
|
self.struct.add_decimal_field("adl400_ac_input_voltage_phase3", 1245, 1)
|
||||||
|
# Testing required
|
||||||
|
self.struct.add_uint_field("adl400_ac_input_current_phase1", 1230)
|
||||||
|
self.struct.add_uint_field("adl400_ac_input_current_phase2", 1238)
|
||||||
|
self.struct.add_uint_field("adl400_ac_input_current_phase3", 1246)
|
||||||
|
|
||||||
|
# Grid Input
|
||||||
|
self.struct.add_decimal_field("grid_frequency", 1300, 1)
|
||||||
|
self.struct.add_uint_field("grid_power_phase1", 1313)
|
||||||
|
self.struct.add_uint_field("grid_power_phase2", 1319)
|
||||||
|
self.struct.add_uint_field("grid_power_phase3", 1325)
|
||||||
|
self.struct.add_decimal_field("grid_voltage_phase1", 1314, 1)
|
||||||
|
self.struct.add_decimal_field("grid_voltage_phase2", 1320, 1)
|
||||||
|
self.struct.add_decimal_field("grid_voltage_phase3", 1326, 1)
|
||||||
|
self.struct.add_decimal_field("grid_current_phase1", 1315, 1)
|
||||||
|
self.struct.add_decimal_field("grid_current_phase2", 1321, 1)
|
||||||
|
self.struct.add_decimal_field("grid_current_phase3", 1327, 1)
|
||||||
|
|
||||||
|
# EP600 AC Output
|
||||||
|
self.struct.add_decimal_field("ac_output_frequency", 1500, 1)
|
||||||
|
self.struct.add_int_field("ac_output_power_phase1", 1510)
|
||||||
|
self.struct.add_int_field("ac_output_power_phase2", 1517)
|
||||||
|
self.struct.add_int_field("ac_output_power_phase3", 1524)
|
||||||
|
self.struct.add_decimal_field("ac_output_voltage_phase1", 1511, 1)
|
||||||
|
self.struct.add_decimal_field("ac_output_voltage_phase2", 1518, 1)
|
||||||
|
self.struct.add_decimal_field("ac_output_voltage_phase3", 1525, 1)
|
||||||
|
self.struct.add_decimal_field("ac_output_current_phase1", 1512, 1)
|
||||||
|
self.struct.add_decimal_field("ac_output_current_phase2", 1519, 1)
|
||||||
|
self.struct.add_decimal_field("ac_output_current_phase3", 1526, 1)
|
||||||
|
|
||||||
|
# KM - Consumption
|
||||||
|
self.struct.add_int_field("consumption_power_phase1", 1430)
|
||||||
|
self.struct.add_int_field("consumption_power_phase2", 1436)
|
||||||
|
self.struct.add_int_field("consumption_power_phase3", 1442)
|
||||||
|
self.struct.add_decimal_field("consumption_voltage_phase1", 1431, 1)
|
||||||
|
self.struct.add_decimal_field("consumption_voltage_phase2", 1437, 1)
|
||||||
|
self.struct.add_decimal_field("consumption_voltage_phase3", 1443, 1)
|
||||||
|
self.struct.add_decimal_field("consumption_current_phase1", 1432, 1)
|
||||||
|
self.struct.add_decimal_field("consumption_current_phase2", 1438, 1)
|
||||||
|
self.struct.add_decimal_field("consumption_current_phase3", 1444, 1)
|
||||||
|
|
||||||
|
# KM - Totals
|
||||||
|
self.struct.add_uint_field('pv_input_power_all', 144) # Total PV in
|
||||||
|
self.struct.add_uint_field('consumption_power_all', 142) # Total AC out
|
||||||
|
self.struct.add_uint_field('grid_power_all', 146) # Total Grid in - value only +/- unknown
|
||||||
|
|
||||||
|
# Statistics
|
||||||
|
self.struct.add_decimal_field(
|
||||||
|
"total_ac_consumption", 152, 1
|
||||||
|
) # Load consumption
|
||||||
|
self.struct.add_decimal_field(
|
||||||
|
"total_grid_consumption", 156, 1
|
||||||
|
) # Grid consumption stats
|
||||||
|
self.struct.add_decimal_field("total_grid_feed", 158, 1)
|
||||||
|
|
||||||
|
# Battery packs
|
||||||
|
self.struct.add_swap_string_field("battery_type", 6101, 6) # internal
|
||||||
|
self.struct.add_sn_field("battery_serial_number", 6107) # internal
|
||||||
|
self.struct.add_version_field("bcu_version", 6175) # internal
|
||||||
|
self.struct.add_version_field("bmu_version", 6178) # internal
|
||||||
|
self.struct.add_version_field("safety_module_version", 6181) # internal
|
||||||
|
self.struct.add_version_field("high_voltage_module_version", 6184) # internal
|
||||||
|
|
||||||
|
@property
|
||||||
|
def polling_commands(self) -> List[ReadHoldingRegisters]:
|
||||||
|
return self.struct.get_read_holding_registers()
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
"""EP760 fields."""
|
||||||
|
|
||||||
|
from ..base_devices.ProtocolV2Device import ProtocolV2Device
|
||||||
|
|
||||||
|
|
||||||
|
class EP760(ProtocolV2Device):
|
||||||
|
def __init__(self, address: str, sn: str):
|
||||||
|
super().__init__(address, "EP760", sn)
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
"""EP800 fields."""
|
||||||
|
|
||||||
|
from ..base_devices.ProtocolV2Device import ProtocolV2Device
|
||||||
|
|
||||||
|
|
||||||
|
class EP800(ProtocolV2Device):
|
||||||
|
def __init__(self, address: str, sn: str):
|
||||||
|
super().__init__(address, "EP800", sn)
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
"""Exceptions."""
|
||||||
|
|
||||||
|
# Modified copy of https://github.com/warhammerkid/bluetti_mqtt/blob/main/bluetti_mqtt/bluetooth/exc.py
|
||||||
|
|
||||||
|
|
||||||
|
class ParseError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class ModbusError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class BadConnectionError(Exception):
|
||||||
|
pass
|
||||||
@@ -0,0 +1,356 @@
|
|||||||
|
"""Field attributes for device fields."""
|
||||||
|
|
||||||
|
from enum import Enum, auto, unique
|
||||||
|
|
||||||
|
from .field_enums import (
|
||||||
|
AutoSleepMode,
|
||||||
|
ChargingMode,
|
||||||
|
EcoShutdown,
|
||||||
|
LedMode,
|
||||||
|
OutputMode,
|
||||||
|
SplitPhaseMachineType,
|
||||||
|
UpsMode,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@unique
|
||||||
|
class FieldType(Enum):
|
||||||
|
NUMERIC = auto()
|
||||||
|
BOOL = auto()
|
||||||
|
ENUM = auto()
|
||||||
|
|
||||||
|
|
||||||
|
class FieldAttributes:
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
type: FieldType = FieldType.NUMERIC,
|
||||||
|
setter: bool = False,
|
||||||
|
name: str = "",
|
||||||
|
unit_of_measurement: str | None = None,
|
||||||
|
device_class: str | None = None,
|
||||||
|
state_class: str | None = None,
|
||||||
|
options: Enum | None = None,
|
||||||
|
):
|
||||||
|
self.type = type
|
||||||
|
self.setter = setter
|
||||||
|
self.name = name
|
||||||
|
self.unit_of_measurement = unit_of_measurement
|
||||||
|
self.device_class = device_class
|
||||||
|
self.state_class = state_class
|
||||||
|
self.options = options
|
||||||
|
|
||||||
|
|
||||||
|
class PowerFieldAttributes(FieldAttributes):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
name: str = "",
|
||||||
|
):
|
||||||
|
super().__init__(
|
||||||
|
name=name,
|
||||||
|
unit_of_measurement="W",
|
||||||
|
device_class="power",
|
||||||
|
state_class="measurement",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class VoltageFieldAttributes(FieldAttributes):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
name: str = "",
|
||||||
|
):
|
||||||
|
super().__init__(
|
||||||
|
name=name,
|
||||||
|
unit_of_measurement="V",
|
||||||
|
device_class="voltage",
|
||||||
|
state_class="measurement",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class CurrentFieldAttributes(FieldAttributes):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
name: str = "",
|
||||||
|
):
|
||||||
|
super().__init__(
|
||||||
|
name=name,
|
||||||
|
unit_of_measurement="A",
|
||||||
|
device_class="current",
|
||||||
|
state_class="measurement",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class EnergyFieldAttributes(FieldAttributes):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
name: str = "",
|
||||||
|
):
|
||||||
|
super().__init__(
|
||||||
|
name=name,
|
||||||
|
unit_of_measurement="kWh",
|
||||||
|
device_class="energy",
|
||||||
|
state_class="total_increasing",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class FrequencyFieldAttributes(FieldAttributes):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
name: str = "",
|
||||||
|
):
|
||||||
|
super().__init__(
|
||||||
|
name=name,
|
||||||
|
unit_of_measurement="Hz",
|
||||||
|
device_class="frequency",
|
||||||
|
state_class="measurement",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class OutletFieldAttributes(FieldAttributes):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
name: str = "",
|
||||||
|
setter: bool = False,
|
||||||
|
):
|
||||||
|
super().__init__(
|
||||||
|
type=FieldType.BOOL,
|
||||||
|
name=name,
|
||||||
|
setter=setter,
|
||||||
|
device_class="outlet",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
FIELD_ATTRIBUTES: dict[str, FieldAttributes] = {
|
||||||
|
# Base device fields
|
||||||
|
"dc_input_power": PowerFieldAttributes("DC Input Power"),
|
||||||
|
"ac_input_power": PowerFieldAttributes("AC Input Power"),
|
||||||
|
"ac_output_power": PowerFieldAttributes("AC Output Power"),
|
||||||
|
"dc_output_power": PowerFieldAttributes("DC Output Power"),
|
||||||
|
"dsp_version": FieldAttributes(
|
||||||
|
type=FieldType.NUMERIC,
|
||||||
|
name=f"DSP Version",
|
||||||
|
),
|
||||||
|
"arm_version": FieldAttributes(
|
||||||
|
type=FieldType.NUMERIC,
|
||||||
|
name=f"ARM Version",
|
||||||
|
),
|
||||||
|
"power_generation": EnergyFieldAttributes("Total Power Generation"),
|
||||||
|
"total_battery_percent": FieldAttributes(
|
||||||
|
type=FieldType.NUMERIC,
|
||||||
|
name="Total Battery Percent",
|
||||||
|
unit_of_measurement="%",
|
||||||
|
device_class="battery",
|
||||||
|
state_class="measurement",
|
||||||
|
),
|
||||||
|
"total_battery_voltage": VoltageFieldAttributes("Total Battery Voltage"),
|
||||||
|
"ac_output_on": OutletFieldAttributes("AC Output"),
|
||||||
|
"dc_output_on": OutletFieldAttributes("DC Output"),
|
||||||
|
# Controls
|
||||||
|
"ac_output_on_switch": OutletFieldAttributes(
|
||||||
|
name="AC Output",
|
||||||
|
setter=True,
|
||||||
|
),
|
||||||
|
"dc_output_on_switch": OutletFieldAttributes(
|
||||||
|
name="DC Output",
|
||||||
|
setter=True,
|
||||||
|
),
|
||||||
|
# Device specific fields
|
||||||
|
"ac_output_mode": FieldAttributes(
|
||||||
|
type=FieldType.ENUM,
|
||||||
|
name="AC Output Mode",
|
||||||
|
options=OutputMode,
|
||||||
|
),
|
||||||
|
"internal_ac_voltage": VoltageFieldAttributes("Internal AC Voltage"),
|
||||||
|
"internal_current_one": CurrentFieldAttributes("Internal Current Sensor 1"),
|
||||||
|
"internal_power_one": PowerFieldAttributes("Internal Power Sensor 1"),
|
||||||
|
"internal_ac_frequency": FrequencyFieldAttributes("Internal AC Frequency"),
|
||||||
|
"internal_current_two": CurrentFieldAttributes("Internal Current Sensor 2"),
|
||||||
|
"internal_power_two": PowerFieldAttributes("Internal Power Sensor 2"),
|
||||||
|
"ac_input_voltage": VoltageFieldAttributes("AC Input Voltage"),
|
||||||
|
"internal_current_three": CurrentFieldAttributes("Internal Current Sensor 3"),
|
||||||
|
"internal_power_three": PowerFieldAttributes("Internal Power Sensor 3"),
|
||||||
|
"ac_input_frequency": FrequencyFieldAttributes("AC Input Frequency"),
|
||||||
|
"internal_dc_input_voltage": VoltageFieldAttributes("Internal DC Input Voltage"),
|
||||||
|
"internal_dc_input_power": PowerFieldAttributes("Internal DC Input Power"),
|
||||||
|
"internal_dc_input_current": CurrentFieldAttributes("Internal DC Input Current"),
|
||||||
|
"pv_input_power1": PowerFieldAttributes("Solar Input Power 1"),
|
||||||
|
"pv_input_power2": PowerFieldAttributes("Solar Input Power 2"),
|
||||||
|
"pv_input_voltage1": VoltageFieldAttributes("Solar Input Voltage 1"),
|
||||||
|
"pv_input_voltage2": VoltageFieldAttributes("Solar Input Voltage 2"),
|
||||||
|
"pv_input_current1": CurrentFieldAttributes("Solar Input Current 1"),
|
||||||
|
"pv_input_current2": CurrentFieldAttributes("Solar Input Current 2"),
|
||||||
|
"adl400_ac_input_power_phase1": PowerFieldAttributes(
|
||||||
|
"AC Solar Input Power Phase 1"
|
||||||
|
),
|
||||||
|
"adl400_ac_input_power_phase2": PowerFieldAttributes(
|
||||||
|
"AC Solar Input Power Phase 2"
|
||||||
|
),
|
||||||
|
"adl400_ac_input_power_phase3": PowerFieldAttributes(
|
||||||
|
"AC Solar Input Power Phase 3"
|
||||||
|
),
|
||||||
|
"adl400_ac_input_voltage_phase1": VoltageFieldAttributes(
|
||||||
|
"AC Solar Input Voltage Phase 1"
|
||||||
|
),
|
||||||
|
"adl400_ac_input_voltage_phase2": VoltageFieldAttributes(
|
||||||
|
"AC Solar Input Voltage Phase 2"
|
||||||
|
),
|
||||||
|
"adl400_ac_input_voltage_phase3": VoltageFieldAttributes(
|
||||||
|
"AC Solar Input Voltage Phase 3"
|
||||||
|
),
|
||||||
|
"adl400_ac_input_current_phase1": CurrentFieldAttributes(
|
||||||
|
"AC Solar Input Current Phase 1"
|
||||||
|
),
|
||||||
|
"adl400_ac_input_current_phase2": CurrentFieldAttributes(
|
||||||
|
"AC Solar Input Current Phase 2"
|
||||||
|
),
|
||||||
|
"adl400_ac_input_current_phase3": CurrentFieldAttributes(
|
||||||
|
"AC Solar Input Current Phase 3"
|
||||||
|
),
|
||||||
|
"grid_frequency": FrequencyFieldAttributes("Grid Frequency"),
|
||||||
|
"grid_power_phase1": PowerFieldAttributes("Grid Power Phase 1"),
|
||||||
|
"grid_power_phase2": PowerFieldAttributes("Grid Power Phase 2"),
|
||||||
|
"grid_power_phase3": PowerFieldAttributes("Grid Power Phase 3"),
|
||||||
|
"grid_voltage_phase1": VoltageFieldAttributes("Grid Voltage Phase 1"),
|
||||||
|
"grid_voltage_phase2": VoltageFieldAttributes("Grid Voltage Phase 2"),
|
||||||
|
"grid_voltage_phase3": VoltageFieldAttributes("Grid Voltage Phase 3"),
|
||||||
|
"grid_current_phase1": CurrentFieldAttributes("Grid Current Phase 1"),
|
||||||
|
"grid_current_phase2": CurrentFieldAttributes("Grid Current Phase 2"),
|
||||||
|
"grid_current_phase3": CurrentFieldAttributes("Grid Current Phase 3"),
|
||||||
|
"ac_output_frequency": FrequencyFieldAttributes("AC Output Frequency"),
|
||||||
|
"ac_output_power_phase1": PowerFieldAttributes("AC Output Power Phase 1"),
|
||||||
|
"ac_output_power_phase2": PowerFieldAttributes("AC Output Power Phase 2"),
|
||||||
|
"ac_output_power_phase3": PowerFieldAttributes("AC Output Power Phase 3"),
|
||||||
|
"ac_output_voltage_phase1": VoltageFieldAttributes("AC Output Voltage Phase 1"),
|
||||||
|
"ac_output_voltage_phase2": VoltageFieldAttributes("AC Output Voltage Phase 2"),
|
||||||
|
"ac_output_voltage_phase3": VoltageFieldAttributes("AC Output Voltage Phase 3"),
|
||||||
|
"total_ac_consumption": EnergyFieldAttributes("Total Load Consumption"),
|
||||||
|
"total_grid_consumption": EnergyFieldAttributes("Total Grid Consumption"),
|
||||||
|
"total_grid_feed": EnergyFieldAttributes("Total Grid Feed"),
|
||||||
|
|
||||||
|
# KM - Consumption
|
||||||
|
"consumption_power_phase1": PowerFieldAttributes("Consumption Power Phase 1"),
|
||||||
|
"consumption_power_phase2": PowerFieldAttributes("Consumption Power Phase 2"),
|
||||||
|
"consumption_power_phase3": PowerFieldAttributes("Consumption Power Phase 3"),
|
||||||
|
"consumption_voltage_phase1": VoltageFieldAttributes("Consumption Voltage Phase 1"),
|
||||||
|
"consumption_voltage_phase2": VoltageFieldAttributes("Consumption Voltage Phase 2"),
|
||||||
|
"consumption_voltage_phase3": VoltageFieldAttributes("Consumption Voltage Phase 3"),
|
||||||
|
"consumption_current_phase1": CurrentFieldAttributes("Consumption Current Phase 1"),
|
||||||
|
"consumption_current_phase2": CurrentFieldAttributes("Consumption Current Phase 2"),
|
||||||
|
"consumption_current_phase3": CurrentFieldAttributes("Consumption Current Phase 3"),
|
||||||
|
# KM - Consumption end
|
||||||
|
|
||||||
|
# KM - Totals
|
||||||
|
"pv_input_power_all": PowerFieldAttributes("PV Input Power All"),
|
||||||
|
"consumption_power_all": PowerFieldAttributes("Consumption Power All"),
|
||||||
|
"grid_power_all": PowerFieldAttributes("Grid Power All"),
|
||||||
|
# KM - Totals end
|
||||||
|
|
||||||
|
|
||||||
|
# Device specific controls
|
||||||
|
"power_off": FieldAttributes(
|
||||||
|
type=FieldType.BOOL,
|
||||||
|
setter=True,
|
||||||
|
name="Power Off",
|
||||||
|
),
|
||||||
|
"auto_sleep_mode": FieldAttributes(
|
||||||
|
type=FieldType.ENUM,
|
||||||
|
setter=True,
|
||||||
|
name="Screen Auto Sleep Mode",
|
||||||
|
options=AutoSleepMode,
|
||||||
|
),
|
||||||
|
"ups_mode": FieldAttributes(
|
||||||
|
type=FieldType.ENUM,
|
||||||
|
setter=False, # Disabled for safety reasons
|
||||||
|
name="UPS Working Mode",
|
||||||
|
options=UpsMode,
|
||||||
|
),
|
||||||
|
"split_phase_on": FieldAttributes(
|
||||||
|
type=FieldType.BOOL,
|
||||||
|
setter=False, # Disabled for safety reasons
|
||||||
|
name="Split Phase",
|
||||||
|
),
|
||||||
|
"split_phase_machine_mode": FieldAttributes(
|
||||||
|
type=FieldType.ENUM,
|
||||||
|
setter=False, # Disabled for safety reasons
|
||||||
|
name="Split Phase Machine Type",
|
||||||
|
options=SplitPhaseMachineType,
|
||||||
|
),
|
||||||
|
"grid_charge_on": FieldAttributes(
|
||||||
|
type=FieldType.BOOL,
|
||||||
|
setter=False, # Disabled for safety reasons
|
||||||
|
name="Grid Charge",
|
||||||
|
),
|
||||||
|
"time_control_on": FieldAttributes(
|
||||||
|
type=FieldType.BOOL,
|
||||||
|
setter=False, # Disabled for safety reasons
|
||||||
|
name="Time Control",
|
||||||
|
),
|
||||||
|
"battery_range_start": FieldAttributes(
|
||||||
|
type=FieldType.NUMERIC,
|
||||||
|
setter=False, # Disabled
|
||||||
|
name="Battery Range Start",
|
||||||
|
unit_of_measurement="%",
|
||||||
|
),
|
||||||
|
"battery_range_end": FieldAttributes(
|
||||||
|
type=FieldType.NUMERIC,
|
||||||
|
setter=False, # Disabled
|
||||||
|
name="Battery Range End",
|
||||||
|
unit_of_measurement="%",
|
||||||
|
),
|
||||||
|
"led_mode": FieldAttributes(
|
||||||
|
type=FieldType.ENUM,
|
||||||
|
setter=True,
|
||||||
|
name="LED Mode",
|
||||||
|
options=LedMode,
|
||||||
|
),
|
||||||
|
"eco_on": FieldAttributes(
|
||||||
|
type=FieldType.BOOL,
|
||||||
|
setter=True,
|
||||||
|
name="Eco Mode",
|
||||||
|
),
|
||||||
|
"eco_shutdown": FieldAttributes(
|
||||||
|
type=FieldType.ENUM,
|
||||||
|
setter=True,
|
||||||
|
name="Eco Shutdown",
|
||||||
|
options=EcoShutdown,
|
||||||
|
),
|
||||||
|
"charging_mode": FieldAttributes(
|
||||||
|
type=FieldType.ENUM,
|
||||||
|
setter=True,
|
||||||
|
name="Charging Mode",
|
||||||
|
options=ChargingMode,
|
||||||
|
),
|
||||||
|
"power_lifting_on": FieldAttributes(
|
||||||
|
type=FieldType.BOOL,
|
||||||
|
setter=True,
|
||||||
|
name="Power Lifting",
|
||||||
|
),
|
||||||
|
"grid_enhancement_mode_on": FieldAttributes(
|
||||||
|
type=FieldType.BOOL,
|
||||||
|
setter=True,
|
||||||
|
name="Grid Enhancement Mode",
|
||||||
|
),
|
||||||
|
"silent_charging_on": FieldAttributes(
|
||||||
|
type=FieldType.BOOL,
|
||||||
|
setter=True,
|
||||||
|
name="Silent Charging",
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def PACK_FIELD_ATTRIBUTES(pack: int):
|
||||||
|
return {
|
||||||
|
"pack_voltage": VoltageFieldAttributes(
|
||||||
|
name=f"Battery Pack {pack} Voltage",
|
||||||
|
),
|
||||||
|
"pack_battery_percent": FieldAttributes(
|
||||||
|
type=FieldType.NUMERIC,
|
||||||
|
name=f"Battery Pack {pack} Percent",
|
||||||
|
unit_of_measurement="%",
|
||||||
|
device_class="battery",
|
||||||
|
state_class="measurement",
|
||||||
|
),
|
||||||
|
"pack_bms_version": FieldAttributes(
|
||||||
|
type=FieldType.NUMERIC,
|
||||||
|
name=f"Battery Pack {pack} BMS Version",
|
||||||
|
),
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
"""Enums for fields."""
|
||||||
|
|
||||||
|
from enum import Enum, unique
|
||||||
|
|
||||||
|
|
||||||
|
@unique
|
||||||
|
class OutputMode(Enum):
|
||||||
|
STOP = 0
|
||||||
|
INVERTER_OUTPUT = 1
|
||||||
|
BYPASS_OUTPUT_C = 2
|
||||||
|
BYPASS_OUTPUT_D = 3
|
||||||
|
LOAD_MATCHING = 4
|
||||||
|
|
||||||
|
|
||||||
|
@unique
|
||||||
|
class AutoSleepMode(Enum):
|
||||||
|
THIRTY_SECONDS = 2
|
||||||
|
ONE_MINUTE = 3
|
||||||
|
FIVE_MINUTES = 4
|
||||||
|
NEVER = 5
|
||||||
|
|
||||||
|
|
||||||
|
@unique
|
||||||
|
class UpsMode(Enum):
|
||||||
|
CUSTOMIZED = 1
|
||||||
|
PV_PRIORITY = 2
|
||||||
|
STANDARD = 3
|
||||||
|
TIME_CONTROL = 4
|
||||||
|
|
||||||
|
|
||||||
|
@unique
|
||||||
|
class SplitPhaseMachineType(Enum):
|
||||||
|
SLAVE = 0
|
||||||
|
MASTER = 1
|
||||||
|
|
||||||
|
|
||||||
|
@unique
|
||||||
|
class LedMode(Enum):
|
||||||
|
LOW = 1
|
||||||
|
HIGH = 2
|
||||||
|
SOS = 3
|
||||||
|
OFF = 4
|
||||||
|
|
||||||
|
|
||||||
|
@unique
|
||||||
|
class EcoShutdown(Enum):
|
||||||
|
ONE_HOUR = 1
|
||||||
|
TWO_HOURS = 2
|
||||||
|
THREE_HOURS = 3
|
||||||
|
FOUR_HOURS = 4
|
||||||
|
|
||||||
|
|
||||||
|
@unique
|
||||||
|
class ChargingMode(Enum):
|
||||||
|
STANDARD = 0
|
||||||
|
SILENT = 1
|
||||||
|
TURBO = 2
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
"""Device commands."""
|
||||||
|
|
||||||
|
# Copy of https://github.com/warhammerkid/bluetti_mqtt/blob/main/bluetti_mqtt/core/commands.py
|
||||||
|
|
||||||
|
import struct
|
||||||
|
import crcmod.predefined
|
||||||
|
|
||||||
|
modbus_crc = crcmod.predefined.mkCrcFun("modbus")
|
||||||
|
|
||||||
|
|
||||||
|
class DeviceCommand:
|
||||||
|
def __init__(self, function_code: int, data: bytes):
|
||||||
|
self.function_code = function_code
|
||||||
|
|
||||||
|
self.cmd = bytearray(len(data) + 4)
|
||||||
|
self.cmd[0] = 1 # MODBUS address
|
||||||
|
self.cmd[1] = function_code
|
||||||
|
self.cmd[2:-2] = data
|
||||||
|
struct.pack_into("<H", self.cmd, -2, modbus_crc(self.cmd[:-2]))
|
||||||
|
|
||||||
|
def response_size(self) -> int:
|
||||||
|
"""Returns the expected response size in bytes"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
"""Provide an iter implemention so that bytes(cmd) works"""
|
||||||
|
return iter(self.cmd)
|
||||||
|
|
||||||
|
def is_exception_response(self, response: bytes):
|
||||||
|
"""Checks the response code to see if it's a MODBUS exception"""
|
||||||
|
if len(response) < 2:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return response[1] == self.function_code + 0x80
|
||||||
|
|
||||||
|
def is_valid_response(self, response: bytes):
|
||||||
|
"""Validates that the reponse is complete and uncorrupted"""
|
||||||
|
if len(response) < 3:
|
||||||
|
return False
|
||||||
|
|
||||||
|
crc = modbus_crc(response[0:-2])
|
||||||
|
crc_bytes = crc.to_bytes(2, byteorder="little")
|
||||||
|
return response[-2:] == crc_bytes
|
||||||
|
|
||||||
|
def parse_response(self, response: bytes):
|
||||||
|
"""Returns the raw body of the response"""
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
class ReadHoldingRegisters(DeviceCommand):
|
||||||
|
def __init__(self, starting_address: int, quantity: int):
|
||||||
|
self.starting_address = starting_address
|
||||||
|
self.quantity = quantity
|
||||||
|
|
||||||
|
super().__init__(3, struct.pack("!HH", starting_address, quantity))
|
||||||
|
|
||||||
|
def response_size(self):
|
||||||
|
# 3 byte header
|
||||||
|
# each returned field is actually 2 bytes (16-bit word)
|
||||||
|
# 2 byte crc
|
||||||
|
return 2 * self.quantity + 5
|
||||||
|
|
||||||
|
def parse_response(self, response: bytes):
|
||||||
|
return bytes(response[3:-2])
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"ReadHoldingRegisters(starting_address={self.starting_address}, quantity={self.quantity})"
|
||||||
|
|
||||||
|
|
||||||
|
class WriteSingleRegister(DeviceCommand):
|
||||||
|
def __init__(self, address: int, value: int):
|
||||||
|
self.address = address
|
||||||
|
self.value = value
|
||||||
|
|
||||||
|
super().__init__(6, struct.pack("!HH", address, value))
|
||||||
|
|
||||||
|
def response_size(self):
|
||||||
|
return 8
|
||||||
|
|
||||||
|
def parse_response(self, response: bytes):
|
||||||
|
return bytes(response[4:6])
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"WriteSingleRegister(address={self.address}, value={self.value:#04x})"
|
||||||
|
|
||||||
|
|
||||||
|
class WriteMultipleRegisters(DeviceCommand):
|
||||||
|
def __init__(self, starting_address: int, data: bytes):
|
||||||
|
if len(data) % 2 != 0:
|
||||||
|
raise ValueError("data size must be multiple of 2")
|
||||||
|
|
||||||
|
self.starting_address = starting_address
|
||||||
|
self.data = data
|
||||||
|
|
||||||
|
body = bytearray(len(data) + 5)
|
||||||
|
half_len = len(data) >> 1
|
||||||
|
struct.pack_into("!HHB", body, 0, starting_address, half_len, len(data))
|
||||||
|
body[5:] = data
|
||||||
|
super().__init__(16, body)
|
||||||
|
|
||||||
|
def response_size(self):
|
||||||
|
return 8
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"WriteMultipleRegisters(starting_address={self.starting_address}, data={self.data})"
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
"""Device builder functions."""
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
|
from ..devices.ac60 import AC60
|
||||||
|
from ..devices.ac70 import AC70
|
||||||
|
from ..devices.ac70p import AC70P
|
||||||
|
from ..devices.ac180 import AC180
|
||||||
|
from ..devices.ac180p import AC180P
|
||||||
|
from ..devices.ac200l import AC200L
|
||||||
|
from ..devices.ac200m import AC200M
|
||||||
|
from ..devices.ac300 import AC300
|
||||||
|
from ..devices.ac500 import AC500
|
||||||
|
from ..devices.eb3a import EB3A
|
||||||
|
from ..devices.ep500 import EP500
|
||||||
|
from ..devices.ep500p import EP500P
|
||||||
|
from ..devices.ep600 import EP600
|
||||||
|
from ..devices.ep760 import EP760
|
||||||
|
from ..devices.ep800 import EP800
|
||||||
|
|
||||||
|
DEVICE_NAME_RE = re.compile(
|
||||||
|
r"^(AC60|AC70|AC70P|AC180|AC180P|AC200L|AC200M|AC300|AC500|EB3A|EP500|EP500P|EP600|EP760|EP800)(\d+)$"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def build_device(address: str, name: str):
|
||||||
|
match = DEVICE_NAME_RE.match(name)
|
||||||
|
if match[1] == "AC60":
|
||||||
|
return AC60(address, match[2])
|
||||||
|
if match[1] == "AC70":
|
||||||
|
return AC70(address, match[2])
|
||||||
|
if match[1] == "AC70P":
|
||||||
|
return AC70P(address, match[2])
|
||||||
|
if match[1] == "AC180":
|
||||||
|
return AC180(address, match[2])
|
||||||
|
if match[1] == "AC180P":
|
||||||
|
return AC180P(address, match[2])
|
||||||
|
if match[1] == "AC200L":
|
||||||
|
return AC200L(address, match[2])
|
||||||
|
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] == "EB3A":
|
||||||
|
return EB3A(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] == "EP760":
|
||||||
|
return EP760(address, match[2])
|
||||||
|
if match[1] == "EP800":
|
||||||
|
return EP800(address, match[2])
|
||||||
|
|
||||||
|
|
||||||
|
def get_type_by_bt_name(bt_name: str):
|
||||||
|
match = DEVICE_NAME_RE.match(bt_name)
|
||||||
|
if match is None:
|
||||||
|
return "Unknown"
|
||||||
|
return match[1]
|
||||||
@@ -0,0 +1,238 @@
|
|||||||
|
"""Struct definitions."""
|
||||||
|
|
||||||
|
# Copy of https://github.com/warhammerkid/bluetti_mqtt/blob/main/bluetti_mqtt/core/devices/struct.py
|
||||||
|
|
||||||
|
from decimal import Decimal
|
||||||
|
from enum import Enum
|
||||||
|
from .commands import ReadHoldingRegisters
|
||||||
|
import struct
|
||||||
|
from typing import Any, List, Optional, Tuple, Type
|
||||||
|
|
||||||
|
|
||||||
|
def swap_bytes(data: bytes):
|
||||||
|
"""Swaps the place of every other byte, returning a new byte array"""
|
||||||
|
arr = bytearray(data)
|
||||||
|
for i in range(0, len(arr) - 1, 2):
|
||||||
|
arr[i], arr[i + 1] = arr[i + 1], arr[i]
|
||||||
|
return arr
|
||||||
|
|
||||||
|
|
||||||
|
class DeviceField:
|
||||||
|
def __init__(self, name: str, address: int, size: int):
|
||||||
|
self.name = name
|
||||||
|
self.address = address
|
||||||
|
self.size = size
|
||||||
|
|
||||||
|
def parse(self, data: bytes) -> Any:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def in_range(self, val: Any) -> bool:
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
class UintField(DeviceField):
|
||||||
|
def __init__(self, name: str, address: int, range: Optional[Tuple[int, int]], multiplier: float):
|
||||||
|
self.range = range
|
||||||
|
self.multiplier = multiplier
|
||||||
|
super().__init__(name, address, 1)
|
||||||
|
|
||||||
|
def parse(self, data: bytes) -> int:
|
||||||
|
val = struct.unpack("!H", data)[0]
|
||||||
|
|
||||||
|
return val * self.multiplier
|
||||||
|
|
||||||
|
def in_range(self, val: int) -> bool:
|
||||||
|
if self.range is None:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return val >= self.range[0] and val <= self.range[1]
|
||||||
|
|
||||||
|
|
||||||
|
class IntField(DeviceField):
|
||||||
|
def __init__(self, name: str, address: int, range: Optional[Tuple[int, int]]):
|
||||||
|
self.range = range
|
||||||
|
super().__init__(name, address, 1)
|
||||||
|
|
||||||
|
def parse(self, data: bytes) -> int:
|
||||||
|
return struct.unpack(">h", data)[0]
|
||||||
|
|
||||||
|
def in_range(self, val: int) -> bool:
|
||||||
|
if self.range is None:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return val >= self.range[0] and val <= self.range[1]
|
||||||
|
|
||||||
|
|
||||||
|
class BoolField(DeviceField):
|
||||||
|
def __init__(self, name: str, address: int):
|
||||||
|
super().__init__(name, address, 1)
|
||||||
|
|
||||||
|
def parse(self, data: bytes) -> bool:
|
||||||
|
return struct.unpack("!H", data)[0] == 1
|
||||||
|
|
||||||
|
|
||||||
|
class EnumField(DeviceField):
|
||||||
|
def __init__(self, name: str, address: int, enum: Type[Enum]):
|
||||||
|
self.enum = enum
|
||||||
|
super().__init__(name, address, 1)
|
||||||
|
|
||||||
|
def parse(self, data: bytes) -> Any:
|
||||||
|
val = struct.unpack("!H", data)[0]
|
||||||
|
return self.enum(val)
|
||||||
|
|
||||||
|
|
||||||
|
class DecimalField(DeviceField):
|
||||||
|
def __init__(
|
||||||
|
self, name: str, address: int, scale: int, range: Optional[Tuple[int, int]], multiplier: float
|
||||||
|
):
|
||||||
|
self.scale = scale
|
||||||
|
self.range = range
|
||||||
|
self.multiplier = multiplier
|
||||||
|
super().__init__(name, address, 1)
|
||||||
|
|
||||||
|
def parse(self, data: bytes) -> Decimal:
|
||||||
|
val = Decimal(struct.unpack("!H", data)[0])
|
||||||
|
return (val / 10 ** self.scale) * Decimal(self.multiplier)
|
||||||
|
|
||||||
|
def in_range(self, val: Decimal) -> bool:
|
||||||
|
if self.range is None:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return val >= self.range[0] and val <= self.range[1]
|
||||||
|
|
||||||
|
|
||||||
|
class DecimalArrayField(DeviceField):
|
||||||
|
def __init__(self, name: str, address: int, size: int, scale: int):
|
||||||
|
self.scale = scale
|
||||||
|
super().__init__(name, address, size)
|
||||||
|
|
||||||
|
def parse(self, data: bytes) -> Decimal:
|
||||||
|
values = list(struct.unpack(f"!{self.size}H", data))
|
||||||
|
return [Decimal(v) / 10 ** self.scale for v in values]
|
||||||
|
|
||||||
|
|
||||||
|
class StringField(DeviceField):
|
||||||
|
"""Fixed-width null-terminated string field"""
|
||||||
|
|
||||||
|
def parse(self, data: bytes) -> str:
|
||||||
|
return data.rstrip(b"\0").decode("ascii")
|
||||||
|
|
||||||
|
|
||||||
|
class SwapStringField(DeviceField):
|
||||||
|
"""Fixed-width null-terminated string field"""
|
||||||
|
|
||||||
|
def parse(self, data: bytes) -> str:
|
||||||
|
return swap_bytes(data).rstrip(b"\0").decode("ascii")
|
||||||
|
|
||||||
|
|
||||||
|
class VersionField(DeviceField):
|
||||||
|
def __init__(self, name: str, address: int):
|
||||||
|
super().__init__(name, address, 2)
|
||||||
|
|
||||||
|
def parse(self, data: bytes) -> int:
|
||||||
|
values = struct.unpack("!2H", data)
|
||||||
|
return Decimal(values[0] + (values[1] << 16)) / 100
|
||||||
|
|
||||||
|
|
||||||
|
class SerialNumberField(DeviceField):
|
||||||
|
def __init__(self, name: str, address: int):
|
||||||
|
super().__init__(name, address, 4)
|
||||||
|
|
||||||
|
def parse(self, data: bytes) -> int:
|
||||||
|
values = struct.unpack("!4H", data)
|
||||||
|
return values[0] + (values[1] << 16) + (values[2] << 32) + (values[3] << 48)
|
||||||
|
|
||||||
|
|
||||||
|
class DeviceStruct:
|
||||||
|
fields: List[DeviceField]
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.fields = []
|
||||||
|
|
||||||
|
def add_uint_field(self, name: str, address: int, range: Tuple[int, int] = None, multiplier: float = 1):
|
||||||
|
self.fields.append(UintField(name, address, range, multiplier))
|
||||||
|
|
||||||
|
def add_int_field(self, name: str, address: int, range: Tuple[int, int] = None):
|
||||||
|
self.fields.append(IntField(name, address, range))
|
||||||
|
|
||||||
|
def add_bool_field(self, name: str, address: int):
|
||||||
|
self.fields.append(BoolField(name, address))
|
||||||
|
|
||||||
|
def add_enum_field(self, name: str, address: int, enum: Type[Enum]):
|
||||||
|
self.fields.append(EnumField(name, address, enum))
|
||||||
|
|
||||||
|
def add_decimal_field(
|
||||||
|
self, name: str, address: int, scale: int, range: Tuple[int, int] = None, multiplier: float = 1
|
||||||
|
):
|
||||||
|
self.fields.append(DecimalField(name, address, scale, range, multiplier))
|
||||||
|
|
||||||
|
def add_decimal_array_field(self, name: str, address: int, size: int, scale: int):
|
||||||
|
self.fields.append(DecimalArrayField(name, address, size, scale))
|
||||||
|
|
||||||
|
def add_string_field(self, name: str, address: int, size: int):
|
||||||
|
self.fields.append(StringField(name, address, size))
|
||||||
|
|
||||||
|
def add_swap_string_field(self, name: str, address: int, size: int):
|
||||||
|
self.fields.append(SwapStringField(name, address, size))
|
||||||
|
|
||||||
|
def add_version_field(self, name: str, address: int):
|
||||||
|
self.fields.append(VersionField(name, address))
|
||||||
|
|
||||||
|
def add_sn_field(self, name: str, address: int):
|
||||||
|
self.fields.append(SerialNumberField(name, address))
|
||||||
|
|
||||||
|
def get_read_holding_registers(self, tolerance=20, filter=lambda address: True):
|
||||||
|
address_set = set()
|
||||||
|
for field in self.fields:
|
||||||
|
for address in range(field.address, field.address + field.size):
|
||||||
|
address_set.add(address)
|
||||||
|
# Filter out addresses that we specificly
|
||||||
|
# specified to exclude.
|
||||||
|
for address in set(address_set):
|
||||||
|
if not filter(address):
|
||||||
|
address_set.remove(address)
|
||||||
|
# Fast path: no addresses -> no commands.
|
||||||
|
if not address_set:
|
||||||
|
return []
|
||||||
|
|
||||||
|
commands = []
|
||||||
|
|
||||||
|
address_list = sorted(address_set)
|
||||||
|
start = address_list[0]
|
||||||
|
last = address_list[0]
|
||||||
|
for address in address_list[1:]:
|
||||||
|
# Create a new group if there's a gap
|
||||||
|
# in the addresses.
|
||||||
|
if address >= last + tolerance:
|
||||||
|
commands.append(ReadHoldingRegisters(start, last - start + 1))
|
||||||
|
start = address
|
||||||
|
last = address
|
||||||
|
commands.append(ReadHoldingRegisters(start, last - start + 1))
|
||||||
|
return commands
|
||||||
|
|
||||||
|
def parse(self, starting_address: int, data: bytes) -> dict:
|
||||||
|
# Offsets and size are counted in 2 byte chunks, so for the range we
|
||||||
|
# need to divide the byte size by 2
|
||||||
|
data_size = int(len(data) / 2)
|
||||||
|
|
||||||
|
# Filter out fields not in range
|
||||||
|
r = range(starting_address, starting_address + data_size)
|
||||||
|
fields = [
|
||||||
|
f for f in self.fields if f.address in r and f.address + f.size - 1 in r
|
||||||
|
]
|
||||||
|
|
||||||
|
# Parse fields
|
||||||
|
parsed = {}
|
||||||
|
for f in fields:
|
||||||
|
data_start = 2 * (f.address - starting_address)
|
||||||
|
field_data = data[data_start : data_start + 2 * f.size]
|
||||||
|
val = f.parse(field_data)
|
||||||
|
|
||||||
|
# Skip if the value is "out-of-range" - sometimes the sensors
|
||||||
|
# report weird values
|
||||||
|
if not f.in_range(val):
|
||||||
|
continue
|
||||||
|
|
||||||
|
parsed[f.name] = val
|
||||||
|
|
||||||
|
return parsed
|
||||||
@@ -4,6 +4,8 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import re
|
import re
|
||||||
import logging
|
import logging
|
||||||
|
from typing import Any
|
||||||
|
from bleak import BleakClient
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@@ -12,12 +14,22 @@ from homeassistant.components.bluetooth import (
|
|||||||
BluetoothServiceInfoBleak,
|
BluetoothServiceInfoBleak,
|
||||||
async_discovered_service_info,
|
async_discovered_service_info,
|
||||||
)
|
)
|
||||||
from homeassistant.const import CONF_ADDRESS, CONF_TYPE, CONF_NAME
|
from homeassistant.const import CONF_ADDRESS, CONF_NAME, CONF_TYPE
|
||||||
from homeassistant.data_entry_flow import FlowResult
|
from homeassistant.data_entry_flow import FlowResult
|
||||||
from homeassistant.helpers import selector
|
from homeassistant.helpers import selector
|
||||||
|
|
||||||
from . import get_type_by_bt_name
|
from .bluetti_bt_lib.utils.device_builder import get_type_by_bt_name
|
||||||
from .const import DOMAIN, CONF_USE_CONTROLS
|
from .bluetti_bt_lib.bluetooth.device_recognizer import recognize_device
|
||||||
|
|
||||||
|
from .const import (
|
||||||
|
CONF_ENCRYPTION,
|
||||||
|
CONF_MAX_RETRIES,
|
||||||
|
CONF_PERSISTENT_CONN,
|
||||||
|
CONF_POLLING_INTERVAL,
|
||||||
|
CONF_POLLING_TIMEOUT,
|
||||||
|
CONF_USE_CONTROLS,
|
||||||
|
DOMAIN,
|
||||||
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -36,6 +48,23 @@ class BluettiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
"""Handle bluetooth discovery."""
|
"""Handle bluetooth discovery."""
|
||||||
await self.async_set_unique_id(discovery_info.address)
|
await self.async_set_unique_id(discovery_info.address)
|
||||||
self._abort_if_unique_id_configured()
|
self._abort_if_unique_id_configured()
|
||||||
|
|
||||||
|
if isinstance(discovery_info.name, str):
|
||||||
|
name = re.sub("[^A-Z0-9]+", "", discovery_info.name)
|
||||||
|
discovery_info.manufacturer_data = {
|
||||||
|
CONF_TYPE: get_type_by_bt_name(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get device type if needed
|
||||||
|
if isinstance(discovery_info.name, str) and discovery_info.name.startswith("PBOX"):
|
||||||
|
bleak_device = BleakClient(discovery_info.device)
|
||||||
|
device_type = await recognize_device(bleak_device, self.hass.loop.create_future)
|
||||||
|
_LOGGER.info("Device identified as %s", device_type)
|
||||||
|
discovery_info.manufacturer_data = {
|
||||||
|
CONF_TYPE: device_type.strip(),
|
||||||
|
}
|
||||||
|
discovery_info.name = discovery_info.name.replace("PBOX", device_type.strip())
|
||||||
|
|
||||||
self._discovery_info = discovery_info
|
self._discovery_info = discovery_info
|
||||||
self.context["title_placeholders"] = {"name": discovery_info.name}
|
self.context["title_placeholders"] = {"name": discovery_info.name}
|
||||||
return await self.async_step_user()
|
return await self.async_step_user()
|
||||||
@@ -50,13 +79,14 @@ class BluettiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
discovery_info = self._discovered_devices[address]
|
discovery_info = self._discovered_devices[address]
|
||||||
await self.async_set_unique_id(address, raise_on_progress=False)
|
await self.async_set_unique_id(address, raise_on_progress=False)
|
||||||
self._abort_if_unique_id_configured()
|
self._abort_if_unique_id_configured()
|
||||||
dev_type = get_type_by_bt_name(discovery_info.name)
|
name = re.sub("[^A-Z0-9]+", "", discovery_info.name)
|
||||||
|
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=discovery_info.name,
|
title=name,
|
||||||
data={
|
data={
|
||||||
CONF_ADDRESS: discovery_info.address,
|
CONF_ADDRESS: discovery_info.address,
|
||||||
CONF_TYPE: dev_type,
|
CONF_NAME: name,
|
||||||
CONF_NAME: re.sub("[^A-Z0-9]+", "", discovery_info.name),
|
CONF_TYPE: discovery_info.manufacturer_data.get(CONF_TYPE, "Unknown"),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -108,24 +138,42 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
|
|||||||
) -> FlowResult:
|
) -> FlowResult:
|
||||||
"""Manage the options."""
|
"""Manage the options."""
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
changed = self.hass.config_entries.async_update_entry(
|
|
||||||
|
# Validate update interval
|
||||||
|
if user_input[CONF_POLLING_INTERVAL] < 5:
|
||||||
|
return self.async_abort(reason="invalid_interval")
|
||||||
|
|
||||||
|
# Validate update timeout
|
||||||
|
if user_input[CONF_POLLING_TIMEOUT] < 1:
|
||||||
|
return self.async_abort(reason="invalid_timeout")
|
||||||
|
|
||||||
|
# Validate max retries
|
||||||
|
if user_input[CONF_MAX_RETRIES] < 1:
|
||||||
|
return self.async_abort(reason="invalid_retries")
|
||||||
|
|
||||||
|
self.hass.config_entries.async_update_entry(
|
||||||
self.config_entry,
|
self.config_entry,
|
||||||
data={
|
data={
|
||||||
**self.config_entry.data,
|
**self.config_entry.data,
|
||||||
**{
|
**{
|
||||||
CONF_USE_CONTROLS: user_input[CONF_USE_CONTROLS],
|
CONF_USE_CONTROLS: user_input[CONF_USE_CONTROLS],
|
||||||
|
CONF_PERSISTENT_CONN: user_input[CONF_PERSISTENT_CONN],
|
||||||
|
CONF_POLLING_INTERVAL: user_input[CONF_POLLING_INTERVAL],
|
||||||
|
CONF_POLLING_TIMEOUT: user_input[CONF_POLLING_TIMEOUT],
|
||||||
|
CONF_MAX_RETRIES: user_input[CONF_MAX_RETRIES],
|
||||||
|
CONF_ENCRYPTION: user_input[CONF_ENCRYPTION],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if changed is False:
|
|
||||||
_LOGGER.error(
|
|
||||||
"Method OptionsFlowHandler.async_step_init: Config entry %s has not been changed",
|
|
||||||
self.config_entry.entry_id,
|
|
||||||
)
|
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title="",
|
title="",
|
||||||
data={
|
data={
|
||||||
CONF_USE_CONTROLS: user_input[CONF_USE_CONTROLS],
|
CONF_USE_CONTROLS: user_input[CONF_USE_CONTROLS],
|
||||||
|
CONF_PERSISTENT_CONN: user_input[CONF_PERSISTENT_CONN],
|
||||||
|
CONF_POLLING_INTERVAL: user_input[CONF_POLLING_INTERVAL],
|
||||||
|
CONF_POLLING_TIMEOUT: user_input[CONF_POLLING_TIMEOUT],
|
||||||
|
CONF_MAX_RETRIES: user_input[CONF_MAX_RETRIES],
|
||||||
|
CONF_ENCRYPTION: user_input[CONF_ENCRYPTION],
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -137,6 +185,26 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
|
|||||||
CONF_USE_CONTROLS,
|
CONF_USE_CONTROLS,
|
||||||
default=self.config_entry.data.get(CONF_USE_CONTROLS, False),
|
default=self.config_entry.data.get(CONF_USE_CONTROLS, False),
|
||||||
): selector.BooleanSelector(),
|
): selector.BooleanSelector(),
|
||||||
|
vol.Required(
|
||||||
|
CONF_PERSISTENT_CONN,
|
||||||
|
default=self.config_entry.data.get(CONF_PERSISTENT_CONN, False),
|
||||||
|
): selector.BooleanSelector(),
|
||||||
|
vol.Required(
|
||||||
|
CONF_POLLING_INTERVAL,
|
||||||
|
default=self.config_entry.data.get(CONF_POLLING_INTERVAL, 20),
|
||||||
|
): int,
|
||||||
|
vol.Required(
|
||||||
|
CONF_POLLING_TIMEOUT,
|
||||||
|
default=self.config_entry.data.get(CONF_POLLING_TIMEOUT, 45),
|
||||||
|
): int,
|
||||||
|
vol.Required(
|
||||||
|
CONF_MAX_RETRIES,
|
||||||
|
default=self.config_entry.data.get(CONF_MAX_RETRIES, 5),
|
||||||
|
): int,
|
||||||
|
vol.Required(
|
||||||
|
CONF_ENCRYPTION,
|
||||||
|
default=self.config_entry.data.get(CONF_ENCRYPTION, False),
|
||||||
|
): selector.BooleanSelector(),
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
@@ -1,19 +1,40 @@
|
|||||||
"""Constants for the Bluetti BT integration."""
|
"""Constants for the Bluetti BT integration."""
|
||||||
|
|
||||||
from bluetti_mqtt.mqtt_client import MqttFieldConfig, MqttFieldType
|
|
||||||
|
|
||||||
DOMAIN = "bluetti_bt"
|
DOMAIN = "bluetti_bt"
|
||||||
MANUFACTURER = "Bluetti"
|
MANUFACTURER = "Bluetti"
|
||||||
|
|
||||||
CONF_OPTIONS = "options"
|
CONF_OPTIONS = "options"
|
||||||
CONF_USE_CONTROLS = "use_controls"
|
CONF_USE_CONTROLS = "use_controls"
|
||||||
|
CONF_PERSISTENT_CONN = "persistent_conn"
|
||||||
|
CONF_POLLING_INTERVAL = "polling_interval"
|
||||||
|
CONF_POLLING_TIMEOUT = "polling_timeout"
|
||||||
|
CONF_MAX_RETRIES = "max_retries"
|
||||||
|
CONF_ENCRYPTION = "use_encryption"
|
||||||
|
|
||||||
DATA_COORDINATOR = "coordinator"
|
DATA_COORDINATOR = "coordinator"
|
||||||
DATA_POLLING_RUNNING = "polling_running"
|
DATA_POLLING_RUNNING = "polling_running"
|
||||||
|
|
||||||
|
SUPPORTED_MODELS = [
|
||||||
|
"AC60",
|
||||||
|
"AC200M",
|
||||||
|
"AC300",
|
||||||
|
"AC500",
|
||||||
|
"EB3A",
|
||||||
|
"EP500",
|
||||||
|
"EP500P",
|
||||||
|
"EP600",
|
||||||
|
"EP760",
|
||||||
|
"AC180P",
|
||||||
|
]
|
||||||
|
|
||||||
CONTROL_FIELDS = [
|
CONTROL_FIELDS = [
|
||||||
"ac_output_on",
|
"ac_output_on_switch",
|
||||||
"dc_output_on",
|
"dc_output_on_switch",
|
||||||
|
"power_off",
|
||||||
|
"eco_on",
|
||||||
|
"power_lifting_on",
|
||||||
|
"grid_enhancement_mode_on",
|
||||||
|
"silent_charging_on",
|
||||||
]
|
]
|
||||||
|
|
||||||
DIAGNOSTIC_FIELDS = [
|
DIAGNOSTIC_FIELDS = [
|
||||||
@@ -22,439 +43,13 @@ DIAGNOSTIC_FIELDS = [
|
|||||||
"max_ac_output_power",
|
"max_ac_output_power",
|
||||||
"max_ac_output_current",
|
"max_ac_output_current",
|
||||||
"bcu_version",
|
"bcu_version",
|
||||||
|
"dsp_version",
|
||||||
|
"arm_version",
|
||||||
"safety_module_version",
|
"safety_module_version",
|
||||||
"high_voltage_module_version",
|
"high_voltage_module_version",
|
||||||
|
"power_generation",
|
||||||
|
"total_ac_consumption",
|
||||||
|
"total_grid_consumption",
|
||||||
|
"total_grid_feed",
|
||||||
|
"ac_input_voltage",
|
||||||
]
|
]
|
||||||
|
|
||||||
ADDITIONAL_DEVICE_FIELDS = {
|
|
||||||
"max_ac_input_power": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "Max AC Input Power per Phase",
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"max_ac_input_current": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "Max AC Input Current per Phase",
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"max_ac_output_power": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "Max AC Output Power per Phase",
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"max_ac_output_current": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "Max AC Output Current per Phase",
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"bcu_version": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={"name": "BCU Version"},
|
|
||||||
),
|
|
||||||
"safety_module_version": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={"name": "Safety Module Version"},
|
|
||||||
),
|
|
||||||
"high_voltage_module_version": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={"name": "High Voltage Safety Module Version"},
|
|
||||||
),
|
|
||||||
"adl400_ac_input_power_phase1": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "ADL400 AC Input Power Phase 1",
|
|
||||||
"unit_of_measurement": "W",
|
|
||||||
"device_class": "power",
|
|
||||||
"state_class": "measurement",
|
|
||||||
"force_update": True,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"adl400_ac_input_power_phase2": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "ADL400 AC Input Power Phase 2",
|
|
||||||
"unit_of_measurement": "W",
|
|
||||||
"device_class": "power",
|
|
||||||
"state_class": "measurement",
|
|
||||||
"force_update": True,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"adl400_ac_input_power_phase3": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "ADL400 AC Input Power Phase 3",
|
|
||||||
"unit_of_measurement": "W",
|
|
||||||
"device_class": "power",
|
|
||||||
"state_class": "measurement",
|
|
||||||
"force_update": True,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"adl400_ac_input_voltage_phase1": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "ADL400 AC Input Voltage Phase 1",
|
|
||||||
"unit_of_measurement": "V",
|
|
||||||
"device_class": "voltage",
|
|
||||||
"state_class": "measurement",
|
|
||||||
"force_update": True,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"adl400_ac_input_voltage_phase2": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "ADL400 AC Input Voltage Phase 2",
|
|
||||||
"unit_of_measurement": "V",
|
|
||||||
"device_class": "voltage",
|
|
||||||
"state_class": "measurement",
|
|
||||||
"force_update": True,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"adl400_ac_input_voltage_phase3": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "ADL400 AC Input Voltage Phase 3",
|
|
||||||
"unit_of_measurement": "V",
|
|
||||||
"device_class": "voltage",
|
|
||||||
"state_class": "measurement",
|
|
||||||
"force_update": True,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"grid_input_voltage_phase1": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "Grid Input Voltage Phase 1",
|
|
||||||
"unit_of_measurement": "V",
|
|
||||||
"device_class": "voltage",
|
|
||||||
"state_class": "measurement",
|
|
||||||
"force_update": True,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"grid_input_voltage_phase2": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "Grid Input Voltage Phase 2",
|
|
||||||
"unit_of_measurement": "V",
|
|
||||||
"device_class": "voltage",
|
|
||||||
"state_class": "measurement",
|
|
||||||
"force_update": True,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"grid_input_voltage_phase3": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "Grid Input Voltage Phase 3",
|
|
||||||
"unit_of_measurement": "V",
|
|
||||||
"device_class": "voltage",
|
|
||||||
"state_class": "measurement",
|
|
||||||
"force_update": True,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"grid_input_current_phase1": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "Grid Input Current Phase 1",
|
|
||||||
"unit_of_measurement": "A",
|
|
||||||
"device_class": "current",
|
|
||||||
"state_class": "measurement",
|
|
||||||
"force_update": True,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"grid_input_current_phase2": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "Grid Input Current Phase 2",
|
|
||||||
"unit_of_measurement": "A",
|
|
||||||
"device_class": "current",
|
|
||||||
"state_class": "measurement",
|
|
||||||
"force_update": True,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"grid_input_current_phase3": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "Grid Input Current Phase 3",
|
|
||||||
"unit_of_measurement": "A",
|
|
||||||
"device_class": "current",
|
|
||||||
"state_class": "measurement",
|
|
||||||
"force_update": True,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"grid_input_power_phase1": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "Grid Input Power Phase 1",
|
|
||||||
"unit_of_measurement": "W",
|
|
||||||
"device_class": "power",
|
|
||||||
"state_class": "measurement",
|
|
||||||
"force_update": True,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"grid_input_power_phase2": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "Grid Input Power Phase 2",
|
|
||||||
"unit_of_measurement": "W",
|
|
||||||
"device_class": "power",
|
|
||||||
"state_class": "measurement",
|
|
||||||
"force_update": True,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"grid_input_power_phase3": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "Grid Input Power Phase 3",
|
|
||||||
"unit_of_measurement": "W",
|
|
||||||
"device_class": "power",
|
|
||||||
"state_class": "measurement",
|
|
||||||
"force_update": True,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"ac_output_frequency": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "AC Output Frequency",
|
|
||||||
"unit_of_measurement": "Hz",
|
|
||||||
"device_class": "frequency",
|
|
||||||
"state_class": "measurement",
|
|
||||||
"force_update": True,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"ac_output_voltage_phase1": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "AC Output Voltage Phase 1",
|
|
||||||
"unit_of_measurement": "V",
|
|
||||||
"device_class": "voltage",
|
|
||||||
"state_class": "measurement",
|
|
||||||
"force_update": True,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"ac_output_voltage_phase2": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "AC Output Voltage Phase 2",
|
|
||||||
"unit_of_measurement": "V",
|
|
||||||
"device_class": "voltage",
|
|
||||||
"state_class": "measurement",
|
|
||||||
"force_update": True,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"ac_output_voltage_phase3": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "AC Output Voltage Phase 3",
|
|
||||||
"unit_of_measurement": "V",
|
|
||||||
"device_class": "voltage",
|
|
||||||
"state_class": "measurement",
|
|
||||||
"force_update": True,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"ac_output_power_phase1": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "AC Output Power Phase 1",
|
|
||||||
"unit_of_measurement": "W",
|
|
||||||
"device_class": "power",
|
|
||||||
"state_class": "measurement",
|
|
||||||
"force_update": True,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"ac_output_power_phase2": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "AC Output Power Phase 2",
|
|
||||||
"unit_of_measurement": "W",
|
|
||||||
"device_class": "power",
|
|
||||||
"state_class": "measurement",
|
|
||||||
"force_update": True,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"ac_output_power_phase3": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "AC Output Power Phase 3",
|
|
||||||
"unit_of_measurement": "W",
|
|
||||||
"device_class": "power",
|
|
||||||
"state_class": "measurement",
|
|
||||||
"force_update": True,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"ac_output_current_phase1": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "AC Output Current Phase 1",
|
|
||||||
"unit_of_measurement": "A",
|
|
||||||
"device_class": "current",
|
|
||||||
"state_class": "measurement",
|
|
||||||
"force_update": True,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"ac_output_current_phase2": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "AC Output Current Phase 2",
|
|
||||||
"unit_of_measurement": "A",
|
|
||||||
"device_class": "current",
|
|
||||||
"state_class": "measurement",
|
|
||||||
"force_update": True,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"ac_output_current_phase3": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "AC Output Current Phase 3",
|
|
||||||
"unit_of_measurement": "A",
|
|
||||||
"device_class": "current",
|
|
||||||
"state_class": "measurement",
|
|
||||||
"force_update": True,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"testing0": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "Register Testing 0",
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"testing1": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "Register Testing 1",
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"testing2": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "Register Testing 2",
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"testing3": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "Register Testing 3",
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"testing4": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "Register Testing 4",
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"testing5": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "Register Testing 5",
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"testing6": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "Register Testing 6",
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"testing7": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "Register Testing 7",
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"testing8": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "Register Testing 8",
|
|
||||||
},
|
|
||||||
),
|
|
||||||
"testing9": MqttFieldConfig(
|
|
||||||
type=MqttFieldType.NUMERIC,
|
|
||||||
setter=False,
|
|
||||||
advanced=False,
|
|
||||||
home_assistant_extra={
|
|
||||||
"name": "Register Testing 9",
|
|
||||||
},
|
|
||||||
),
|
|
||||||
}
|
|
||||||
@@ -1,14 +1,11 @@
|
|||||||
"""Coordinator for Bluetti integration."""
|
"""Coordinator for Bluetti integration."""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from typing import List, cast
|
|
||||||
|
|
||||||
import asyncio
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
import async_timeout
|
|
||||||
|
|
||||||
from bleak import BleakClient, BleakError
|
from bleak import BleakClient
|
||||||
|
|
||||||
from homeassistant.components import bluetooth
|
from homeassistant.components import bluetooth
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
@@ -16,135 +13,56 @@ from homeassistant.helpers.update_coordinator import (
|
|||||||
DataUpdateCoordinator,
|
DataUpdateCoordinator,
|
||||||
)
|
)
|
||||||
|
|
||||||
from bluetti_mqtt.bluetooth.client import BluetoothClient
|
from .bluetti_bt_lib.bluetooth.device_reader import DeviceReader
|
||||||
from bluetti_mqtt.bluetooth import (
|
from .bluetti_bt_lib.utils.device_builder import build_device
|
||||||
BadConnectionError,
|
|
||||||
ModbusError,
|
|
||||||
ParseError,
|
|
||||||
build_device,
|
|
||||||
)
|
|
||||||
from bluetti_mqtt.core.devices.bluetti_device import BluettiDevice
|
|
||||||
from bluetti_mqtt.core.commands import ReadHoldingRegisters
|
|
||||||
|
|
||||||
from .const import DATA_POLLING_RUNNING, DOMAIN
|
from .utils import mac_loggable
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class DummyDevice(BluettiDevice):
|
|
||||||
"""Dummy device used to add more fields to existing devices.
|
|
||||||
|
|
||||||
Changes made here are only temporary and should also be
|
|
||||||
contributed to https://github.com/warhammerkid/bluetti_mqtt
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, device: BluettiDevice):
|
|
||||||
"""Init dummy device with real device."""
|
|
||||||
self.struct = device.struct
|
|
||||||
|
|
||||||
if device.type == "EP600":
|
|
||||||
# ADL400 Smart Meter for AC Solar
|
|
||||||
self.struct.add_uint_field("adl400_ac_input_power_phase1", 1228)
|
|
||||||
self.struct.add_uint_field("adl400_ac_input_power_phase2", 1236)
|
|
||||||
self.struct.add_uint_field("adl400_ac_input_power_phase3", 1244)
|
|
||||||
self.struct.add_decimal_field("adl400_ac_input_voltage_phase1", 1229, 1)
|
|
||||||
self.struct.add_decimal_field("adl400_ac_input_voltage_phase2", 1237, 1)
|
|
||||||
self.struct.add_decimal_field("adl400_ac_input_voltage_phase3", 1245, 1)
|
|
||||||
# Grid Input
|
|
||||||
self.struct.add_decimal_field("grid_input_frequency", 1300, 1)
|
|
||||||
self.struct.add_uint_field("grid_input_power_phase1", 1313)
|
|
||||||
self.struct.add_uint_field("grid_input_power_phase2", 1319)
|
|
||||||
self.struct.add_uint_field("grid_input_power_phase3", 1325)
|
|
||||||
self.struct.add_decimal_field("grid_input_voltage_phase1", 1314, 1)
|
|
||||||
self.struct.add_decimal_field("grid_input_voltage_phase2", 1320, 1)
|
|
||||||
self.struct.add_decimal_field("grid_input_voltage_phase3", 1326, 1)
|
|
||||||
self.struct.add_decimal_field("grid_input_current_phase1", 1315, 1)
|
|
||||||
self.struct.add_decimal_field("grid_input_current_phase2", 1321, 1)
|
|
||||||
self.struct.add_decimal_field("grid_input_current_phase3", 1327, 1)
|
|
||||||
# EP600 AC Output
|
|
||||||
self.struct.add_decimal_field("ac_output_frequency", 1500, 1)
|
|
||||||
self.struct.add_uint_field("ac_output_power_phase1", 1510)
|
|
||||||
self.struct.add_uint_field("ac_output_power_phase2", 1517)
|
|
||||||
self.struct.add_uint_field("ac_output_power_phase3", 1524)
|
|
||||||
self.struct.add_decimal_field("ac_output_voltage_phase1", 1511, 1)
|
|
||||||
self.struct.add_decimal_field("ac_output_voltage_phase2", 1518, 1)
|
|
||||||
self.struct.add_decimal_field("ac_output_voltage_phase3", 1525, 1)
|
|
||||||
self.struct.add_decimal_field("ac_output_current_phase1", 1512, 1)
|
|
||||||
self.struct.add_decimal_field("ac_output_current_phase2", 1519, 1)
|
|
||||||
self.struct.add_decimal_field("ac_output_current_phase3", 1526, 1)
|
|
||||||
# Testing
|
|
||||||
# for i in range(0, 10):
|
|
||||||
# self.struct.add_decimal_field("testing"+str(i), 1520+i, 1)
|
|
||||||
|
|
||||||
super().__init__(device.address, device.type, device.sn)
|
|
||||||
self._parent = device
|
|
||||||
|
|
||||||
@property
|
|
||||||
def polling_commands(self) -> List[ReadHoldingRegisters]:
|
|
||||||
if self.type == "EP600":
|
|
||||||
return [
|
|
||||||
ReadHoldingRegisters(100, 62),
|
|
||||||
ReadHoldingRegisters(1228, 19),
|
|
||||||
ReadHoldingRegisters(1300, 28),
|
|
||||||
ReadHoldingRegisters(1500, 27),
|
|
||||||
ReadHoldingRegisters(2022, 6),
|
|
||||||
ReadHoldingRegisters(2213, 4),
|
|
||||||
# Battery
|
|
||||||
ReadHoldingRegisters(6101, 7),
|
|
||||||
ReadHoldingRegisters(6175, 11),
|
|
||||||
# Testing
|
|
||||||
# ReadHoldingRegisters(100, 62),
|
|
||||||
# ReadHoldingRegisters(1100, 51),
|
|
||||||
# ReadHoldingRegisters(1200, 80),
|
|
||||||
# ReadHoldingRegisters(1300, 31),
|
|
||||||
# ReadHoldingRegisters(1400, 48),
|
|
||||||
# ReadHoldingRegisters(1500, 30),
|
|
||||||
# ReadHoldingRegisters(2000, 89),
|
|
||||||
# ReadHoldingRegisters(2200, 41),
|
|
||||||
# ReadHoldingRegisters(2300, 36),
|
|
||||||
# ReadHoldingRegisters(6000, 32),
|
|
||||||
# ReadHoldingRegisters(6100, 100),
|
|
||||||
# ReadHoldingRegisters(6300, 100),
|
|
||||||
]
|
|
||||||
|
|
||||||
return self._parent.polling_commands
|
|
||||||
|
|
||||||
@property
|
|
||||||
def pack_polling_commands(self) -> List[ReadHoldingRegisters]:
|
|
||||||
return self._parent.pack_logging_commands
|
|
||||||
|
|
||||||
@property
|
|
||||||
def logging_commands(self) -> List[ReadHoldingRegisters]:
|
|
||||||
return self._parent.logging_commands
|
|
||||||
|
|
||||||
@property
|
|
||||||
def pack_logging_commands(self) -> List[ReadHoldingRegisters]:
|
|
||||||
return self._parent.pack_logging_commands
|
|
||||||
|
|
||||||
@property
|
|
||||||
def writable_ranges(self) -> List[range]:
|
|
||||||
return self._parent.writable_ranges
|
|
||||||
|
|
||||||
|
|
||||||
class PollingCoordinator(DataUpdateCoordinator):
|
class PollingCoordinator(DataUpdateCoordinator):
|
||||||
"""Polling coordinator."""
|
"""Polling coordinator."""
|
||||||
|
|
||||||
def __init__(self, hass: HomeAssistant, address, device_name: str):
|
def __init__(
|
||||||
|
self,
|
||||||
|
hass: HomeAssistant,
|
||||||
|
address: str,
|
||||||
|
device_name: str,
|
||||||
|
polling_interval: int,
|
||||||
|
persistent_conn: bool,
|
||||||
|
polling_timeout: int,
|
||||||
|
max_retries: int,
|
||||||
|
encrypted: bool,
|
||||||
|
):
|
||||||
"""Initialize coordinator."""
|
"""Initialize coordinator."""
|
||||||
super().__init__(
|
super().__init__(
|
||||||
hass,
|
hass,
|
||||||
_LOGGER,
|
_LOGGER,
|
||||||
name="Bluetti polling coordinator",
|
name="Bluetti polling coordinator",
|
||||||
update_interval=timedelta(seconds=20),
|
update_interval=timedelta(seconds=polling_interval),
|
||||||
)
|
)
|
||||||
self._address = address
|
|
||||||
self.notify_future = None
|
self.address = address
|
||||||
self.current_command = None
|
|
||||||
self.notify_response = bytearray()
|
# Create client
|
||||||
|
self.logger.debug("Creating client")
|
||||||
|
device = bluetooth.async_ble_device_from_address(hass, address)
|
||||||
|
if device is None:
|
||||||
|
self.logger.error("Device %s not available", mac_loggable(address))
|
||||||
|
return None
|
||||||
|
client = BleakClient(device)
|
||||||
bluetti_device = build_device(address, device_name)
|
bluetti_device = build_device(address, device_name)
|
||||||
|
|
||||||
# Add or modify device fields
|
self.reader = DeviceReader(
|
||||||
self.bluetti_device = DummyDevice(bluetti_device)
|
client,
|
||||||
|
bluetti_device,
|
||||||
|
self.hass.loop.create_future,
|
||||||
|
persistent_conn=persistent_conn,
|
||||||
|
polling_timeout=polling_timeout,
|
||||||
|
max_retries=max_retries,
|
||||||
|
encrypted=encrypted,
|
||||||
|
)
|
||||||
|
|
||||||
async def _async_update_data(self):
|
async def _async_update_data(self):
|
||||||
"""Fetch data from API endpoint.
|
"""Fetch data from API endpoint.
|
||||||
@@ -152,112 +70,11 @@ class PollingCoordinator(DataUpdateCoordinator):
|
|||||||
This is the place to pre-process the data to lookup tables
|
This is the place to pre-process the data to lookup tables
|
||||||
so entities can quickly look up their data.
|
so entities can quickly look up their data.
|
||||||
"""
|
"""
|
||||||
self.hass.data[DOMAIN][self.config_entry.entry_id][DATA_POLLING_RUNNING] = True
|
|
||||||
|
|
||||||
self.logger.debug("Polling data")
|
# Check if device is connected
|
||||||
|
if bluetooth.async_address_present(self.hass, self.address, connectable=True) is False:
|
||||||
device = bluetooth.async_ble_device_from_address(self.hass, self._address)
|
self.logger.warning("Device not connected")
|
||||||
if device is None:
|
self.last_update_success = False
|
||||||
self.logger.error("Device %s not available", self._address)
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if self.bluetti_device is None:
|
return await self.reader.read_data()
|
||||||
self.logger.error("Device type for %s not found", self._address)
|
|
||||||
return None
|
|
||||||
|
|
||||||
# Polling
|
|
||||||
client = BleakClient(device)
|
|
||||||
parsed_data: dict = {}
|
|
||||||
|
|
||||||
try:
|
|
||||||
async with async_timeout.timeout(15):
|
|
||||||
await client.connect()
|
|
||||||
|
|
||||||
await client.start_notify(
|
|
||||||
BluetoothClient.NOTIFY_UUID, self._notification_handler
|
|
||||||
)
|
|
||||||
|
|
||||||
for command in self.bluetti_device.polling_commands:
|
|
||||||
try:
|
|
||||||
# Prepare to make request
|
|
||||||
self.current_command = command
|
|
||||||
self.notify_future = self.hass.loop.create_future()
|
|
||||||
self.notify_response = bytearray()
|
|
||||||
|
|
||||||
# Make request
|
|
||||||
self.logger.debug("Requesting %s", command)
|
|
||||||
await client.write_gatt_char(
|
|
||||||
BluetoothClient.WRITE_UUID, bytes(command)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Wait for response
|
|
||||||
res = await asyncio.wait_for(
|
|
||||||
self.notify_future, timeout=BluetoothClient.RESPONSE_TIMEOUT
|
|
||||||
)
|
|
||||||
|
|
||||||
# Process data
|
|
||||||
self.logger.debug("Got %s bytes", len(res))
|
|
||||||
response = cast(bytes, res)
|
|
||||||
body = command.parse_response(response)
|
|
||||||
parsed = self.bluetti_device.parse(
|
|
||||||
command.starting_address, body
|
|
||||||
)
|
|
||||||
|
|
||||||
self.logger.debug("Parsed data: %s", parsed)
|
|
||||||
parsed_data.update(parsed)
|
|
||||||
|
|
||||||
except TimeoutError:
|
|
||||||
self.logger.warning(
|
|
||||||
"Polling timed out (address: %s)", self._address
|
|
||||||
)
|
|
||||||
except ParseError:
|
|
||||||
self.logger.warning("Got a parse exception...")
|
|
||||||
except ModbusError as err:
|
|
||||||
self.logger.warning(
|
|
||||||
"Got an invalid request error for %s: %s",
|
|
||||||
command,
|
|
||||||
err,
|
|
||||||
)
|
|
||||||
except (BadConnectionError, BleakError) as err:
|
|
||||||
self.logger.warning(
|
|
||||||
"Needed to disconnect due to error: %s (This can also be the case if you used device controls)", err
|
|
||||||
)
|
|
||||||
except TimeoutError:
|
|
||||||
self.logger.warning("Polling timed out for device %s", self._address)
|
|
||||||
return None
|
|
||||||
except BleakError as err:
|
|
||||||
self.logger.warning("Bleak error: %s", err)
|
|
||||||
return None
|
|
||||||
finally:
|
|
||||||
await client.disconnect()
|
|
||||||
|
|
||||||
self.hass.data[DOMAIN][self.config_entry.entry_id][DATA_POLLING_RUNNING] = False
|
|
||||||
|
|
||||||
# Pass data back to sensors
|
|
||||||
return parsed_data
|
|
||||||
|
|
||||||
def _notification_handler(self, _sender: int, data: bytearray):
|
|
||||||
"""Handle bt data."""
|
|
||||||
|
|
||||||
# Ignore notifications we don't expect
|
|
||||||
if not self.notify_future or self.notify_future.done():
|
|
||||||
return
|
|
||||||
|
|
||||||
# If something went wrong, we might get weird data.
|
|
||||||
if data == b"AT+NAME?\r" or data == b"AT+ADV?\r":
|
|
||||||
err = BadConnectionError("Got AT+ notification")
|
|
||||||
self.notify_future.set_exception(err)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Save data
|
|
||||||
self.notify_response.extend(data)
|
|
||||||
|
|
||||||
if len(self.notify_response) == self.current_command.response_size():
|
|
||||||
if self.current_command.is_valid_response(self.notify_response):
|
|
||||||
self.notify_future.set_result(self.notify_response)
|
|
||||||
else:
|
|
||||||
self.notify_future.set_exception(ParseError("Failed checksum"))
|
|
||||||
elif self.current_command.is_exception_response(self.notify_response):
|
|
||||||
# We got a MODBUS command exception
|
|
||||||
msg = f"MODBUS Exception {self.current_command}: {self.notify_response[2]}"
|
|
||||||
self.notify_future.set_exception(ModbusError(msg))
|
|
||||||
@@ -2,21 +2,27 @@
|
|||||||
"domain": "bluetti_bt",
|
"domain": "bluetti_bt",
|
||||||
"name": "Bluetti BT",
|
"name": "Bluetti BT",
|
||||||
"bluetooth": [
|
"bluetooth": [
|
||||||
|
{ "local_name": "AC60*" },
|
||||||
|
{ "local_name": "AC70*" },
|
||||||
|
{ "local_name": "AC180*" },
|
||||||
|
{ "local_name": "AC200L*" },
|
||||||
{ "local_name": "AC200M*" },
|
{ "local_name": "AC200M*" },
|
||||||
{ "local_name": "AC300*" },
|
{ "local_name": "AC300*" },
|
||||||
{ "local_name": "AC500*" },
|
{ "local_name": "AC500*" },
|
||||||
{ "local_name": "AC60*" },
|
|
||||||
{ "local_name": "EB3A*" },
|
{ "local_name": "EB3A*" },
|
||||||
{ "local_name": "EP500*" },
|
{ "local_name": "EP500*" },
|
||||||
{ "local_name": "EP600*" }
|
{ "local_name": "EP600*" },
|
||||||
|
{ "local_name": "EP760*" },
|
||||||
|
{ "local_name": "EP800*" },
|
||||||
|
{ "local_name": "PBOX*" }
|
||||||
],
|
],
|
||||||
"codeowners": ["@Patrick762"],
|
"codeowners": ["@Patrick762"],
|
||||||
"config_flow": true,
|
"config_flow": true,
|
||||||
"dependencies": ["bluetooth"],
|
"dependencies": ["bluetooth_adapters"],
|
||||||
"documentation": "https://github.com/Patrick762/hassio-bluetti-bt",
|
"documentation": "https://github.com/Patrick762/hassio-bluetti-bt",
|
||||||
"integration_type": "device",
|
"integration_type": "device",
|
||||||
"iot_class": "local_polling",
|
"iot_class": "local_polling",
|
||||||
"issue_tracker": "https://github.com/Patrick762/hassio-bluetti-bt/issues",
|
"issue_tracker": "https://github.com/Patrick762/hassio-bluetti-bt/issues",
|
||||||
"requirements": ["bluetti_mqtt==0.15.0"],
|
"requirements": [],
|
||||||
"version": "0.0.8"
|
"version": "0.1.4"
|
||||||
}
|
}
|
||||||
@@ -6,32 +6,25 @@ from enum import Enum
|
|||||||
import logging
|
import logging
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorEntity, CONF_STATE_CLASS
|
from homeassistant.components.sensor import SensorEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_ADDRESS,
|
CONF_ADDRESS,
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
CONF_UNIT_OF_MEASUREMENT,
|
|
||||||
CONF_DEVICE_CLASS,
|
|
||||||
EntityCategory,
|
EntityCategory,
|
||||||
)
|
)
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
from homeassistant.helpers.update_coordinator import (
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
CoordinatorEntity,
|
|
||||||
)
|
|
||||||
|
|
||||||
from bluetti_mqtt.bluetooth import build_device
|
from .bluetti_bt_lib.field_attributes import FIELD_ATTRIBUTES, PACK_FIELD_ATTRIBUTES, FieldType
|
||||||
from bluetti_mqtt.mqtt_client import (
|
from .bluetti_bt_lib.utils.device_builder import build_device
|
||||||
NORMAL_DEVICE_FIELDS,
|
|
||||||
DC_INPUT_FIELDS,
|
|
||||||
MqttFieldType,
|
|
||||||
)
|
|
||||||
|
|
||||||
from . import device_info as dev_info, get_unique_id
|
from . import device_info as dev_info, get_unique_id
|
||||||
from .const import DATA_COORDINATOR, DOMAIN, CONF_OPTIONS, DIAGNOSTIC_FIELDS, ADDITIONAL_DEVICE_FIELDS
|
from .const import DATA_COORDINATOR, DOMAIN, DIAGNOSTIC_FIELDS
|
||||||
from .coordinator import PollingCoordinator, DummyDevice
|
from .coordinator import PollingCoordinator
|
||||||
|
from .utils import unique_id_loggable
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -52,40 +45,45 @@ async def async_setup_entry(
|
|||||||
|
|
||||||
# Add sensors according to device_info
|
# Add sensors according to device_info
|
||||||
bluetti_device = build_device(address, device_name)
|
bluetti_device = build_device(address, device_name)
|
||||||
bluetti_device = DummyDevice(bluetti_device)
|
|
||||||
|
|
||||||
sensors_to_add = []
|
sensors_to_add = []
|
||||||
all_fields = NORMAL_DEVICE_FIELDS
|
all_fields = FIELD_ATTRIBUTES
|
||||||
all_fields.update(DC_INPUT_FIELDS)
|
|
||||||
all_fields.update(ADDITIONAL_DEVICE_FIELDS)
|
if len(bluetti_device.pack_polling_commands) > 0:
|
||||||
|
# add pack fields for device
|
||||||
|
_LOGGER.info("Device type(%s) pack_num_max(%s)", bluetti_device.type, bluetti_device.pack_num_max)
|
||||||
|
for pack in range (1, bluetti_device.pack_num_max + 1):
|
||||||
|
for name, field in PACK_FIELD_ATTRIBUTES(pack).items():
|
||||||
|
all_fields.update({name+str(pack): field})
|
||||||
|
|
||||||
for field_key, field_config in all_fields.items():
|
for field_key, field_config in all_fields.items():
|
||||||
if bluetti_device.has_field(field_key):
|
if bluetti_device.has_field(field_key) or (len(bluetti_device.pack_polling_commands) > 0 and field_key.startswith("pack_")):
|
||||||
category = None
|
category = None
|
||||||
if field_config.setter is True or field_key in DIAGNOSTIC_FIELDS:
|
if field_config.setter is True or field_key in DIAGNOSTIC_FIELDS:
|
||||||
category = EntityCategory.DIAGNOSTIC
|
category = EntityCategory.DIAGNOSTIC
|
||||||
if field_config.type == MqttFieldType.NUMERIC:
|
if field_config.type == FieldType.NUMERIC:
|
||||||
sensors_to_add.append(
|
sensors_to_add.append(
|
||||||
BluettiSensor(
|
BluettiSensor(
|
||||||
hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR],
|
hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR],
|
||||||
device_info,
|
device_info,
|
||||||
address,
|
address,
|
||||||
field_key,
|
field_key,
|
||||||
field_config.home_assistant_extra.get(CONF_NAME, ""),
|
field_config.name,
|
||||||
field_config.home_assistant_extra.get(CONF_UNIT_OF_MEASUREMENT),
|
field_config.unit_of_measurement,
|
||||||
field_config.home_assistant_extra.get(CONF_DEVICE_CLASS),
|
field_config.device_class,
|
||||||
field_config.home_assistant_extra.get(CONF_STATE_CLASS),
|
field_config.state_class,
|
||||||
category=category,
|
category=category,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
elif field_config.type == MqttFieldType.ENUM:
|
elif field_config.type == FieldType.ENUM:
|
||||||
sensors_to_add.append(
|
sensors_to_add.append(
|
||||||
BluettiSensor(
|
BluettiSensor(
|
||||||
hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR],
|
hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR],
|
||||||
device_info,
|
device_info,
|
||||||
address,
|
address,
|
||||||
field_key,
|
field_key,
|
||||||
field_config.home_assistant_extra.get(CONF_NAME, ""),
|
field_config.name,
|
||||||
options=field_config.home_assistant_extra.get(CONF_OPTIONS),
|
options=[o.value for o in field_config.options],
|
||||||
category=category,
|
category=category,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@@ -112,12 +110,14 @@ class BluettiSensor(CoordinatorEntity, SensorEntity):
|
|||||||
"""Init battery entity."""
|
"""Init battery entity."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
|
|
||||||
|
self._attr_has_entity_name = True
|
||||||
e_name = f"{device_info.get('name')} {name}"
|
e_name = f"{device_info.get('name')} {name}"
|
||||||
self._address = address
|
self._address = address
|
||||||
self._response_key = response_key
|
self._response_key = response_key
|
||||||
|
self._unavailable_counter = 0
|
||||||
|
|
||||||
self._attr_device_info = device_info
|
self._attr_device_info = device_info
|
||||||
self._attr_name = e_name
|
self._attr_name = name
|
||||||
self._attr_available = False
|
self._attr_available = False
|
||||||
self._attr_unique_id = get_unique_id(e_name)
|
self._attr_unique_id = get_unique_id(e_name)
|
||||||
self._attr_native_unit_of_measurement = unit_of_measurement
|
self._attr_native_unit_of_measurement = unit_of_measurement
|
||||||
@@ -126,20 +126,58 @@ class BluettiSensor(CoordinatorEntity, SensorEntity):
|
|||||||
self._attr_entity_category = category
|
self._attr_entity_category = category
|
||||||
self._options = options
|
self._options = options
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self) -> bool:
|
||||||
|
"""Return if entity is available."""
|
||||||
|
return self._attr_available
|
||||||
|
|
||||||
|
def _set_available(self):
|
||||||
|
"""Set sensor as available."""
|
||||||
|
self._attr_available = True
|
||||||
|
self._unavailable_counter = 0
|
||||||
|
self._attr_extra_state_attributes = {}
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
def _set_unavailable(self, cause: str = "Unknown"):
|
||||||
|
"""Set sensor as unavailable."""
|
||||||
|
self._unavailable_counter += 1
|
||||||
|
|
||||||
|
self._attr_extra_state_attributes = {
|
||||||
|
"unavailable_counter": self._unavailable_counter,
|
||||||
|
"unavailable_cause": cause,
|
||||||
|
}
|
||||||
|
|
||||||
|
if self._unavailable_counter >= 5:
|
||||||
|
self._attr_available = False
|
||||||
|
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _handle_coordinator_update(self) -> None:
|
def _handle_coordinator_update(self) -> None:
|
||||||
"""Handle updated data from the coordinator."""
|
"""Handle updated data from the coordinator."""
|
||||||
_LOGGER.debug("Updating state of %s", self._attr_unique_id)
|
|
||||||
if not isinstance(self.coordinator.data, dict):
|
if self.coordinator.reader.persistent_conn and not self.coordinator.reader.client.is_connected:
|
||||||
_LOGGER.error(
|
return
|
||||||
"Invalid data from coordinator (sensor.%s)", self._attr_unique_id
|
|
||||||
|
if self.coordinator.data is None:
|
||||||
|
_LOGGER.debug(
|
||||||
|
"Data from coordinator is None",
|
||||||
)
|
)
|
||||||
self._attr_available = False
|
self._set_unavailable("Data is None")
|
||||||
|
return
|
||||||
|
|
||||||
|
_LOGGER.debug("Updating state of %s", unique_id_loggable(self._attr_unique_id))
|
||||||
|
if not isinstance(self.coordinator.data, dict):
|
||||||
|
_LOGGER.warning(
|
||||||
|
"Invalid data from coordinator (sensor.%s)", unique_id_loggable(self._attr_unique_id)
|
||||||
|
)
|
||||||
|
self._set_unavailable("Invalid data")
|
||||||
return
|
return
|
||||||
|
|
||||||
response_data = self.coordinator.data.get(self._response_key)
|
response_data = self.coordinator.data.get(self._response_key)
|
||||||
if response_data is None:
|
if response_data is None:
|
||||||
self._attr_available = False
|
_LOGGER.debug("No data for available for (%s)", self._response_key)
|
||||||
|
self._set_unavailable("No data")
|
||||||
return
|
return
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@@ -151,23 +189,22 @@ class BluettiSensor(CoordinatorEntity, SensorEntity):
|
|||||||
):
|
):
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"Invalid response data type from coordinator (sensor.%s): %s has type %s",
|
"Invalid response data type from coordinator (sensor.%s): %s has type %s",
|
||||||
self._attr_unique_id,
|
unique_id_loggable(self._attr_unique_id),
|
||||||
response_data,
|
response_data,
|
||||||
type(response_data),
|
type(response_data),
|
||||||
)
|
)
|
||||||
self._attr_available = False
|
self._set_unavailable("Invalid data type")
|
||||||
return
|
return
|
||||||
|
|
||||||
self._attr_available = True
|
self._set_available()
|
||||||
|
|
||||||
# Different for enum and numeric
|
# Different for enum and numeric
|
||||||
if (
|
if (
|
||||||
self._options is not None
|
self._options is not None
|
||||||
and isinstance(response_data, Enum)
|
and isinstance(response_data, Enum)
|
||||||
and response_data.value < len(self._options)
|
|
||||||
):
|
):
|
||||||
# Enum
|
# Enum
|
||||||
self._attr_native_value = self._options[response_data.value]
|
self._attr_native_value = response_data.name
|
||||||
else:
|
else:
|
||||||
# Numeric
|
# Numeric
|
||||||
self._attr_native_value = response_data
|
self._attr_native_value = response_data
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import asyncio
|
|||||||
import logging
|
import logging
|
||||||
import async_timeout
|
import async_timeout
|
||||||
|
|
||||||
from bleak import BleakClient, BleakError
|
from bleak import BleakError
|
||||||
|
|
||||||
from homeassistant.components import bluetooth
|
from homeassistant.components import bluetooth
|
||||||
from homeassistant.components.switch import SwitchEntity, SwitchDeviceClass
|
from homeassistant.components.switch import SwitchEntity, SwitchDeviceClass
|
||||||
@@ -23,18 +23,15 @@ from homeassistant.helpers.update_coordinator import (
|
|||||||
CoordinatorEntity,
|
CoordinatorEntity,
|
||||||
)
|
)
|
||||||
|
|
||||||
from bluetti_mqtt.bluetooth import build_device
|
from .bluetti_bt_lib.base_devices.BluettiDevice import BluettiDevice
|
||||||
from bluetti_mqtt.bluetooth.client import BluetoothClient
|
from .bluetti_bt_lib.const import WRITE_UUID
|
||||||
from bluetti_mqtt.core.devices.bluetti_device import BluettiDevice
|
from .bluetti_bt_lib.field_attributes import FIELD_ATTRIBUTES, PACK_FIELD_ATTRIBUTES, FieldType
|
||||||
from bluetti_mqtt.mqtt_client import (
|
from .bluetti_bt_lib.utils.device_builder import build_device
|
||||||
NORMAL_DEVICE_FIELDS,
|
|
||||||
DC_INPUT_FIELDS,
|
|
||||||
MqttFieldType,
|
|
||||||
)
|
|
||||||
|
|
||||||
from . import device_info as dev_info, get_unique_id
|
from . import device_info as dev_info, get_unique_id
|
||||||
from .const import CONTROL_FIELDS, DATA_COORDINATOR, DATA_POLLING_RUNNING, DOMAIN, ADDITIONAL_DEVICE_FIELDS
|
from .const import CONTROL_FIELDS, DATA_COORDINATOR, DOMAIN
|
||||||
from .coordinator import PollingCoordinator, DummyDevice
|
from .coordinator import PollingCoordinator
|
||||||
|
from .utils import mac_loggable, unique_id_loggable
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -50,20 +47,17 @@ async def async_setup_entry(
|
|||||||
_LOGGER.error("Device has no address")
|
_LOGGER.error("Device has no address")
|
||||||
|
|
||||||
# Generate device info
|
# Generate device info
|
||||||
_LOGGER.info("Creating sensors for device with address %s", address)
|
_LOGGER.info("Creating switches for device with address %s", address)
|
||||||
device_info = dev_info(entry)
|
device_info = dev_info(entry)
|
||||||
|
|
||||||
# Add sensors according to device_info
|
# Add sensors according to device_info
|
||||||
bluetti_device = build_device(address, device_name)
|
bluetti_device = build_device(address, device_name)
|
||||||
bluetti_device = DummyDevice(bluetti_device)
|
|
||||||
|
|
||||||
sensors_to_add = []
|
sensors_to_add = []
|
||||||
all_fields = NORMAL_DEVICE_FIELDS
|
all_fields = FIELD_ATTRIBUTES
|
||||||
all_fields.update(DC_INPUT_FIELDS)
|
|
||||||
all_fields.update(ADDITIONAL_DEVICE_FIELDS)
|
|
||||||
for field_key, field_config in all_fields.items():
|
for field_key, field_config in all_fields.items():
|
||||||
if bluetti_device.has_field(field_key):
|
if bluetti_device.has_field(field_key):
|
||||||
if field_config.type == MqttFieldType.BOOL:
|
if field_config.type == FieldType.BOOL:
|
||||||
if field_config.setter is True and field_key in CONTROL_FIELDS:
|
if field_config.setter is True and field_key in CONTROL_FIELDS:
|
||||||
sensors_to_add.append(
|
sensors_to_add.append(
|
||||||
BluettiSwitch(
|
BluettiSwitch(
|
||||||
@@ -72,7 +66,7 @@ async def async_setup_entry(
|
|||||||
device_info,
|
device_info,
|
||||||
address,
|
address,
|
||||||
field_key,
|
field_key,
|
||||||
field_config.home_assistant_extra.get(CONF_NAME, ""),
|
field_config.name,
|
||||||
entry.entry_id
|
entry.entry_id
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@@ -98,41 +92,57 @@ class BluettiSwitch(CoordinatorEntity, SwitchEntity):
|
|||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
|
|
||||||
self._bluetti_device = bluetti_device
|
self._bluetti_device = bluetti_device
|
||||||
|
self._coordinator = coordinator
|
||||||
|
self._client = coordinator.reader.client
|
||||||
|
self._polling_lock = coordinator.reader.polling_lock
|
||||||
e_name = f"{device_info.get('name')} {name}"
|
e_name = f"{device_info.get('name')} {name}"
|
||||||
self._address = address
|
self._address = address
|
||||||
self._response_key = response_key
|
self._response_key = response_key
|
||||||
self._entry_id = entry_id
|
self._entry_id = entry_id
|
||||||
|
|
||||||
self._attr_device_info = device_info
|
self._attr_device_info = device_info
|
||||||
self._attr_name = e_name
|
self._attr_has_entity_name = True
|
||||||
|
self._attr_name = name
|
||||||
self._attr_available = False
|
self._attr_available = False
|
||||||
self._attr_unique_id = get_unique_id(e_name)
|
self._attr_unique_id = get_unique_id(e_name)
|
||||||
self._attr_entity_category = category
|
self._attr_entity_category = category
|
||||||
self._attr_device_class = SwitchDeviceClass.OUTLET
|
self._attr_device_class = SwitchDeviceClass.OUTLET
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self) -> bool:
|
||||||
|
"""Return if entity is available."""
|
||||||
|
return self._attr_available
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _handle_coordinator_update(self) -> None:
|
def _handle_coordinator_update(self) -> None:
|
||||||
"""Handle updated data from the coordinator."""
|
"""Handle updated data from the coordinator."""
|
||||||
_LOGGER.debug("Updating state of %s", self._attr_unique_id)
|
|
||||||
|
if self.coordinator.reader.persistent_conn and not self.coordinator.reader.client.is_connected:
|
||||||
|
return
|
||||||
|
|
||||||
|
_LOGGER.debug("Updating state of %s", unique_id_loggable(self._attr_unique_id))
|
||||||
if not isinstance(self.coordinator.data, dict):
|
if not isinstance(self.coordinator.data, dict):
|
||||||
_LOGGER.error(
|
_LOGGER.debug(
|
||||||
"Invalid data from coordinator (sensor.%s)", self._attr_unique_id
|
"Invalid data from coordinator (switch.%s)", unique_id_loggable(self._attr_unique_id)
|
||||||
)
|
)
|
||||||
self._attr_available = False
|
self._attr_available = False
|
||||||
|
self.async_write_ha_state()
|
||||||
return
|
return
|
||||||
|
|
||||||
response_data = self.coordinator.data.get(self._response_key)
|
response_data = self.coordinator.data.get(self._response_key)
|
||||||
if response_data is None:
|
if response_data is None:
|
||||||
self._attr_available = False
|
self._attr_available = False
|
||||||
|
self.async_write_ha_state()
|
||||||
return
|
return
|
||||||
|
|
||||||
if not isinstance(response_data, bool):
|
if not isinstance(response_data, bool):
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"Invalid response data type from coordinator (sensor.%s): %s",
|
"Invalid response data type from coordinator (switch.%s): %s",
|
||||||
self._attr_unique_id,
|
unique_id_loggable(self._attr_unique_id),
|
||||||
response_data,
|
response_data,
|
||||||
)
|
)
|
||||||
self._attr_available = False
|
self._attr_available = False
|
||||||
|
self.async_write_ha_state()
|
||||||
return
|
return
|
||||||
|
|
||||||
self._attr_available = True
|
self._attr_available = True
|
||||||
@@ -141,48 +151,42 @@ class BluettiSwitch(CoordinatorEntity, SwitchEntity):
|
|||||||
|
|
||||||
async def async_turn_on(self, **kwargs):
|
async def async_turn_on(self, **kwargs):
|
||||||
"""Turn the entity on."""
|
"""Turn the entity on."""
|
||||||
_LOGGER.debug("Turn on %s on %s", self._response_key, self._address)
|
_LOGGER.debug("Turn on %s on %s", self._response_key, mac_loggable(self._address))
|
||||||
await self.write_to_device(True)
|
await self.write_to_device(True)
|
||||||
|
|
||||||
async def async_turn_off(self, **kwargs):
|
async def async_turn_off(self, **kwargs):
|
||||||
"""Turn the entity off."""
|
"""Turn the entity off."""
|
||||||
_LOGGER.debug("Turn off %s on %s", self._response_key, self._address)
|
_LOGGER.debug("Turn off %s on %s", self._response_key, mac_loggable(self._address))
|
||||||
await self.write_to_device(False)
|
await self.write_to_device(False)
|
||||||
|
|
||||||
async def write_to_device(self, state: bool):
|
async def write_to_device(self, state: bool):
|
||||||
"""Write to device."""
|
"""Write to device."""
|
||||||
device = bluetooth.async_ble_device_from_address(self.hass, self._address)
|
|
||||||
if device is None:
|
|
||||||
_LOGGER.error("Device %s not available", self._address)
|
|
||||||
return
|
|
||||||
|
|
||||||
command = self._bluetti_device.build_setter_command(self._response_key, state)
|
command = self._bluetti_device.build_setter_command(self._response_key, state)
|
||||||
|
|
||||||
# Wait until polling is done
|
async with self._polling_lock:
|
||||||
while self.hass.data[DOMAIN][self._entry_id][DATA_POLLING_RUNNING] is True:
|
|
||||||
_LOGGER.debug("Waiting for polling to finish...")
|
|
||||||
asyncio.sleep(1)
|
|
||||||
|
|
||||||
client = BleakClient(device)
|
|
||||||
try:
|
try:
|
||||||
async with async_timeout.timeout(15):
|
async with async_timeout.timeout(15):
|
||||||
await client.connect()
|
if not self._client.is_connected:
|
||||||
|
await self._client.connect()
|
||||||
|
|
||||||
# Send command
|
# Send command
|
||||||
await client.write_gatt_char(
|
_LOGGER.debug("Requesting %s (%s,%s)", command, self._response_key, state)
|
||||||
BluetoothClient.WRITE_UUID, bytes(command)
|
await self._client.write_gatt_char(
|
||||||
|
WRITE_UUID, bytes(command)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Wait until device has changed value, otherwise reading register might reset it
|
# Wait until device has changed value, otherwise reading register might reset it
|
||||||
await asyncio.sleep(5)
|
await asyncio.sleep(5)
|
||||||
|
|
||||||
except TimeoutError:
|
except TimeoutError:
|
||||||
_LOGGER.error("Timed out for device %s", self._address)
|
_LOGGER.error("Timed out for device %s", mac_loggable(self._address))
|
||||||
return None
|
return None
|
||||||
except BleakError as err:
|
except BleakError as err:
|
||||||
_LOGGER.error("Bleak error: %s", err)
|
_LOGGER.error("Bleak error: %s", err)
|
||||||
return None
|
return None
|
||||||
finally:
|
finally:
|
||||||
await client.disconnect()
|
# Disconnect if connection not persistant
|
||||||
|
if not self._coordinator.reader.persistent_conn:
|
||||||
|
await self._client.disconnect()
|
||||||
|
|
||||||
await self.coordinator.async_request_refresh()
|
await self.coordinator.async_request_refresh()
|
||||||
@@ -1,11 +1,31 @@
|
|||||||
{
|
{
|
||||||
|
"config": {
|
||||||
|
"step": {
|
||||||
|
"user": {
|
||||||
|
"data": {
|
||||||
|
"address": "Gerät",
|
||||||
|
"type": "Modell (erforderlich bei PBOX Geräten)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"options": {
|
"options": {
|
||||||
"step": {
|
"step": {
|
||||||
"init": {
|
"init": {
|
||||||
"data": {
|
"data": {
|
||||||
"use_controls": "Steuerung aktivieren (auf eigenes Risiko)"
|
"use_controls": "Steuerung aktivieren (auf eigenes Risiko)",
|
||||||
|
"persistent_conn": "Dauerhafte Verbindung (Neustart erforderlich)",
|
||||||
|
"polling_interval": "Datenabruf-Intervall in Sekunden (Neustart erforderlich)",
|
||||||
|
"polling_timeout": "Datenabruf-Timeout in Sekunden (Neustart erforderlich)",
|
||||||
|
"max_retries": "Maximale Verbindungsversuche (Neustart erforderlich)",
|
||||||
|
"use_encryption": "Verschlüsselte Verbindung (erforderlich für neue Firmware-Versionen)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"abort": {
|
||||||
|
"invalid_interval": "Ungültiger Datenabruf-Intervall. Verwende 5 Sekunden oder mehr",
|
||||||
|
"invalid_timeout": "Ungültiger Datenabruf-Timeout. Verwende 1 Sekunde oder mehr",
|
||||||
|
"invalid_retries": "Ungültige maximale Verbindungsversuche. Verwende 1 oder mehr"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,31 @@
|
|||||||
{
|
{
|
||||||
|
"config": {
|
||||||
|
"step": {
|
||||||
|
"user": {
|
||||||
|
"data": {
|
||||||
|
"address": "Device",
|
||||||
|
"type": "Model (required for PBOX devices)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"options": {
|
"options": {
|
||||||
"step": {
|
"step": {
|
||||||
"init": {
|
"init": {
|
||||||
"data": {
|
"data": {
|
||||||
"use_controls": "Use controls (use at own risk)"
|
"use_controls": "Use controls (use at own risk)",
|
||||||
|
"persistent_conn": "Persistent connection (restart required)",
|
||||||
|
"polling_interval": "Polling interval in seconds (restart required)",
|
||||||
|
"polling_timeout": "Polling timeout in seconds (restart required)",
|
||||||
|
"max_retries": "Maximum amount of connection retries (restart required)",
|
||||||
|
"use_encryption": "Encrypted connection (necessary for latest firmware versions)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"abort": {
|
||||||
|
"invalid_interval": "Invalid polling interval. Use 5 seconds or more",
|
||||||
|
"invalid_timeout": "Invalid polling timeout. Use 1 second or more",
|
||||||
|
"invalid_retries": "Invalid max retries. Use 1 or more"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"options": {
|
||||||
|
"step": {
|
||||||
|
"init": {
|
||||||
|
"data": {
|
||||||
|
"use_controls": "Usar os comandos",
|
||||||
|
"persistent_conn": "Ligação presistente (necessario reninciar)",
|
||||||
|
"polling_interval": "Intervalo deatualização (necessario reninciar)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"abort": {
|
||||||
|
"invalid_interval": "Valor invalido . Tem de ser no minimo 5 segundos"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
"""Tools."""
|
||||||
|
|
||||||
|
def mac_loggable(mac: str) -> str:
|
||||||
|
"""Remove parts of the mac address for logging."""
|
||||||
|
splitted = mac.split(":")
|
||||||
|
return "XX:XX:XX:XX:XX:" + splitted[-1]
|
||||||
|
|
||||||
|
def unique_id_loggable(unique_id: str) -> str:
|
||||||
|
"""Remove parts of the unique id for logging."""
|
||||||
|
splitted = unique_id.split("_", maxsplit=1)
|
||||||
|
serial = splitted[0][:6]
|
||||||
|
return serial + "XXXXXXXXXX" + splitted[1]
|
||||||
@@ -1 +0,0 @@
|
|||||||
robocopy .\custom_components\bluetti_bt T:\custom_components\bluetti_bt /E
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
# This script is only for development purposes
|
||||||
|
|
||||||
|
sudo rm -rf /var/ha_config/custom_components/*/__pycache__
|
||||||
|
sudo cp -r ./custom_components/* /var/ha_config/custom_components/
|
||||||
|
docker restart homeassistant
|
||||||
|
docker logs -f homeassistant
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
homeassistant
|
||||||
|
bleak_retry_connector
|
||||||
|
pyserial
|
||||||
|
bluetooth_auto_recovery
|
||||||
|
crcmod
|
||||||
|
typing
|
||||||
|
asyncio
|
||||||
|
logging
|
||||||
|
bleak
|
||||||
|
async_timeout
|
||||||
|
aiousbwatcher
|
||||||
|
pyasn1
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
# Execute unittests of python library inside docker
|
||||||
|
sudo docker build --progress=plain -t bluetti-bt-tests .
|
||||||
@@ -0,0 +1,151 @@
|
|||||||
|
"""Unittest for device builder."""
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from custom_components.bluetti_bt.bluetti_bt_lib.utils.device_builder import build_device
|
||||||
|
from custom_components.bluetti_bt.bluetti_bt_lib.devices.ac60 import AC60
|
||||||
|
from custom_components.bluetti_bt.bluetti_bt_lib.devices.ac70 import AC70
|
||||||
|
from custom_components.bluetti_bt.bluetti_bt_lib.devices.ac70p import AC70P
|
||||||
|
from custom_components.bluetti_bt.bluetti_bt_lib.devices.ac180 import AC180
|
||||||
|
from custom_components.bluetti_bt.bluetti_bt_lib.devices.ac180p import AC180P
|
||||||
|
from custom_components.bluetti_bt.bluetti_bt_lib.devices.ac200l import AC200L
|
||||||
|
from custom_components.bluetti_bt.bluetti_bt_lib.devices.ac200m import AC200M
|
||||||
|
from custom_components.bluetti_bt.bluetti_bt_lib.devices.ac300 import AC300
|
||||||
|
from custom_components.bluetti_bt.bluetti_bt_lib.devices.ac500 import AC500
|
||||||
|
from custom_components.bluetti_bt.bluetti_bt_lib.devices.eb3a import EB3A
|
||||||
|
from custom_components.bluetti_bt.bluetti_bt_lib.devices.ep500 import EP500
|
||||||
|
from custom_components.bluetti_bt.bluetti_bt_lib.devices.ep500p import EP500P
|
||||||
|
from custom_components.bluetti_bt.bluetti_bt_lib.devices.ep600 import EP600
|
||||||
|
from custom_components.bluetti_bt.bluetti_bt_lib.devices.ep760 import EP760
|
||||||
|
from custom_components.bluetti_bt.bluetti_bt_lib.devices.ep800 import EP800
|
||||||
|
|
||||||
|
|
||||||
|
class TestDeviceBuilder(unittest.TestCase):
|
||||||
|
def test_build_Unknow(self):
|
||||||
|
bt_addr = "aa:bb:cc:dd:ee:ff"
|
||||||
|
bt_name = "PBOX56786746478"
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
build_device(bt_addr, bt_name)
|
||||||
|
|
||||||
|
def test_build_ac60(self):
|
||||||
|
bt_addr = "aa:bb:cc:dd:ee:ff"
|
||||||
|
bt_name = "AC6056786746478"
|
||||||
|
built = build_device(bt_addr, bt_name)
|
||||||
|
|
||||||
|
self.assertIsInstance(built, AC60)
|
||||||
|
self.assertEqual(built.address, bt_addr)
|
||||||
|
|
||||||
|
def test_build_ac70(self):
|
||||||
|
bt_addr = "aa:bb:cc:dd:ee:ff"
|
||||||
|
bt_name = "AC7056786746478"
|
||||||
|
built = build_device(bt_addr, bt_name)
|
||||||
|
|
||||||
|
self.assertIsInstance(built, AC70)
|
||||||
|
self.assertEqual(built.address, bt_addr)
|
||||||
|
|
||||||
|
def test_build_ac70p(self):
|
||||||
|
bt_addr = "aa:bb:cc:dd:ee:ff"
|
||||||
|
bt_name = "AC70P56786746478"
|
||||||
|
built = build_device(bt_addr, bt_name)
|
||||||
|
|
||||||
|
self.assertIsInstance(built, AC70P)
|
||||||
|
self.assertEqual(built.address, bt_addr)
|
||||||
|
|
||||||
|
def test_build_ac180(self):
|
||||||
|
bt_addr = "aa:bb:cc:dd:ee:ff"
|
||||||
|
bt_name = "AC18056786746478"
|
||||||
|
built = build_device(bt_addr, bt_name)
|
||||||
|
|
||||||
|
self.assertIsInstance(built, AC180)
|
||||||
|
self.assertEqual(built.address, bt_addr)
|
||||||
|
|
||||||
|
def test_build_ac180p(self):
|
||||||
|
bt_addr = "aa:bb:cc:dd:ee:ff"
|
||||||
|
bt_name = "AC180P56786746478"
|
||||||
|
built = build_device(bt_addr, bt_name)
|
||||||
|
|
||||||
|
self.assertIsInstance(built, AC180P)
|
||||||
|
self.assertEqual(built.address, bt_addr)
|
||||||
|
|
||||||
|
def test_build_ac200l(self):
|
||||||
|
bt_addr = "aa:bb:cc:dd:ee:ff"
|
||||||
|
bt_name = "AC200L56786746478"
|
||||||
|
built = build_device(bt_addr, bt_name)
|
||||||
|
|
||||||
|
self.assertIsInstance(built, AC200L)
|
||||||
|
self.assertEqual(built.address, bt_addr)
|
||||||
|
|
||||||
|
def test_build_ac200m(self):
|
||||||
|
bt_addr = "aa:bb:cc:dd:ee:ff"
|
||||||
|
bt_name = "AC200M56786746478"
|
||||||
|
built = build_device(bt_addr, bt_name)
|
||||||
|
|
||||||
|
self.assertIsInstance(built, AC200M)
|
||||||
|
self.assertEqual(built.address, bt_addr)
|
||||||
|
|
||||||
|
def test_build_ac300(self):
|
||||||
|
bt_addr = "aa:bb:cc:dd:ee:ff"
|
||||||
|
bt_name = "AC30056786746478"
|
||||||
|
built = build_device(bt_addr, bt_name)
|
||||||
|
|
||||||
|
self.assertIsInstance(built, AC300)
|
||||||
|
self.assertEqual(built.address, bt_addr)
|
||||||
|
|
||||||
|
def test_build_ac500(self):
|
||||||
|
bt_addr = "aa:bb:cc:dd:ee:ff"
|
||||||
|
bt_name = "AC50056786746478"
|
||||||
|
built = build_device(bt_addr, bt_name)
|
||||||
|
|
||||||
|
self.assertIsInstance(built, AC500)
|
||||||
|
self.assertEqual(built.address, bt_addr)
|
||||||
|
|
||||||
|
def test_build_eb3a(self):
|
||||||
|
bt_addr = "aa:bb:cc:dd:ee:ff"
|
||||||
|
bt_name = "EB3A56786746478"
|
||||||
|
built = build_device(bt_addr, bt_name)
|
||||||
|
|
||||||
|
self.assertIsInstance(built, EB3A)
|
||||||
|
self.assertEqual(built.address, bt_addr)
|
||||||
|
|
||||||
|
def test_build_ep500(self):
|
||||||
|
bt_addr = "aa:bb:cc:dd:ee:ff"
|
||||||
|
bt_name = "EP50056786746478"
|
||||||
|
built = build_device(bt_addr, bt_name)
|
||||||
|
|
||||||
|
self.assertIsInstance(built, EP500)
|
||||||
|
self.assertEqual(built.address, bt_addr)
|
||||||
|
|
||||||
|
def test_build_ep500p(self):
|
||||||
|
bt_addr = "aa:bb:cc:dd:ee:ff"
|
||||||
|
bt_name = "EP500P56786746478"
|
||||||
|
built = build_device(bt_addr, bt_name)
|
||||||
|
|
||||||
|
self.assertIsInstance(built, EP500P)
|
||||||
|
self.assertEqual(built.address, bt_addr)
|
||||||
|
|
||||||
|
def test_build_EP600(self):
|
||||||
|
bt_addr = "aa:bb:cc:dd:ee:ff"
|
||||||
|
bt_name = "EP60056786746478"
|
||||||
|
built = build_device(bt_addr, bt_name)
|
||||||
|
|
||||||
|
self.assertIsInstance(built, EP600)
|
||||||
|
self.assertEqual(built.address, bt_addr)
|
||||||
|
|
||||||
|
def test_build_EP760(self):
|
||||||
|
bt_addr = "aa:bb:cc:dd:ee:ff"
|
||||||
|
bt_name = "EP76056786746478"
|
||||||
|
built = build_device(bt_addr, bt_name)
|
||||||
|
|
||||||
|
self.assertIsInstance(built, EP760)
|
||||||
|
self.assertEqual(built.address, bt_addr)
|
||||||
|
|
||||||
|
def test_build_EP800(self):
|
||||||
|
bt_addr = "aa:bb:cc:dd:ee:ff"
|
||||||
|
bt_name = "EP80056786746478"
|
||||||
|
built = build_device(bt_addr, bt_name)
|
||||||
|
|
||||||
|
self.assertIsInstance(built, EP800)
|
||||||
|
self.assertEqual(built.address, bt_addr)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
"""Unittest for device struct."""
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from custom_components.bluetti_bt.bluetti_bt_lib.utils.commands import ReadHoldingRegisters
|
||||||
|
from custom_components.bluetti_bt.bluetti_bt_lib.utils.struct import DeviceStruct
|
||||||
|
|
||||||
|
class TestDeviceStruct(unittest.TestCase):
|
||||||
|
def test_get_read_holding_registers(self):
|
||||||
|
struct = DeviceStruct()
|
||||||
|
|
||||||
|
struct.add_swap_string_field("device_type", 110, 6)
|
||||||
|
struct.add_sn_field("serial_number", 116)
|
||||||
|
struct.add_decimal_field(
|
||||||
|
"power_generation", 154, 1
|
||||||
|
)
|
||||||
|
struct.add_uint_field("total_battery_percent", 102)
|
||||||
|
|
||||||
|
registers = struct.get_read_holding_registers()
|
||||||
|
|
||||||
|
self.assertGreater(len(registers), 0)
|
||||||
|
|
||||||
|
for reg in registers:
|
||||||
|
self.assertIsInstance(reg, ReadHoldingRegisters)
|
||||||
Reference in new issue
Block a user