<!DOCTYPE html>
<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Ciao,</p>
    <p>vorrei leggere lo stato di un dispositivo ewelink (sonoff)
      tramite script ma ricevo un comportamento che non capisco.</p>
    <p>Se faccio uno script isolato <br>
    </p>
    <div
style="color: #000000;background-color: #ffffff;font-family: Consolas, 'Courier New', monospace;font-weight: normal;font-size: 14px;line-height: 19px;white-space: pre;"><div><span
    style="color: #af00db;">import</span><span style="color: #000000;"> </span><span
    style="color: #267f99;">ewelink</span></div><div><span
    style="color: #af00db;">from</span><span style="color: #000000;"> </span><span
    style="color: #267f99;">ewelink</span><span style="color: #000000;"> </span><span
    style="color: #af00db;">import</span><span style="color: #000000;"> </span><span
    style="color: #267f99;">Client</span><span style="color: #000000;">, </span><span
    style="color: #267f99;">DeviceOffline</span></div><div><span
    style="color: #795e26;">@</span><span style="color: #267f99;">ewelink</span><span
    style="color: #795e26;">.</span><span style="color: #795e26;">login</span><span
    style="color: #000000;">(</span><span style="color: #a31515;">'Password'</span><span
    style="color: #000000;">, </span><span style="color: #a31515;">'<a class="moz-txt-link-abbreviated" href="mailto:email@email.eu">email@email.eu</a>'</span><span
    style="color: #000000;">)</span></div><div><span
    style="color: #0000ff;">async</span><span style="color: #000000;"> </span><span
    style="color: #0000ff;">def</span><span style="color: #000000;"> </span>ask_status<span
    style="color: #000000;">(</span><span style="color: #001080;">client</span><span
    style="color: #000000;">: </span><span style="color: #267f99;">Client</span><span
    style="color: #000000;">):</span></div><div><span
    style="color: #000000;">    </span><span style="color: #001080;">device</span><span
    style="color: #000000;"> = </span><span style="color: #001080;">client</span><span
    style="color: #000000;">.</span><span style="color: #795e26;">get_device</span><span
    style="color: #000000;">(</span><span style="color: #a31515;">'100000000'</span><span
    style="color: #000000;">) </span></div><div><span
    style="color: #000000;">    </span><span style="color: #af00db;">try</span><span
    style="color: #000000;">:</span></div><div><span
    style="color: #000000;">        </span><span style="color: #795e26;">print</span><span
    style="color: #000000;">(</span><span style="color: #001080;">device</span><span
    style="color: #000000;">.</span><span style="color: #001080;">state</span><span
    style="color: #000000;">.</span><span style="color: #001080;">value</span><span
    style="color: #000000;">)</span></div><div><span
    style="color: #000000;">    </span><span style="color: #af00db;">except</span><span
    style="color: #000000;"> </span><span style="color: #267f99;">DeviceOffline</span><span
    style="color: #000000;">:</span></div><div><span
    style="color: #000000;">        </span><span style="color: #795e26;">print</span><span
    style="color: #000000;">(</span><span style="color: #a31515;">"Device is offline!"</span><span
    style="color: #000000;">)</span></div></div>
    <p></p>
    <p>ottengo esattamente quello che voglio ovvero il print mi dice se
      il dispositivo è on, off o offline.</p>
    <p><br>
    </p>
    <p>ho provato ad integrare la stessa funzione in uno script un po'
      più articolato per comunicare con un bot telegram.</p>
    <div
style="color: #000000;background-color: #ffffff;font-family: Consolas, 'Courier New', monospace;font-weight: normal;font-size: 14px;line-height: 19px;white-space: pre;"><div><span
    style="color: #af00db;">from</span><span style="color: #000000;"> </span><span
    style="color: #267f99;">telegram</span><span style="color: #000000;"> </span><span
    style="color: #af00db;">import</span><span style="color: #000000;"> </span><span
    style="color: #267f99;">Update</span></div><div><span
    style="color: #af00db;">from</span><span style="color: #000000;"> </span><span
    style="color: #267f99;">telegram</span><span style="color: #000000;">.</span><span
    style="color: #267f99;">ext</span><span style="color: #000000;"> </span><span
    style="color: #af00db;">import</span><span style="color: #000000;"> </span><span
    style="color: #267f99;">ApplicationBuilder</span><span
    style="color: #000000;">, </span><span style="color: #267f99;">ContextTypes</span><span
    style="color: #000000;">, </span><span style="color: #267f99;">CommandHandler</span><span
    style="color: #000000;">, </span><span style="color: #267f99;">MessageHandler</span><span
    style="color: #000000;">, </span><span style="color: #267f99;">filters</span></div><div><span
    style="color: #af00db;">from</span><span style="color: #000000;"> </span><span
    style="color: #267f99;">credentials</span><span
    style="color: #000000;"> </span><span style="color: #af00db;">import</span><span
    style="color: #000000;"> </span><span style="color: #0070c1;">TOKEN</span></div><div><span
    style="color: #af00db;">import</span><span style="color: #000000;"> </span><span
    style="color: #267f99;">ewelink</span></div><div><span
    style="color: #af00db;">from</span><span style="color: #000000;"> </span><span
    style="color: #267f99;">ewelink</span><span style="color: #000000;"> </span><span
    style="color: #af00db;">import</span><span style="color: #000000;"> </span><span
    style="color: #267f99;">Client</span><span style="color: #000000;">, </span><span
    style="color: #267f99;">DeviceOffline</span></div></div>
    <div
