Skip to content

Commit

Permalink
Merge pull request #8 from unregist/dev
Browse files Browse the repository at this point in the history
add function return_hex_bytearray to fix split_data function
  • Loading branch information
pink88 authored Feb 4, 2024
2 parents d2f7907 + a885b37 commit ad5597c
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions custom_components/tuiss2ha/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,17 @@ async def battery_callback(self, sender: BleakGATTCharacteristic, data: bytearra
decimals = self.split_data(data)

if decimals[4] == 210:
if len(decimals) == 5:
if len(decimals) == 7:
_LOGGER.debug(
"%s: Please charge device", self.name
) # think its based on the length of the response? ff010203d2 (bad) vs ff010203d202e803 (good)
self._battery_status = True
elif decimals[5] > 10:
elif decimals[5] >= 10:
_LOGGER.debug(
"%s: Please charge device", self.name
) # think its based on the length of the response? ff010203d2 (bad) vs ff010203d202e803 (good)
self._battery_status = True
elif decimals[5] <= 10:
elif decimals[5] < 10:
_LOGGER.debug("%s: Battery is good", self.name)
self._battery_status = False
else:
Expand All @@ -216,20 +216,27 @@ async def battery_callback(self, sender: BleakGATTCharacteristic, data: bytearra

async def position_callback(self, sender: BleakGATTCharacteristic, data: bytearray):
"""Wait for response from the blind and updates entity status."""
_LOGGER.debug("%s: Attempting to get battery status", self.name)
_LOGGER.debug("%s: Attempting to get position", self.name)

decimals = self.split_data(data)

blindPos = (decimals[-3] + (decimals[-2] * 256)) / 10
blindPos = (decimals[-4] + (decimals[-3] * 256)) / 10
_LOGGER.debug("%s: Blind position is %s", self.name, blindPos)
self._current_cover_position = blindPos

await self.blind_disconnect()


def return_hex_bytearray(self, x):
"""make sure we print ascii symbols as hex"""
return ''.join([type(x).__name__, "('",
*['\\x'+'{:02x}'.format(i) for i in x], "')"])


def split_data(self, data):
"""Split the byte response into decimal."""
customdecode = str(data)
data_hex = self.return_hex_bytearray(data)
customdecode = str(data_hex)
customdecodesplit = customdecode.split("\\x")
response = ""
decimals = []
Expand All @@ -241,7 +248,7 @@ def split_data(self, data):
decimals.append(int(resp, 16))
x += 1

_LOGGER.debug("%s: As byte:%s", self.name, data)
_LOGGER.debug("%s: As byte:%s", self.name, data_hex)
_LOGGER.debug("%s: As string:%s", self.name, response)
_LOGGER.debug("%s: As decimals:%s", self.name, decimals)
return decimals
Expand Down

0 comments on commit ad5597c

Please sign in to comment.