11 files changed
+130
-45
No files matched your search
@@ -12,7 +12,7 @@ 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_PERSISTENT_CONN, CONF_POLLING_INTERVAL, 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]
|
||||||
@@ -25,6 +25,13 @@ 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)
|
use_controls = entry.data.get(CONF_USE_CONTROLS)
|
||||||
|
polling_interval = entry.data.get(CONF_POLLING_INTERVAL, 20)
|
||||||
|
persistent_conn = entry.data.get(CONF_PERSISTENT_CONN, False)
|
||||||
|
|
||||||
|
# Prevent persistant connection and controls at same time
|
||||||
|
if use_controls is True and persistent_conn is True:
|
||||||
|
_LOGGER.warning("Enabling controls and persistant connection at the same time is currently not supported. Disabled persistant connection")
|
||||||
|
persistent_conn = False
|
||||||
|
|
||||||
if address is None:
|
if address is None:
|
||||||
return False
|
return False
|
||||||
@@ -38,7 +45,7 @@ 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)
|
coordinator = PollingCoordinator(hass, address, device_name, polling_interval, persistent_conn)
|
||||||
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)
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ from bluetti_mqtt.mqtt_client import (
|
|||||||
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, ADDITIONAL_DEVICE_FIELDS
|
||||||
from .coordinator import PollingCoordinator, DummyDevice
|
from .coordinator import PollingCoordinator, DummyDevice
|
||||||
|
from .utils import unique_id_loggable
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -35,7 +36,7 @@ _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)
|
||||||
@@ -43,7 +44,7 @@ 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 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
|
||||||
@@ -103,10 +104,10 @@ class BluettiBinarySensor(CoordinatorEntity, BinarySensorEntity):
|
|||||||
@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)
|
_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 (binary_sensor.%s)", unique_id_loggable(self._attr_unique_id)
|
||||||
)
|
)
|
||||||
self._attr_available = False
|
self._attr_available = False
|
||||||
return
|
return
|
||||||
@@ -118,8 +119,8 @@ class BluettiBinarySensor(CoordinatorEntity, BinarySensorEntity):
|
|||||||
|
|
||||||
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._attr_available = False
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ 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 . import get_type_by_bt_name
|
||||||
from .const import DOMAIN, CONF_USE_CONTROLS
|
from .const import CONF_PERSISTENT_CONN, CONF_POLLING_INTERVAL, DATA_COORDINATOR, DOMAIN, CONF_USE_CONTROLS
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -108,12 +108,19 @@ 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:
|
||||||
|
|
||||||
|
# Validate update interval
|
||||||
|
if user_input[CONF_POLLING_INTERVAL] < 5:
|
||||||
|
return self.async_abort(reason="invalid_interval")
|
||||||
|
|
||||||
changed = self.hass.config_entries.async_update_entry(
|
changed = 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],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@@ -126,6 +133,8 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
|
|||||||
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],
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -137,6 +146,14 @@ 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,
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
@@ -7,6 +7,8 @@ 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"
|
||||||
|
|
||||||
DATA_COORDINATOR = "coordinator"
|
DATA_COORDINATOR = "coordinator"
|
||||||
DATA_POLLING_RUNNING = "polling_running"
|
DATA_POLLING_RUNNING = "polling_running"
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ from bluetti_mqtt.core.devices.bluetti_device import BluettiDevice
|
|||||||
from bluetti_mqtt.core.commands import ReadHoldingRegisters
|
from bluetti_mqtt.core.commands import ReadHoldingRegisters
|
||||||
|
|
||||||
from .const import DATA_POLLING_RUNNING, DOMAIN
|
from .const import DATA_POLLING_RUNNING, DOMAIN
|
||||||
|
from .utils import mac_loggable
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -129,23 +130,39 @@ class DummyDevice(BluettiDevice):
|
|||||||
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,
|
||||||
|
):
|
||||||
"""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._address = address
|
||||||
|
self.has_notifier = False
|
||||||
self.notify_future = None
|
self.notify_future = None
|
||||||
self.current_command = None
|
self.current_command = None
|
||||||
self.notify_response = bytearray()
|
self.notify_response = bytearray()
|
||||||
bluetti_device = build_device(address, device_name)
|
bluetti_device = build_device(address, device_name)
|
||||||
|
self.persistent_conn = persistent_conn
|
||||||
|
|
||||||
# Add or modify device fields
|
# Add or modify device fields
|
||||||
self.bluetti_device = DummyDevice(bluetti_device)
|
self.bluetti_device = DummyDevice(bluetti_device)
|
||||||
|
|
||||||
|
# Create 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
|
||||||
|
self.client = BleakClient(device)
|
||||||
|
|
||||||
async def _async_update_data(self):
|
async def _async_update_data(self):
|
||||||
"""Fetch data from API endpoint.
|
"""Fetch data from API endpoint.
|
||||||
|
|
||||||
@@ -156,26 +173,41 @@ class PollingCoordinator(DataUpdateCoordinator):
|
|||||||
|
|
||||||
self.logger.debug("Polling data")
|
self.logger.debug("Polling data")
|
||||||
|
|
||||||
device = bluetooth.async_ble_device_from_address(self.hass, self._address)
|
|
||||||
if device is None:
|
|
||||||
self.logger.error("Device %s not available", self._address)
|
|
||||||
return None
|
|
||||||
|
|
||||||
if self.bluetti_device is None:
|
if self.bluetti_device is None:
|
||||||
self.logger.error("Device type for %s not found", self._address)
|
self.logger.error("Device type for %s not found", mac_loggable(self._address))
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Polling
|
|
||||||
client = BleakClient(device)
|
|
||||||
parsed_data: dict = {}
|
parsed_data: dict = {}
|
||||||
|
|
||||||
|
# Connect to device
|
||||||
|
try:
|
||||||
|
async with async_timeout.timeout(20):
|
||||||
|
await self.client.connect()
|
||||||
|
|
||||||
|
await self.client.start_notify(
|
||||||
|
BluetoothClient.NOTIFY_UUID, self._notification_handler
|
||||||
|
)
|
||||||
|
self.has_notifier = True
|
||||||
|
except TimeoutError:
|
||||||
|
self.logger.debug("Connection timed out for device %s", mac_loggable(self._address))
|
||||||
|
return None
|
||||||
|
except BleakError as err:
|
||||||
|
self.logger.debug("Bleak error: %s", err)
|
||||||
|
return None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with async_timeout.timeout(15):
|
async with async_timeout.timeout(15):
|
||||||
await client.connect()
|
|
||||||
|
|
||||||
await client.start_notify(
|
# Reconnect if not connected
|
||||||
BluetoothClient.NOTIFY_UUID, self._notification_handler
|
if not self.client.is_connected:
|
||||||
)
|
await self.client.connect()
|
||||||
|
|
||||||
|
# Attach notifier if needed
|
||||||
|
if not self.has_notifier:
|
||||||
|
await self.client.start_notify(
|
||||||
|
BluetoothClient.NOTIFY_UUID, self._notification_handler
|
||||||
|
)
|
||||||
|
self.has_notifier = True
|
||||||
|
|
||||||
for command in self.bluetti_device.polling_commands:
|
for command in self.bluetti_device.polling_commands:
|
||||||
try:
|
try:
|
||||||
@@ -186,7 +218,7 @@ class PollingCoordinator(DataUpdateCoordinator):
|
|||||||
|
|
||||||
# Make request
|
# Make request
|
||||||
self.logger.debug("Requesting %s", command)
|
self.logger.debug("Requesting %s", command)
|
||||||
await client.write_gatt_char(
|
await self.client.write_gatt_char(
|
||||||
BluetoothClient.WRITE_UUID, bytes(command)
|
BluetoothClient.WRITE_UUID, bytes(command)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -207,8 +239,8 @@ class PollingCoordinator(DataUpdateCoordinator):
|
|||||||
parsed_data.update(parsed)
|
parsed_data.update(parsed)
|
||||||
|
|
||||||
except TimeoutError:
|
except TimeoutError:
|
||||||
self.logger.warning(
|
self.logger.debug(
|
||||||
"Polling timed out (address: %s)", self._address
|
"Polling timed out (address: %s)", mac_loggable(self._address)
|
||||||
)
|
)
|
||||||
except ParseError:
|
except ParseError:
|
||||||
self.logger.warning("Got a parse exception...")
|
self.logger.warning("Got a parse exception...")
|
||||||
@@ -223,13 +255,15 @@ class PollingCoordinator(DataUpdateCoordinator):
|
|||||||
"Needed to disconnect due to error: %s (This can also be the case if you used device controls)", err
|
"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.warning("Polling timed out for device %s", self._address)
|
self.logger.debug("Polling timed out for device %s", mac_loggable(self._address))
|
||||||
return None
|
return None
|
||||||
except BleakError as err:
|
except BleakError as err:
|
||||||
self.logger.warning("Bleak error: %s", err)
|
self.logger.warning("Bleak error: %s", err)
|
||||||
return None
|
return None
|
||||||
finally:
|
finally:
|
||||||
await client.disconnect()
|
# Disconnect if connection not persistant
|
||||||
|
if not self.persistent_conn:
|
||||||
|
await self.client.disconnect()
|
||||||
|
|
||||||
self.hass.data[DOMAIN][self.config_entry.entry_id][DATA_POLLING_RUNNING] = False
|
self.hass.data[DOMAIN][self.config_entry.entry_id][DATA_POLLING_RUNNING] = False
|
||||||
|
|
||||||
|
|||||||
@@ -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.8"
|
"version": "0.0.10"
|
||||||
}
|
}
|
||||||
@@ -32,6 +32,7 @@ from bluetti_mqtt.mqtt_client import (
|
|||||||
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, CONF_OPTIONS, DIAGNOSTIC_FIELDS, ADDITIONAL_DEVICE_FIELDS
|
||||||
from .coordinator import PollingCoordinator, DummyDevice
|
from .coordinator import PollingCoordinator, DummyDevice
|
||||||
|
from .utils import unique_id_loggable
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -129,10 +130,10 @@ class BluettiSensor(CoordinatorEntity, SensorEntity):
|
|||||||
@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)
|
_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 (sensor.%s)", unique_id_loggable(self._attr_unique_id)
|
||||||
)
|
)
|
||||||
self._attr_available = False
|
self._attr_available = False
|
||||||
return
|
return
|
||||||
@@ -151,7 +152,7 @@ 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),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ from bluetti_mqtt.mqtt_client import (
|
|||||||
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, DATA_POLLING_RUNNING, DOMAIN, ADDITIONAL_DEVICE_FIELDS
|
||||||
from .coordinator import PollingCoordinator, DummyDevice
|
from .coordinator import PollingCoordinator, DummyDevice
|
||||||
|
from .utils import mac_loggable, unique_id_loggable
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -50,7 +51,7 @@ 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
|
||||||
@@ -113,10 +114,10 @@ class BluettiSwitch(CoordinatorEntity, SwitchEntity):
|
|||||||
@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)
|
_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.error(
|
||||||
"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
|
||||||
return
|
return
|
||||||
@@ -128,8 +129,8 @@ class BluettiSwitch(CoordinatorEntity, SwitchEntity):
|
|||||||
|
|
||||||
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
|
||||||
@@ -141,19 +142,19 @@ 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)
|
device = bluetooth.async_ble_device_from_address(self.hass, self._address)
|
||||||
if device is None:
|
if device is None:
|
||||||
_LOGGER.error("Device %s not available", self._address)
|
_LOGGER.error("Device %s not available", mac_loggable(self._address))
|
||||||
return
|
return
|
||||||
|
|
||||||
command = self._bluetti_device.build_setter_command(self._response_key, state)
|
command = self._bluetti_device.build_setter_command(self._response_key, state)
|
||||||
@@ -177,7 +178,7 @@ class BluettiSwitch(CoordinatorEntity, SwitchEntity):
|
|||||||
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)
|
||||||
|
|||||||
@@ -3,9 +3,14 @@
|
|||||||
"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)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"abort": {
|
||||||
|
"invalid_interval": "Ungültiger Datenabruf-Intervall. Verwende 5 Sekunden oder mehr"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,9 +3,14 @@
|
|||||||
"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)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"abort": {
|
||||||
|
"invalid_interval": "Invalid polling interval. Use 5 seconds or more"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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]
|
||||||
Reference in new issue
Block a user