Rpi_gpio and tof integration generating errors

Hi I’m trying to get HA to extend some push rods to push an object a specific distance away from a wall upon detecting an Insteon keypad press. It appears the Time of Flight and rpi_gpio components are a good match.

Let’s say the distance to extend to is 2 meters. The pseudo code will be something like:

  1. Detect a specific keypad press with Insteon.
  2. Use rpi_gpio to switch on a relay to extend the push rods as long as it’s less than 2 meters.
  3. Use Time of Flight to measure distance away from wall.
  4. As we approach 2 meters use rpi_gpio to control another relay to modulate power applied to the pushrods to control speed. It operates in 24VDC so I’d have to do some PWM here. For example 10% duty cycle means 2.4mm/s for a pushrod that operates at 24mms/s under 100% duty cycle.
  5. Stop pushrods once reaching or having having reached or exceeded 2 meters.
  6. Use rpi_gpio to control the relays to retract / extend the pushrods until distance is +/- 2 millimeters.

To be honest I’m not sure if HA is the best platform to implement something like this but I need to be able to integrate with Insteon so I guess there’s no choice. Comments? Yes I could have used a servo motor based push rod but they are significantly more expensive.

Anyway my configuration.yml snippet looks like this.

sensor:
  - platform: tof
    name: Left Pushrod Distance
    i2c_address: 0x29
    xshut: 17
  - platform: tof
    name: Right Pushrod Distance
    i2c_address: 0x30
    xshut: 18
switch:
  - platform: rpi_gpio
    invert_logic: false
    ports:
      17: Left TOF Sensor Reset
      18: Right TOF Sensor Reset
      26: Pushrod On/Off Relay
      20: Pushrod Directional Relay 1
      21: Pushrod Directional Relay 2

And I’m seeing the following in the Configurations -> Logs section. Any ideas? I did install HA in a docker on Raspberry Pi OS and am not using Hassio.

Errors for the rpi_gpio integration

Logger: aiohttp.server
Source: components/rpi_gpio/__init__.py:2
First occurred: 9:32:12 PM (1 occurrences)
Last logged: 9:32:12 PM

Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/aiohttp/web_protocol.py", line 422, in _handle_request
    resp = await self._request_handler(request)
  File "/usr/local/lib/python3.8/site-packages/aiohttp/web_app.py", line 499, in _handle
    resp = await handler(request)
  File "/usr/local/lib/python3.8/site-packages/aiohttp/web_middlewares.py", line 118, in impl
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/request_context.py", line 18, in request_context_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 72, in ban_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 127, in auth_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/view.py", line 129, in handle
    result = await result
  File "/usr/src/homeassistant/homeassistant/components/config/core.py", line 29, in post
    errors = await async_check_ha_config_file(request.app["hass"])
  File "/usr/src/homeassistant/homeassistant/config.py", line 874, in async_check_ha_config_file
    res = await check_config.async_check_ha_config_file(hass)
  File "/usr/src/homeassistant/homeassistant/helpers/check_config.py", line 164, in async_check_ha_config_file
    platform = p_integration.get_platform(domain)
  File "/usr/src/homeassistant/homeassistant/loader.py", line 424, in get_platform
    cache[full_name] = self._import_platform(platform_name)
  File "/usr/src/homeassistant/homeassistant/loader.py", line 429, in _import_platform
    return importlib.import_module(f"{self.pkg_path}.{platform_name}")
  File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/usr/src/homeassistant/homeassistant/components/rpi_gpio/__init__.py", line 2, in <module>
    from RPi import GPIO  # pylint: disable=import-error
  File "/usr/local/lib/python3.8/site-packages/RPi/GPIO/__init__.py", line 23, in <module>
    from RPi._GPIO import *
RuntimeError: This module can only be run on a Raspberry Pi!

Errors for the TOF Integration

