Coverage for custom_components/remote_logger/helpers.py: 100%
22 statements
« prev ^ index » next coverage.py v7.10.6, created at 2026-04-07 04:46 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2026-04-07 04:46 +0000
1import datetime as dt
2from typing import Any
4from homeassistant.util import dt as dt_util
7def flatten_event_data(prefix: str, value: Any, state_only: bool) -> list[tuple[str, Any]]:
8 """Flatten a value into (key, scalar) pairs using dotted key notation.
10 If the value has an ``as_dict`` method it is called first. The resulting
11 dict (or any plain dict) is recursively expanded. Any other value is
12 returned as-is as a single pair.
13 """
14 if hasattr(value, "as_dict"):
15 value = value.as_dict()
16 if hasattr(value, "value"):
17 value = value.value
18 if isinstance(value, dict):
19 result: list[tuple[str, Any]] = []
20 for k, v in value.items():
21 if k not in ("attributes", "context") or not state_only:
22 result.extend(flatten_event_data(f"{prefix}.{k}", v, state_only))
23 return result
24 return [(prefix, value)]
27def isotimestamp(time_value: float) -> str | None:
28 if time_value and isinstance(time_value, float):
29 ts = dt.datetime.fromtimestamp(time_value, tz=dt_util.get_default_time_zone())
30 if dt_util.get_default_time_zone() == dt.UTC:
31 return f"{ts.isoformat()[:26]}Z"
32 return f"{ts.isoformat()}"
33 return None