Ciao,
vorrei leggere lo stato di un dispositivo ewelink (sonoff)
tramite script ma ricevo un comportamento che non capisco.
Se faccio uno script isolato
import ewelink
from ewelink import Client, DeviceOffline
async def ask_status(client: Client):
device = client.get_device('100000000')
try:
print(device.state.value)
except DeviceOffline:
print("Device is offline!")
ottengo esattamente quello che voglio ovvero il print mi dice se
il dispositivo è on, off o offline.
ho provato ad integrare la stessa funzione in uno script un po'
più articolato per comunicare con un bot telegram.
from telegram import Update
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, filters
from credentials import TOKEN
import ewelink
from ewelink import Client, DeviceOffline
async def ask_status(client: Client):
device = client.get_device('100000000')
try:
print(device.state.value)
except DeviceOffline:
print("Device is offline!")
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!")
async def check(update: Update, context: ContextTypes.DEFAULT_TYPE):
await context.bot.send_message(chat_id=update.effective_chat.id, text=ask_status)
if __name__ == '__main__':
application = ApplicationBuilder().token(TOKEN).build()
start_handler = CommandHandler('start', start)
check_handler = CommandHandler('check', check)
application.add_handler(start_handler)
application.add_handler(check_handler)
application.run_polling()
Il bot funziona (è un po' più complicato di così) ma quando
chiamo "ask_status" mi restituisce sempre lo stato che aveva il
dispositivo quando faccio partire il bot.
Come posso fare per forzare l'esecuzione di ask_status ogni volta
che richiamo la funzione check?
Cosa mi sto perdendo?
Grazie a chi mi può dare un suggerimento
Matteo
_______________________________________________Python mailing listPython@lists.python.ithttps://lists.python.it/mailman/listinfo/python