-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpomodoro.py
More file actions
executable file
·123 lines (106 loc) · 3.83 KB
/
pomodoro.py
File metadata and controls
executable file
·123 lines (106 loc) · 3.83 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
from ticktick.oauth2 import OAuth2 # OAuth2 Manager
from ticktick.api import TickTickClient # Main Interface
from urllib.parse import urlencode
import threading
import websocket
import requests
import syslog
import json
import os
def validate_config(config):
if not config["homeassistant"]["token"]:
msg = "Missing Home Assistant TOKEN"
syslog.syslog(syslog.LOG_ERR, msg)
raise Exception(msg)
if not config["homeassistant"]["url"]:
msg = "Missing Home Assistant URL"
syslog.syslog(syslog.LOG_ERR, msg)
raise Exception(msg)
if not config["ticktick"]["oauth"]["id"]:
msg = "Missing TickTick OAuth ID"
syslog.syslog(syslog.LOG_ERR, msg)
raise Exception(msg)
if not config["ticktick"]["oauth"]["secret"]:
msg = "Missing TickTick OAuth Secret"
syslog.syslog(syslog.LOG_ERR, msg)
raise Exception(msg)
with open(os.path.expanduser("~/.PomodoroHomeAssistant")) as f:
config = json.load(f)
validate_config(config)
homeassistant_token = config["homeassistant"]["token"]
homeassistant_url = config["homeassistant"]["url"]
auth_client = OAuth2(config["ticktick"]["oauth"]["id"],
client_secret=config["ticktick"]["oauth"]["secret"],
redirect_uri="https://echo.free.beeceptor.com/")
client = TickTickClient(config["ticktick"]["username"], config["ticktick"]["password"], auth_client)
# Define a mapping of TickTick 'op' events to Home Assistant entity_id values
ticktick_scenes = {
"start": "scene.pomodoro_enabled",
"pause": "scene.pomodoro_suspended",
"continue": "scene.pomodoro_enabled",
"startBreak": "scene.pomodoro_short_break",
"longBreak": "scene.pomodoro_long_break",
"endBreak": "scene.pomodoro_suspended",
"finish": "scene.normal",
"drop": "scene.normal",
"exit": "scene.normal",
"focus": None,
}
homeassistant_scene_url = f"{homeassistant_url}/api/services/scene/turn_on"
homeassistant_headers = {
"Authorization": f"Bearer {homeassistant_token}",
"content-type": "application/json",
}
def set_scene(ticktick_op):
if ticktick_op not in ticktick_scenes:
msg = f"Unknown TickTick op: {ticktick_op}"
print(msg)
syslog.syslog(syslog.LOG_WARNING, msg)
return
data = {}
data["entity_id"] = ticktick_scenes[ticktick_op]
if not data["entity_id"]:
return
syslog.syslog(syslog.LOG_INFO, f"{ticktick_op} --> {data}\n")
response = requests.post(homeassistant_scene_url, headers=homeassistant_headers, json=data)
print(response.text)
def on_message(ws, message):
print(f"Received message: {message}")
parsed_message = json.loads(message)
if 'data' in parsed_message:
data = parsed_message['data']
op = data['op']
if op == 'startBreak' and data['duration'] > 5:
op = 'longBreak'
try:
set_scene(op)
except Exception as e:
msg = f"Error setting scene: {e}"
print(msg)
syslog.syslog(syslog.LOG_ERR, msg)
def on_error(ws, error):
print(f"An error occurred: {error}")
def on_close(ws, close_status_code, close_msg):
print("Connection closed.")
def on_open(ws):
print("Connection opened.")
send_ping()
params = {
'x-device': TickTickClient.X_DEVICE_,
'hl': 'en_US'
}
ws = websocket.WebSocketApp("wss://wssp.ticktick.com/web?" + urlencode(params),
on_message=on_message,
on_error=on_error,
on_close=on_close,
on_open=on_open,
cookie='t=' + client.cookies['t'])
def send_ping():
print("ping")
ping = {
"type": "ping"
}
ws.send(json.dumps(ping))
threading.Timer(60, send_ping).start()
# Run the WebSocket
ws.run_forever()