-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsnoo
executable file
·289 lines (216 loc) · 7.87 KB
/
snoo
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
#!/usr/bin/env python
"""Command line tool for interacting with the Snoo Bassinet API"""
import asyncio
import argparse
import getpass
import json
from pprint import pprint
from datetime import datetime, timedelta
from pysnoo import SnooAuthSession, Snoo, SnooPubNub, SessionLevel
from pysnoo.models import dt_str_to_dt
# pylint: disable=unused-argument
async def _setup_pubnub(snoo: Snoo):
"""Utility Function to setup SnooPubNub"""
# Also checks for valid token
devices = await snoo.get_devices()
if not devices:
# No devices
print('There is no Snoo connected to that account!')
return
return SnooPubNub(snoo.auth.access_token,
devices[0].serial_number,
f'pn-pysnoo-{devices[0].serial_number}')
async def user(snoo: Snoo, args):
"""user command"""
resonse = await snoo.get_me()
pprint(resonse.to_dict())
async def device(snoo: Snoo, args):
"""device command"""
resonse = await snoo.get_devices()
if len(resonse) > 0:
pprint(resonse[0].to_dict())
async def baby(snoo: Snoo, args):
"""baby command"""
resonse = await snoo.get_baby()
pprint(resonse.to_dict())
async def last_session(snoo: Snoo, args):
"""last_session command"""
resonse = await snoo.get_last_session()
pprint(resonse.to_dict())
async def status(snoo: Snoo, args):
"""status command"""
resonse = await snoo.get_last_session()
print(f'{resonse.current_status.value} (since: {resonse.current_status_duration})')
async def sessions(snoo: Snoo, args):
"""sessions command"""
resonse = await snoo.get_aggregated_session(args.datetime)
pprint(resonse.to_dict())
async def session_avg(snoo: Snoo, args):
"""session_avg command"""
baby_resonse = await snoo.get_baby()
resonse = await snoo.get_aggregated_session_avg(baby_resonse.baby, args.datetime)
pprint(resonse.to_dict())
async def total(snoo: Snoo, args):
"""total command"""
baby_resonse = await snoo.get_baby()
resonse = await snoo.get_session_total_time(baby_resonse.baby)
print(resonse)
async def monitor(snoo: Snoo, args):
"""monitor command"""
def as_callback(activity_state):
pprint(activity_state.to_dict())
pubnub = await _setup_pubnub(snoo)
pubnub.add_listener(as_callback)
for activity_state in await pubnub.history():
as_callback(activity_state)
await pubnub.subscribe_and_await_connect()
try:
while True:
await asyncio.sleep(1)
except asyncio.CancelledError:
pass
finally:
await pubnub.unsubscribe_and_await_disconnect()
await pubnub.stop()
async def history(snoo: Snoo, args):
"""history command"""
pubnub = await _setup_pubnub(snoo)
for activity_state in await pubnub.history(100):
pprint(activity_state.to_dict())
await pubnub.stop()
async def toggle(snoo: Snoo, args):
"""toggle command"""
pubnub = await _setup_pubnub(snoo)
last_activity_state = (await pubnub.history())[0]
if last_activity_state.state_machine.state == SessionLevel.ONLINE:
# Start
await pubnub.publish_start()
else:
# Stop
await pubnub.publish_goto_state(SessionLevel.ONLINE)
await pubnub.stop()
async def toggle_hold(snoo: Snoo, args):
"""toggleHold command"""
pubnub = await _setup_pubnub(snoo)
last_activity_state = (await pubnub.history())[0]
current_state = last_activity_state.state_machine.state
current_hold = last_activity_state.state_machine.hold
if current_state.is_active_level():
# Toggle
await pubnub.publish_goto_state(current_state, not current_hold)
else:
print('Cannot toggle hold when Snoo is not running!')
await pubnub.stop()
async def level_up(snoo: Snoo, args):
"""up command"""
pubnub = await _setup_pubnub(snoo)
last_activity_state = (await pubnub.history())[0]
up_transition = last_activity_state.state_machine.up_transition
if up_transition.is_active_level():
# Toggle
await pubnub.publish_goto_state(up_transition)
else:
print('No valid up-transition available!')
await pubnub.stop()
async def level_down(snoo: Snoo, args):
"""down command"""
pubnub = await _setup_pubnub(snoo)
last_activity_state = (await pubnub.history())[0]
down_transition = last_activity_state.state_machine.down_transition
if down_transition.is_active_level():
# Toggle
await pubnub.publish_goto_state(down_transition)
else:
print('No valid down-transition available!')
await pubnub.stop()
# Commands dictionary
commands = {
'user': user,
'device': device,
'baby': baby,
'last_session': last_session,
'status': status,
'sessions': sessions,
'session_avg': session_avg,
'total': total,
'monitor': monitor,
'history': history,
'toggle': toggle,
'toggle_hold': toggle_hold,
'up': level_up,
'down': level_down,
}
async def async_main(username, password, token, token_updater, args: any):
"""Async Main"""
async with SnooAuthSession(token, token_updater) as auth:
if not auth.authorized:
# Init Auth
new_token = await auth.fetch_token(username, password)
token_updater(new_token)
snoo = Snoo(auth)
await commands[args.command](snoo, args)
def get_token_updater(token_file):
"""Return an token_updater function writing tokens to token_file"""
def token_updater(token):
with open(token_file, 'w') as outfile:
json.dump(token, outfile)
return token_updater
def get_token(token_file):
"""Read a token from a token_file (fails silently)"""
try:
with open(token_file) as infile:
token = json.load(infile)
return token
except FileNotFoundError:
pass
except ValueError:
pass
def get_username():
"""read username from STDIN"""
username = input("Username: ")
return username
def main():
"""Sync Main"""
parser = argparse.ArgumentParser(
description='Snoo Smart Bassinett',
epilog='https://github.com/rado0x54/pysnoo',
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'command', default='user', choices=['user', 'device', 'baby',
'last_session', 'status', 'sessions',
'session_avg', 'total', 'monitor',
'history', 'toggle', 'toggle_hold', 'up', 'down']
)
parser.add_argument('-u',
'--username',
type=str,
help='username for Snoo account')
parser.add_argument('-p',
'--password',
type=str,
help='username for Snoo account')
parser.add_argument('-t',
'--token_file',
metavar='file',
default='.snoo_token.txt',
help='Cached token file to read and write an existing OAuth Token to.')
parser.add_argument('-d',
'--datetime',
default=datetime.now() - timedelta(1), # 24h prior
type=dt_str_to_dt,
help='Datetime in ISO8601 fromat. Used for some commands.'
)
args = parser.parse_args()
token = get_token(args.token_file)
token_updater = get_token_updater(args.token_file)
if not token and not args.username:
args.username = get_username()
if not token and not args.password:
args.password = getpass.getpass("Password: ")
# Python 3.7+
try:
asyncio.run(async_main(args.username, args.password, token, token_updater, args))
except KeyboardInterrupt:
pass
if __name__ == "__main__":
main()