Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4c286324c7 | ||
|
|
6361e661c0 | ||
|
|
0d237f2e45 | ||
|
|
95f2331cc3 | ||
|
|
61bc379d3a | ||
|
|
dd7665f069 | ||
|
|
8fce8270b2 | ||
|
|
886ab957bb | ||
|
|
5a6dbb8d00 | ||
|
|
d54bf7c526 | ||
|
|
22c2f0c7d0 | ||
|
|
23dc2b7f41 | ||
|
|
84a742968a | ||
|
|
ed818070c2 | ||
|
|
beb4e6b8fb | ||
|
|
7444097c60 | ||
|
|
0912fcbc49 | ||
|
|
80b03fc1d9 | ||
|
|
92c130b2d6 | ||
|
|
f8e382347e | ||
|
|
bab008b89c | ||
|
|
811501b382 | ||
|
|
90f66c8548 | ||
|
|
924eaa90b6 | ||
|
|
4f95fed85a | ||
|
|
332cf6dc28 | ||
|
|
cc7891ed6a | ||
|
|
12cd2719d2 | ||
|
|
e1d4446fb8 |
No files matched your search
@@ -3,6 +3,7 @@
|
|||||||
[](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
|
||||||
|
|
||||||
@@ -22,3 +23,8 @@ 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.
|
||||||
|
|
||||||
|
### Available controls:
|
||||||
|
If enabled in the Integration options (you need to reload the integration if you change this option):
|
||||||
|
AC and DC outputs
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
import logging
|
||||||
|
|
||||||
from homeassistant.components import bluetooth
|
from homeassistant.components import bluetooth
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
@@ -11,10 +12,11 @@ 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 DOMAIN, MANUFACTURER
|
from .const import DATA_COORDINATOR, DOMAIN, MANUFACTURER, CONF_USE_CONTROLS, DATA_POLLING_RUNNING
|
||||||
from .coordinator import PollingCoordinator
|
from .coordinator import PollingCoordinator
|
||||||
|
|
||||||
PLATFORMS: [Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
PLATFORMS: [Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
@@ -22,6 +24,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
|
|
||||||
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)
|
||||||
|
|
||||||
if address is None:
|
if address is None:
|
||||||
return False
|
return False
|
||||||
@@ -29,16 +32,23 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
if not bluetooth.async_address_present(hass, address):
|
if not bluetooth.async_address_present(hass, address):
|
||||||
raise ConfigEntryNotReady("Bluetti device not present")
|
raise ConfigEntryNotReady("Bluetti device not present")
|
||||||
|
|
||||||
|
# Create data structure
|
||||||
|
hass.data.setdefault(DOMAIN, {})
|
||||||
|
hass.data[DOMAIN].setdefault(entry.entry_id, {})
|
||||||
|
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)
|
coordinator = PollingCoordinator(hass, address, device_name)
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
|
hass.data[DOMAIN][entry.entry_id].setdefault(DATA_COORDINATOR, coordinator)
|
||||||
|
|
||||||
# Create data structure
|
platforms: list = PLATFORMS
|
||||||
hass.data.setdefault(DOMAIN, {})
|
if use_controls is True:
|
||||||
hass.data[DOMAIN].setdefault(entry.entry_id, coordinator)
|
_LOGGER.warning("You are using controls with this integration at your own risk!")
|
||||||
|
platforms.append(Platform.SWITCH)
|
||||||
|
|
||||||
# Setup platforms
|
# Setup platforms
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, platforms)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.components import bluetooth
|
|
||||||
from homeassistant.components.binary_sensor import BinarySensorEntity
|
from homeassistant.components.binary_sensor import BinarySensorEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant, callback
|
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.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 DATA_COORDINATOR, DOMAIN, ADDITIONAL_DEVICE_FIELDS
|
||||||
from .coordinator import PollingCoordinator
|
from .coordinator import PollingCoordinator, DummyDevice
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -45,9 +48,13 @@ 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 = []
|
||||||
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
|
||||||
@@ -56,7 +63,7 @@ async def async_setup_entry(
|
|||||||
|
|
||||||
sensors_to_add.append(
|
sensors_to_add.append(
|
||||||
BluettiBinarySensor(
|
BluettiBinarySensor(
|
||||||
hass.data[DOMAIN][entry.entry_id],
|
hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR],
|
||||||
device_info,
|
device_info,
|
||||||
address,
|
address,
|
||||||
field_key,
|
field_key,
|
||||||
@@ -89,15 +96,10 @@ class BluettiBinarySensor(CoordinatorEntity, BinarySensorEntity):
|
|||||||
|
|
||||||
self._attr_device_info = device_info
|
self._attr_device_info = device_info
|
||||||
self._attr_name = e_name
|
self._attr_name = e_name
|
||||||
|
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
|
||||||
|
|
||||||
@property
|
|
||||||
def available(self) -> bool:
|
|
||||||
if self._address is None:
|
|
||||||
return False
|
|
||||||
return bluetooth.async_address_present(self.hass, self._address)
|
|
||||||
|
|
||||||
@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."""
|
||||||
@@ -106,10 +108,12 @@ class BluettiBinarySensor(CoordinatorEntity, BinarySensorEntity):
|
|||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
"Invalid data from coordinator (sensor.%s)", self._attr_unique_id
|
"Invalid data from coordinator (sensor.%s)", self._attr_unique_id
|
||||||
)
|
)
|
||||||
|
self._attr_available = False
|
||||||
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
|
||||||
return
|
return
|
||||||
|
|
||||||
if not isinstance(response_data, bool):
|
if not isinstance(response_data, bool):
|
||||||
@@ -118,7 +122,9 @@ class BluettiBinarySensor(CoordinatorEntity, BinarySensorEntity):
|
|||||||
self._attr_unique_id,
|
self._attr_unique_id,
|
||||||
response_data,
|
response_data,
|
||||||
)
|
)
|
||||||
|
self._attr_available = False
|
||||||
return
|
return
|
||||||
|
|
||||||
|
self._attr_available = True
|
||||||
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()
|
||||||
@@ -14,9 +14,10 @@ from homeassistant.components.bluetooth import (
|
|||||||
)
|
)
|
||||||
from homeassistant.const import CONF_ADDRESS, CONF_TYPE, CONF_NAME
|
from homeassistant.const import CONF_ADDRESS, CONF_TYPE, CONF_NAME
|
||||||
from homeassistant.data_entry_flow import FlowResult
|
from homeassistant.data_entry_flow import FlowResult
|
||||||
|
from homeassistant.helpers import selector
|
||||||
|
|
||||||
from . import get_type_by_bt_name
|
from . import get_type_by_bt_name
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN, CONF_USE_CONTROLS
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -86,3 +87,56 @@ class BluettiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
step_id="user",
|
step_id="user",
|
||||||
data_schema=data_schema,
|
data_schema=data_schema,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def async_get_options_flow(
|
||||||
|
config_entry: config_entries.ConfigEntry,
|
||||||
|
) -> config_entries.OptionsFlow:
|
||||||
|
"""Create the options flow."""
|
||||||
|
return OptionsFlowHandler(config_entry)
|
||||||
|
|
||||||
|
|
||||||
|
class OptionsFlowHandler(config_entries.OptionsFlow):
|
||||||
|
"""Handle a option flow."""
|
||||||
|
|
||||||
|
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
|
||||||
|
"""Initialize options flow."""
|
||||||
|
self.config_entry = config_entry
|
||||||
|
|
||||||
|
async def async_step_init(
|
||||||
|
self, user_input: dict[str, Any] | None = None
|
||||||
|
) -> FlowResult:
|
||||||
|
"""Manage the options."""
|
||||||
|
if user_input is not None:
|
||||||
|
changed = self.hass.config_entries.async_update_entry(
|
||||||
|
self.config_entry,
|
||||||
|
data={
|
||||||
|
**self.config_entry.data,
|
||||||
|
**{
|
||||||
|
CONF_USE_CONTROLS: user_input[CONF_USE_CONTROLS],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
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(
|
||||||
|
title="",
|
||||||
|
data={
|
||||||
|
CONF_USE_CONTROLS: user_input[CONF_USE_CONTROLS],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
return self.async_show_form(
|
||||||
|
step_id="init",
|
||||||
|
data_schema=vol.Schema(
|
||||||
|
{
|
||||||
|
vol.Required(
|
||||||
|
CONF_USE_CONTROLS,
|
||||||
|
default=self.config_entry.data.get(CONF_USE_CONTROLS, False),
|
||||||
|
): selector.BooleanSelector(),
|
||||||
|
}
|
||||||
|
),
|
||||||
|
)
|
||||||
@@ -1,6 +1,460 @@
|
|||||||
"""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"
|
||||||
|
|
||||||
|
DATA_COORDINATOR = "coordinator"
|
||||||
|
DATA_POLLING_RUNNING = "polling_running"
|
||||||
|
|
||||||
|
CONTROL_FIELDS = [
|
||||||
|
"ac_output_on",
|
||||||
|
"dc_output_on",
|
||||||
|
]
|
||||||
|
|
||||||
|
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_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,11 +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
|
import asyncio
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
from typing import cast
|
|
||||||
import async_timeout
|
import async_timeout
|
||||||
|
|
||||||
from bleak import BleakClient, BleakError
|
from bleak import BleakClient, BleakError
|
||||||
@@ -23,10 +23,109 @@ 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
|
||||||
|
|
||||||
|
from .const import DATA_POLLING_RUNNING, DOMAIN
|
||||||
|
|
||||||
_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."""
|
||||||
|
|
||||||
@@ -42,7 +141,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.
|
||||||
@@ -50,6 +152,8 @@ 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")
|
self.logger.debug("Polling data")
|
||||||
|
|
||||||
device = bluetooth.async_ble_device_from_address(self.hass, self._address)
|
device = bluetooth.async_ble_device_from_address(self.hass, self._address)
|
||||||
@@ -103,28 +207,32 @@ class PollingCoordinator(DataUpdateCoordinator):
|
|||||||
parsed_data.update(parsed)
|
parsed_data.update(parsed)
|
||||||
|
|
||||||
except TimeoutError:
|
except TimeoutError:
|
||||||
self.logger.error(
|
self.logger.warning(
|
||||||
"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 (This can also be the case if you used device controls)", err
|
||||||
|
)
|
||||||
except TimeoutError:
|
except TimeoutError:
|
||||||
self.logger.error("Polling timed out for device %s", self._address)
|
self.logger.warning("Polling timed out for device %s", self._address)
|
||||||
return None
|
return None
|
||||||
except BleakError as err:
|
except BleakError as err:
|
||||||
self.logger.error("Bleak error: %s", err)
|
self.logger.warning("Bleak error: %s", err)
|
||||||
return None
|
return None
|
||||||
finally:
|
finally:
|
||||||
await client.disconnect()
|
await client.disconnect()
|
||||||
|
|
||||||
|
self.hass.data[DOMAIN][self.config_entry.entry_id][DATA_POLLING_RUNNING] = False
|
||||||
|
|
||||||
# Pass data back to sensors
|
# Pass data back to sensors
|
||||||
return parsed_data
|
return parsed_data
|
||||||
|
|
||||||
|
|||||||
@@ -18,5 +18,5 @@
|
|||||||
"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": ["bluetti_mqtt==0.15.0"],
|
||||||
"version": "0.0.2"
|
"version": "0.0.8"
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
"""Bluetti BT sensors."""
|
"""Bluetti BT sensors."""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
import logging
|
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.components.sensor import SensorEntity, CONF_STATE_CLASS
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
@@ -23,11 +23,15 @@ 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 DATA_COORDINATOR, DOMAIN, CONF_OPTIONS, DIAGNOSTIC_FIELDS, ADDITIONAL_DEVICE_FIELDS
|
||||||
from .coordinator import PollingCoordinator
|
from .coordinator import PollingCoordinator, DummyDevice
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -48,33 +52,35 @@ 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 = []
|
||||||
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(
|
||||||
BluettiSensor(
|
BluettiSensor(
|
||||||
hass.data[DOMAIN][entry.entry_id],
|
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.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,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
elif field_config.type == MqttFieldType.ENUM:
|
elif field_config.type == MqttFieldType.ENUM:
|
||||||
sensors_to_add.append(
|
sensors_to_add.append(
|
||||||
BluettiSensor(
|
BluettiSensor(
|
||||||
hass.data[DOMAIN][entry.entry_id],
|
hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR],
|
||||||
device_info,
|
device_info,
|
||||||
address,
|
address,
|
||||||
field_key,
|
field_key,
|
||||||
@@ -112,6 +118,7 @@ class BluettiSensor(CoordinatorEntity, SensorEntity):
|
|||||||
|
|
||||||
self._attr_device_info = device_info
|
self._attr_device_info = device_info
|
||||||
self._attr_name = e_name
|
self._attr_name = e_name
|
||||||
|
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
|
||||||
self._attr_device_class = device_class
|
self._attr_device_class = device_class
|
||||||
@@ -119,12 +126,6 @@ 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:
|
|
||||||
if self._address is None:
|
|
||||||
return False
|
|
||||||
return bluetooth.async_address_present(self.hass, self._address)
|
|
||||||
|
|
||||||
@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."""
|
||||||
@@ -133,10 +134,12 @@ class BluettiSensor(CoordinatorEntity, SensorEntity):
|
|||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
"Invalid data from coordinator (sensor.%s)", self._attr_unique_id
|
"Invalid data from coordinator (sensor.%s)", self._attr_unique_id
|
||||||
)
|
)
|
||||||
|
self._attr_available = False
|
||||||
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
|
||||||
return
|
return
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@@ -144,6 +147,7 @@ class BluettiSensor(CoordinatorEntity, SensorEntity):
|
|||||||
and not isinstance(response_data, float)
|
and not isinstance(response_data, float)
|
||||||
and not isinstance(response_data, complex)
|
and not isinstance(response_data, complex)
|
||||||
and not isinstance(response_data, Decimal)
|
and not isinstance(response_data, Decimal)
|
||||||
|
and not isinstance(response_data, Enum)
|
||||||
):
|
):
|
||||||
_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",
|
||||||
@@ -151,16 +155,19 @@ class BluettiSensor(CoordinatorEntity, SensorEntity):
|
|||||||
response_data,
|
response_data,
|
||||||
type(response_data),
|
type(response_data),
|
||||||
)
|
)
|
||||||
|
self._attr_available = False
|
||||||
return
|
return
|
||||||
|
|
||||||
|
self._attr_available = True
|
||||||
|
|
||||||
# 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, int)
|
and isinstance(response_data, Enum)
|
||||||
and response_data < len(self._options)
|
and response_data.value < len(self._options)
|
||||||
):
|
):
|
||||||
# Enum
|
# Enum
|
||||||
self._attr_native_value = self._options[response_data]
|
self._attr_native_value = self._options[response_data.value]
|
||||||
else:
|
else:
|
||||||
# Numeric
|
# Numeric
|
||||||
self._attr_native_value = response_data
|
self._attr_native_value = response_data
|
||||||
|
|||||||
@@ -0,0 +1,188 @@
|
|||||||
|
"""Bluetti BT switches."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
import async_timeout
|
||||||
|
|
||||||
|
from bleak import BleakClient, BleakError
|
||||||
|
|
||||||
|
from homeassistant.components import bluetooth
|
||||||
|
from homeassistant.components.switch import SwitchEntity, SwitchDeviceClass
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.core import HomeAssistant, callback
|
||||||
|
from homeassistant.const import (
|
||||||
|
CONF_ADDRESS,
|
||||||
|
CONF_NAME,
|
||||||
|
EntityCategory,
|
||||||
|
)
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
|
from homeassistant.helpers.update_coordinator import (
|
||||||
|
CoordinatorEntity,
|
||||||
|
)
|
||||||
|
|
||||||
|
from bluetti_mqtt.bluetooth import build_device
|
||||||
|
from bluetti_mqtt.bluetooth.client import BluetoothClient
|
||||||
|
from bluetti_mqtt.core.devices.bluetti_device import BluettiDevice
|
||||||
|
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 CONTROL_FIELDS, DATA_COORDINATOR, DATA_POLLING_RUNNING, DOMAIN, ADDITIONAL_DEVICE_FIELDS
|
||||||
|
from .coordinator import PollingCoordinator, DummyDevice
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
|
) -> None:
|
||||||
|
"""Setup switch entities."""
|
||||||
|
|
||||||
|
device_name = entry.data.get(CONF_NAME)
|
||||||
|
address = entry.data.get(CONF_ADDRESS)
|
||||||
|
if address is None:
|
||||||
|
_LOGGER.error("Device has no address")
|
||||||
|
|
||||||
|
# Generate device info
|
||||||
|
_LOGGER.info("Creating sensors for device with address %s", address)
|
||||||
|
device_info = dev_info(entry)
|
||||||
|
|
||||||
|
# Add sensors according to device_info
|
||||||
|
bluetti_device = build_device(address, device_name)
|
||||||
|
bluetti_device = DummyDevice(bluetti_device)
|
||||||
|
|
||||||
|
sensors_to_add = []
|
||||||
|
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:
|
||||||
|
if field_config.setter is True and field_key in CONTROL_FIELDS:
|
||||||
|
sensors_to_add.append(
|
||||||
|
BluettiSwitch(
|
||||||
|
bluetti_device,
|
||||||
|
hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR],
|
||||||
|
device_info,
|
||||||
|
address,
|
||||||
|
field_key,
|
||||||
|
field_config.home_assistant_extra.get(CONF_NAME, ""),
|
||||||
|
entry.entry_id
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
async_add_entities(sensors_to_add)
|
||||||
|
|
||||||
|
|
||||||
|
class BluettiSwitch(CoordinatorEntity, SwitchEntity):
|
||||||
|
"""Bluetti universal switch."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
bluetti_device: BluettiDevice,
|
||||||
|
coordinator: PollingCoordinator,
|
||||||
|
device_info: DeviceInfo,
|
||||||
|
address,
|
||||||
|
response_key: str,
|
||||||
|
name: str,
|
||||||
|
entry_id: str,
|
||||||
|
category: EntityCategory | None = None,
|
||||||
|
):
|
||||||
|
"""Init entity."""
|
||||||
|
super().__init__(coordinator)
|
||||||
|
|
||||||
|
self._bluetti_device = bluetti_device
|
||||||
|
e_name = f"{device_info.get('name')} {name}"
|
||||||
|
self._address = address
|
||||||
|
self._response_key = response_key
|
||||||
|
self._entry_id = entry_id
|
||||||
|
|
||||||
|
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
|
||||||
|
self._attr_device_class = SwitchDeviceClass.OUTLET
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _handle_coordinator_update(self) -> None:
|
||||||
|
"""Handle updated data from the coordinator."""
|
||||||
|
_LOGGER.debug("Updating state of %s", self._attr_unique_id)
|
||||||
|
if not isinstance(self.coordinator.data, dict):
|
||||||
|
_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):
|
||||||
|
_LOGGER.warning(
|
||||||
|
"Invalid response data type from coordinator (sensor.%s): %s",
|
||||||
|
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()
|
||||||
|
|
||||||
|
async def async_turn_on(self, **kwargs):
|
||||||
|
"""Turn the entity on."""
|
||||||
|
_LOGGER.debug("Turn on %s on %s", self._response_key, self._address)
|
||||||
|
await self.write_to_device(True)
|
||||||
|
|
||||||
|
async def async_turn_off(self, **kwargs):
|
||||||
|
"""Turn the entity off."""
|
||||||
|
_LOGGER.debug("Turn off %s on %s", self._response_key, self._address)
|
||||||
|
await self.write_to_device(False)
|
||||||
|
|
||||||
|
async def write_to_device(self, state: bool):
|
||||||
|
"""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)
|
||||||
|
|
||||||
|
# Wait until polling is done
|
||||||
|
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:
|
||||||
|
async with async_timeout.timeout(15):
|
||||||
|
await client.connect()
|
||||||
|
|
||||||
|
# Send command
|
||||||
|
await client.write_gatt_char(
|
||||||
|
BluetoothClient.WRITE_UUID, bytes(command)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Wait until device has changed value, otherwise reading register might reset it
|
||||||
|
await asyncio.sleep(5)
|
||||||
|
|
||||||
|
except TimeoutError:
|
||||||
|
_LOGGER.error("Timed out for device %s", self._address)
|
||||||
|
return None
|
||||||
|
except BleakError as err:
|
||||||
|
_LOGGER.error("Bleak error: %s", err)
|
||||||
|
return None
|
||||||
|
finally:
|
||||||
|
await client.disconnect()
|
||||||
|
|
||||||
|
await self.coordinator.async_request_refresh()
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"options": {
|
||||||
|
"step": {
|
||||||
|
"init": {
|
||||||
|
"data": {
|
||||||
|
"use_controls": "Steuerung aktivieren (auf eigenes Risiko)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"options": {
|
||||||
|
"step": {
|
||||||
|
"init": {
|
||||||
|
"data": {
|
||||||
|
"use_controls": "Use controls (use at own risk)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user