added option to turn on and off outputs
This commit is contained in:
1 parent
8fce8270b2
commit
dd7665f069
6 files changed
+82
-18
No files matched your search
@@ -2,8 +2,13 @@
|
||||
|
||||
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
|
||||
@@ -19,6 +24,8 @@ from homeassistant.helpers.update_coordinator import (
|
||||
)
|
||||
|
||||
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,
|
||||
@@ -26,7 +33,7 @@ from bluetti_mqtt.mqtt_client import (
|
||||
)
|
||||
|
||||
from . import device_info as dev_info, get_unique_id
|
||||
from .const import CONTROL_FIELDS, DOMAIN, ADDITIONAL_DEVICE_FIELDS
|
||||
from .const import CONTROL_FIELDS, DATA_COORDINATOR, DATA_POLLING_RUNNING, DOMAIN, ADDITIONAL_DEVICE_FIELDS
|
||||
from .coordinator import PollingCoordinator, DummyDevice
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -60,11 +67,13 @@ async def async_setup_entry(
|
||||
if field_config.setter is True and field_key in CONTROL_FIELDS:
|
||||
sensors_to_add.append(
|
||||
BluettiSwitch(
|
||||
hass.data[DOMAIN][entry.entry_id],
|
||||
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
|
||||
)
|
||||
)
|
||||
|
||||
@@ -76,19 +85,23 @@ class BluettiSwitch(CoordinatorEntity, SwitchEntity):
|
||||
|
||||
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
|
||||
@@ -128,8 +141,48 @@ class BluettiSwitch(CoordinatorEntity, SwitchEntity):
|
||||
|
||||
async def async_turn_on(self, **kwargs):
|
||||
"""Turn the entity on."""
|
||||
_LOGGER.warning("Turn 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.warning("Turn 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()
|
||||
Reference in new issue
Block a user