mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Reduce indent in recorder event processing (#69659)
This commit is contained in:
parent
72fffde77a
commit
5076437413
@ -1154,61 +1154,61 @@ class Recorder(threading.Thread):
|
|||||||
return
|
return
|
||||||
|
|
||||||
self.event_session.add(dbevent)
|
self.event_session.add(dbevent)
|
||||||
if event.event_type == EVENT_STATE_CHANGED:
|
if event.event_type != EVENT_STATE_CHANGED:
|
||||||
try:
|
return
|
||||||
dbstate = States.from_event(event)
|
|
||||||
shared_attrs = StateAttributes.shared_attrs_from_event(
|
|
||||||
event, self._exclude_attributes_by_domain
|
|
||||||
)
|
|
||||||
except (TypeError, ValueError) as ex:
|
|
||||||
_LOGGER.warning(
|
|
||||||
"State is not JSON serializable: %s: %s",
|
|
||||||
event.data.get("new_state"),
|
|
||||||
ex,
|
|
||||||
)
|
|
||||||
return
|
|
||||||
|
|
||||||
dbstate.attributes = None
|
try:
|
||||||
# Matching attributes found in the pending commit
|
dbstate = States.from_event(event)
|
||||||
if pending_attributes := self._pending_state_attributes.get(shared_attrs):
|
shared_attrs = StateAttributes.shared_attrs_from_event(
|
||||||
dbstate.state_attributes = pending_attributes
|
event, self._exclude_attributes_by_domain
|
||||||
# Matching attributes id found in the cache
|
)
|
||||||
elif attributes_id := self._state_attributes_ids.get(shared_attrs):
|
except (TypeError, ValueError) as ex:
|
||||||
dbstate.attributes_id = attributes_id
|
_LOGGER.warning(
|
||||||
else:
|
"State is not JSON serializable: %s: %s",
|
||||||
attr_hash = StateAttributes.hash_shared_attrs(shared_attrs)
|
event.data.get("new_state"),
|
||||||
# Matching attributes found in the database
|
ex,
|
||||||
if (
|
)
|
||||||
attributes := self.event_session.query(
|
return
|
||||||
StateAttributes.attributes_id
|
|
||||||
)
|
|
||||||
.filter(StateAttributes.hash == attr_hash)
|
|
||||||
.filter(StateAttributes.shared_attrs == shared_attrs)
|
|
||||||
.first()
|
|
||||||
):
|
|
||||||
dbstate.attributes_id = attributes[0]
|
|
||||||
self._state_attributes_ids[shared_attrs] = attributes[0]
|
|
||||||
# No matching attributes found, save them in the DB
|
|
||||||
else:
|
|
||||||
dbstate_attributes = StateAttributes(
|
|
||||||
shared_attrs=shared_attrs, hash=attr_hash
|
|
||||||
)
|
|
||||||
dbstate.state_attributes = dbstate_attributes
|
|
||||||
self._pending_state_attributes[shared_attrs] = dbstate_attributes
|
|
||||||
self.event_session.add(dbstate_attributes)
|
|
||||||
|
|
||||||
if old_state := self._old_states.pop(dbstate.entity_id, None):
|
dbstate.attributes = None
|
||||||
if old_state.state_id:
|
# Matching attributes found in the pending commit
|
||||||
dbstate.old_state_id = old_state.state_id
|
if pending_attributes := self._pending_state_attributes.get(shared_attrs):
|
||||||
else:
|
dbstate.state_attributes = pending_attributes
|
||||||
dbstate.old_state = old_state
|
# Matching attributes id found in the cache
|
||||||
if event.data.get("new_state"):
|
elif attributes_id := self._state_attributes_ids.get(shared_attrs):
|
||||||
self._old_states[dbstate.entity_id] = dbstate
|
dbstate.attributes_id = attributes_id
|
||||||
self._pending_expunge.append(dbstate)
|
else:
|
||||||
|
attr_hash = StateAttributes.hash_shared_attrs(shared_attrs)
|
||||||
|
# Matching attributes found in the database
|
||||||
|
if (
|
||||||
|
attributes := self.event_session.query(StateAttributes.attributes_id)
|
||||||
|
.filter(StateAttributes.hash == attr_hash)
|
||||||
|
.filter(StateAttributes.shared_attrs == shared_attrs)
|
||||||
|
.first()
|
||||||
|
):
|
||||||
|
dbstate.attributes_id = attributes[0]
|
||||||
|
self._state_attributes_ids[shared_attrs] = attributes[0]
|
||||||
|
# No matching attributes found, save them in the DB
|
||||||
else:
|
else:
|
||||||
dbstate.state = None
|
dbstate_attributes = StateAttributes(
|
||||||
dbstate.event = dbevent
|
shared_attrs=shared_attrs, hash=attr_hash
|
||||||
self.event_session.add(dbstate)
|
)
|
||||||
|
dbstate.state_attributes = dbstate_attributes
|
||||||
|
self._pending_state_attributes[shared_attrs] = dbstate_attributes
|
||||||
|
self.event_session.add(dbstate_attributes)
|
||||||
|
|
||||||
|
if old_state := self._old_states.pop(dbstate.entity_id, None):
|
||||||
|
if old_state.state_id:
|
||||||
|
dbstate.old_state_id = old_state.state_id
|
||||||
|
else:
|
||||||
|
dbstate.old_state = old_state
|
||||||
|
if event.data.get("new_state"):
|
||||||
|
self._old_states[dbstate.entity_id] = dbstate
|
||||||
|
self._pending_expunge.append(dbstate)
|
||||||
|
else:
|
||||||
|
dbstate.state = None
|
||||||
|
dbstate.event = dbevent
|
||||||
|
self.event_session.add(dbstate)
|
||||||
|
|
||||||
def _handle_database_error(self, err: Exception) -> bool:
|
def _handle_database_error(self, err: Exception) -> bool:
|
||||||
"""Handle a database error that may result in moving away the corrupt db."""
|
"""Handle a database error that may result in moving away the corrupt db."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user