tomaae / homeassistant-truenas

TrueNAS integration for Home Assistant
Apache License 2.0
183 stars 16 forks source link

[Bug] HA 2024.5.0b0 - Integration fails to update entities #155

Closed firstof9 closed 3 months ago

firstof9 commented 4 months ago

Describe the issue

Integration fails to update entities.

How to reproduce the issue

Setup integration and wait for an update coordinator call.

Expected behavior

No errors when updating entities.

Screenshots

Software versions

Diagnostics data

Traceback/Error logs

2024-04-25 07:04:26.367 ERROR (MainThread) [custom_components.truenas.coordinator] Unexpected error fetching TrueNAS data
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 315, in _async_refresh
    self.data = await self._async_update_data()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/truenas/coordinator.py", line 102, in _async_update_data
    await self.hass.async_add_executor_job(self.get_dataset)
  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/truenas/coordinator.py", line 899, in get_dataset
    entity_registry.async_remove(entity_id)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_registry.py", line 908, in async_remove
    self.hass.bus.async_fire(
  File "/usr/src/homeassistant/homeassistant/core.py", line 1475, in async_fire
    self._hass.verify_event_loop_thread("async_fire")
  File "/usr/src/homeassistant/homeassistant/core.py", line 440, in verify_event_loop_thread
    frame.report(
  File "/usr/src/homeassistant/homeassistant/helpers/frame.py", line 162, in report
    _report_integration(what, integration_frame, level, error_if_integration)
  File "/usr/src/homeassistant/homeassistant/helpers/frame.py", line 203, in _report_integration
    raise RuntimeError(
RuntimeError: Detected that custom integration 'truenas' calls async_fire from a thread at custom_components/truenas/coordinator.py, line 899: entity_registry.async_remove(entity_id). Please report it to the author of the 'truenas' custom integration.

Additional context

https://github.com/tomaae/homeassistant-truenas/blob/4234ed7cfeefc793a054318e882f7fd1e141e529/custom_components/truenas/coordinator.py#L899

Should use the following call:

self.hass.async_create_task(entity_registry.async_remove(entity_id))
tjorim commented 4 months ago

Link to discussion: https://discord.com/channels/330944238910963714/427516175237382144/1233058851893411950

firstof9 commented 4 months ago

FYI, I tested this change locally and it doesn't fix it.

tomaae commented 4 months ago

in regards to discussion, not sure what a problem is there. it does not "spam", its called only when dataset is deleted, so it will get removed from HA.

tomaae commented 4 months ago

so from what I was told, async_remove should be called only from main thread. that means removal of obsolete datasets wont be possible right now.

casapella commented 4 months ago

Hi all. I'm having a pretty similar problem after update

Este error se originó a partir de una integración personalizada.

I'll paste the error in the logs:

Registrador: custom_components.truenas.coordinator
Fuente: helpers/update_coordinator.py:315
integración: TrueNAS (documentación, problemas)
Ocurrió por primera vez: 4 de mayo de 2024, 02:11:41 (2653 ocurrencias)
Último inicio de sesión: 13:28:20

Unexpected error fetching TrueNAS data
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 315, in _async_refresh
    self.data = await self._async_update_data()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/truenas/coordinator.py", line 96, in _async_update_data
    await self.hass.async_add_executor_job(self.get_systemstats)
  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/truenas/coordinator.py", line 427, in get_systemstats
    max(tmp_graph[i]["aggregations"]["mean"].values()), 2
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: max() iterable argument is empty

any Ideas? the integration worked great in the past.

Thanks for the help

firstof9 commented 4 months ago

This is not a similar error, but try using the 1.3b2 release.

tomaae commented 4 months ago

Hi all. I'm having a pretty similar problem after update

Este error se originó a partir de una integración personalizada.

I'll paste the error in the logs:

Registrador: custom_components.truenas.coordinator
Fuente: helpers/update_coordinator.py:315
integración: TrueNAS (documentación, problemas)
Ocurrió por primera vez: 4 de mayo de 2024, 02:11:41 (2653 ocurrencias)
Último inicio de sesión: 13:28:20

Unexpected error fetching TrueNAS data
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 315, in _async_refresh
    self.data = await self._async_update_data()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/truenas/coordinator.py", line 96, in _async_update_data
    await self.hass.async_add_executor_job(self.get_systemstats)
  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/truenas/coordinator.py", line 427, in get_systemstats
    max(tmp_graph[i]["aggregations"]["mean"].values()), 2
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: max() iterable argument is empty

any Ideas? the integration worked great in the past.

Thanks for the help

not, that is not related at all. My guess would be that truenas is having issue with its stats database. If not, you can open new issue for it and add debugs.

casapella commented 4 months ago

This is not a similar error, but try using the 1.3b2 release.

I was using that release. The problem was solved after rolling back to 1.2.5. I've no idea what it was

firstof9 commented 4 months ago

The problem was solved after rolling back to 1.2.5. I've no idea what it was

What version of TrueNAS?

casapella commented 4 months ago

The problem was solved after rolling back to 1.2.5. I've no idea what it was

What version of TrueNAS?

That was the issue. I had TrueNAS-SCALE-23.10.1.XX (I don't remember the exact version.) I updated to TrueNAS-SCALE-23.10.2 and now it's working with 1.3b2.

Thanks for the support!

github-actions[bot] commented 3 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

firstof9 commented 3 months ago

Fix in 1.3b2