Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0912fcbc49 | ||
|
|
80b03fc1d9 | ||
|
|
92c130b2d6 | ||
|
|
f8e382347e | ||
|
|
bab008b89c | ||
|
|
811501b382 | ||
|
|
90f66c8548 | ||
|
|
924eaa90b6 | ||
|
|
4f95fed85a | ||
|
|
332cf6dc28 | ||
|
|
cc7891ed6a | ||
|
|
12cd2719d2 | ||
|
|
e1d4446fb8 |
No files matched your search
@@ -22,3 +22,4 @@ Based on [bluetti_mqtt](https://github.com/warhammerkid/bluetti_mqtt).
|
||||
|
||||
### 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))
|
||||
excluding battery pack fields.
|
||||
@@ -4,7 +4,6 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from homeassistant.components import bluetooth
|
||||
from homeassistant.components.binary_sensor import BinarySensorEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
@@ -20,11 +19,15 @@ from homeassistant.helpers.update_coordinator import (
|
||||
)
|
||||
|
||||
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 .const import DOMAIN
|
||||
from .coordinator import PollingCoordinator
|
||||
from .const import DOMAIN, ADDITIONAL_DEVICE_FIELDS
|
||||
from .coordinator import PollingCoordinator, DummyDevice
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -45,9 +48,13 @@ async def async_setup_entry(
|
||||
|
||||
# Add sensors according to device_info
|
||||
bluetti_device = build_device(address, device_name)
|
||||
bluetti_device = DummyDevice(bluetti_device)
|
||||
|
||||
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 field_config.type == MqttFieldType.BOOL:
|
||||
category = None
|
||||
@@ -89,15 +96,10 @@ class BluettiBinarySensor(CoordinatorEntity, BinarySensorEntity):
|
||||
|
||||
self._attr_device_info = device_info
|
||||
self._attr_name = e_name
|
||||
self._attr_available = False
|
||||
self._attr_unique_id = get_unique_id(e_name)
|
||||
self._attr_entity_category = category
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
if self._address is None:
|
||||
return False
|
||||
return bluetooth.async_address_present(self.hass, self._address)
|
||||
|
||||
@callback
|
||||
def _handle_coordinator_update(self) -> None:
|
||||
"""Handle updated data from the coordinator."""
|
||||
@@ -106,10 +108,12 @@ class BluettiBinarySensor(CoordinatorEntity, BinarySensorEntity):
|
||||
_LOGGER.error(
|
||||
"Invalid data from coordinator (sensor.%s)", self._attr_unique_id
|
||||
)
|
||||
self._attr_available = False
|
||||
return
|
||||
|
||||
response_data = self.coordinator.data.get(self._response_key)
|
||||
if response_data is None:
|
||||
self._attr_available = False
|
||||
return
|
||||
|
||||
if not isinstance(response_data, bool):
|
||||
@@ -118,7 +122,9 @@ class BluettiBinarySensor(CoordinatorEntity, BinarySensorEntity):
|
||||
self._attr_unique_id,
|
||||
response_data,
|
||||
)
|
||||
self._attr_available = False
|
||||
return
|
||||
|
||||
self._attr_available = True
|
||||
self._attr_is_on = self.coordinator.data[self._response_key] is True
|
||||
self.async_write_ha_state()
|
||||
@@ -1,6 +1,107 @@
|
||||
"""Constants for the Bluetti BT integration."""
|
||||
|
||||
from bluetti_mqtt.mqtt_client import MqttFieldConfig, MqttFieldType
|
||||
|
||||
DOMAIN = "bluetti_bt"
|
||||
MANUFACTURER = "Bluetti"
|
||||
|
||||
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"},
|
||||
),
|
||||
"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,
|
||||
},
|
||||
),
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
"""Coordinator for Bluetti integration."""
|
||||
|
||||
from __future__ import annotations
|
||||
from typing import List
|
||||
|
||||
import asyncio
|
||||
from datetime import timedelta
|
||||
@@ -23,10 +24,64 @@ from bluetti_mqtt.bluetooth import (
|
||||
ParseError,
|
||||
build_device,
|
||||
)
|
||||
from bluetti_mqtt.core.devices.bluetti_device import BluettiDevice
|
||||
from bluetti_mqtt.core.commands import ReadHoldingRegisters
|
||||
|
||||
_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":
|
||||
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)
|
||||
|
||||
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(1229, 19),
|
||||
ReadHoldingRegisters(2022, 6),
|
||||
ReadHoldingRegisters(2213, 4),
|
||||
# Battery
|
||||
ReadHoldingRegisters(6101, 7),
|
||||
ReadHoldingRegisters(6175, 11),
|
||||
# current limit for reading ReadHoldingRegisters(0x2F2F, 1),
|
||||
]
|
||||
|
||||
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):
|
||||
"""Polling coordinator."""
|
||||
|
||||
@@ -42,7 +97,10 @@ class PollingCoordinator(DataUpdateCoordinator):
|
||||
self.notify_future = None
|
||||
self.current_command = None
|
||||
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):
|
||||
"""Fetch data from API endpoint.
|
||||
@@ -107,15 +165,17 @@ class PollingCoordinator(DataUpdateCoordinator):
|
||||
"Polling timed out (address: %s)", self._address
|
||||
)
|
||||
except ParseError:
|
||||
self.logger.debug("Got a parse exception...")
|
||||
self.logger.warning("Got a parse exception...")
|
||||
except ModbusError as err:
|
||||
self.logger.debug(
|
||||
self.logger.warning(
|
||||
"Got an invalid request error for %s: %s",
|
||||
command,
|
||||
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:
|
||||
self.logger.error("Polling timed out for device %s", self._address)
|
||||
return None
|
||||
|
||||
@@ -3,9 +3,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from decimal import *
|
||||
from decimal import Decimal
|
||||
|
||||
from homeassistant.components import bluetooth
|
||||
from homeassistant.components.sensor import SensorEntity, CONF_STATE_CLASS
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
@@ -23,11 +22,15 @@ from homeassistant.helpers.update_coordinator import (
|
||||
)
|
||||
|
||||
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 .const import DOMAIN, CONF_OPTIONS
|
||||
from .coordinator import PollingCoordinator
|
||||
from .const import DOMAIN, CONF_OPTIONS, DIAGNOSTIC_FIELDS, ADDITIONAL_DEVICE_FIELDS
|
||||
from .coordinator import PollingCoordinator, DummyDevice
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -48,12 +51,16 @@ async def async_setup_entry(
|
||||
|
||||
# Add sensors according to device_info
|
||||
bluetti_device = build_device(address, device_name)
|
||||
bluetti_device = DummyDevice(bluetti_device)
|
||||
|
||||
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):
|
||||
category = None
|
||||
if field_config.setter is True:
|
||||
if field_config.setter is True or field_key in DIAGNOSTIC_FIELDS:
|
||||
category = EntityCategory.DIAGNOSTIC
|
||||
if field_config.type == MqttFieldType.NUMERIC:
|
||||
sensors_to_add.append(
|
||||
@@ -63,11 +70,9 @@ async def async_setup_entry(
|
||||
address,
|
||||
field_key,
|
||||
field_config.home_assistant_extra.get(CONF_NAME, ""),
|
||||
field_config.home_assistant_extra.get(
|
||||
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_UNIT_OF_MEASUREMENT),
|
||||
field_config.home_assistant_extra.get(CONF_DEVICE_CLASS),
|
||||
field_config.home_assistant_extra.get(CONF_STATE_CLASS),
|
||||
category=category,
|
||||
)
|
||||
)
|
||||
@@ -112,6 +117,7 @@ class BluettiSensor(CoordinatorEntity, SensorEntity):
|
||||
|
||||
self._attr_device_info = device_info
|
||||
self._attr_name = e_name
|
||||
self._attr_available = False
|
||||
self._attr_unique_id = get_unique_id(e_name)
|
||||
self._attr_native_unit_of_measurement = unit_of_measurement
|
||||
self._attr_device_class = device_class
|
||||
@@ -119,12 +125,6 @@ class BluettiSensor(CoordinatorEntity, SensorEntity):
|
||||
self._attr_entity_category = category
|
||||
self._options = options
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
if self._address is None:
|
||||
return False
|
||||
return bluetooth.async_address_present(self.hass, self._address)
|
||||
|
||||
@callback
|
||||
def _handle_coordinator_update(self) -> None:
|
||||
"""Handle updated data from the coordinator."""
|
||||
@@ -133,10 +133,12 @@ class BluettiSensor(CoordinatorEntity, SensorEntity):
|
||||
_LOGGER.error(
|
||||
"Invalid data from coordinator (sensor.%s)", self._attr_unique_id
|
||||
)
|
||||
self._attr_available = False
|
||||
return
|
||||
|
||||
response_data = self.coordinator.data.get(self._response_key)
|
||||
if response_data is None:
|
||||
self._attr_available = False
|
||||
return
|
||||
|
||||
if (
|
||||
@@ -151,8 +153,11 @@ class BluettiSensor(CoordinatorEntity, SensorEntity):
|
||||
response_data,
|
||||
type(response_data),
|
||||
)
|
||||
self._attr_available = False
|
||||
return
|
||||
|
||||
self._attr_available = True
|
||||
|
||||
# Different for enum and numeric
|
||||
if (
|
||||
self._options is not None
|
||||
|
||||
Reference in new issue
Block a user