erlendsellie / priceanalyzer

Priceanalyzer for Home Assistant, based on Nordpool Prices.
64 stars 5 forks source link

async_write_ha_state errors in latest HA beta (2024.5.0b4) #37

Open aherbjornsen opened 6 months ago

aherbjornsen commented 6 months ago

Version of the custom_component

1.5

Homeassistant version

2024.5.0b4

Describe the bug

Integration throws warnings and runtime errors related to 'async_write_ha_state' in latest HA beta release.

Debug log


2024-05-01 13:06:40.481 WARNING (SyncWorker_34) [homeassistant.helpers.frame] Detected that custom integration 'priceanalyzer' calls async_write_ha_state from a thread at custom_components/priceanalyzer/sensor.py, line 403: self.async_write_ha_state(), please report it to the author of the 'priceanalyzer' custom integration
2024-05-01 13:06:40.544 ERROR (SyncWorker_0) [homeassistant.util.logging] Exception in update_sensor when dispatching 'pricanalyzer_checked_stuff': ()
Traceback (most recent call last):
  File "/config/custom_components/priceanalyzer/sensor.py", line 332, in update_sensor
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1008, in async_write_ha_state
    self.hass.verify_event_loop_thread("async_write_ha_state")
  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 'priceanalyzer' calls async_write_ha_state from a thread at custom_components/priceanalyzer/sensor.py, line 332: self.async_write_ha_state(). Please report it to the author of the 'priceanalyzer' custom integration.
erlendsellie commented 6 months ago

Thanks for the heads up! Can you try with the latest beta and see if it works now? I don't have much time and energy to maintain this repo at the moment, but this seemed kinda urgent

aherbjornsen commented 6 months ago

Thanks for the fast response.

I installed 1.5.3 beta, and the errors reported have disappeared

But the integration does not seem to load and I see this during startup:


2024-05-01 21:24:05.370 ERROR (MainThread) [homeassistant.config_entries] Error setting up entry priceanalyzerOslo for priceanalyzer
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 575, in async_setup
    result = await component.async_setup_entry(hass, self)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/priceanalyzer/__init__.py", line 270, in async_setup_entry
    res = await _dry_setup(hass, entry)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/priceanalyzer/__init__.py", line 235, in _dry_setup
    data = Data(
           ^^^^^
  File "/config/custom_components/priceanalyzer/data.py", line 172, in __init__
    self._multiply_template = cv.template(self._multiply_template)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/config_validation.py", line 664, in template
    raise vol.Invalid("template value should be a string")
voluptuous.error.Invalid: template value should be a string
erlendsellie commented 6 months ago

I think this is a bug related to updating to above 1.5 (the betas). Could you try to click "Configure" for the integration, and save it (no changes necessary) from /config/integrations/integration/priceanalyzer ? The new multiply-template seems to cause some issues, and the "default when nothing is provided" fails.

aherbjornsen commented 6 months ago

Yes, that worked. Now the integration is back up again and seems to be working fine. Thanks again!

jreidel commented 6 months ago

Similar problem with 2024.5.2, but latest beta + reconfigure (and restart HA) solved the problem.

aherbjornsen commented 3 months ago

FWIW I still see asyncio related warning from priceanalyzer, using HA 2024.7.2:

2024-07-24 08:37:49.304 WARNING (MainThread) [homeassistant.util.loop] Detected blocking call to open with args ('/usr/local/lib/python3.12/site-packages/pytz/zoneinfo/Europe/Stockholm', 'rb') inside the event loop by custom integration 'priceanalyzer' at custom_components/priceanalyzer/init.py, line 207: tz=timezone("Europe/Stockholm"), (offender: /usr/local/lib/python3.12/site-packages/pytz/init.py, line 108: return open(filename, 'rb')), please create a bug report at https://github.com/erlendsellie/priceanalyzer/issues For developers, please see https://developers.home-assistant.io/docs/asyncio_blocking_operations/#open