michaelarnauts / home-assistant-comfoconnect

Home Assistant integration for Zehnder ComfoAir trough a Zehnder ComfoConnect LAN C.
Other
26 stars 13 forks source link

Comfoconnect calls async_write_ha_state from a thread at custom_components/comfoconnect/select.py #50

Closed mrvanes closed 2 months ago

mrvanes commented 3 months ago

After upgrade to HomeAssistant 24.05 I get this error for comfoconnect integration and was adviced to report here.

May 03 09:13:36 kronos hass[13957]: 2024-05-03 09:13:36.876 ERROR (SyncWorker_0) [homeassistant.util.logging] Exception in _handle_update when dispatching 'comfoconnect_update_00000000002710168001144fd71e2941_56': (-1,)
May 03 09:13:36 kronos hass[13957]: Traceback (most recent call last):
May 03 09:13:36 kronos hass[13957]:   File "/opt/homeassistant/config/custom_components/comfoconnect/select.py", line 195, in _handle_update
May 03 09:13:36 kronos hass[13957]:     self.async_write_ha_state()
May 03 09:13:36 kronos hass[13957]:   File "/opt/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/entity.py", line 1008, in async_write_ha_state
May 03 09:13:36 kronos hass[13957]:     self.hass.verify_event_loop_thread("async_write_ha_state")
May 03 09:13:36 kronos hass[13957]:   File "/opt/homeassistant/lib/python3.12/site-packages/homeassistant/core.py", line 440, in verify_event_loop_thread
May 03 09:13:36 kronos hass[13957]:     frame.report(
May 03 09:13:36 kronos hass[13957]:   File "/opt/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/frame.py", line 162, in report
May 03 09:13:36 kronos hass[13957]:     _report_integration(what, integration_frame, level, error_if_integration)
May 03 09:13:36 kronos hass[13957]:   File "/opt/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/frame.py", line 203, in _report_integration
May 03 09:13:36 kronos hass[13957]:     raise RuntimeError(
May 03 09:13:36 kronos hass[13957]: RuntimeError: Detected that custom integration 'comfoconnect' calls async_write_ha_state from a thread at custom_components/comfoconnect/select.py, line 195: self.async_write_ha_state(). Please report it to the author of the 'comfoconnect' custom integration.
mrvanes commented 3 months ago

The fix is quite simple:

diff --git a/custom_components/comfoconnect/select.py b/custom_components/comfoconnect/select.py
index d44bc61..31e8237 100644
--- a/custom_components/comfoconnect/select.py
+++ b/custom_components/comfoconnect/select.py
@@ -192,7 +192,7 @@ class ComfoConnectSelect(SelectEntity):
         )

         self._attr_current_option = self.entity_description.sensor_value_fn(value)
-        self.async_write_ha_state()
+        self.schedule_update_ha_state()

     async def async_update(self) -> None:
         """Update the state."""

Source: https://community.home-assistant.io/t/how-to-fix-custom-integration-brematic-calls-async-write-ha-state/722238

zollak commented 3 months ago

Hi! I have updated HA Core from 2024.4.4 to 2024.5.0 and then 2024.5.1. The issue was the same in both cases.

Thanks for the fix recommendation! I started to test it on my system and it seems works. I'm going to give some feedback couple days later, how it goes with this one line modification.

litinoveweedle commented 3 months ago

@mrvanes thanks for fix, I can confirm it works OK. Could you please prepare PR so @michaelarnauts can easily merge it? I know it is very small change, but to get this to the release this is probably quickest way. ;-) thank you.

szibis commented 3 months ago

After this merged https://github.com/michaelarnauts/home-assistant-comfoconnect/pull/59 whole init is now fast and all sensors/modes appears without issues.

michaelarnauts commented 2 months ago

Fixed in https://github.com/michaelarnauts/home-assistant-comfoconnect/pull/59

Thanks!