style="color: #000000;background-color: #ffffff;font-family: Consolas, 'Courier New', monospace;font-weight: normal;font-size: 14px;line-height: 19px;white-space: pre;">
</div>
    <div
style="color: #000000;background-color: #ffffff;font-family: Consolas, 'Courier New', monospace;font-weight: normal;font-size: 14px;line-height: 19px;white-space: pre;"><div><span
    style="color: #795e26;">@</span><span style="color: #267f99;">ewelink</span><span
    style="color: #795e26;">.</span><span style="color: #795e26;">login</span><span
    style="color: #000000;">(</span><span style="color: #a31515;">'Password'</span><span
    style="color: #000000;">, </span><span style="color: #a31515;">'<a class="moz-txt-link-abbreviated" href="mailto:email@email.eu">email@email.eu</a>'</span><span
    style="color: #000000;">)</span></div><div><span
    style="color: #0000ff;">async</span><span style="color: #000000;"> </span><span
    style="color: #0000ff;">def</span><span style="color: #000000;"> </span>ask_status<span
    style="color: #000000;">(</span><span style="color: #001080;">client</span><span
    style="color: #000000;">: </span><span style="color: #267f99;">Client</span><span
    style="color: #000000;">):</span></div><div><span
    style="color: #000000;">    </span><span style="color: #001080;">device</span><span
    style="color: #000000;"> = </span><span style="color: #001080;">client</span><span
    style="color: #000000;">.</span><span style="color: #795e26;">get_device</span><span
    style="color: #000000;">(</span><span style="color: #a31515;">'100000000'</span><span
    style="color: #000000;">) </span></div><div><span
    style="color: #000000;">    </span><span style="color: #af00db;">try</span><span
    style="color: #000000;">:</span></div><div><span
    style="color: #000000;">        </span><span style="color: #795e26;">print</span><span
    style="color: #000000;">(</span><span style="color: #001080;">device</span><span
    style="color: #000000;">.</span><span style="color: #001080;">state</span><span
    style="color: #000000;">.</span><span style="color: #001080;">value</span><span
    style="color: #000000;">)</span></div><div><span
    style="color: #000000;">    </span><span style="color: #af00db;">except</span><span
    style="color: #000000;"> </span><span style="color: #267f99;">DeviceOffline</span><span
    style="color: #000000;">:</span></div><div><span
    style="color: #000000;">        </span><span style="color: #795e26;">print</span><span
    style="color: #000000;">(</span><span style="color: #a31515;">"Device is offline!"</span><span
    style="color: #000000;">)</span></div><div><span
    style="color: #000000;">
</span></div><div><span style="color: #0000ff;">async</span><span
    style="color: #000000;"> </span><span style="color: #0000ff;">def</span><span
    style="color: #000000;"> </span><span style="color: #795e26;">start</span><span
    style="color: #000000;">(</span><span style="color: #001080;">update</span><span
    style="color: #000000;">: </span><span style="color: #267f99;">Update</span><span
    style="color: #000000;">, </span><span style="color: #001080;">context</span><span
    style="color: #000000;">: </span><span style="color: #267f99;">ContextTypes</span><span
    style="color: #000000;">.</span><span style="color: #001080;">DEFAULT_TYPE</span><span
    style="color: #000000;">):</span></div><div><span
    style="color: #000000;">    </span><span style="color: #af00db;">await</span><span
    style="color: #000000;"> </span><span style="color: #001080;">context</span><span
    style="color: #000000;">.</span><span style="color: #001080;">bot</span><span
    style="color: #000000;">.</span><span style="color: #795e26;">send_message</span><span
    style="color: #000000;">(</span><span style="color: #001080;">chat_id</span><span
    style="color: #000000;">=</span><span style="color: #001080;">update</span><span
    style="color: #000000;">.</span><span style="color: #001080;">effective_chat</span><span
    style="color: #000000;">.</span><span style="color: #001080;">id</span><span
    style="color: #000000;">, </span><span style="color: #001080;">text</span><span
    style="color: #000000;">=</span><span style="color: #a31515;">"I'm a bot, please talk to me!"</span><span
    style="color: #000000;">)</span></div>
