Hope someone here could help out.
I have been struggling to create an appdaemon app to telnet read from my proprietary home automation server (easyplus) containing switches, lights and temperature.
I have connected easyplus to a sonoff switch so I can turn it off when not in use, mostly during the day and when away. Also I use a template binary sensor (binary_sensor.easyplus_telnet) to check whether telnet connection is up.
In a first instance I would like to live read data from easyplus when connection is up so I can update states of my home assistant switches. Later on I would like to telnet write data to easyplus to toggle switches, and set lights and temperature.
So my issue: my current code works but creates a queue in appdaemon when telnet connection is down. So it seems that it is not able to break from the read function. Below main snippets from my code.
def initialize(self):
self.HOST = self.args["host"] #set telnet ip
self.PORT = self.args["port"] #set telnet port
self.TIMEOUT = 1 #set telnet timeout
self.PASS = self.args["pass"]
self.EEBS = telnetlib.Telnet() #telnet object
self.log_level = 0 #set to 0 to supress logs in functions
self.connect = False #1 means telnet connected and ready to read data
# easyplus component start getdata schedule and live scan
self.listen_state(self.EEBS_stop, 'binary_sensor.easyplus_telnet', new = "off")
self.listen_state(self.EEBS_live, 'binary_sensor.easyplus_telnet', new = "on")
self.set_state('binary_sensor.easyplus_telnet', state = "off") #to initialize during ad restart and binary online
def EEBS_live(self, entity, attribute, old, new, kwargs):
self.log("EEBS_live initiated")
try:
try:
self.EEBS.open(self.HOST,self.PORT,self.TIMEOUT)
except (ConnectionRefusedError, OSError):
self.log("EEBS_live - refused/unavailable connection")
return
self.EEBS.write(self.PASS.encode())
self.log("connection successful")
self.log("EEBS_live is ready")
self.connect = True
except telnetlib.socket.timeout:
self.log("EEBS_live - connection timed out")
self.run_in(self.EEBS_read(kwargs),0)
def EEBS_read(self, kwargs):
if self.log_level > 0:
self.log("EEBS_read started")
if self.connect: #ideally if binary offline to stop EES_read
timeout = self.datetime() + timedelta(seconds=1)
while True: #additional setting to stop EEBS_read when binary offline after timeout
self.data = self.EEBS.read_until(b"\r\n", timeout=0.2)
if self.data.startswith(b">"):
self.EEBS_encode(kwargs)
if self.datetime() > timeout:
break
self.run_in(self.EEBS_read(kwargs), 1)