Want to control a TV over HDMI-CEC via a remote RasPi? Include the following as a switch:
platform: command_line
switches:
tv_power:
command_on: "ssh pi@<ip_address> -i /home/homeassistant/.homeassistant/id_homeassistant 'echo on 0 | cec-client -s -d 1'"
command_off: "ssh pi@<ip_address> -i /home/homeassistant/.homeassistant/id_homeassistant 'echo standby 0 | cec-client -s -d 1'"
command_state: "tvstatus() { local RESULTS; RESULTS=$(ssh pi@<ip_address> -i <path_to_ssh_key> \"echo pow 0 | cec-client -s -d 1 | grep -q 'power status: on'\"); echo $?; }; tvstatus"
value_template: '{{ value == "0" }}'
friendly_name: TV
Be sure to replace '<ip_address>' and '<path_to_ssh_key>'
'command_state' looks overly complicated but a shell function is required to return the proper exit code, passed to HASS
Inspiration from: /u/rockNme2349 on Reddit
As of 2023.6 command_line switches YAML needs to be changed.
Should now be
For anyone like me that has never dealt with SSH keys before below is the steps I took to get it working.
SSH into Home Assistant.
Then type
ssh-keygen
Enter file in which to save the key:
/config/.ssh/id_rsa
chmod 600 /config/.ssh/id_rsa
ssh-copy-id -i /config/.ssh/id_rsa.pub pi@<ip_address>
My full YAML code in configuration.yaml