-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvacuum.py
More file actions
149 lines (116 loc) · 3.35 KB
/
vacuum.py
File metadata and controls
149 lines (116 loc) · 3.35 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
148
149
#! python3
# -*- coding: utf-8 -*-
try:
from commands import *
except ImportError:
import sys
print("install https://github.com/egigoka/commands")
sys.exit(1)
try:
import telebot
except ImportError:
import sys
print("install pytelegrambotapi")
sys.exit(1)
try:
import telegrame
except ImportError:
import sys
print("install https://github.com/egigoka/telegrame")
sys.exit(1)
try:
from miio import Device
from miio.exceptions import DeviceException
except ImportError:
import sys
print("install python-miio")
sys.exit(1)
try:
from secrets import BATTERY_TELEGRAM_TOKEN, MY_CHAT_ID, VACUUM_IP, VACUUM_TOKEN
except ImportError:
import sys
print("create secrets.py with BATTERY_TELEGRAM_TOKEN, MY_CHAT_ID, VACUUM_IP and VACUUM_TOKEN")
sys.exit(1)
__version__ = "0.0.2"
ONCE = "--once" in OS.args
DEBUG = "--debug" in OS.args
PREVIOUS = JsonDict("./configs/vacuum.json")
RUN_EVERY = 30
HOSTNAME = OS.hostname
STATUS_MAP = {
1: "Idle",
2: "Sweeping",
3: "Paused",
4: "Error",
5: "Charging",
6: "Returning",
7: "Mopping",
}
TELEGRAM_API = telebot.TeleBot(BATTERY_TELEGRAM_TOKEN, threaded=False)
print_original = print
def print(*args, **kwargs):
kwargs["flush"] = True
print_original(*args, **kwargs)
def get_vacuum_struct():
d = Device(VACUUM_IP, VACUUM_TOKEN)
result = d.send('get_properties', [
{'did': 'status', 'siid': 2, 'piid': 1},
{'did': 'battery', 'siid': 3, 'piid': 1},
{'did': 'charging', 'siid': 3, 'piid': 2},
])
struct = {}
for r in result:
struct[r['did']] = r['value']
return struct
def save_previous(state):
PREVIOUS.string = state
PREVIOUS.save()
def status_name(code):
return STATUS_MAP.get(code, f"Unknown({code})")
def check_vacuum():
try:
output = get_vacuum_struct()
except (DeviceException, TypeError) as e:
print(f"vacuum unreachable: {e}")
return
if DEBUG:
print(output)
now_battery = output['battery']
now_status = output['status']
print(f"vacuum: {now_battery}%, {status_name(now_status)}")
if DEBUG:
print(f"now = {now_battery}%, {status_name(now_status)}")
if PREVIOUS:
if DEBUG:
print(f"previous = {PREVIOUS['battery']}%, {status_name(PREVIOUS['status'])}")
else:
save_previous(output)
return
try:
previous_battery = int(PREVIOUS['battery'])
except (ValueError, KeyError):
previous_battery = 0
battery_bucket_changed = (now_battery // 10) != (previous_battery // 10)
changed = battery_bucket_changed \
or PREVIOUS['status'] != now_status
if changed:
message = f"{HOSTNAME} vacuum: {now_battery}%, {status_name(now_status)}"
if DEBUG:
print(message)
telegrame.send_message(TELEGRAM_API, MY_CHAT_ID, message)
save_previous(output)
def _start_bot_sender():
while True:
check_vacuum()
Time.sleep(RUN_EVERY)
def safe_threads_run():
print(f"Main thread v{__version__} started")
threads = Threading()
threads.add(telegrame.very_safe_start_bot, args=(_start_bot_sender,))
threads.start(wait_for_keyboard_interrupt=True)
print("Main thread quited")
if __name__ == '__main__':
if ONCE:
check_vacuum()
else:
safe_threads_run()