Hello, I’m trying to turn on a switch in homeassistant. I call the shell command through the switch and it doesnt work, but when I run it in the terminal, it works fine. I believe this has to do with the homeassistant user permissions. I’m not familiar with Linux and was wondering if someone could help me out. Below is the code I am using. Thank you
Python Code
#!/usr/bin/python
from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor
import time
import atexit
# create a default object, no changes to I2C address or frequency
mh = Adafruit_MotorHAT(addr=0x60)
def turnOffMotors():
mh.getMotor(1).run(Adafruit_MotorHAT.RELEASE)
mh.getMotor(2).run(Adafruit_MotorHAT.RELEASE)
mh.getMotor(3).run(Adafruit_MotorHAT.RELEASE)
mh.getMotor(4).run(Adafruit_MotorHAT.RELEASE)
atexit.register(turnOffMotors)
myMotor = mh.getMotor(3)
myMotorTwo = mh.getMotor(4)
# set the speed to start, from 0 (off) to 255 (max speed)
myMotor.setSpeed(255)
myMotorTwo.setSpeed(255)
while (True):
myMotor.run(Adafruit_MotorHAT.BACKWARD)
for i in range(255):
myMotor.setSpeed(i)
while True:
time.sleep(0.01)
time.sleep(1.0)
Configuration.yaml
switch:
- platform: rpi_gpio
ports:
22: TestLED1
12: TestLED2
- platform: command_line
switches:
medium:
command_on: 'python /home/pi/test2-bright.py'
command_off: 'python /home/pi/test2-bright.py'
Error when running command from homeassistant user
pi@raspberrypi:~ $ sudo su -s /bin/bash homeassistant
(homeassistant_venv) homeassistant@raspberrypi:/home/pi $ python2 /home/pi/test2-bright.py
Traceback (most recent call last):
File "/home/pi/test2-bright.py", line 8, in <module>
mh = Adafruit_MotorHAT(addr=0x60)
File "build/bdist.linux-armv7l/egg/Adafruit_MotorHAT/Adafruit_MotorHAT_Motors.py", line 231, in __init__
File "build/bdist.linux-armv7l/egg/Adafruit_MotorHAT/Adafruit_PWM_Servo_Driver.py", line 57, in __init__
File "build/bdist.linux-armv7l/egg/Adafruit_MotorHAT/Adafruit_PWM_Servo_Driver.py", line 21, in get_i2c_device
File "build/bdist.linux-armv7l/egg/Adafruit_GPIO/I2C.py", line 64, in get_i2c_device
File "build/bdist.linux-armv7l/egg/Adafruit_GPIO/I2C.py", line 97, in __init__
File "build/bdist.linux-armv7l/egg/Adafruit_PureIO/smbus.py", line 97, in __init__
File "build/bdist.linux-armv7l/egg/Adafruit_PureIO/smbus.py", line 122, in open
IOError: [Errno 13] Permission denied: '/dev/i2c-1'
Thank you!