forked from coolcode/tomato-clock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lifx-tomato.py
305 lines (234 loc) · 8.23 KB
/
lifx-tomato.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Pomodoro 番茄工作法 https://en.wikipedia.org/wiki/Pomodoro_Technique
# ====== 🍅 Tomato Clock =======
# ./tomato.py # start a 25 minutes tomato clock + 5 minutes break
# ./tomato.py -t # start a 25 minutes tomato clock
# ./tomato.py -t <n> # start a <n> minutes tomato clock
# ./tomato.py -b # take a 5 minutes break
# ./tomato.py -b <n> # take a <n> minutes break
# ./tomato.py -h # help
import sys
import time
import subprocess
import requests
from pathlib import Path
import yaml
import time
# Set globals
LIFX_YAML_FILEPATH = Path.home() / ".lifx" / "lifx.yaml"
INITIAL_STATE_URL = "https://api.lifx.com/v1/lights/{0[selector]}/state"
CYCLE_STATE_URL = "https://api.lifx.com/v1/lights/{0[selector]}/cycle"
# Set states
STATES = {
"Working":
{
"power": "on",
"color": "white",
"brightness": 1.0,
"duration": 5
},
"Relaxing":
{
"power": "on",
"color": "red saturation:1.0",
"brightness": 1.0,
"duration": 5
}
}
WORK_MINUTES = 25
BREAK_MINUTES = 5
TOMATO_UNICODE = u"\U0001F345"
BATH_UNICODE = u"\U0001F6C0"
GOODBYE_UNICODE = u"\U0001F44B"
ALARM_CLOCK_UNICODE = u"\u23F0"
def read_lifx_yaml_file():
# Open yaml file
if not LIFX_YAML_FILEPATH.is_file():
print("Error, couldn't open file {}, are you sure it exists".format(LIFX_YAML_FILEPATH))
# Read with yaml
with open(LIFX_YAML_FILEPATH, 'r') as access_token_fh:
yaml_dict = yaml.load(access_token_fh, yaml.FullLoader)
return yaml_dict
def get_access_token(yaml_dict):
"""
Access token should be found in ~/.lifx/access-token.yaml
:return:
"""
# Check key exists
if 'access-token' not in yaml_dict.keys():
print("Error, could not find key 'access-token' in {}.".format(LIFX_YAML_FILEPATH))
# Return value
return yaml_dict['access-token']
def get_header(yaml_dict):
"""
Header merely comprises the access token
:return:
"""
headers = {"Authorization": "Bearer {}".format(get_access_token(yaml_dict)),
"Content-Type": "application/json"}
return headers
def get_group(yaml_dict):
"""
Return the attributes of the light group
:param yaml_dict:
:return:
"""
group_name = list(yaml_dict["groups"].keys())[0]
group_dict = yaml_dict["groups"][group_name]
# Check group_dict has an id attribute
if 'id' not in group_dict.keys():
print("Error, expected to find an 'id' attribute in the group object")
return group_dict
def get_init_state_url(group_dict):
"""
Create the url for the put command
Use the group ID for this component
:param group_dict:
:return:
"""
# Get the group _id
group_id = group_dict['id']
selector = "group_id:{}".format(group_id)
# Place this as the selector in the state url
put_url = INITIAL_STATE_URL.format({"selector": selector})
# Return the put url
return put_url
def set_initial_state(group_dict, yaml_dict):
"""
Set initial state to white
:return:
"""
# Request vars
headers = get_header(yaml_dict)
put_url = get_init_state_url(group_dict)
json_body = STATES["Working"]
# Request obj
r = requests.put(put_url, headers=headers, json=json_body)
json_response = r.json()
def get_cycle_state_url(group_dict):
"""
Create the url for the put command
Use the group ID for this component
:param group_dict:
:return:
"""
# Get the group _id
group_id = group_dict['id']
selector = "group_id:{}".format(group_id)
# Place this as the selector in the state url
post_url = CYCLE_STATE_URL.format({"selector": selector})
# Return the put url
return post_url
def change_state(group_dict, yaml_dict):
"""
Set initial state to white
:return:
"""
# Request vars
headers = get_header(yaml_dict)
post_url = get_cycle_state_url(group_dict)
json_body = {"states": [STATES["Working"], STATES["Relaxing"]],
"defaults": STATES["Working"]}
# Request obj
r = requests.post(post_url, headers=headers, json=json_body)
json_response = r.json()
print(json_response)
def main():
# Read in vars
yaml_dict = read_lifx_yaml_file()
# Get group dict
group_dict = get_group(yaml_dict)
# Set light to main light
set_initial_state(group_dict, yaml_dict)
try:
if len(sys.argv) <= 1:
while True:
print(f'{TOMATO_UNICODE} tomato {WORK_MINUTES} minutes. Ctrl+C to exit')
tomato(WORK_MINUTES, 'It is time to take a break')
change_state(group_dict, yaml_dict)
print(f'{TOMATO_UNICODE} break {BREAK_MINUTES} minutes. Ctrl+C to exit')
tomato(BREAK_MINUTES, 'It is time to work')
change_state(group_dict, yaml_dict)
elif sys.argv[1] == '-t':
minutes = int(sys.argv[2]) if len(sys.argv) > 2 else WORK_MINUTES
print(f'{BATH_UNICODE} tomato {minutes} minutes. Ctrl+C to exit')
tomato(minutes, 'It is time to take a break')
change_state(group_dict, yaml_dict)
elif sys.argv[1] == '-b':
minutes = int(sys.argv[2]) if len(sys.argv) > 2 else BREAK_MINUTES
change_state(group_dict, yaml_dict)
print(f'{BATH_UNICODE} break {minutes} minutes. Ctrl+C to exit')
tomato(minutes, 'It is time to work')
change_state(group_dict, yaml_dict)
elif sys.argv[1] == '-h':
help()
else:
help()
except KeyboardInterrupt:
# TODO: Revert to original light state if selected
print(f"\n{GOODBYE_UNICODE} goodbye")
except Exception as ex:
print(ex)
exit(1)
def tomato(minutes, notify_msg):
start_time = time.perf_counter()
while True:
diff_seconds = int(round(time.perf_counter() - start_time))
left_seconds = minutes * 60 - diff_seconds
if left_seconds <= 0:
print('')
break
countdown = '{}:{} {}'.format(int(left_seconds / 60), int(left_seconds % 60), ALARM_CLOCK_UNICODE)
duration = min(minutes, 25)
progressbar(diff_seconds, minutes * 60, duration, countdown)
time.sleep(1)
notify_me(notify_msg)
def progressbar(curr, total, duration=10, extra=''):
frac = curr / total
filled = round(frac * duration)
print('\r', f'{TOMATO_UNICODE}' * filled + '--' * (duration - filled), '[{:.0%}]'.format(frac), extra, end='')
def notify_me(msg):
"""
# macos desktop notification
terminal-notifier -> https://github.com/julienXX/terminal-notifier#download
terminal-notifier -message <msg>
# ubuntu desktop notification
notify-send
# voice notification
say -v <lang> <msg>
lang options:
- Daniel: British English
- Ting-Ting: Mandarin
- Sin-ji: Cantonese
"""
print(msg)
try:
if sys.platform == 'darwin':
# macos desktop notification
subprocess.run(['terminal-notifier', '-title', f"{TOMATO_UNICODE}", '-message', msg])
subprocess.run(['say', '-v', 'Daniel', msg])
elif sys.platform.startswith('linux'):
# ubuntu desktop notification
subprocess.Popen(["notify-send", f"{TOMATO_UNICODE}", msg])
# TODO add optional volume command
else:
# windows?
# TODO: windows notification
pass
except:
# skip the notification error
pass
def help():
# FIXME: use argparse module instead.
appname = sys.argv[0]
appname = appname if appname.endswith('.py') else 'tomato' # tomato is pypi package
print('====== 🍅 Tomato Clock =======')
print(f'{appname} # start a {WORK_MINUTES} minutes tomato clock + {BREAK_MINUTES} minutes break')
print(f'{appname} -t # start a {WORK_MINUTES} minutes tomato clock')
print(f'{appname} -t <n> # start a <n> minutes tomato clock')
print(f'{appname} -b # take a {BREAK_MINUTES} minutes break')
print(f'{appname} -b <n> # take a <n> minutes break')
print(f'{appname} -h # help')
if __name__ == "__main__":
main()