waveform80 / rpi-lgpio

A compatibility shim for lgpio emulating the RPi.GPIO API
https://rpi-lgpio.readthedocs.io/
Other
12 stars 7 forks source link

Drop in replacement issues #11

Open m4recek opened 5 months ago

m4recek commented 5 months ago

Hello, right from the start I am not even sure if what I am doing makes any sense, my apologies if it is nonsense.

I am trying to find a way to switch gpio pins on Raspberry 5 and it looks like all GPIO integrations are broken.

So I tried to use rpi.lgpi as a drop-in replacement for rpi.gpio yesterday in HomeAsistant by changing the required gpio package in standard HomeAssistant package ha-rpi_gpio.

My commit is here: https://github.com/thecode/ha-rpi_gpio/compare/main...m4recek:ha-rpi_lgpio:main

I was then able to import my new package into HomeAssistant and it looks like it is trying to start using lgpio python package. However, it then fails to start with the following errors: AttributeError: module 'lgpio' has no attribute 'SET_PULL_NONE' AttributeError: module 'lgpio' has no attribute 'SET_BIAS_DISABLE

I am not sure if this is completely outside the scope of lgpio or whether this is an error as lgpio should be drop-in replacement.

Full Stacktrace:

2024-05-07 00:56:17.511 ERROR (ImportExecutor_0) [homeassistant.loader] Unexpected exception importing platform custom_components.rpi_gpio.switch
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/RPi/GPIO/__init__.py", line 18, in <module>
    lgpio.SET_PULL_NONE
AttributeError: module 'lgpio' has no attribute 'SET_PULL_NONE'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/loader.py", line 1238, in _load_platform
    cache[full_name] = self._import_platform(platform_name)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/loader.py", line 1270, in _import_platform
    return importlib.import_module(f"{self.pkg_path}.{platform_name}")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/util/loop.py", line 144, in protected_loop_func
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1310, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/config/custom_components/rpi_gpio/__init__.py", line 3, in <module>
    from RPi import GPIO  # pylint: disable=import-error
    ^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/RPi/GPIO/__init__.py", line 20, in <module>
    lgpio.SET_PULL_NONE = lgpio.SET_BIAS_DISABLE
                          ^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'lgpio' has no attribute 'SET_BIAS_DISABLE'
2024-05-07 00:56:17.517 WARNING (MainThread) [homeassistant.util.loop] Detected blocking call to import_module with args ('custom_components.rpi_gpio.switch',) in /usr/src/homeassistant/homeassistant/loader.py, line 1270: return importlib.import_module(f"{self.pkg_path}.{platform_name}") inside the event loop
2024-05-07 00:56:17.521 ERROR (MainThread) [homeassistant.loader] Unexpected exception importing platform custom_components.rpi_gpio.switch
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/RPi/GPIO/__init__.py", line 18, in <module>
    lgpio.SET_PULL_NONE
AttributeError: module 'lgpio' has no attribute 'SET_PULL_NONE'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/loader.py", line 1238, in _load_platform
    cache[full_name] = self._import_platform(platform_name)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/loader.py", line 1270, in _import_platform
    return importlib.import_module(f"{self.pkg_path}.{platform_name}")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/util/loop.py", line 144, in protected_loop_func
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1310, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/config/custom_components/rpi_gpio/__init__.py", line 3, in <module>
    from RPi import GPIO  # pylint: disable=import-error
    ^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/RPi/GPIO/__init__.py", line 20, in <module>
    lgpio.SET_PULL_NONE = lgpio.SET_BIAS_DISABLE
                          ^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'lgpio' has no attribute 'SET_BIAS_DISABLE'
2024-05-07 00:56:17.523 ERROR (MainThread) [homeassistant.config] Platform error: switch - Exception importing custom_components.rpi_gpio.switch
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/RPi/GPIO/__init__.py", line 18, in <module>
    lgpio.SET_PULL_NONE
AttributeError: module 'lgpio' has no attribute 'SET_PULL_NONE'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/loader.py", line 1238, in _load_platform
    cache[full_name] = self._import_platform(platform_name)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/loader.py", line 1270, in _import_platform
    return importlib.import_module(f"{self.pkg_path}.{platform_name}")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/util/loop.py", line 144, in protected_loop_func
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1310, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/config/custom_components/rpi_gpio/__init__.py", line 3, in <module>
    from RPi import GPIO  # pylint: disable=import-error
    ^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/RPi/GPIO/__init__.py", line 20, in <module>
    lgpio.SET_PULL_NONE = lgpio.SET_BIAS_DISABLE
                          ^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'lgpio' has no attribute 'SET_BIAS_DISABLE'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config.py", line 1439, in _async_load_and_validate_platform_integration
    platform = await p_integration.integration.async_get_platform(domain)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/loader.py", line 1074, in async_get_platform
    platforms = await self.async_get_platforms((platform_name,))
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/loader.py", line 1151, in async_get_platforms
    import_future.result()
  File "/usr/src/homeassistant/homeassistant/loader.py", line 1139, in async_get_platforms
    platforms.update(self._load_platforms(platform_names))
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/loader.py", line 1064, in _load_platforms
    platform_name: self._load_platform(platform_name)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/loader.py", line 1258, in _load_platform
    raise ImportError(
ImportError: Exception importing custom_components.rpi_gpio.switch
waveform80 commented 5 months ago

rpi-lgpio should be a drop-in replacement for RPi.GPIO, so what you're doing is not crazy, and the way you've gone about it looks like it ought to work to me. However, I wonder if you've got an old version of lgpio installed?

I'm not at all familiar with Home Assistant (one of those things that seems to be permanently on my "I should look at that" list which I never find time for!), but if it's using pip as the package manager, doing something like "pip install --upgrade lgpio" might fix things if it winds up pulling down a new version of lgpio. If it doesn't, could you try the following and we'll find out what version of lgpio you've got installed:

$ python3
Python 3.12.3 (main, Apr 10 2024, 05:33:47) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import lgpio
>>> lgpio.get_module_version()
'lgpio.py_0.2.2.0'