Skip to content

Instantly share code, notes, and snippets.

@edwork
Last active October 4, 2023 11:08
Show Gist options
  • Save edwork/6aa6fdab04bc6e5612a912c645318c0f to your computer and use it in GitHub Desktop.
Save edwork/6aa6fdab04bc6e5612a912c645318c0f to your computer and use it in GitHub Desktop.

CEC-Detection

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

@boralyl
Copy link

boralyl commented Jan 26, 2019

For what it's worth, I had to add a switch in my ssh command to get this to work on hassio: -o StrictHostKeyChecking=no. It tells the SSH client to not prompt you with warning messages or yes/no questions when host has changed. e.g.

switch:
  - platform: command_line
    switches:
      tv_power:
        command_on: "ssh pi@<ip_address> -i /home/homeassistant/.homeassistant/id_homeassistant -o StrictHostKeyChecking=no 'echo on 0 | cec-client -s -d 1'"
        command_off: "ssh pi@<ip_address> -i /home/homeassistant/.homeassistant/id_homeassistant -o StrictHostKeyChecking=no 'echo standby 0 | cec-client -s -d 1'"
        command_state: "tvstatus() { local RESULTS; RESULTS=$(ssh pi@<ip_address> -i <path_to_ssh_key> -o StrictHostKeyChecking=no \"echo pow 0 | cec-client -s -d 1 | grep -q 'power status: on'\"); echo $?; }; tvstatus"
        value_template: '{{ value == "0" }}'
        friendly_name: TV

From: https://megamorphf.github.io/homeassistant/hyperion/ssh/hassio/2018/01/22/controlling-anything-via-ssh.html

@BenJamesAndo
Copy link

As of 2023.6 command_line switches YAML needs to be changed.
Should now be

command_line:
  - switch:
      unique_id: tv_power
      name: TV
      command_on:

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

command_line:
  - switch:
      unique_id: tv_power
      name: TV
      command_on: "ssh pi@<ip_address> -i /config/.ssh/id_rsa -o StrictHostKeyChecking=no 'echo on 0 | cec-client -s -d 1'"
      command_off: "ssh pi@<ip_address> -i /config/.ssh/id_rsa -o StrictHostKeyChecking=no 'echo standby 0 | cec-client -s -d 1'"
      command_state: "tvstatus() { local RESULTS; RESULTS=$(ssh pi@<ip_address> -i /config/.ssh/id_rsa -o StrictHostKeyChecking=no \"echo pow 0 | cec-client -s -d 1 | grep -q 'power status: on'\"); echo $?; }; tvstatus"
      value_template: '{{ value == "0" }}'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment