Hello forum,
I’m trying my way to make the Asus Tinker Board’s GPIO I got the modules loading correctly. I have the only problem that init.py is calling a shell executable in a subprocess library.
The fact is that if I call the executable in a shell it works as expected, without root privileges. But inside HA, it doesn’t give any error but no actions is done.
This is the script:
"""Support for controlling GPIO pins of a Raspberry Pi."""
import logging
import shlex
import sys
from homeassistant.const import (
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
from subprocess import check_output, CalledProcessError
_LOGGER = logging.getLogger(__name__)
DOMAIN = 'asus_gpio'
def run_cmd(command, **options):
""" Trying out the command or return None at failure."""
if isinstance(command, str):
command = shlex.split(command)
test_cmd = ''
if not options:
options = {'universal_newlines' : True}
try:
test_cmd = check_output(command, **options)
except (FileNotFoundError, CalledProcessError):
return None
_LOGGER.warning('Error executing %s' %command)
sys.stdout.flush()
sys.stderr.flush()
return test_cmd
def setup(hass, config):
"""Set up the Asus Tinker Board component."""
def cleanup_gpio(event):
"""Stuff to do before stopping."""
if run_cmd('gpio exports'):
run_cmd('gpio unexportall')
def prepare_gpio(event):
"""Stuff to do when home assistant starts."""
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, cleanup_gpio)
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, prepare_gpio)
return True
def setup_output(port, mode):
"""Set up a GPIO as output."""
mode = mode.lower()
if mode not in ("down", "up", "tri", "pwm", "out", "output"):
_LOGGER.warning(" selected mode {} is not valid".format(mode))
run_cmd("gpio mode {} {}".format(port,mode))
def setup_input(port, pull_mode):
"""Set up a GPIO as input."""
mode = pull_mode.lower()
if mode not in ("down", "up", "tri", "in", "input"):
_LOGGER.warning(" selected mode {} is not valid".format(pull_mode))
run_cmd("gpio mode {} {}".format(port,mode))
def write_output(port, value):
"""Write a value to a GPIO."""
run_cmd("gpio write {} {}".format(port,value))
def read_input(port):
"""Read a value from a GPIO."""
run_cmd("gpio read {}".format(port))
This is the gpio command is also available for raspberry.
I set the logging, but it’s not reporting even in the configuration.yaml is prepared like this:
logger:
default: critical
logs:
home.assistant.components.asus_gpio: warning