Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added function to request favorites #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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: 2 additions & 0 deletions pyhatchbabyrest/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

COLOR_GRADIENT = (254, 254, 254) # setting this color turns on Gradient mode
CHAR_TX = "02240002-5efd-47eb-9c1a-de53f7a2b232"
CHAR_RX = "02240003-5EFD-47EB-9C1A-DE53F7A2B232"
CHAR_FEEDBACK = "02260002-5efd-47eb-9c1a-de53f7a2b232"
BT_MANUFACTURER_ID = 1076
FAVORITE_ENABLED = 0x96


class PyHatchBabyRestSound(IntEnum):
Expand Down
58 changes: 56 additions & 2 deletions pyhatchbabyrest/pyhatchbabyrestasync.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import asyncio
from typing import Optional
from typing import Union
from dataclasses import dataclass
from typing import Optional, Callable

from bleak import BleakClient
from bleak import BleakScanner
Expand All @@ -9,7 +11,9 @@
from .constants import BT_MANUFACTURER_ID
from .constants import CHAR_FEEDBACK
from .constants import CHAR_TX
from .constants import CHAR_RX
from .constants import PyHatchBabyRestSound
from .constants import FAVORITE_ENABLED


class PyHatchBabyRestAsync(object):
Expand Down Expand Up @@ -43,21 +47,29 @@ async def _ensure_scan(self) -> BLEDevice:
return await self.scan()
return self.device

async def _send_command(self, command: str):
async def _send_command(self, command: str, response_callback: Optional[Callable] = None):
"""Send a command do the device.

:param command: The command to send.
"""
self.device = await self._ensure_scan()

async with BleakClient(self.device) as client:
if response_callback:
await client.start_notify(CHAR_RX, response_callback)

await client.write_gatt_char(
char_specifier=CHAR_TX,
data=bytearray(command, "utf-8"),
response=True,
)

if response_callback:
await asyncio.sleep(0.5)
await client.stop_notify(CHAR_RX)

await asyncio.sleep(0.25)
await self.refresh_data()
await self.refresh_data()

async def scan(self) -> BLEDevice:
self.scanner = BleakScanner() if self.scanner is None else self.scanner
Expand Down Expand Up @@ -141,6 +153,48 @@ async def set_brightness(self, brightness):
self.color[0], self.color[1], self.color[2], brightness
)
return await self._send_command(command)

@dataclass
class Favorite:
color: tuple[int, int, int]
sound: PyHatchBabyRestSound
enabled: bool
brightness: int
volume: int

async def req_favorite(self, num):
# initialize favorites list
if not hasattr(self, 'favorites'):
default_fav = self.Favorite(
color=(255, 255, 255),
sound=PyHatchBabyRestSound.none,
enabled=False,
brightness=0,
volume=0
)
# make 9 spots so index 0 is always unused
self.favorites = [default_fav]*9

# request favorite number num
command = "PGB{:02}".format(num)
self.favorite_number = num
await self._send_command(command, self._parse_favorite_callback)
return self.favorites[num]

def _parse_favorite_callback(self, characteristic, data):
if (len(data) == 15):
red, green, blue, brightness = data[12:8:-1]
volume = data[2]
sound = data[1]
enabled = data[13] == FAVORITE_ENABLED
self.favorites[self.favorite_number] = self.Favorite(
color = (red, green, blue),
sound = PyHatchBabyRestSound(sound),
enabled = enabled,
brightness = brightness,
volume = volume
)


@property
async def connected(self):
Expand Down