Teach Context about deepcopy (#118206)

Teach context about deepcopy
This commit is contained in:
Paulus Schoutsen 2024-05-26 23:14:02 -04:00 committed by GitHub
parent c15f7f304f
commit 87fc27eeae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 0 deletions

View File

@ -1259,6 +1259,14 @@ class Context:
"""Compare contexts."""
return isinstance(other, Context) and self.id == other.id
def __copy__(self) -> Context:
"""Create a shallow copy of this context."""
return Context(user_id=self.user_id, parent_id=self.parent_id, id=self.id)
def __deepcopy__(self, memo: dict[int, Any]) -> Context:
"""Create a deep copy of this context."""
return Context(user_id=self.user_id, parent_id=self.parent_id, id=self.id)
@cached_property
def _as_dict(self) -> dict[str, str | None]:
"""Return a dictionary representation of the context.

View File

@ -90,6 +90,7 @@ async def test_assist_api(hass: HomeAssistant) -> None:
assert str(tool) == "<IntentTool - test_intent>"
test_context = Context()
assert test_context.json_fragment # To reproduce an error case in tracing
intent_response = intent.IntentResponse("*")
intent_response.matched_states = [State("light.matched", "on")]
intent_response.unmatched_states = [State("light.unmatched", "on")]