Hassio Integration with other entities using SSH - OrangeReaper/homeassistant GitHub Wiki

cf: Home Assistant Wake on Lan

Prerequisites: SSH add on

Table of Contents

Generate SSH Key

from hassio (terminal), change to the config directory, Generate a passphraseless SSH key and push it to entity.

  $ cd /config
  $ mkdir .ssh
  $ ssh-keygen -t rsa -b 2048

Response:

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
......    cut     ......

Push Key to Entity

Next copy the files to /config/.ssh

  $ cp /root/.ssh/* .ssh/

This is what we end up with:

  $ ls .ssh
  authorized_keys  id_rsa           id_rsa.pub       known_hosts8

Next Copy your keys to the target server(s); ie for 192.168.1.185:

  $ ssh-copy-id [email protected]
  [email protected]'s password: 

This command establishes trust between the server (192.168.1.185) and any SSH Client connecting to the server, using these keys to authenticate.

Note: You may wish to use a different user (not root); in my case I wish root access to the device

Now try logging into the machine, with ssh like this

  $ ssh [email protected]

If everything worked then you should get an ssh connection to 192.168.1.185

Example Automation

In this case, as we are connecting as root we can issue any command we like from home assistant. In the following example we setup a shell_command which actually issues the command to the entity, using ssh; a switch, which invokes the shell_command; and an automation that uses the switch...

Shell Command

shell_command:
  turn_off_defiant: 'ssh -o UserKnownHostsFile=/config/.ssh/knownhosts -o StrictHostKeyChecking=no -i /config/.ssh/id_rsa [email protected] sudo shutdown -Ph now'

Switch

switch:
  - platform: wake_on_lan
    name: defiant_wol
    mac_address: "AA:BB:CC:DD:EE:FF"
    host: "192.168.1.185"
    turn_off:
      service: shell_command.turn_off_defiant

Automation

alias: NAS Drive OFF
initial_state: false
trigger:
  platform: state  
  entity_id: group.computers
  from: "on"
  to: "off"
  for: "00:05:00"
action:
  - service: switch.turn_off
    entity_id: switch.defiant_wol
⚠️ **GitHub.com Fallback** ⚠️