We need to make Home Assistant able to access a Computer to launch BASH Scripts using the command_line integration.
Since I am running Home Assistant Core on Docker I don’t have the Supervisor options so I have to ssh into the Server running Docker and drop into the Home Assistant Container Shell. So here is a example.
firstname.lastname@example.org's password: XXXXXX
docker ps | grep homeassistant
81b764f18c58 homeassistant/home-assistant:stable "/init" 4 days ago Up 3 days homeassistant
docker exec -it 81b764f18c58 bash
What I did here was log into my Server running Docker then ran the docker ps command that will list all of my Containers but I piped it into Grep to only list Home Assistant so I can see the Container ID 81b764f18c58. I then ran docker exec -it 81b764f18c58 bash to drop into a Shell inside the Container.
Keep in mind this logs you in as Root in the container and Home Assistant uses Root.
Now we need to create a SSH Key to be able to access the PC we want without needing to use a password for each time it connects to it.
Just run ssh-keygen and just follow the defaults and leave the passphrase empty. Now we need to copy the Key. To do this issue something like this, be sure to change the User and IP address to match your layout/setup.
ssh-copy-id -i $HOME/.ssh/id_rsa.pub email@example.com
Enter your User’s password and you should see this.
Number of key(s) added: 1
Do a test by logging in and it shouldn’t ask you for a password.
On the PC we want to control we need to make sure xdotool is installed. Since I use Debian it uses APT as the Package Manager.
apt install xdotool
Now some Keyboard Macros need to be created on the PC we want to Control. For a example with KDE Plasma I used the Custom Shortcuts menu and right clicked then selected New > then Global Shortcut > then Command/URL. For this example we will do Terminator Shell. So label it as Terminal. Under Trigger I selected CTRL+1 and under Action I just entered terminator. Hit Apply and test it by hitting CTRL 1
Now the easy part. In the Home Assistant Configuration.yaml file we need to create a Switch.
- platform: command_line
command_on: ssh -X firstname.lastname@example.org "DISPLAY=:0 xdotool key ctrl+1"
- platform: command_line
command_on: ssh -X email@example.com "DISPLAY=:0 pkill terminator"
The command_on: statements is having HA login with SSH with Xforwarding and we are telling it run the command xdotool key ctrl+1. Since this is doing it remotely we need to tell it to do this to the main screen of the PC and that is when DISPLAY=:0 comes in.
As you can see there are two switches, one to Launch Terminator and one to Close Terminator. There are many ways to make Terminator to close but pkill seems the easiest but beware if you have two or more terminator processes running pkill will close them all.
After creating the two switches you need to restart Home Assistant.
Since the Control Panel running Room-Assistant is just forcing the GPIO to switch high or low with the toggle switches they’re programmed as Binary Switches.
Example Room-Assistant local.yml file
- name: controlswitch1
- name: controlswitch2
So in Home Assistant it sees it as binary_sensor.controlswitch1 and binary_sensor.controlswitch2.
In Home Assistant we need to create two Automations. You can do it the graphical way but I’m hardcore and use the Command Line and Nano.
alias: Terminal Launch
description: 'Terminal Launch'
alias: Terminal Close
description: 'Terminal Close'
After saving the file you can either restart Home Assistant again or just tell it to reload the Automations.
If you want to control other things that is already in Home Assistant you just create a Automation and call a service to switch.turn_on switch.turn_off or switch.toggle.
For a example controlling the Garage Lights.
Now that is pretty darn cool if you ask me.
Later on I will refine the code and show other things this simple Control Panel can do.