I’m trying to run a few lines of telnet commands which log into my Draytek router return state of my wifi network (which clients are connected to which access point). I have a standalone python script which does that just fine (raw text which needs to be tidied up, but that’s the next step).
My questions:
- What’s the best way to call telnet command (open connection → report → print the output → close connection) from HA? E.g. I can run this code in PyScript:
import telnetlib
@service
def report_stations(action=None, id=None):
HOST = "XXX.XXX.X.X"
user = "admin"
password = "!secret"
tn = telnetlib.Telnet()
tn.open(HOST)
tn.read_until(b"Account:")
tn.write(user.encode('ascii') + b"\n")
tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")
tn.write(b"apm show stalist\n")
tn.write(b"exit\n")
log.info(tn.read_all().decode('ascii'))
tn.close()
But it only writes the result into HA log. If I try to use hass object to save it as a state or an attribute of an object, then I’m running into Event Loop problems (‘RuntimeError: Cannot be called from within the event loop’)
- After I solve #1 above, what’s the best way to store the output in HA? I understand that a sensor or helper allows only 255 characters, should I store it as an sensor attributes? I assume so, but then I need to first solve #1…