From c875ff8648c5bfbcdf9f57d1f1ccf8c7384a2d51 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Fri, 23 Jul 2021 00:05:59 +0200 Subject: [PATCH] Store JSON in database in compact format (#53364) * Store JSON in database in compact format * Fix logbook --- homeassistant/components/logbook/__init__.py | 9 ++++----- homeassistant/components/recorder/models.py | 7 +++++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/logbook/__init__.py b/homeassistant/components/logbook/__init__.py index 0a367907464..8992ca2d7fc 100644 --- a/homeassistant/components/logbook/__init__.py +++ b/homeassistant/components/logbook/__init__.py @@ -48,11 +48,10 @@ from homeassistant.helpers.integration_platform import ( from homeassistant.loader import bind_hass import homeassistant.util.dt as dt_util -ENTITY_ID_JSON_TEMPLATE = '"entity_id": "{}"' -ENTITY_ID_JSON_EXTRACT = re.compile('"entity_id": "([^"]+)"') -DOMAIN_JSON_EXTRACT = re.compile('"domain": "([^"]+)"') -ICON_JSON_EXTRACT = re.compile('"icon": "([^"]+)"') - +ENTITY_ID_JSON_TEMPLATE = '"entity_id": ?"{}"' +ENTITY_ID_JSON_EXTRACT = re.compile('"entity_id": ?"([^"]+)"') +DOMAIN_JSON_EXTRACT = re.compile('"domain": ?"([^"]+)"') +ICON_JSON_EXTRACT = re.compile('"icon": ?"([^"]+)"') ATTR_MESSAGE = "message" CONTINUOUS_DOMAINS = ["proximity", "sensor"] diff --git a/homeassistant/components/recorder/models.py b/homeassistant/components/recorder/models.py index 4fd51886246..929115bdf25 100644 --- a/homeassistant/components/recorder/models.py +++ b/homeassistant/components/recorder/models.py @@ -101,7 +101,8 @@ class Events(Base): # type: ignore """Create an event database object from a native event.""" return Events( event_type=event.event_type, - event_data=event_data or json.dumps(event.data, cls=JSONEncoder), + event_data=event_data + or json.dumps(event.data, cls=JSONEncoder, separators=(",", ":")), origin=str(event.origin.value), time_fired=event.time_fired, context_id=event.context.id, @@ -184,7 +185,9 @@ class States(Base): # type: ignore else: dbstate.domain = state.domain dbstate.state = state.state - dbstate.attributes = json.dumps(dict(state.attributes), cls=JSONEncoder) + dbstate.attributes = json.dumps( + dict(state.attributes), cls=JSONEncoder, separators=(",", ":") + ) dbstate.last_changed = state.last_changed dbstate.last_updated = state.last_updated