-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlywsd02_time.py
61 lines (46 loc) · 1.85 KB
/
lywsd02_time.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python3
import time
from datetime import datetime
import struct
from bluepy import btle
# MAC address of LYWSD02
mac_addr = '3F:59:C8:81:XX:XX'
# UUIDs
uuid_time = 'ebe0ccb7-7a0a-4b0c-8a1a-6ff2997da3a6'
uuid_battery = 'EBE0CCC4-7A0A-4B0C-8A1A-6FF2997DA3A6'
# Get offset in hours from GMT
utc_offset = time.localtime().tm_gmtoff
local_offset = int(utc_offset//3600)
# connect to device
try:
print('Connecting to {0}'.format(mac_addr))
p = btle.Peripheral (mac_addr)
except Exception as e:
print('Error: {0}'.format(e))
exit()
chtime = p.getCharacteristics (uuid = uuid_time)[0]
# read time and offset from device
value = chtime.read()
device_ts, device_offset = struct.unpack('Ib', value)
print('Current Time/Offset: {:%H:%M} / {}'.format(datetime.utcfromtimestamp(device_ts), device_offset))
try:
# Get current epoch timestamp
ts = int(round(time.time()))
# Create array with bitmask (or shifted?) time parts
# Idea from: https://github.com/Freeyourgadget/Gadgetbridge/blob/master/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/mijia_lywsd02/MijiaLywsd02Support.java
arr_time_bytes = [(ts & 0xFF), ((ts>>8) & 0xFF),((ts>>16) & 0xFF),((ts>>24) & 0xFF),local_offset]
# Send time to device
print('Sending time and offset...'.format(mac_addr))
chtime.write(bytearray(arr_time_bytes), withResponse = True)
finally:
# read new time from device
value = chtime.read()
device_ts, device_offset = struct.unpack('Ib', value)
print('New Time/Offset: {:%H:%M} / {}'.format(datetime.utcfromtimestamp(device_ts), device_offset))
# get battery percentage from device
chbatt = p.getCharacteristics(uuid=uuid_battery)[0]
value = chbatt.read()
print('Battery: {0}%'.format(ord(value)))
# disconnect
print('Done! Disconnecting from {0}'.format(mac_addr))
p.disconnect()