Skip to content

Commit

Permalink
debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
fabio-garavini committed Aug 15, 2024
1 parent 0c3582a commit 64ac86f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions custom_components/openai_whisper_cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up a config entry."""

_LOGGER.info("Setting up %s", entry)

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

return True


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""

_LOGGER.info("Unloading %s", entry)

return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

async def async_migrate_entry(hass, config_entry: ConfigEntry):
Expand Down
13 changes: 13 additions & 0 deletions custom_components/openai_whisper_cloud/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
async def validate_input(data: dict):
"""Validate the user input."""

obscured_api_key = data.get(CONF_API_KEY)
data[CONF_API_KEY] = "<api_key>"
_LOGGER.debug("User validation got: %s", data)
data[CONF_API_KEY] = obscured_api_key

if data.get(CONF_TEMPERATURE) is None:
data[CONF_TEMPERATURE] = DEFAULT_TEMPERATURE
if data.get(CONF_PROMPT) is None:
Expand All @@ -59,6 +64,8 @@ async def validate_input(data: dict):
},
)

_LOGGER.debug("Models request took %f s and returned %d - %s", response.elapsed.seconds, response.status_code, response.reason)

if response.status_code == 401:
raise InvalidAPIKey

Expand All @@ -73,6 +80,8 @@ async def validate_input(data: dict):
break
if model == response.json().get("data")[-1]:
raise WhisperModelBlocked

_LOGGER.debug("User validation successful")


class ConfigFlow(ConfigFlow, domain=DOMAIN):
Expand Down Expand Up @@ -100,12 +109,16 @@ async def async_step_user(
_LOGGER.error(e)
errors["base"] = "connection_error"
except UnauthorizedError:
_LOGGER.exception("Unauthorized")
errors["base"] = "unauthorized"
except InvalidAPIKey:
_LOGGER.exception("Invalid API key")
errors[CONF_API_KEY] = "invalid_api_key"
except WhisperModelBlocked:
_LOGGER.exception("Whisper Model Not Found")
errors["base"] = "whisper_blocked"
except UnknownError:
_LOGGER.exception("Unknown error")
errors["base"] = "unknown"

return self.async_show_form(
Expand Down
8 changes: 8 additions & 0 deletions custom_components/openai_whisper_cloud/stt.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Demo speech platform via config entry."""
_LOGGER.debug("Setup Entry %s", config_entry.entry_id)
async_add_entities(
[OpenAIWhisperCloudEntity(**config_entry.data, unique_id=config_entry.entry_id)]
)
Expand Down Expand Up @@ -98,11 +99,14 @@ async def async_process_audio_stream(
) -> SpeechResult:
"""Process an audio stream to STT service."""

_LOGGER.debug("Processing audio stream: %s", metadata)

data = b""
async for chunk in stream:
data += chunk

if not data:
_LOGGER.error("No audio data received")
return SpeechResult("", SpeechResultState.ERROR)

try:
Expand All @@ -116,6 +120,8 @@ async def async_process_audio_stream(
# Ensure the buffer is at the start before passing it
temp_file.seek(0)

_LOGGER.debug("Temp wav audio file created")

# Prepare the files parameter with a proper filename
files = {
"file": ("audio.wav", temp_file, "audio/wav"),
Expand All @@ -140,6 +146,8 @@ async def async_process_audio_stream(
data=data,
)

_LOGGER.debug("Transcription request took %f s and returned %d - %s", response.elapsed.seconds, response.status_code, response.reason)

# Parse the JSON response
transcription = response.json().get("text", "")

Expand Down

0 comments on commit 64ac86f

Please sign in to comment.