Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "neosmartblue.py"
version = "0.1.3"
version = "0.1.4"
description = "A Python library for controlling Neo Smart Blinds via BlueLink Bluetooth connection"
authors = [
{name = "ikifar2012", email = "[email protected]"}
Expand Down
27 changes: 18 additions & 9 deletions src/neosmartblue/py/device.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
from bleak import BleakClient
from bleak.backends.characteristic import BleakGATTCharacteristic
from .parse_status import parse_status_data

from .const import _RX_UUID, _TX_UUID
Expand Down Expand Up @@ -89,26 +90,34 @@ async def ping(self):
command = generate_ping_command()
await self.send_command(command)

async def receive_data(self, timeout: float = 5.0) -> bytes:
async def receive_data(self, timeout: float = 5.0):
"""
Listen for a notification on the TX characteristic.

Parameters:
timeout (float): The number of seconds to wait for a notification.

Returns:
bytes: The data received, or None if no data is received within the timeout.
Parsed status object (type depends on parse_status_data implementation), or None if no data is received within the timeout.
"""
data = None

def notification_handler(sender: int, received_data: bytes):
def notification_handler(sender: BleakGATTCharacteristic, received_data: bytearray):
nonlocal data
print("Notification from", sender, ":", received_data.hex().upper())
# extract the status payload from the received data
status_payload = received_data[4:9]
status = parse_status_data(status_payload)
data = status
print("Parsed status:", status)
# sender is a BleakGATTCharacteristic object per Bleak's callback signature
try:
printable = bytes(received_data)
print("Notification from characteristic", getattr(sender, 'uuid', sender), ":", printable.hex().upper())
# ensure payload long enough before slicing
if len(printable) >= 9:
status_payload = printable[4:9]
status = parse_status_data(status_payload)
data = status
print("Parsed status:", status)
else:
print("Received data too short to parse status payload (len=", len(printable), ")")
except Exception as e:
print("Error handling notification:", e)

await self.client.start_notify(_TX_UUID, notification_handler)
await asyncio.sleep(timeout)
Expand Down