jdeneef / ha_gpiod

gpiod based RPI gpio switches and sensors
https://github.com/jdeneef/ha_gpiod
MIT License
11 stars 2 forks source link

Gpio not working #1

Closed metju13 closed 1 month ago

metju13 commented 1 month ago

I installed the plugin through hacs in home assistant, updated the yaml file but by connecting a led on the gpio (for example 6) the logical state is not changed when I operate in the developer console and set value -> on. Furthermore, if I insert the switch inside the IDE when I think about changing its state I receive an error -> Invocation of the switch/turn_off service failed. 'NoneType' object has no attribute 'set_value'

jdeneef commented 1 month ago

Can you share debug logs? and the yaml config?

metju13 commented 1 month ago

2024-05-15 11:01:28.849 DEBUG (SyncWorker_19) [custom_components.gpiod.hub] in turn_on 2024-05-15 11:01:28.850 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [547831122880] 'NoneType' object has no attribute 'set_value' Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 239, in handle_call_service response = await hass.services.async_call( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/core.py", line 2319, in async_call response_data = await coro ^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/core.py", line 2356, in _execute_service return await target(service_call) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 905, in entity_service_call single_response = await _handle_entity_call( ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 975, in _handle_entity_call result = await task ^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1645, in async_turn_on await self.hass.async_add_executor_job(ft.partial(self.turn_on, kwargs)) File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run result = self.fn(self.args, self.kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/config/custom_components/gpiod/switch.py", line 85, in turn_on self._hub.turn_on(self._port) File "/config/custom_components/gpiod/hub.py", line 130, in turn_on self._lines.set_value(port, Value.ACTIVE) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'set_value' 2024-05-15 11:01:29.702 DEBUG (SyncWorker_45) [custom_components.gpiod.hub] in turn_off 2024-05-15 11:01:29.702 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [547831122880] 'NoneType' object has no attribute 'set_value' Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 239, in handle_call_service response = await hass.services.async_call( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/core.py", line 2319, in async_call response_data = await coro ^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/core.py", line 2356, in _execute_service return await target(service_call) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 905, in entity_service_call single_response = await _handle_entity_call( ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 975, in _handle_entity_call result = await task ^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1653, in async_turn_off await self.hass.async_add_executor_job(ft.partial(self.turn_off, kwargs)) File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run result = self.fn(self.args, self.kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/config/custom_components/gpiod/switch.py", line 90, in turn_off self._hub.turn_off(self._port) File "/config/custom_components/gpiod/hub.py", line 134, in turn_off self._lines.set_value(port, Value.INACTIVE) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'set_value'

metju13 commented 1 month ago

--------------------------------------------------------------------------

Descrizione: GPIO Raspberry HACS

Da sito https://github.com/jdeneef/ha_gpiod

--------------------------------------------------------------------------

gpiod: path: '/dev/gpiochip0'

switch:

metju13 commented 1 month ago

Cattura

jdeneef commented 1 month ago

Can you add the debugconfig below to your configuration.yaml, enabling debugging, I need to know if initialization has worked. Those error lines show that the gpio device was not, and only shows the error when trying to switch the led.

Also make sure to really install the integration, I think I missed a step there. Add custom repository, go back to hacs, should show new repository, install repository, reboot, add config, reboot.

logger:
  default: info
  logs:
    custom_components.gpiod: debug

Your log should show something like this at startup:

2024-05-15 17:45:03.842 INFO (MainThread) [homeassistant.setup] Setting up gpiod
2024-05-15 17:45:03.843 DEBUG (MainThread) [custom_components.gpiod.hub] in hub.__init__ /dev/gpiochip0
2024-05-15 17:45:03.846 INFO (MainThread) [custom_components.gpiod.hub] initialized: /dev/gpiochip0
2024-05-15 17:45:03.847 INFO (MainThread) [homeassistant.setup] Setup of domain gpiod took 0.00 seconds
...
2024-05-15 17:45:03.914 INFO (MainThread) [homeassistant.setup] Setting up switch
2024-05-15 17:45:03.917 INFO (MainThread) [homeassistant.components.switch] Setting up gpiod.switch
2024-05-15 17:45:03.918 DEBUG (MainThread) [custom_components.gpiod.switch] setup_platform: {'platform': 'gpiod', 'switches': [{'port': 17, 'name': 'Led 2 Red', 'invert_logic': False}, {'port': 22, 'name': 'Led 3 Yellow', 'invert_logic': T
rue}]}
2024-05-15 17:45:03.918 DEBUG (MainThread) [custom_components.gpiod.switch] GPIODSwitch init: 17 - Led 2 Red - gpiod_17_led_2_red
2024-05-15 17:45:03.919 DEBUG (MainThread) [custom_components.gpiod.hub] in add_switch 17
2024-05-15 17:45:03.921 DEBUG (MainThread) [custom_components.gpiod.switch] GPIODSwitch init: 22 - Led 3 Yellow - gpiod_22_led_3_yellow
2024-05-15 17:45:03.921 DEBUG (MainThread) [custom_components.gpiod.hub] in add_switch 22
metju13 commented 1 month ago

past de log

2024-05-16 11:19:33.831 DEBUG (MainThread) [custom_components.gpiod.hub] in hub.init /dev/gpiochip0 2024-05-16 11:19:33.831 DEBUG (MainThread) [custom_components.gpiod.hub] initialization failed: /dev/gpiochip0 no pinctrl

2024-05-16 11:19:34.406 DEBUG (MainThread) [custom_components.gpiod.switch] setup_platform: {'platform': 'gpiod', 'switches': [{'name': 'GPIO-4 Led GREEN', 'port': 4, 'invert_logic': False}]} 2024-05-16 11:19:34.407 DEBUG (MainThread) [custom_components.gpiod.switch] GPIODSwitch init: 4 - GPIO-4 Led GREEN - gpiod_4_gpio-4_led_green 2024-05-16 11:19:34.407 DEBUG (MainThread) [custom_components.gpiod.hub] in add_switch 4

metju13 commented 1 month ago

I reinstalled according to the procedure but I get this result. Raspberry pi 5

jdeneef commented 1 month ago

Thanks. Your issue is that /dev/gpiochip0 is not en gpio device, or at least has no pinctrl. Could possibly also be a pi5 issue. I will add a check for you later. From this:

2024-05-16 11:19:33.831 DEBUG (MainThread) [custom_components.gpiod.hub] initialization failed: /dev/gpiochip0 no pinctrl

Can you check access to gpio device is correct for homeassistant, gpio device exists at all? If eg using docker you should pass through the gpio device ..

metju13 commented 1 month ago

I use hassio on raspberry, how can I check if gpio exists?

jdeneef commented 1 month ago

I will drop an update this weekend, removing the blocking on no pinctrl. /dev/gpiochip0 is recognized as gpiochip just does not show pinctrl in info, this was a good indicator on pi3 and pi4, possibly not on pi5, don't have one yet, so I can't test.

metju13 commented 1 month ago

excellent, I'm waiting for news!!! Thank you

jdeneef commented 1 month ago

checking some sites it seems for rpi5 the gpiochip is in /dev/gpiochip4 and should have pinctrl in the info. Can you check? just update the yaml to:

gpiod:
  path: '/dev/gpiochip4'

Other easy way to check is to login into your hass server, and preferably as root:

# python
Python 3.12.2 (main, Mar 18 2024, 23:18:19) [GCC 13.2.1 20231014] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gpiod
>>> gpiod.is_gpiochip_device("/dev/gpiochip0")
True
>>> gpiod.is_gpiochip_device("/dev/gpiochip1")
False
>>> gpiod.is_gpiochip_device("/dev/gpiochip2")
False
>>> gpiod.is_gpiochip_device("/dev/gpiochip3")
False
>>> gpiod.is_gpiochip_device("/dev/gpiochip4")
False

Where in rpi5 case the last one /dev/gpiochip4 possibly returns True (the above was from a pi4). If that is successful I can update to code to test for this automatically, when no input from config, would be a quick and easy addition.

metju13 commented 1 month ago

Fantastic!!!! now it works perfectly!! Thank you for your effort and help.

jdeneef commented 1 month ago

nice, thanx! I will add this to documentation and close the ticket.

martinkoe09 commented 1 month ago

Even though this issue is closed I still would like to add a comment.

The same for me RPI3 with latest Home Assistant. GPIO Ports aren't switched. Voltage stays on 5V wether on or off. Or am I doing something wrong?

Start of GPIOD is looking fine

2024-05-28 13:58:57.671 DEBUG (MainThread) [custom_components.gpiod.hub] in hub.init path: /dev/gpiochip0 2024-05-28 13:58:57.671 DEBUG (MainThread) [custom_components.gpiod.hub] verify_gpiochip: /dev/gpiochip0 is a gpiochip_device 2024-05-28 13:58:57.673 DEBUG (MainThread) [custom_components.gpiod.hub] verify_gpiochip gpiodevice: /dev/gpiochip0 has pinctrl 2024-05-28 13:58:57.675 DEBUG (MainThread) [custom_components.gpiod.hub] using gpio_device: /dev/gpiochip0 2024-05-28 13:58:57.675 INFO (MainThread) [homeassistant.setup] Setup of domain gpiod took 0.01 seconds 2024-05-28 13:58:57.677 INFO (MainThread) [homeassistant.setup] Setting up input_number 2024-05-28 13:58:57.689 INFO (MainThread) [homeassistant.setup] Setup of domain input_number took 0.01 seconds 2024-05-28 13:58:57.690 INFO (MainThread) [homeassistant.setup] Setting up zone 2024-05-28 13:58:57.699 INFO (MainThread) [homeassistant.setup] Setup of domain zone took 0.01 seconds 2024-05-28 13:58:57.700 INFO (MainThread) [homeassistant.setup] Setting up input_button 2024-05-28 13:58:57.704 INFO (MainThread) [homeassistant.setup] Setup of domain input_button took 0.00 seconds 2024-05-28 13:58:57.705 INFO (MainThread) [homeassistant.setup] Setting up system_health 2024-05-28 13:58:57.707 INFO (MainThread) [homeassistant.setup] Setup of domain system_health took 0.00 seconds 2024-05-28 13:58:57.710 INFO (MainThread) [homeassistant.setup] Setting up script 2024-05-28 13:58:57.713 INFO (MainThread) [homeassistant.setup] Setup of domain script took 0.00 seconds 2024-05-28 13:58:57.716 INFO (MainThread) [homeassistant.setup] Setting up input_select 2024-05-28 13:58:57.728 INFO (MainThread) [homeassistant.setup] Setup of domain input_select took 0.01 seconds 2024-05-28 13:58:57.772 INFO (MainThread) [homeassistant.setup] Setting up switch 2024-05-28 13:58:57.774 INFO (MainThread) [homeassistant.components.switch] Setting up gpiod.switch 2024-05-28 13:58:57.775 DEBUG (MainThread) [custom_components.gpiod.switch] setup_platform: {'platform': 'gpiod', 'switches': [{'name': 'Led 2 red', 'port': 17, 'unique_id': 'gpio17_led_red_2', 'invert_logic': False}]} 2024-05-28 13:58:57.775 DEBUG (MainThread) [custom_components.gpiod.switch] GPIODSwitch init: 17 - Led 2 red - gpio17_led_red_2 2024-05-28 13:58:57.775 DEBUG (MainThread) [custom_components.gpiod.hub] in add_switch 17

jdeneef commented 1 month ago

debug looks good, any logs from switching? /dev/gpiochip0 is indeed the gpiodevice, with pinctrl. For RPI3, my test RPI is a RPI3B, so it is working for RPI3, on /dev/gpiochip0. Any option to test with another device? I will check libgpiod to verify if I there is anything I can see on a failed switch.

martinkoe09 commented 1 month ago

Thanks for you quick reply.

Switching seems ok.

2024-05-28 16:56:39.606 DEBUG (SyncWorker_0) [custom_components.gpiod.hub] in turn_on 2024-05-28 16:56:40.947 DEBUG (SyncWorker_10) [custom_components.gpiod.hub] in turn_off 2024-05-28 16:56:41.899 DEBUG (SyncWorker_4) [custom_components.gpiod.hub] in turn_on 2024-05-28 16:56:42.892 DEBUG (SyncWorker_13) [custom_components.gpiod.hub] in turn_off

Am I doing wrong just measureing with a Voltmeter Pin-to-Ground?

I encounter one difficulty with gpiod.

Python output of import gpiod

~ # python Python 3.12.3 (main, Apr 18 2024, 07:52:31) [GCC 13.2.1 20240309] on linux Type "help", "copyright", "credits" or "license" for more information.

import gpiod Traceback (most recent call last): File "", line 1, in ModuleNotFoundError: No module named 'gpiod'

jdeneef commented 1 month ago

gpiod is installed at least in your homeassistant environment otherwise the initial checks on the /dev/gpiochip0 would not return positive. Are you using docker for Homeassistant, than you should execute from within the docker environment. For measuring I don't know, never tried to measure the gpio ports .. I would expect measuring with DC over the GPIO and ground should work, not an expert though.

martinkoe09 commented 1 month ago

Thank you for your quick reply. Don't really know where to look. I double checked my equipment. The relay card works fine with an ESP32. I can measure 3.3 V while GPIO one high.

With my RPI IT seems Like the GPIOs are one PWM. Relays are constantly switching.

Do I have to enable boot options for the RPI?

Thanks in advance

jdeneef commented 1 month ago

what linux distro are you using? gpio should be enabled, I think it is seeing the responses. maybe try and connect a led? Can you test with gpioset? see https://lloydrochester.com/post/hardware/libgpiod-blink-led-rpi/ for some examples? from the page, and some more steps using libgpiod. You possibly need to install gpioset

$ gpioset gpiochip0 21=1
$ gpioset gpiochip0 21=0
martinkoe09 commented 1 month ago

Hello and Thanks,

reinstalled it. Bootet twice to poweroff. Now it works. Thank you

jdeneef commented 1 month ago

very nice! Strange effect but success nevertheless!