-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
144 lines (105 loc) · 3.17 KB
/
bot.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
import asyncio
import logging
import time
import ipaddress
from config_reader import config
from aiogram import Bot, Dispatcher, types, html
from aiogram.filters.command import Command, CommandObject
from aiogram.types import Message
from yeelight import Bulb
logging.basicConfig(level=logging.INFO)
bot = Bot(token=config.bot_token.get_secret_value())
dp = Dispatcher()
# ip
strIP = ""
# yeelight
YeelightConnect = None
@dp.message(Command("start"))
async def cmd_start(message: types.Message):
if not check_ip(''):
help_command = html.underline(f"/ip 127.0.0.1")
await message.answer(f"Укажите ip\n\n{help_command}", parse_mode="HTML")
return
await render_button(message)
@dp.message(Command("ip"))
async def input_ip(
message: Message,
command: CommandObject
):
global strIP, YeelightConnect
if not check_ip(command.args):
await message.answer(
"Ошибка: ip некорректный"
)
return
try:
YeelightConnect = Bulb(strIP)
YeelightConnect.toggle()
time.sleep(1)
YeelightConnect.toggle()
await message.answer(
"💡"
)
await message.answer(
"✅ Подключение выполнено"
)
await render_button(message)
except Exception as err:
await message.answer(
"❌ Ошибка: " + str(err)
)
@dp.message()
async def bulb_turn(message: types.Message, reconnect=False):
global YeelightConnect
try:
if YeelightConnect is None:
raise Exception("Подключение не выполнено, укажите /ip")
if reconnect:
time.sleep(1)
if message.text.lower() == "включить":
YeelightConnect.turn_on()
elif message.text.lower() == "выключить":
YeelightConnect.turn_off()
elif int(message.text.replace('%', '')):
YeelightConnect.set_brightness(int(message.text.replace('%', '')))
except Exception as err:
if not reconnect:
await bulb_turn(message, True)
return
await message.answer(
"❌ Ошибка: " + str(err)
)
async def render_button(message):
kb = [
[
types.KeyboardButton(text="20%"),
types.KeyboardButton(text="40%"),
types.KeyboardButton(text="50%"),
types.KeyboardButton(text="70%"),
types.KeyboardButton(text="100%"),
],
[
types.KeyboardButton(text="Включить"),
types.KeyboardButton(text="Выключить")
]
]
keyboard = types.ReplyKeyboardMarkup(
keyboard=kb,
resize_keyboard=True,
input_field_placeholder="Лампочка.."
)
await message.answer("Действие:", reply_markup=keyboard)
def check_ip(ip):
global strIP
try:
if ip:
strIP = ip
ipaddress.ip_address(strIP)
return True
except ValueError:
return False
# start
async def main():
await dp.start_polling(bot)
if __name__ == '__main__':
asyncio.run(main())