Skip to content

Commit

Permalink
Added initial skeleton for battery sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
pink88 authored Nov 8, 2023
1 parent 494d229 commit 2e78f43
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
48 changes: 44 additions & 4 deletions custom_components/tuiss2ha/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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")
Expand Down
26 changes: 16 additions & 10 deletions custom_components/tuiss2ha/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


0 comments on commit 2e78f43

Please sign in to comment.