[Solved] Migrating to virtualenv: nmap, shell scripts and command line sensor no longer working

I am currently migrating a homeassistant installation (without virtualenv) into a new virtualenv one. I followed the corresponding guide on the website except using the defaut user pi instead of an own system account for homeassistant. After copying my old configuration everything worked out of the box except nmap presence detection, command line sensors and switches and shell scripts. The old config:

The command line sensor reads the first line of a txt file:

- platform: command_line
  name: foo
  unit_of_measurement: "bar"
  scan_interval: 21600
  command: head -1 /home/pi/data.txt | tail -1

The command line switch checks state of my PS4 and uses netcat and ps4-waker:

- platform: command_line
  switches:
    playstation4:
      command_on: 'sudo ps4-waker -c /home/pi/.homeassistant/ps4-wake.credentials.json'
      command_off: 'sudo ps4-waker standby -c /home/pi/.homeassistant/ps4-wake.credentials.json'
      command_state: 'nc -z -w 5 ps4-2d31a 9295'
      friendly_name: "Playstation 4"

The shell commands are triggered by an input select from the gui:

raspi_reboot: "sudo reboot"
raspi_shutdown: "sudo shutdown -h now"

When looking into /var/log/daemon.log the command line switch throws the following error: hass[2687]: /bin/sh: 1: nc: not found. Netcat is installed but cannot be found from venv. I guess it’s the same problem with nmap, tail and head.

Before moving into virtualenv all these commands executed fine. I have two questions:

  1. In what way do I have to change them to make them work in a virtual environment?
  2. Is it possible to use sudo commands from venv like sudo reboot?

I finally got it working. If anyone else is having troubles here are the solutions:

The command line sensors and the shell scripts require full path:

- platform: command_line
  name: foo
  unit_of_measurement: "bar"
  scan_interval: 21600
  command: /usr/bin/head -1 /home/pi/data.txt | tail -1

and

raspi_reboot: "/usr/bin/sudo /sbin/shutdown -r now"
raspi_shutdown: "/usr/bin/sudo /sbin/shutdown -h now"

nmap
nmap could not find package: arp. Solution is in the Forum.

I just applied a fix to the code, testing it now - will update in 15mins
I think the problem is that root does not know where to find it, so I told it.

/usr/local/lib/python3.4/dist-packages/homeassistant/components/device_tracker/nmap_tracker.py line 45
update to:
cmd = [‘/usr/sbin/arp’, ‘-n’, ip_address]
from
cmd = [‘arp’, ‘-n’, ip_address]

The above solution is for a system installation. For virtualenv users the path to nmap_tracker.py would be: ‘/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/device_tracker/nmap_tracker.py’

1 Like