<div><span style="color: #0000ff;">async</span><span
    style="color: #000000;"> </span><span style="color: #0000ff;">def</span><span
    style="color: #000000;"> </span><span style="color: #795e26;">check</span><span
    style="color: #000000;">(</span><span style="color: #001080;">update</span><span
    style="color: #000000;">: </span><span style="color: #267f99;">Update</span><span
    style="color: #000000;">, </span><span style="color: #001080;">context</span><span
    style="color: #000000;">: </span><span style="color: #267f99;">ContextTypes</span><span
    style="color: #000000;">.</span><span style="color: #001080;">DEFAULT_TYPE</span><span
    style="color: #000000;">):</span></div><div><span
    style="color: #000000;">    </span><span style="color: #af00db;">await</span><span
    style="color: #000000;"> </span><span style="color: #001080;">context</span><span
    style="color: #000000;">.</span><span style="color: #001080;">bot</span><span
    style="color: #000000;">.</span><span style="color: #795e26;">send_message</span><span
    style="color: #000000;">(</span><span style="color: #001080;">chat_id</span><span
    style="color: #000000;">=</span><span style="color: #001080;">update</span><span
    style="color: #000000;">.</span><span style="color: #001080;">effective_chat</span><span
    style="color: #000000;">.</span><span style="color: #001080;">id</span><span
    style="color: #000000;">, </span><span style="color: #001080;">text</span><span
    style="color: #000000;">=</span><span style="color: #795e26;">ask_status</span><span
    style="color: #000000;">)</span></div>
<div><span style="color: #af00db;">if</span><span
    style="color: #000000;"> </span><span style="color: #001080;">__name__</span><span
    style="color: #000000;"> == </span><span style="color: #a31515;">'__main__'</span><span
    style="color: #000000;">:</span></div><div><span
    style="color: #000000;">    </span><span style="color: #001080;">application</span><span
    style="color: #000000;"> = </span><span style="color: #267f99;">ApplicationBuilder</span><span
    style="color: #000000;">().</span><span style="color: #795e26;">token</span><span
    style="color: #000000;">(</span><span style="color: #0070c1;">TOKEN</span><span
    style="color: #000000;">).</span><span style="color: #795e26;">build</span><span
    style="color: #000000;">()</span></div><div><span
    style="color: #000000;">    </span></div><div><span
    style="color: #000000;">    </span><span style="color: #001080;">start_handler</span><span
    style="color: #000000;"> = </span><span style="color: #267f99;">CommandHandler</span><span
    style="color: #000000;">(</span><span style="color: #a31515;">'start'</span><span
    style="color: #000000;">, </span><span style="color: #795e26;">start</span><span
    style="color: #000000;">)</span></div><div><span
    style="color: #000000;">    </span><span style="color: #001080;">check_handler</span><span
    style="color: #000000;"> = </span><span style="color: #267f99;">CommandHandler</span><span
    style="color: #000000;">(</span><span style="color: #a31515;">'check'</span><span
    style="color: #000000;">, </span><span style="color: #795e26;">check</span><span
    style="color: #000000;">)</span></div><div><span
    style="color: #000000;">    </span><span style="color: #001080;">application</span><span
    style="color: #000000;">.</span><span style="color: #795e26;">add_handler</span><span
    style="color: #000000;">(</span><span style="color: #001080;">start_handler</span><span
    style="color: #000000;">)</span></div><div><span
    style="color: #000000;">    </span><span style="color: #001080;">application</span><span
    style="color: #000000;">.</span><span style="color: #795e26;">add_handler</span><span
    style="color: #000000;">(</span><span style="color: #001080;">check_handler</span><span
    style="color: #000000;">)</span></div><div><span
    style="color: #000000;">    </span><span style="color: #001080;">application</span><span
    style="color: #000000;">.</span><span style="color: #795e26;">run_polling</span><span
    style="color: #000000;">()</span></div></div>
    <p></p>
    <p><br>
    </p>
    <p>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.</p>
    <p>Come posso fare per forzare l'esecuzione di ask_status ogni volta
      che richiamo la funzione check?</p>
    <p>Cosa mi sto perdendo?</p>
    <p><br>
    </p>
    <p>Grazie a chi mi può dare un suggerimento</p>
    <p>Matteo <br>
    </p>
  </body>
</html>