2020-12-03 12:46:26 ERROR (SyncWorker_1) [homeassistant.util.package] Unable to install package VL53L1X2==0.1.5: ERROR: Command errored out with exit status 1:
command: /usr/local/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-4eo_8y8r/vl53l1x2/setup.py'"'"'; __file__='"'"'/tmp/pip-install-4eo_8y8r/vl53l1x2/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-eix25r1i
cwd: /tmp/pip-install-4eo_8y8r/vl53l1x2/
Complete output (16 lines):
running bdist_wheel
running build
running build_py
creating build
creating build/lib.linux-armv7l-3.8
copying python/VL53L1X2.py -> build/lib.linux-armv7l-3.8
running build_ext
building 'vl53l1x_python' extension
creating build/temp.linux-armv7l-3.8
creating build/temp.linux-armv7l-3.8/api
creating build/temp.linux-armv7l-3.8/api/core
creating build/temp.linux-armv7l-3.8/api/platform
creating build/temp.linux-armv7l-3.8/python_lib
gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fno-semantic-interposition -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free -ljemalloc -DTHREAD_STACK_SIZE=0x100000 -fPIC -I. -Iapi/core -Iapi/platform -I/usr/local/include/python3.8 -c api/core/vl53l1_api_calibration.c -o build/temp.linux-armv7l-3.8/api/core/vl53l1_api_calibration.o -std=c99
unable to execute 'gcc': No such file or directory
error: command 'gcc' failed with exit status 1
----------------------------------------
ERROR: Failed building wheel for VL53L1X2
ERROR: Command errored out with exit status 1:
command: /usr/local/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-4eo_8y8r/vl53l1x2/setup.py'"'"'; __file__='"'"'/tmp/pip-install-4eo_8y8r/vl53l1x2/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-h6utfa5u/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.8/VL53L1X2
cwd: /tmp/pip-install-4eo_8y8r/vl53l1x2/
Complete output (16 lines):
running install
running build
running build_py
creating build
creating build/lib.linux-armv7l-3.8
copying python/VL53L1X2.py -> build/lib.linux-armv7l-3.8
running build_ext
building 'vl53l1x_python' extension
creating build/temp.linux-armv7l-3.8
creating build/temp.linux-armv7l-3.8/api
creating build/temp.linux-armv7l-3.8/api/core
creating build/temp.linux-armv7l-3.8/api/platform
creating build/temp.linux-armv7l-3.8/python_lib
gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fno-semantic-interposition -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free -ljemalloc -DTHREAD_STACK_SIZE=0x100000 -fPIC -I. -Iapi/core -Iapi/platform -I/usr/local/include/python3.8 -c api/core/vl53l1_api_calibration.c -o build/temp.linux-armv7l-3.8/api/core/vl53l1_api_calibration.o -std=c99
unable to execute 'gcc': No such file or directory
error: command 'gcc' failed with exit status 1
----------------------------------------
ERROR: Command errored out with exit status 1: /usr/local/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-4eo_8y8r/vl53l1x2/setup.py'"'"'; __file__='"'"'/tmp/pip-install-4eo_8y8r/vl53l1x2/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-h6utfa5u/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.8/VL53L1X2 Check the logs for full command output.
WARNING: You are using pip version 20.2.4; however, version 20.3.1 is available.
You should consider upgrading via the '/usr/local/bin/python3 -m pip install --upgrade pip' command.

You might want to just try to use the remote_rpi_gpio itegration with localhost as your host instead. Especially if you are using them for other things. Just reading the Raspberry Pi GPIO documents shows that theres a note at the bottom about expectations.

Also, might be worth checking the forums for people using this integration with a docker instance.

Okay so apparently the problems with the rpi_gpio was due to the container not having access to raw devices, and the Time of Light one was due to it not having GCC available, which can be seen in the /config/home-assistant.log file inside the Home Assistant Docker container… Duh!

Here is what’s required to fix.

docker-compose.yml

version: "3"
services:
  homeassistant:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: home-assistant    
    volumes:
      - /opt/homeassistant:/config
    environment:
      - TZ=Asia/Hong_Kong
    restart: always
    network_mode: host
    privileged: true
    cap_add:
      - SYS_RAWIO

Dockerfile

FROM homeassistant/home-assistant:stable
RUN apk add build-base

Errors
After this I am still seeing the following but they appear to be harmless.

rpi_gpio
I can still see the switches and binary_sensor integration using rpi_gpio appearing on the Configuration -> Entities list. The switches also appear on the dashboard on their own without me having to add them explicitly.

2020-12-04 14:42:43 ERROR (Dummy-5) [root] Uncaught exception
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/rpi_gpio/binary_sensor.py", line 70, in read_gpio
    self.schedule_update_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 422, in schedule_update_ha_state
    assert self.hass is not None
AssertionError

This originated from the following block. Not familiar enough with HA to understand why it’s trying to do this assertion.
/usr/src/homeassistant/homeassistant/helpers/entity.py:422

    def schedule_update_ha_state(self, force_refresh: bool = False) -> None:
        """Schedule an update ha state change task.
                                                                 
        Scheduling the update avoids executor deadlocks.
                                               
        Entity state and attributes are read when the update ha state change
        task is executed.
        If state is changed more than once before the ha state change task has
        been executed, the intermediate state transitions will be missed.
        """            
        assert self.hass is not None
        self.hass.add_job(self.async_update_ha_state(force_refresh))  # type: ignore

