-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmarthome.py
More file actions
182 lines (137 loc) · 5.89 KB
/
Copy pathsmarthome.py
File metadata and controls
182 lines (137 loc) · 5.89 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
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
import httpx
import miio
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import ContextTypes
from yeelight import Bulb
import config
from cron_jobs import plot_boiler_stats
from utils import ForgeCommand, no_can_do, printlog
def get_bulb(name: str):
ip_pranzo = "192.168.1.206"
token_pranzo = "e9466aedeab8b3eecd97ac6f337257fe" # nosec
ip_cucina = "192.168.1.207"
token_cucina = "33188b32b80cca8eeead7036b4da32da" # nosec
ip_salotto = "192.168.1.150"
token_salotto = "8b81464003a7a0d4126ea8e6cb8d28e3" # nosec
ip_penisola = "192.168.1.50"
salotto = miio.Yeelight(ip_salotto, token_salotto)
pranzo = miio.Yeelight(ip_pranzo, token_pranzo)
cucina = miio.Yeelight(ip_cucina, token_cucina)
penisola = Bulb(ip_penisola)
bulbs = {
"salotto": {"label": "Luce salotto", "object": salotto},
"pranzo": {"label": "Luce pranzo", "object": pranzo},
"cucina": {"label": "Luce cucina", "object": cucina},
"penisola": {"label": "Luce penisola", "object": penisola},
}
if name.lower() not in bulbs.keys():
return None
else:
return bulbs[name.lower()]
def get_status(bulbname):
if isinstance(bulbname, Bulb): # Yeelight
if bulbname.get_properties()["power"] == "on":
return True
return False
elif isinstance(bulbname, miio.Yeelight): # xiaomi
if bulbname.get_properties(["power"])[0] == "on":
return True
return False
else:
raise TypeError
def toggle(bulbname):
if isinstance(bulbname, Bulb): # Yeelight
bulbname.toggle()
elif isinstance(bulbname, miio.Yeelight): # xiaomi
bulbname.toggle()
else:
raise TypeError
def get_light_label(bulbname):
bulbs = ["salotto", "pranzo", "cucina", "penisola"]
if bulbname not in bulbs:
return None
bulb = get_bulb(bulbname)
if get_status(bulb["object"]):
return f"🟢 {bulb['label']}"
else:
return f"🔴 {bulb['label']}"
async def luci_status(update: Update, context: ContextTypes.DEFAULT_TYPE):
if await no_can_do(update, context):
return
if update.effective_user.id not in config.ADMINS:
return
await printlog(update, "chiede lo status delle luci")
bulbs = ["salotto", "pranzo", "cucina", "penisola"]
callbacks = [
ForgeCommand(original_update=update, new_text=f"/toggle {x}", new_args=[x], callable=toggle_light)
for x in bulbs
]
luci_keyb = [
[
InlineKeyboardButton(f"{get_light_label(bulbs[0])}", callback_data=callbacks[0]),
InlineKeyboardButton(f"{get_light_label(bulbs[1])}", callback_data=callbacks[1]),
],
[
InlineKeyboardButton(f"{get_light_label(bulbs[2])}", callback_data=callbacks[2]),
InlineKeyboardButton(f"{get_light_label(bulbs[3])}", callback_data=callbacks[3]),
],
]
reply_markup = InlineKeyboardMarkup(luci_keyb)
await update.message.reply_html("Luci:", reply_markup=reply_markup, quote=False)
async def toggle_light(update: Update, context: ContextTypes.DEFAULT_TYPE):
try:
if update.effective_user.id not in config.ADMINS:
return
except Exception:
if update.from_user.id not in config.ADMINS:
return
bulbs = ["salotto", "pranzo", "cucina", "penisola"]
if not context.args or context.args[0] not in bulbs:
return
await printlog(update, "toggla una luce", context.args[0])
toggle(get_bulb(context.args[0])["object"])
if update.message.reply_markup:
bulbs = ["salotto", "pranzo", "cucina", "penisola"]
callbacks = [
ForgeCommand(original_update=update, new_text=f"/toggle {x}", new_args=[x], callable=toggle_light)
for x in bulbs
]
luci_keyb = [
[
InlineKeyboardButton(f"{get_light_label(bulbs[0])}", callback_data=callbacks[0]),
InlineKeyboardButton(f"{get_light_label(bulbs[1])}", callback_data=callbacks[1]),
],
[
InlineKeyboardButton(f"{get_light_label(bulbs[2])}", callback_data=callbacks[2]),
InlineKeyboardButton(f"{get_light_label(bulbs[3])}", callback_data=callbacks[3]),
],
]
reply_markup = InlineKeyboardMarkup(luci_keyb)
await update.message.edit_reply_markup(reply_markup=reply_markup)
return
async def consumo(update: Update, context: ContextTypes.DEFAULT_TYPE):
if update.effective_user.id not in config.ADMINS:
return
await printlog(update, "controlla il consumo")
async with httpx.AsyncClient() as session:
r = await session.get("http://192.168.1.246/emeter/0")
data = r.json()
await update.message.reply_html(f"Consumo istantaneo: {data['power']} W")
async def purificatore(update: Update, context: ContextTypes.DEFAULT_TYPE):
if update.effective_user.id not in config.ADMINS:
return
await printlog(update, "chiede lo status del purificatore d'aria")
air = miio.AirPurifierMiot("192.168.1.84", "e8e5a8bb036e21662c4708b61a829c04") # 54:48:E6:55:B3:72
s = air.status()
message = ""
message += f"Purificatore d'aria Xiaomi Air Purifier 3H: {'Acceso' if s.is_on else 'Spento'}\n"
message += f"Temperatura: {s.temperature}°C - Umidità: {s.humidity}% - AQI: {s.aqi}\n"
message += f"Ore d'uso: {s.filter_hours_used}h - Aria purificati: {s.purify_volume} m³ - Filtro rimanente: {s.filter_life_remaining}%\n"
await update.message.reply_html(message)
async def riscaldamento_stats(update: Update, context: ContextTypes.DEFAULT_TYPE):
if update.effective_user.id not in config.ADMINS:
return
# if '-new' in context.args:
await plot_boiler_stats(context)
await printlog(update, "genera un nuovo grafico della caldaia")
await update.message.reply_photo(open("images/boiler48h.jpg", "rb"))