From 2e78f436e9065cfb76da6645bc2dfab03a9a3f4e Mon Sep 17 00:00:00 2001 From: pink88 Date: Wed, 8 Nov 2023 21:16:21 +0000 Subject: [PATCH] Added initial skeleton for battery sensor --- custom_components/tuiss2ha/hub.py | 48 +++++++++++++++++++++++++--- custom_components/tuiss2ha/sensor.py | 26 +++++++++------ 2 files changed, 60 insertions(+), 14 deletions(-) diff --git a/custom_components/tuiss2ha/hub.py b/custom_components/tuiss2ha/hub.py index 5c0058d..84f567f 100644 --- a/custom_components/tuiss2ha/hub.py +++ b/custom_components/tuiss2ha/hub.py @@ -144,13 +144,13 @@ def hex_convert(self, userPercent): # Send the data async def send_command(self, UUID, command): - _LOGGER.info("%s connected state is %s",self._ble_device,self._client.is_connected) + _LOGGER.info("%s (%s) connected state is %s",self.name, self._ble_device,self._client.is_connected) if self._client.is_connected: try: - _LOGGER.info("Sending the command") + _LOGGER.info("%s: Sending the command",self.name) await self._client.write_gatt_char(UUID, command) except Exception as e: - _LOGGER.error(("Send Command error: %s",e)) + _LOGGER.error(("%s: Send Command error: %s",self.name,e)) finally: await self.blind_disconnect() @@ -169,10 +169,50 @@ def remove_callback(self, callback: Callable[[], None]) -> None: # Set the position and send to be run async def set_position(self, userPercent) -> None: UUID = "00010405-0405-0607-0809-0a0b0c0d1910" - _LOGGER.info("Attempting to set position to: %s", userPercent) + _LOGGER.info("%s: Attempting to set position to: %s", self.name,userPercent) command = bytes.fromhex(self.hex_convert(userPercent)) await self.send_command(UUID, command) + + # Get information on the battery status good or needs to be charged + async def get_battery(self) -> None: + UUID = "00010405-0405-0607-0809-0a0b0c0d1910" + command = bytes.fromhex("ff78ea41f00301") + await client.start_notify(17,battery_callback) + await self.send_command(UUID, command) + while self._client.is_connected: + await asyncio.sleep(1) + + + # Waits and handles the response code from the battery and records to sensor + async def battery_callback(sender: BleakGATTCharacteristic, data: bytearray): + + _LOGGER.info("%s: Attempting to get battery status", self.name) + + customdecode = str(data) + customdecodesplit = customdecode.split('\\x') + response = '' + decimals = [] + + x = 1 + while x < len(customdecodesplit): + resp = customdecodesplit[x][0:2] + response += resp + decimals.append(int(resp,16)) + x+=1 + + _LOGGER.info("As byte:%s", data) + _LOGGER.info("As string:%s", response) + _LOGGER.info("As decimals:%s", decimals) + + if decimals[4] == 210: + if decimals[5] == 3: + _LOGGER.info("%s: Battery is good",self.name) + elif decimals[5] == 11: + _LOGGER.info("%s: Please charge device",self.name) + await blind_disconnect() + + # async def stop(self): # UUID = "00010405-0405-0607-0809-0a0b0c0d1910" # command = bytes.fromhex("ff78ea415f0301") diff --git a/custom_components/tuiss2ha/sensor.py b/custom_components/tuiss2ha/sensor.py index e464efb..9f970f8 100644 --- a/custom_components/tuiss2ha/sensor.py +++ b/custom_components/tuiss2ha/sensor.py @@ -10,23 +10,29 @@ ) from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( - PERCENTAGE, EntityCategory, ) from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from .const import DOMAIN -from .coordinator import SwitchbotDataUpdateCoordinator -from .entity import SwitchbotEntity PARALLEL_UPDATES = 0 +SENSOR_TYPES= { +"battery_description": [ + "Battery Status Description", + None, + "mdi:battery", + None, + None,] +} -# "battery": SensorEntityDescription( - # key="battery", - # native_unit_of_measurement=PERCENTAGE, - # device_class=SensorDeviceClass.BATTERY, - # state_class=SensorStateClass.MEASUREMENT, - # entity_category=EntityCategory.DIAGNOSTIC, - # ) +async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry, async_add_entities) -> None: + """Set up Tuiss2ha Battery sensor""" + sensors = [] + for sensor in SENSOR_TYPES: + sensors.append() + async_add_entities(sensors, True) + +