Sicuro che il problema sia che ask_status non viene eseguita?
Prova a mettere un print(datetime.now(UTC)) e vedi se e quante volte stampa la data

Federico 

On 13 May 2024, at 22:19, Perini Matteo <perini.mat...@gmail.com> wrote:



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
@ewelink.login('Password', 'em...@email.eu')
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
@ewelink.login('Password', 'em...@email.eu')
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 list
Python@lists.python.it
https://lists.python.it/mailman/listinfo/python
_______________________________________________
Python mailing list
Python@lists.python.it
https://lists.python.it/mailman/listinfo/python

Rispondere a