Compare commits

...
5 Commits
Author SHA1 Message Date
Patrick762 924eaa90b6 Merge pull request #3 from Patrick762/device-support
Added more fields for ep600
2023-08-02 21:23:00 +02:00
Patrick762 4f95fed85a added more fields for ep600 2023-08-02 21:07:24 +02:00
Patrick762 332cf6dc28 Merge pull request #2 from Patrick762/device-support
Support for more fields
2023-08-01 21:23:38 +02:00
Patrick762 cc7891ed6a added more fields 2023-08-01 21:20:02 +02:00
Patrick762 12cd2719d2 added dummy device class to append new fields 2023-08-01 20:37:52 +02:00
5 changed files with 147 additions and 17 deletions

No files matched your search

+1
View File
@@ -22,3 +22,4 @@ Based on [bluetti_mqtt](https://github.com/warhammerkid/bluetti_mqtt).
### Available sensors: ### Available sensors:
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)) 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.
+10 -3
View File
@@ -20,10 +20,14 @@ from homeassistant.helpers.update_coordinator import (
) )
from bluetti_mqtt.bluetooth import build_device from bluetti_mqtt.bluetooth import build_device
from bluetti_mqtt.mqtt_client import NORMAL_DEVICE_FIELDS, MqttFieldType from bluetti_mqtt.mqtt_client import (
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 DOMAIN from .const import DOMAIN, ADDITIONAL_DEVICE_FIELDS
from .coordinator import PollingCoordinator from .coordinator import PollingCoordinator
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@@ -47,7 +51,10 @@ async def async_setup_entry(
bluetti_device = build_device(address, device_name) bluetti_device = build_device(address, device_name)
sensors_to_add = [] sensors_to_add = []
for field_key, field_config in NORMAL_DEVICE_FIELDS.items(): all_fields = NORMAL_DEVICE_FIELDS
all_fields.update(DC_INPUT_FIELDS)
all_fields.update(ADDITIONAL_DEVICE_FIELDS)
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 == MqttFieldType.BOOL:
category = None category = None
+65
View File
@@ -1,6 +1,71 @@
"""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"
DIAGNOSTIC_FIELDS = [
"max_ac_input_power",
"max_ac_input_current",
"max_ac_output_power",
"max_ac_output_current",
"bcu_version",
"safety_module_version",
"high_voltage_module_version",
]
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"},
),
}
+57 -5
View File
@@ -1,6 +1,7 @@
"""Coordinator for Bluetti integration.""" """Coordinator for Bluetti integration."""
from __future__ import annotations from __future__ import annotations
from typing import List
import asyncio import asyncio
from datetime import timedelta from datetime import timedelta
@@ -23,10 +24,56 @@ from bluetti_mqtt.bluetooth import (
ParseError, ParseError,
build_device, build_device,
) )
from bluetti_mqtt.core.devices.bluetti_device import BluettiDevice
from bluetti_mqtt.core.commands import ReadHoldingRegisters
_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
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(2022, 6),
ReadHoldingRegisters(2213, 4),
# Battery
ReadHoldingRegisters(6101, 7),
ReadHoldingRegisters(6175, 11),
]
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."""
@@ -42,7 +89,10 @@ class PollingCoordinator(DataUpdateCoordinator):
self.notify_future = None self.notify_future = None
self.current_command = None self.current_command = None
self.notify_response = bytearray() self.notify_response = bytearray()
self.bluetti_device = build_device(address, device_name) bluetti_device = build_device(address, device_name)
# Add or modify device fields
self.bluetti_device = DummyDevice(bluetti_device)
async def _async_update_data(self): async def _async_update_data(self):
"""Fetch data from API endpoint. """Fetch data from API endpoint.
@@ -99,7 +149,7 @@ class PollingCoordinator(DataUpdateCoordinator):
command.starting_address, body command.starting_address, body
) )
self.logger.debug("Parsed data: %s", parsed) self.logger.error("[NO ERROR] Parsed data: %s", parsed)
parsed_data.update(parsed) parsed_data.update(parsed)
except TimeoutError: except TimeoutError:
@@ -107,15 +157,17 @@ class PollingCoordinator(DataUpdateCoordinator):
"Polling timed out (address: %s)", self._address "Polling timed out (address: %s)", self._address
) )
except ParseError: except ParseError:
self.logger.debug("Got a parse exception...") self.logger.warning("Got a parse exception...")
except ModbusError as err: except ModbusError as err:
self.logger.debug( self.logger.warning(
"Got an invalid request error for %s: %s", "Got an invalid request error for %s: %s",
command, command,
err, err,
) )
except (BadConnectionError, BleakError) as err: except (BadConnectionError, BleakError) as err:
self.logger.debug("Needed to disconnect due to error: %s", err) self.logger.warning(
"Needed to disconnect due to error: %s", err
)
except TimeoutError: except TimeoutError:
self.logger.error("Polling timed out for device %s", self._address) self.logger.error("Polling timed out for device %s", self._address)
return None return None
+14 -9
View File
@@ -23,10 +23,14 @@ from homeassistant.helpers.update_coordinator import (
) )
from bluetti_mqtt.bluetooth import build_device from bluetti_mqtt.bluetooth import build_device
from bluetti_mqtt.mqtt_client import NORMAL_DEVICE_FIELDS, MqttFieldType from bluetti_mqtt.mqtt_client import (
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 DOMAIN, CONF_OPTIONS from .const import DOMAIN, CONF_OPTIONS, DIAGNOSTIC_FIELDS, ADDITIONAL_DEVICE_FIELDS
from .coordinator import PollingCoordinator from .coordinator import PollingCoordinator
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@@ -50,10 +54,13 @@ async def async_setup_entry(
bluetti_device = build_device(address, device_name) bluetti_device = build_device(address, device_name)
sensors_to_add = [] sensors_to_add = []
for field_key, field_config in NORMAL_DEVICE_FIELDS.items(): all_fields = NORMAL_DEVICE_FIELDS
all_fields.update(DC_INPUT_FIELDS)
all_fields.update(ADDITIONAL_DEVICE_FIELDS)
for field_key, field_config in all_fields.items():
if bluetti_device.has_field(field_key): if bluetti_device.has_field(field_key):
category = None category = None
if field_config.setter is True: 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 == MqttFieldType.NUMERIC:
sensors_to_add.append( sensors_to_add.append(
@@ -63,11 +70,9 @@ async def async_setup_entry(
address, address,
field_key, field_key,
field_config.home_assistant_extra.get(CONF_NAME, ""), field_config.home_assistant_extra.get(CONF_NAME, ""),
field_config.home_assistant_extra.get( field_config.home_assistant_extra.get(CONF_UNIT_OF_MEASUREMENT),
CONF_UNIT_OF_MEASUREMENT, "" field_config.home_assistant_extra.get(CONF_DEVICE_CLASS),
), field_config.home_assistant_extra.get(CONF_STATE_CLASS),
field_config.home_assistant_extra.get(CONF_DEVICE_CLASS, ""),
field_config.home_assistant_extra.get(CONF_STATE_CLASS, ""),
category=category, category=category,
) )
) )