mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 03:37:07 +00:00
Cleanup db_schema from_event constructors (#120803)
This commit is contained in:
parent
6ed0960648
commit
852bb19223
@ -238,7 +238,6 @@ class JSONLiteral(JSON):
|
|||||||
|
|
||||||
|
|
||||||
EVENT_ORIGIN_ORDER = [EventOrigin.local, EventOrigin.remote]
|
EVENT_ORIGIN_ORDER = [EventOrigin.local, EventOrigin.remote]
|
||||||
EVENT_ORIGIN_TO_IDX = {origin: idx for idx, origin in enumerate(EVENT_ORIGIN_ORDER)}
|
|
||||||
|
|
||||||
|
|
||||||
class Events(Base):
|
class Events(Base):
|
||||||
@ -305,18 +304,19 @@ class Events(Base):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def from_event(event: Event) -> Events:
|
def from_event(event: Event) -> Events:
|
||||||
"""Create an event database object from a native event."""
|
"""Create an event database object from a native event."""
|
||||||
|
context = event.context
|
||||||
return Events(
|
return Events(
|
||||||
event_type=None,
|
event_type=None,
|
||||||
event_data=None,
|
event_data=None,
|
||||||
origin_idx=EVENT_ORIGIN_TO_IDX.get(event.origin),
|
origin_idx=event.origin.idx,
|
||||||
time_fired=None,
|
time_fired=None,
|
||||||
time_fired_ts=event.time_fired_timestamp,
|
time_fired_ts=event.time_fired_timestamp,
|
||||||
context_id=None,
|
context_id=None,
|
||||||
context_id_bin=ulid_to_bytes_or_none(event.context.id),
|
context_id_bin=ulid_to_bytes_or_none(context.id),
|
||||||
context_user_id=None,
|
context_user_id=None,
|
||||||
context_user_id_bin=uuid_hex_to_bytes_or_none(event.context.user_id),
|
context_user_id_bin=uuid_hex_to_bytes_or_none(context.user_id),
|
||||||
context_parent_id=None,
|
context_parent_id=None,
|
||||||
context_parent_id_bin=ulid_to_bytes_or_none(event.context.parent_id),
|
context_parent_id_bin=ulid_to_bytes_or_none(context.parent_id),
|
||||||
)
|
)
|
||||||
|
|
||||||
def to_native(self, validate_entity_id: bool = True) -> Event | None:
|
def to_native(self, validate_entity_id: bool = True) -> Event | None:
|
||||||
@ -492,41 +492,42 @@ class States(Base):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def from_event(event: Event[EventStateChangedData]) -> States:
|
def from_event(event: Event[EventStateChangedData]) -> States:
|
||||||
"""Create object from a state_changed event."""
|
"""Create object from a state_changed event."""
|
||||||
entity_id = event.data["entity_id"]
|
|
||||||
state = event.data["new_state"]
|
state = event.data["new_state"]
|
||||||
dbstate = States(
|
|
||||||
entity_id=entity_id,
|
|
||||||
attributes=None,
|
|
||||||
context_id=None,
|
|
||||||
context_id_bin=ulid_to_bytes_or_none(event.context.id),
|
|
||||||
context_user_id=None,
|
|
||||||
context_user_id_bin=uuid_hex_to_bytes_or_none(event.context.user_id),
|
|
||||||
context_parent_id=None,
|
|
||||||
context_parent_id_bin=ulid_to_bytes_or_none(event.context.parent_id),
|
|
||||||
origin_idx=EVENT_ORIGIN_TO_IDX.get(event.origin),
|
|
||||||
last_updated=None,
|
|
||||||
last_changed=None,
|
|
||||||
)
|
|
||||||
# None state means the state was removed from the state machine
|
# None state means the state was removed from the state machine
|
||||||
if state is None:
|
if state is None:
|
||||||
dbstate.state = ""
|
state_value = ""
|
||||||
dbstate.last_updated_ts = event.time_fired_timestamp
|
last_updated_ts = event.time_fired_timestamp
|
||||||
dbstate.last_changed_ts = None
|
last_changed_ts = None
|
||||||
dbstate.last_reported_ts = None
|
last_reported_ts = None
|
||||||
return dbstate
|
|
||||||
|
|
||||||
dbstate.state = state.state
|
|
||||||
dbstate.last_updated_ts = state.last_updated_timestamp
|
|
||||||
if state.last_updated == state.last_changed:
|
|
||||||
dbstate.last_changed_ts = None
|
|
||||||
else:
|
else:
|
||||||
dbstate.last_changed_ts = state.last_changed_timestamp
|
state_value = state.state
|
||||||
if state.last_updated == state.last_reported:
|
last_updated_ts = state.last_updated_timestamp
|
||||||
dbstate.last_reported_ts = None
|
if state.last_updated == state.last_changed:
|
||||||
else:
|
last_changed_ts = None
|
||||||
dbstate.last_reported_ts = state.last_reported_timestamp
|
else:
|
||||||
|
last_changed_ts = state.last_changed_timestamp
|
||||||
return dbstate
|
if state.last_updated == state.last_reported:
|
||||||
|
last_reported_ts = None
|
||||||
|
else:
|
||||||
|
last_reported_ts = state.last_reported_timestamp
|
||||||
|
context = event.context
|
||||||
|
return States(
|
||||||
|
state=state_value,
|
||||||
|
entity_id=event.data["entity_id"],
|
||||||
|
attributes=None,
|
||||||
|
context_id=None,
|
||||||
|
context_id_bin=ulid_to_bytes_or_none(context.id),
|
||||||
|
context_user_id=None,
|
||||||
|
context_user_id_bin=uuid_hex_to_bytes_or_none(context.user_id),
|
||||||
|
context_parent_id=None,
|
||||||
|
context_parent_id_bin=ulid_to_bytes_or_none(context.parent_id),
|
||||||
|
origin_idx=event.origin.idx,
|
||||||
|
last_updated=None,
|
||||||
|
last_changed=None,
|
||||||
|
last_updated_ts=last_updated_ts,
|
||||||
|
last_changed_ts=last_changed_ts,
|
||||||
|
last_reported_ts=last_reported_ts,
|
||||||
|
)
|
||||||
|
|
||||||
def to_native(self, validate_entity_id: bool = True) -> State | None:
|
def to_native(self, validate_entity_id: bool = True) -> State | None:
|
||||||
"""Convert to an HA state object."""
|
"""Convert to an HA state object."""
|
||||||
|
@ -1308,6 +1308,11 @@ class EventOrigin(enum.Enum):
|
|||||||
"""Return the event."""
|
"""Return the event."""
|
||||||
return self.value
|
return self.value
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def idx(self) -> int:
|
||||||
|
"""Return the index of the origin."""
|
||||||
|
return next((idx for idx, origin in enumerate(EventOrigin) if origin is self))
|
||||||
|
|
||||||
|
|
||||||
class Event(Generic[_DataT]):
|
class Event(Generic[_DataT]):
|
||||||
"""Representation of an event within the bus."""
|
"""Representation of an event within the bus."""
|
||||||
|
@ -920,6 +920,14 @@ def test_event_repr() -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_event_origin_idx() -> None:
|
||||||
|
"""Test the EventOrigin idx."""
|
||||||
|
assert ha.EventOrigin.remote is ha.EventOrigin.remote
|
||||||
|
assert ha.EventOrigin.local is ha.EventOrigin.local
|
||||||
|
assert ha.EventOrigin.local.idx == 0
|
||||||
|
assert ha.EventOrigin.remote.idx == 1
|
||||||
|
|
||||||
|
|
||||||
def test_event_as_dict() -> None:
|
def test_event_as_dict() -> None:
|
||||||
"""Test an Event as dictionary."""
|
"""Test an Event as dictionary."""
|
||||||
event_type = "some_type"
|
event_type = "some_type"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user