Skip to content

Commit

Permalink
fix(sentence): serialize RecognizeResult for JSON compatibility
Browse files Browse the repository at this point in the history
Fixes #327
  • Loading branch information
zachowj committed Dec 15, 2024
1 parent a3fe116 commit f826058
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion custom_components/nodered/sentence.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
from typing import Any

from hassil.expression import Sentence
from hassil.recognize import RecognizeResult
from homeassistant.components.conversation.default_agent import (
DATA_DEFAULT_ENTITY,
Expand Down Expand Up @@ -71,14 +72,17 @@ async def handle_trigger(
device_id was added in 2024.4.0
"""

# RecognizeResult in 2024.12 is not serializable, so we need to convert it to a serializable format
serialized = convert_recognize_result_to_dict(result)

_LOGGER.debug(f"Sentence trigger: {sentence}")
connection.send_message(
event_message(
message_id,
{
"data": {
"sentence": sentence,
"result": result,
"result": serialized,
"deviceId": device_id,
"responseId": message_id,
}
Expand Down Expand Up @@ -174,3 +178,34 @@ async def websocket_sentence_response(
connection.send_message(
error_message(message_id, "sentence_response_not_found", message),
)


def convert_recognize_result_to_dict(result: Any) -> dict:
"""
Serializes a RecognizeResult object into a JSON-serializable dictionary.
"""

def serialize(obj):
if isinstance(obj, Sentence):
# Custom serialization for Sentence
return {
"text": obj.text,
"pattern": obj.pattern.pattern if obj.pattern else None,
}
elif hasattr(obj, "__dict__"):
# For objects with attributes, serialize attributes
return {key: serialize(value) for key, value in vars(obj).items()}
elif isinstance(obj, list):
# Recursively handle lists
return [serialize(item) for item in obj]
elif isinstance(obj, dict):
# Recursively handle dictionaries
return {key: serialize(value) for key, value in obj.items()}
elif isinstance(obj, (int, float, str, type(None))):
# Primitive types are already serializable
return obj
else:
# Fallback for non-serializable types
return str(obj)

return serialize(result)

0 comments on commit f826058

Please sign in to comment.