|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | | -from datetime import date |
6 | | -import decimal |
7 | 5 | import logging |
8 | 6 | from typing import TYPE_CHECKING, Any |
9 | 7 |
|
|
44 | 42 | from .const import CONF_ADVANCED_OPTIONS, CONF_COLUMN_NAME, CONF_QUERY, DOMAIN |
45 | 43 | from .util import ( |
46 | 44 | async_create_sessionmaker, |
| 45 | + ensure_serializable, |
47 | 46 | generate_lambda_stmt, |
48 | 47 | redact_credentials, |
49 | 48 | resolve_db_url, |
@@ -247,19 +246,13 @@ def extra_state_attributes(self) -> dict[str, Any] | None: |
247 | 246 | def _process(self, result: Result) -> None: |
248 | 247 | """Process the SQL result.""" |
249 | 248 | data = None |
250 | | - extra_state_attributes = {} |
251 | | - for res in result.mappings(): |
252 | | - _LOGGER.debug("Query %s result in %s", self._query, res.items()) |
253 | | - data = res[self._column_name] |
254 | | - for key, value in res.items(): |
255 | | - if isinstance(value, decimal.Decimal): |
256 | | - value = float(value) |
257 | | - elif isinstance(value, date): |
258 | | - value = value.isoformat() |
259 | | - elif isinstance(value, (bytes, bytearray)): |
260 | | - value = f"0x{value.hex()}" |
261 | | - extra_state_attributes[key] = value |
262 | | - self._attr_extra_state_attributes[key] = value |
| 249 | + |
| 250 | + for row in result.mappings(): |
| 251 | + row_items = row.items() |
| 252 | + _LOGGER.debug("Query %s result in %s", self._query, row_items) |
| 253 | + data = row[self._column_name] |
| 254 | + for key, value in row_items: |
| 255 | + self._attr_extra_state_attributes[key] = ensure_serializable(value) |
263 | 256 |
|
264 | 257 | if data is not None and isinstance(data, (bytes, bytearray)): |
265 | 258 | data = f"0x{data.hex()}" |
|
0 commit comments