Fix context_user_id round trip when calling to_native (#91098)

We do not actually use this in the history or logbook
APIs so nothing broke but there was a bug here for anyone
calling this directly

fixes #91090
This commit is contained in:
J. Nick Koston 2023-04-08 16:31:28 -10:00 committed by Paulus Schoutsen
parent 19567e7fee
commit 0a8f399655
2 changed files with 20 additions and 4 deletions

View File

@ -285,7 +285,7 @@ class Events(Base):
"""Convert to a native HA Event."""
context = Context(
id=bytes_to_ulid_or_none(self.context_id_bin),
user_id=bytes_to_uuid_hex_or_none(self.context_user_id),
user_id=bytes_to_uuid_hex_or_none(self.context_user_id_bin),
parent_id=bytes_to_ulid_or_none(self.context_parent_id_bin),
)
try:
@ -509,7 +509,7 @@ class States(Base):
"""Convert to an HA state object."""
context = Context(
id=bytes_to_ulid_or_none(self.context_id_bin),
user_id=bytes_to_uuid_hex_or_none(self.context_user_id),
user_id=bytes_to_uuid_hex_or_none(self.context_user_id_bin),
parent_id=bytes_to_ulid_or_none(self.context_parent_id_bin),
)
try:

View File

@ -29,7 +29,15 @@ from homeassistant.util import dt, dt as dt_util
def test_from_event_to_db_event() -> None:
"""Test converting event to db event."""
event = ha.Event("test_event", {"some_data": 15})
event = ha.Event(
"test_event",
{"some_data": 15},
context=ha.Context(
id="01EYQZJXZ5Z1Z1Z1Z1Z1Z1Z1Z1",
parent_id="01EYQZJXZ5Z1Z1Z1Z1Z1Z1Z1Z1",
user_id="12345678901234567890123456789012",
),
)
db_event = Events.from_event(event)
dialect = SupportedDialect.MYSQL
db_event.event_data = EventData.shared_data_bytes_from_event(event, dialect)
@ -39,7 +47,15 @@ def test_from_event_to_db_event() -> None:
def test_from_event_to_db_state() -> None:
"""Test converting event to db state."""
state = ha.State("sensor.temperature", "18")
state = ha.State(
"sensor.temperature",
"18",
context=ha.Context(
id="01EYQZJXZ5Z1Z1Z1Z1Z1Z1Z1Z1",
parent_id="01EYQZJXZ5Z1Z1Z1Z1Z1Z1Z1Z1",
user_id="12345678901234567890123456789012",
),
)
event = ha.Event(
EVENT_STATE_CHANGED,
{"entity_id": "sensor.temperature", "old_state": None, "new_state": state},