tof
This one appears to be complaining about gcc not being able to find the header file vl53l1_ll_def.h. Again not familiar enough with HA to know how it is pulling down source files to build.

2020-12-04 15:23:44 ERROR (SyncWorker_4) [homeassistant.util.package] Unable to install package VL53L1X2==0.1.5: ERROR: Command errored out with exit status 1:
   command: /usr/local/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-x9txim5b/vl53l1x2_21d53a6e85674e4d93b36f4e723789a9/setup.py'"'"'; __file__='"'"'/tmp/pip-install-x9txim5b/vl53l1x2_21d53a6e85674e4d93b36f4e723789a9/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-rnz2ptbm
       cwd: /tmp/pip-install-x9txim5b/vl53l1x2_21d53a6e85674e4d93b36f4e723789a9/
  Complete output (19 lines):
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.linux-armv7l-3.8
  copying python/VL53L1X2.py -> build/lib.linux-armv7l-3.8
  running build_ext
  building 'vl53l1x_python' extension
  creating build/temp.linux-armv7l-3.8
  creating build/temp.linux-armv7l-3.8/api
  creating build/temp.linux-armv7l-3.8/api/core
  creating build/temp.linux-armv7l-3.8/api/platform
  creating build/temp.linux-armv7l-3.8/python_lib
  gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fno-semantic-interposition -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free -ljemalloc -DTHREAD_STACK_SIZE=0x100000 -fPIC -I. -Iapi/core -Iapi/platform -I/usr/local/include/python3.8 -c api/core/vl53l1_api_calibration.c -o build/temp.linux-armv7l-3.8/api/core/vl53l1_api_calibration.o -std=c99
  api/core/vl53l1_api_calibration.c:70:10: fatal error: vl53l1_ll_def.h: No such file or directory
     70 | #include "vl53l1_ll_def.h"
        |          ^~~~~~~~~~~~~~~~~
  compilation terminated.
  error: command 'gcc' failed with exit status 1
  ----------------------------------------
  ERROR: Failed building wheel for VL53L1X2
    ERROR: Command errored out with exit status 1:
     command: /usr/local/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-x9txim5b/vl53l1x2_21d53a6e85674e4d93b36f4e723789a9/setup.py'"'"'; __file__='"'"'/tmp/pip-install-x9txim5b/vl53l1x2_21d53a6e85674e4d93b36f4e723789a9/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-bzs68qq5/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.8/VL53L1X2
         cwd: /tmp/pip-install-x9txim5b/vl53l1x2_21d53a6e85674e4d93b36f4e723789a9/
    Complete output (19 lines):
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-armv7l-3.8
    copying python/VL53L1X2.py -> build/lib.linux-armv7l-3.8
    running build_ext
    building 'vl53l1x_python' extension
    creating build/temp.linux-armv7l-3.8
    creating build/temp.linux-armv7l-3.8/api
    creating build/temp.linux-armv7l-3.8/api/core
    creating build/temp.linux-armv7l-3.8/api/platform
    creating build/temp.linux-armv7l-3.8/python_lib
    gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fno-semantic-interposition -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free -ljemalloc -DTHREAD_STACK_SIZE=0x100000 -fPIC -I. -Iapi/core -Iapi/platform -I/usr/local/include/python3.8 -c api/core/vl53l1_api_calibration.c -o build/temp.linux-armv7l-3.8/api/core/vl53l1_api_calibration.o -std=c99
    api/core/vl53l1_api_calibration.c:70:10: fatal error: vl53l1_ll_def.h: No such file or directory
       70 | #include "vl53l1_ll_def.h"
          |          ^~~~~~~~~~~~~~~~~
    compilation terminated.
    error: command 'gcc' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /usr/local/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-x9txim5b/vl53l1x2_21d53a6e85674e4d93b36f4e723789a9/setup.py'"'"'; __file__='"'"'/tmp/pip-install-x9txim5b/vl53l1x2_21d53a6e85674e4d93b36f4e723789a9/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-bzs68qq5/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.8/VL53L1X2 Check the logs for full command output.
2020-12-04 15:23:44 ERROR (MainThread) [homeassistant.config] Platform error: sensor - Requirements for tof not found: ['VL53L1X2==0.1.5'].