#Help with using HA to SSH to an ethernet switch and control PoE port power

1 messages · Page 1 of 1 (latest)

sour patio
#

I'm running HA on a RPi, installed using the standard Home Assistant OS image method. I want to be able to have HA run commands on a PoE ethernet switch I have for work. I planned on using an expect script to ssh into the device and run the commands. That doesn't work since expect isn't installed. I found some notes about possibly using pexpect in a python script, but it doesn't look like pexpect is installed either. Is there a way to get expect or pexpect installed? Or maybe someone knows a better method of doing this? The script just turns PoE on or off to specific ports based on the MAC_ADDRESS provided. I have a working expect script. Hoping for something like this:

command_line:
  - switch:
      name: Device Power Command Line
      command_on: 'python /config/python/poe.py <MAC_ADDRESS> on'
      command_off: 'python /config/python/poe.py <MAC_ADDRESS> off'
#

Help with using HA to SSH to an ethernet switch and control PoE port power

sharp zealot
#

You can install expect via apk.

#

So basically you need an automation that runs apk add expect when HA is started and can then use it in your commands.

sour patio
#

@sharp zealot Yes, I was able to install expect using the terminal addon and was able to successfully run my script. However, the methods I tried to setup up a shell or command line switch didn't seem to recognize expect.

#

Ah, that might be the difference

sharp zealot
#

The SSH addon is in its own container. Installing things there doesn't affect core.

sour patio
#

I did not know you could do that. From my understanding, running from the terminal addon is... yes, what you said, a different container

#

Let me try that out

sharp zealot
#

If you use the Advanced SSH Addon with disabled protection mode you can do

docker exec -ti homeassistant sh

to join the HA container. Test your commands in there.

sour patio
#

@sharp zealot Alright, adding a command line apk add expect is not as straight forward as I thought. Is the simple way to add a command line switch and then have the automation at startup run switch turn on?

#
command_line:
  - switch:
      name: Install Expect
      command_on: "apk add expect"
sharp zealot
#

Like I said I'd recommend an automation that does that when HA starts.

sour patio
#

Yeah, the automation has to have something to run, a device or button or something

sharp zealot
#

Oh. Right.

#

Just a shell command should be fine.

sour patio
#

Oh duh, forgot about shell_command. Used to be more familiar with HA but it's been years since I last dove in

sour patio
#

I've got it working except for one thing. When the PoE switch powers on, the devices connected are default on too. The issue is that the switch shows up as default off when added to the UI and the value_template doesn't seem to be updating it's state. The binary_sensor.192_168_0_51 is just a ping integration that shows the device is online and connected and the template evaluates to true in the template tester. @sharp zealot any ideas?

command_line:
  - switch:
      name: Power Command Line
      value_template: '{{ is_state("binary_sensor.192_168_0_51", "on") }}'
      command_on: 'expect /config/shell_scripts/poe.exp <MAC_ADDRESS> on'
      command_off: 'expect /config/shell_scripts/poe.exp <MAC_ADDRESS> off'
#

Interestingly, a helper switch, with turn on/off actions just set to shell_command integrations to run those scripts works with that exact same value_template.