Appdaemon 4 under HASSIO on Raspberry Pi4 - how to access GPIO

How can I access GPIO ports on Raspberry PI under HASSIO and Appdaemon 4?
After running the python script where I use “import RPi.GPIO as GPIO” I see this error:

File “/usr/lib/python3.8/site-packages/RPi/GPIO/init.py”, line 23, in
from RPi._GPIO import *
RuntimeError: This module can only be run on a Raspberry Pi!

My python script:

import appdaemon.plugins.hass.hassapi as hass

import os
import time
from time import sleep
import signal
import sys
import RPi.GPIO as GPIO

class FanPwm(hass.Hass):

    def initialize(self):
        self.log("Reading Raspberry Pi temperature")
        temperature = os.popen('cat /sys/class/thermal/thermal_zone0/temp').readline()
        temperatureDouble = int(temperature) / 1000
        print(temperatureDouble)

Log:
[s6-init] making user provided files available at /var/run/s6/etc…exited 0.
[s6-init] ensuring user provided files have correct perms…exited 0.
[fix-attrs.d] applying ownership & permissions fixes…
[fix-attrs.d] done.
[cont-init.d] executing container initialization scripts…
[cont-init.d] 00-banner.sh: executing…

Add-on: AppDaemon 4
Python Apps and Dashboard using AppDaemon 4.x for Home Assistant

Add-on version: 0.3.1
You are running the latest version of this add-on.
System: Raspbian GNU/Linux 10 (buster) (armv7 / raspberrypi4)
Home Assistant Core: 0.118.2
Home Assistant Supervisor: 2020.11.0

Please, share the above information when looking for help
or support in, e.g., GitHub, forums or the Discord chat.

