From b4031241e5104e86665935a3f02f615b2bad3e9e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Erik=20Hedenstr=C3=B6m?= <erik@hedenstroem.com>
Date: Tue, 18 Jun 2024 16:49:22 +0000
Subject: [PATCH] Fixed non blocking file read

---
 openai_stt/stt.py | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/openai_stt/stt.py b/openai_stt/stt.py
index 52ff0d2..66622dc 100644
--- a/openai_stt/stt.py
+++ b/openai_stt/stt.py
@@ -6,6 +6,7 @@ import aiohttp
 import logging
 import os
 import tempfile
+import aiofiles
 import voluptuous as vol
 from homeassistant.components.stt import (
     AudioBitRates,
@@ -105,9 +106,11 @@ class OpenAISTTProvider(Provider):
                 'Authorization': f'Bearer {self._api_key}',
             }
 
-            file_to_send = open(temp_file_path, 'rb')
+            async with aiofiles.open(temp_file_path, mode='rb') as file:
+                audio_data = await file.read()
+
             form = aiohttp.FormData()
-            form.add_field('file', file_to_send, filename='audio.wav', content_type='audio/wav')
+            form.add_field('file', audio_data, filename='audio.wav', content_type='audio/wav')
             form.add_field('language', metadata.language)
             form.add_field('model', 'whisper-1')
 
@@ -124,7 +127,5 @@ class OpenAISTTProvider(Provider):
             _LOGGER.warning(e)
             return SpeechResult("", SpeechResultState.ERROR)
         finally:
-            if 'file_to_send' in locals():
-                file_to_send.close()
             if temp_file_path:
                 os.remove(temp_file_path)
-- 
GitLab