-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtestclient.py
More file actions
147 lines (106 loc) · 4.25 KB
/
testclient.py
File metadata and controls
147 lines (106 loc) · 4.25 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from govee_api import api, device
import time
import colour
def main():
# Create Govee client and configure event handlers
govee_cli = api.Govee('your_email', 'your_password', 'your_client_id_or_EMPTY')
# BEWARE: This will create a new Govee Client ID with every login. It is recommended to provide an existing client ID
# within the `Govee` contructor. You can fetch your generated client ID via `govee.client_id` after your successful login
# Event raised when a new device is discovered
govee_cli.on_new_device = _on_new_device
# Event raised when a device status was updated
govee_cli.on_device_update = _on_device_update
# Login to Govee
govee_cli.login()
# Print out the used client ID
print('Current client ID is: {}'.format(govee_cli.client_id))
# Fetch known devices from server
govee_cli.update_device_list()
print('Preparing for action :-)')
# Don't do this in real life. Use the callbacks the client provides to you!
time.sleep(10)
# Loop over all devices
for dev in govee_cli.devices.values():
if dev.connected:
print('Fun with device {} ...'.format(dev.name))
# Turn on device
dev.on = True
# Wait a second
time.sleep(1)
# Save initial brightness
brightness_backup = dev.brightness
# Set brightness to 50%
dev.brightness = 0.5
# Wait a second
time.sleep(1)
# Set brightness to 100%
dev.brightness = 1.0
# Wait a second
time.sleep(1)
if isinstance(dev, device.GoveeRgbLight):
# Save initial color
color_backup = dev.color
# Set color temperature to 2100 kelvin (warm white)
dev.color_temperature = 2100
# Wait a second
time.sleep(1)
# Set color to green
dev.color = colour.Color('green')
# Wait a second
time.sleep(1)
# Set color to red
dev.color = (255, 0, 0)
# Wait a second
time.sleep(1)
# Set color to dodgerblue
dev.color = colour.Color('dodgerblue')
# Wait a second
time.sleep(1)
# Restore color
if color_backup:
dev.color = color_backup
# Wait a second
time.sleep(1)
# Restore initial brightness
dev.brightness = brightness_backup
# Wait a second
time.sleep(1)
# Turn the device off
dev.on = False
else:
print('Device {} is not connected. Skipping...'.format(dev.name))
print('All done!')
# Event handlers
def _on_new_device(govee_cli, dev, raw_data):
""" New device event """
connected_str = _get_connected_str(dev.connected)
print('NEW DEVICE [{}][{} {}] {} -> Connected: {}'.format(dev.identifier, dev.sku, dev.friendly_name, dev.name, \
connected_str))
def _on_device_update(govee_cli, dev, old_dev, raw_data):
""" Device update event """
connected_str = _get_connected_str(dev.connected)
on_str = 'No'
if dev.on:
on_str = 'Yes'
if isinstance(dev, device.GoveeRgbLight):
color_str = 'Non-RGB'
if dev.color:
color_str = dev.color.hex_l
elif dev.color_temperature and dev.color_temperature > 0:
color_str = '{} Kelvin'.format(dev.color_temperature)
print('DEVICE UPDATE [{}][{} {}] {} -> Connected: {}, On: {}, Brightness: {}, Color: {}'.format(dev.identifier, \
dev.sku, dev.friendly_name, dev.name, connected_str, on_str, dev.brightness, color_str))
else:
print('DEVICE UPDATE [{}][{} {}] {} -> Connected: {}, On: {}, Brightness: {}'.format(dev.identifier, dev.sku, \
dev.friendly_name, dev.name, connected_str, on_str, dev.brightness))
# Helper
def _get_connected_str(connected):
""" Get connection status string """
connected_str = 'No'
if connected:
connected_str = 'Yes'
elif connected is None:
connected_str = '???'
return connected_str
if __name__ == '__main__':
main()