thecode / ha-rpi_gpio

Home Assistant Raspberry Pi GPIO Integration
Apache License 2.0
182 stars 39 forks source link

AttributeError: 'NoneType' object has no attribute 'set_value' #306

Open STELTZ-MANIA opened 1 day ago

STELTZ-MANIA commented 1 day ago

After upgrading to the most recent version 2024.10.0 my switch is no longer working. I have now rolled back to version 2022.7.0 and it is working again. Hardware is a Raspberry Pi 4.

The error message is:

AttributeError: 'NoneType' object has no attribute 'set_value'

The switch is configured as: switch:

and the debug file is below:

2024-10-25 16:05:09.359 DEBUG (MainThread) [custom_components.rpi_gpio.hub] in turn_on 27 2024-10-25 16:05:09.359 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [547406852976] Unexpected exception Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 245, in handle_call_service response = await hass.services.async_call( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/core.py", line 2761, in async_call response_data = await coro ^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/core.py", line 2804, in _execute_service return await target(service_call) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 996, in entity_service_call single_response = await _handle_entity_call( ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 1068, in _handle_entity_call result = await task ^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1712, in async_toggle await self.async_turn_on(**kwargs) File "/config/custom_components/rpi_gpio/switch.py", line 102, in async_turn_on self._hub.turn_on(self._port) File "/config/custom_components/rpi_gpio/hub.py", line 151, in turn_on self._lines.set_value(port, Value.ACTIVE) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'set_value'

alastaid commented 21 hours ago

Just to add to this, I have had to roll back as well, whilst I didnt see the error reported here, I had completely different behaviour of the GPIO pins in HA, I control a relay to mimic a button press for a gate, and a relay switch to see the state of the gate. I had to completely reboot the host before it worked for a bit with the new version, then on restarts of HA, it didn't seem to report the GPIO pins in HA, ie it wouldn't open the gate, relay never activated, and it lost the state of the reed switch, just reported open all the time.

tomer-w commented 21 hours ago

Hi @alastaid, I'm not able to repro the same problem but I will be glad to investigate. Can you enable debug logging for the integration, restart home assistant and send all the logs or at least the ones which start with custom_components.rpi_gpio?

alastaid commented 21 hours ago

Booting up:

2024-10-25 12:01:40.363 DEBUG (MainThread) [custom_components.rpi_gpio.hub] verify_gpiochip: /dev/gpiochip0 is a gpiochip_device 2024-10-25 12:01:40.364 DEBUG (MainThread) [custom_components.rpi_gpio.hub] verify_gpiochip gpiodevice: /dev/gpiochip0 has pinctrl 2024-10-25 12:01:40.365 DEBUG (MainThread) [custom_components.rpi_gpio.hub] using gpio_device: /dev/gpiochip0 2024-10-25 12:01:40.366 DEBUG (MainThread) [custom_components.rpi_gpio.cover] setup_platform: {'platform': 'rpi_gpio', 'invert_state': True, 'relay_time': 1, 'covers': [{'relay_pin': 27, 'state_pin': 22, 'name': 'gate', 'unique_id': 'Front_Gate_relay_pin_27_state_pin_22'}], 'state_pull_mode': 'UP', 'invert_relay': False} 2024-10-25 12:01:40.366 DEBUG (MainThread) [custom_components.rpi_gpio.cover] GPIODCover init: 27:22 - gate - Front_Gate_relay_pin_27_state_pin_22 - 1 2024-10-25 12:01:40.367 DEBUG (MainThread) [custom_components.rpi_gpio.hub] in add_cover 27 22 2024-10-25 12:01:40.367 DEBUG (MainThread) [custom_components.rpi_gpio.hub] in add_switch 27 2024-10-25 12:01:40.368 DEBUG (MainThread) [custom_components.rpi_gpio.hub] in add_sensor 22 2024-10-25 12:01:40.380 DEBUG (MainThread) [custom_components.rpi_gpio.hub] current value for port 22: True 2024-10-25 12:01:40.380 DEBUG (MainThread) [custom_components.rpi_gpio.hub] updating lines: {27: gpiod.LineSettings(direction=Direction.OUTPUT, edge_detection=Edge.NONE, bias=Bias.AS_IS, drive=Drive.PUSH_PULL, active_low=False, debounce_period=datetime.timedelta(0), event_clock=Clock.MONOTONIC, output_value=Value.INACTIVE), 22: gpiod.LineSettings(direction=Direction.INPUT, edge_detection=Edge.BOTH, bias=Bias.PULL_UP, drive=Drive.PUSH_PULL, active_low=True, debounce_period=datetime.timedelta(microseconds=50000), event_clock=Clock.REALTIME, output_value=Value.ACTIVE)}

opening the gate (which did work, but the reed switch doesnt):

2024-10-25 12:01:59.919 DEBUG (MainThread) [custom_components.rpi_gpio.hub] startup rpi_gpio hub 2024-10-25 12:01:59.922 DEBUG (MainThread) [custom_components.rpi_gpio.hub] updating lines: {27: gpiod.LineSettings(direction=Direction.OUTPUT, edge_detection=Edge.NONE, bias=Bias.AS_IS, drive=Drive.PUSH_PULL, active_low=False, debounce_period=datetime.timedelta(0), event_clock=Clock.MONOTONIC, output_value=Value.INACTIVE), 22: gpiod.LineSettings(direction=Direction.INPUT, edge_detection=Edge.BOTH, bias=Bias.PULL_UP, drive=Drive.PUSH_PULL, active_low=True, debounce_period=datetime.timedelta(microseconds=50000), event_clock=Clock.REALTIME, output_value=Value.ACTIVE)} 2024-10-25 12:01:59.935 DEBUG (MainThread) [custom_components.rpi_gpio.hub] Start listener 2024-10-25 12:02:39.844 DEBUG (SyncWorker_5) [custom_components.rpi_gpio.hub] in turn_on 27 2024-10-25 12:02:40.848 DEBUG (SyncWorker_5) [custom_components.rpi_gpio.hub] in turn_off 27

So after looking at it a bit more, the reed switch sometimes works, sometimes doesn't, if I restart HA sometimes it opens the gate, sometimes not. If I roll back it works perfectly. Let me know how else I can help. Thanks Alastair

tomer-w commented 21 hours ago

Not sure yet what the problem is but there is some anomaly here. startup rpi_gpio hub supposed to be executed and traced as part of the HA startup sequence but it seems it happen really late in your case. Did you wait for everything to finish booting before you started to play with it? Also I dont see the same problem from your original trace so not sure what really changed. Last one, you mention reed switch, can you explain? are you talking about sensor or a switch? I thought reed switch is a type of sensor which changes status based on magnetic power. But maybe I dont understand the setup.

alastaid commented 20 hours ago

Hi, sorry firstly I jumped on this thread the initial logs weren't mine. Hence the difference from the original trace. Second, it is possible I didn't wait for HA to fully restart hence while it was late. It maybe worth focussing on the reed switch, which is a magnet and reciever attached to the gate making and breaking the circuit. My GPIO setup is as follows:

cover:

I rolled back to 2022.7 and enabled debugging, to show what happens when it works, but I get nothing in the logs (is this what you would expect). But last time I installed 2024.10, I got no state change on pin 22 which is the reed switch. Is it perhaps because I am using "invert_state: true" ? Thanks

srempfer commented 17 hours ago

Same problem here with version 2024.10.0 running HA 2024.10 (OS13.2) on a Pi 4

Config:

switch:
  - platform: rpi_gpio
    switches:
      - port: 16
        invert_logic: true
        unique_id: "front_door_opener"
        name: "Front Door Opener"

Output during startup - doesn't matter if I just restart HA, restart the Pi4 or shutdown the Pi4 before booting it again

2024-10-25 17:35:36.430 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved (None)
Traceback (most recent call last):
  File "/config/custom_components/rpi_gpio/hub.py", line 94, in startup
    self.update_lines()
  File "/config/custom_components/rpi_gpio/hub.py", line 127, in update_lines
    self._lines = gpiod.request_lines(
                  ^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/gpiod/__init__.py", line 54, in request_lines
    return chip.request_lines(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/gpiod/chip.py", line 316, in request_lines
    req_internal = self._chip.request_lines(line_cfg, consumer, event_buffer_size)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 16] Resource busy

Trigger switch:

[547860772704] Unexpected exception
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 245, in handle_call_service
    response = await hass.services.async_call(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2761, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2804, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 996, in entity_service_call
    single_response = await _handle_entity_call(
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 1068, in _handle_entity_call
    result = await task
             ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/lock/__init__.py", line 239, in async_handle_unlock_service
    await self.async_unlock(**self.add_default_code(kwargs))
  File "/usr/src/homeassistant/homeassistant/components/template/lock.py", line 193, in async_unlock
    await self.async_run_script(
  File "/usr/src/homeassistant/homeassistant/components/template/template_entity.py", line 563, in async_run_script
    await script.async_run(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1795, in async_run
    return await asyncio.shield(create_eager_task(run.async_run()))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 463, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 527, in _async_step
    self._handle_exception(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 557, in _handle_exception
    raise exception
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 525, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 763, in _async_call_service_step
    response_data = await self._async_run_long_action(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 726, in _async_run_long_action
    return await long_task
           ^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2761, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2804, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 996, in entity_service_call
    single_response = await _handle_entity_call(
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 1068, in _handle_entity_call
    result = await task
             ^^^^^^^^^^
  File "/config/custom_components/rpi_gpio/switch.py", line 102, in async_turn_on
    self._hub.turn_on(self._port)
  File "/config/custom_components/rpi_gpio/hub.py", line 151, in turn_on
    self._lines.set_value(port, Value.ACTIVE)
    ^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'set_value'
tomer-w commented 14 hours ago

@srempfer , any chance you have another GPIO integration installed on the same box? It seems like the initialization failed as the GPIO was busy.

STELTZ-MANIA commented 9 hours ago

I'm not sure what other GPIO integration there is to install but like other have said, works perfectly with version 2022.7.0 and fails with version 2024.10.0. I can provide a list of my installed Integrations if you think that would help.

tomer-w commented 9 hours ago

I understand it worked with the previous version. The latest version uses a completely different GPIO codebase as the old one is obsolete and didn't support RPi5. As there was a major rewrite with how we use the GPIO it could be that things that used to work in the past will fail with the latest version.

To help debug the issue I created a new PR which improve error handling and have better traces. It will be great if you will move to this latest version when it is released (or just apply the single file that I changed manually if you know how to do it) and provide the debug tracing. If the traces will not reveal anything that I would want to see what other integrations are loaded. Those shows up in the log starting with We found a custom integration ...