davidohne / ha_whisper-api_stt

Home Assistant Whisper API SST integration
13 stars 6 forks source link

[homeassistant.util.loop] Detected blocking call to open inside the event loop by custom integration 'whisper_api_stt' #2

Open gpayer opened 5 months ago

gpayer commented 5 months ago

I'll just copy the warning message from the home assistant log:

2024-06-22 23:33:40.704 WARNING (MainThread) [homeassistant.util.loop] Detected blocking call to open inside the event loop by custom integration 'whisper_api_stt' at custom_components/whisper_api_stt/stt.py, line 128: file_to_send = open(temp_file_path, 'rb') (offender: /config/custom_components/whisper_api_stt/stt.py, line 128: file_to_send = open(temp_file_path, 'rb')), please create a bug report at https://github.com/davidohne/ha_whisper-api_stt/issues
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/usr/src/homeassistant/homeassistant/__main__.py", line 223, in <module>
    sys.exit(main())
  File "/usr/src/homeassistant/homeassistant/__main__.py", line 209, in main
    exit_code = runner.run(runtime_conf)
  File "/usr/src/homeassistant/homeassistant/runner.py", line 190, in run
    return loop.run_until_complete(setup_and_run_hass(runtime_config))
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 672, in run_until_complete
    self.run_forever()
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 639, in run_forever
    self._run_once()
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 1988, in _run_once
    handle._run()
  File "/usr/local/lib/python3.12/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/usr/src/homeassistant/homeassistant/components/assist_pipeline/__init__.py", line 124, in async_pipeline_from_audio_stream
    await pipeline_input.execute()
  File "/usr/src/homeassistant/homeassistant/components/assist_pipeline/pipeline.py", line 1402, in execute
    intent_input = await self.run.speech_to_text(
  File "/usr/src/homeassistant/homeassistant/components/assist_pipeline/pipeline.py", line 882, in speech_to_text
    result = await self.stt_provider.async_process_audio_stream(
  File "/config/custom_components/whisper_api_stt/stt.py", line 128, in async_process_audio_stream
    file_to_send = open(temp_file_path, 'rb')
gpayer commented 5 months ago

Just use an io.BytesIO() object (without initial data) as your temp_file. Then call temp_file.seek(0) after putting in the wav data and you can put it directly inside form.add_field instead of file_to_send. Then you don't have the hassle of blocking operations and local temp files. And that means you can get rid of the finally part as well.