Telnet switch with login (command_line switch )

I use this to login over telnet to a raspberry pi and control the display, (black it at night) but you should be able to modify it to do what ever you need.
rpi_display_control_off.py

#!/usr/bin/python3
import telnetlib
import time
HOST = "192.168.1.165"                                      # ip adress of whatever you want to control
telnet = telnetlib.Telnet(HOST)
telnet.read_until(b":",3)                                   # wait for : before writing username
telnet.write(b"pi\n")
telnet.read_until(b":",3)                                   # wait for : before writing password
telnet.write(b"raspberry\n")
telnet.read_until(b"pi@raspberrypi:",10)                    # wait for pi@raspberrypi: before writing command
telnet.write(b"vcgencmd display_power 0\n")                 # command
telnet.read_until(b"pi@raspberrypi:",5)                     # wait for pi@raspberrypi: now command are executed
telnet.write(b"exit\n")                                     # all done exit and close
telnet.close()

add this to a folder called python_scripts_commandline in your ha directory
the path to this folder may wary depending on your ha installation method

and in configuration.yaml
you might need to change the path to the file

switch:
  - platform: command_line
    switches:
      rpi_display:
        command_on: "python3 /config/python_scripts_commandline/rpi_display_control_on.py"
        command_off: "python3 /config/python_scripts_commandline/rpi_display_control_off.py"

this looks simple but took me many hours to figure out, i hope it will save time for someone

5 Likes

What version of HA you running ? I downloaded the virtual appliance and set that up.

i.e. python3 built into the build or do any packages need installed ?

not sure, i’m running latest version of Home Assistant Supervised and it just works, not sure what you need on other versions of HA this is the only one i have used so far.

@swedude Your hours spent just saved me countless hours! Much appreciated! I was able to use the commands, modified for my use, to remotely turn off/on the outlets of a PDU. I then added that to an automation to automatically power cycle the outlet if the current draw on that outlet fell below a certain level. I’m able to pull the current from SNMP.

Again, much thanks!!

Had this parked for a while but as is the way i have renewed vigour to get all the small jobs that i didnt finish done.

This is one of them … telenet control of my HDMI matrix

I have it working on my command line so not worried so much about the python script itself, however when i run it through the python scripts in HA i get the following error in the logs

2023-01-12 10:05:38.359 ERROR (SyncWorker_18) [homeassistant.components.python_script.fire_stick_to_living_room_tv.py] Error executing script: __import__ not found
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 224, in execute
    exec(compiled.code, restricted_globals)
  File "fire_stick_to_living_room_tv.py", line 2, in <module>
ImportError: __import__ not found

So considering that your example above has exactly the same imports i was wondering how you got around this ?

Thanks

hello I didn’t get around it I skipped HA by just calling it as a command line switch, notice I don’t have the code in the ordinary python folder instead I have it in “python_scripts_commandline”.
I’m running Home Assistant Supervised installation not sure how this works for other installs sorry.