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

Add reading and setting time using datetimes #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions pyhatchbabyrest/pyhatchbabyrestasync.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
from typing import Optional
from typing import Union
from datetime import datetime, UTC

from bleak import BleakClient
from bleak import BleakScanner
Expand Down Expand Up @@ -88,6 +89,8 @@ async def refresh_data(self):

response = [hex(x) for x in raw_char_read]

timestamp = int.from_bytes(bytes(raw_char_read[1:5]))

# Make sure the data is where we think it is
assert response[5] == "0x43" # color
assert response[10] == "0x53" # audio
Expand All @@ -101,6 +104,7 @@ async def refresh_data(self):

power = not bool(int("11000000", 2) & int(response[14], 16))

self.time = datetime.fromtimestamp(timestamp, UTC)
self.color = (red, green, blue)
self.brightness = brightness
self.sound = sound
Expand Down Expand Up @@ -142,6 +146,15 @@ async def set_brightness(self, brightness):
)
return await self._send_command(command)

async def set_time(self, new_time: Optional[datetime] = None):
if not new_time:
new_time = datetime.now()

self.time = new_time

command = datetime.now().strftime("ST%Y%m%d%H%M%SU")
return await self._send_command(command)

@property
async def connected(self):
self.device = await self._ensure_scan()
Expand Down