Files
2026-08-24 21:55:33 +08:00

21 lines
547 B
Python

"""Helpers for node-red."""
from datetime import timedelta
from typing import Any
from homeassistant.helpers.json import JSONEncoder
class NodeRedJSONEncoder(JSONEncoder):
"""JSONEncoder that supports timedelta objects and falls back to the Home Assistant Encoder."""
def default(self, o: Any) -> Any:
"""Convert timedelta objects.
Hand other objects to the Home Assistant JSONEncoder.
"""
if isinstance(o, timedelta):
return o.total_seconds()
return JSONEncoder.default(self, o)