[cont-init.d] 00-banner.sh: exited 0.
[cont-init.d] 01-log-level.sh: executing…
Log level is set to INFO
[cont-init.d] 01-log-level.sh: exited 0.
[cont-init.d] appdaemon.sh: executing…
Looking in links: https://wheels.home-assistant.io/alpine-3.12/armv7/
Collecting RPi.GPIO
Downloading https://wheels.home-assistant.io/alpine-3.12/armv7/RPi.GPIO-0.7.0-cp38-none-any.whl (24 kB)
Installing collected packages: RPi.GPIO
Successfully installed RPi.GPIO-0.7.0
[cont-init.d] appdaemon.sh: exited 0.
[cont-init.d] done.
[services.d] starting services
[services.d] done.
[00:29:02] INFO: Starting AppDaemon…
2020-11-24 00:29:04.464307 INFO AppDaemon: AppDaemon Version 4.0.5 starting
2020-11-24 00:29:04.464719 INFO AppDaemon: Python version is 3.8.5
2020-11-24 00:29:04.465100 INFO AppDaemon: Configuration read from: /config/appdaemon/appdaemon.yaml
2020-11-24 00:29:04.465579 INFO AppDaemon: Added log: AppDaemon
2020-11-24 00:29:04.465959 INFO AppDaemon: Added log: Error
2020-11-24 00:29:04.466340 INFO AppDaemon: Added log: Access
2020-11-24 00:29:04.466751 INFO AppDaemon: Added log: Diag
2020-11-24 00:29:04.508335 INFO AppDaemon: Loading Plugin HASS using class HassPlugin from module hassplugin
2020-11-24 00:29:04.552731 INFO HASS: HASS Plugin Initializing
2020-11-24 00:29:04.553222 INFO HASS: HASS Plugin initialization complete
2020-11-24 00:29:04.554411 INFO AppDaemon: Initializing HTTP
2020-11-24 00:29:04.555325 INFO AppDaemon: Using ‘ws’ for event stream
2020-11-24 00:29:04.562242 INFO AppDaemon: Starting API
2020-11-24 00:29:04.571023 INFO AppDaemon: Starting Admin Interface
2020-11-24 00:29:04.572189 INFO AppDaemon: Starting Dashboards
2020-11-24 00:29:04.604845 INFO HASS: Connected to Home Assistant 0.118.2
2020-11-24 00:29:04.619685 INFO AppDaemon: App ‘hello_world’ added
2020-11-24 00:29:04.620983 INFO AppDaemon: App ‘fan_pwm’ added
2020-11-24 00:29:04.622869 INFO AppDaemon: Found 2 total apps
2020-11-24 00:29:04.623987 INFO AppDaemon: Starting Apps with 2 workers and 2 pins
2020-11-24 00:29:04.626588 INFO AppDaemon: Running on port 5050
2020-11-24 00:29:04.736516 INFO HASS: Evaluating startup conditions
2020-11-24 00:29:04.755692 INFO HASS: Startup condition met: hass state=RUNNING
2020-11-24 00:29:04.756655 INFO HASS: All startup conditions met
2020-11-24 00:29:04.794497 INFO AppDaemon: Got initial state from namespace default
2020-11-24 00:29:06.700761 INFO AppDaemon: Scheduler running in realtime
2020-11-24 00:29:06.780092 INFO AppDaemon: Adding /config/appdaemon/apps to module import path
2020-11-24 00:29:06.785226 INFO AppDaemon: Loading App Module: /config/appdaemon/apps/hello.py
2020-11-24 00:29:06.796205 INFO AppDaemon: Loading App Module: /config/appdaemon/apps/fan.py
2020-11-24 00:29:06.801771 WARNING Error: ------------------------------------------------------------
2020-11-24 00:29:06.802631 WARNING Error: Unexpected error loading module: /config/appdaemon/apps/fan.py:
2020-11-24 00:29:06.803448 WARNING Error: ------------------------------------------------------------
2020-11-24 00:29:06.808272 WARNING Error: Traceback (most recent call last):
File “/usr/lib/python3.8/site-packages/appdaemon/app_management.py”, line 875, in check_app_updates
await utils.run_in_executor(self, self.read_app, mod[“name”], mod[“reload”])
File “/usr/lib/python3.8/site-packages/appdaemon/utils.py”, line 290, in run_in_executor
response = future.result()
File “/usr/lib/python3.8/concurrent/futures/thread.py”, line 57, in run
result = self.fn(*self.args, **self.kwargs)
File “/usr/lib/python3.8/site-packages/appdaemon/app_management.py”, line 668, in read_app
self.modules[module_name] = importlib.import_module(module_name)
File “/usr/lib/python3.8/importlib/init.py”, line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “”, line 1014, in _gcd_import
File “”, line 991, in _find_and_load
File “”, line 975, in _find_and_load_unlocked
File “”, line 671, in _load_unlocked
File “”, line 783, in exec_module
File “”, line 219, in _call_with_frames_removed
File “/config/appdaemon/apps/fan.py”, line 8, in
import RPi.GPIO as GPIO
File “/usr/lib/python3.8/site-packages/RPi/GPIO/init.py”, line 23, in
from RPi._GPIO import *
RuntimeError: This module can only be run on a Raspberry Pi!
2020-11-24 00:29:06.809912 WARNING Error: ------------------------------------------------------------
2020-11-24 00:29:06.810654 WARNING AppDaemon: Removing associated apps:
2020-11-24 00:29:06.811552 WARNING AppDaemon: fan_pwm
2020-11-24 00:29:06.813039 INFO AppDaemon: Initializing app hello_world using class HelloWorld from module hello
2020-11-24 00:29:06.997062 INFO hello_world: Hello from AppDaemon
2020-11-24 00:29:06.999350 INFO hello_world: You are now ready to run Apps!
2020-11-24 00:29:07.002900 INFO AppDaemon: App initialization complete
2020-11-24 00:29:10.123180 INFO AppDaemon: New client Admin Client connected

Used configuration:

system_packages: []
python_packages:
– RPi.GPIO
init_commands: []
log_level: info

1 Like