mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Use python defaults for comparing State, LazyState, and Event objects (#86856)
* Speed up comparing State and Event objects Use default python implementation for State and Event __hash__ and __eq__ The default implementation compared based on the id() of the object which is effectively what we want here anyways. These overrides are left over from the days when these used to be attrs objects By avoiding implementing these ourselves all of the equality checks can happen in native code * tweak * adjust tests * write out some more * fix test to not compare objects * more test fixes * more test fixes * correct stats tests * fix more tests * fix more tests * update sensor recorder tests
This commit is contained in:
@@ -813,11 +813,6 @@ class Event:
|
||||
id=ulid_util.ulid(dt_util.utc_to_timestamp(self.time_fired))
|
||||
)
|
||||
|
||||
def __hash__(self) -> int:
|
||||
"""Make hashable."""
|
||||
# The only event type that shares context are the TIME_CHANGED
|
||||
return hash((self.event_type, self.context.id, self.time_fired))
|
||||
|
||||
def as_dict(self) -> dict[str, Any]:
|
||||
"""Create a dict representation of this Event.
|
||||
|
||||
@@ -841,17 +836,6 @@ class Event:
|
||||
|
||||
return f"<Event {self.event_type}[{str(self.origin)[0]}]>"
|
||||
|
||||
def __eq__(self, other: Any) -> bool:
|
||||
"""Return the comparison."""
|
||||
return ( # type: ignore[no-any-return]
|
||||
self.__class__ == other.__class__
|
||||
and self.event_type == other.event_type
|
||||
and self.data == other.data
|
||||
and self.origin == other.origin
|
||||
and self.time_fired == other.time_fired
|
||||
and self.context == other.context
|
||||
)
|
||||
|
||||
|
||||
class _FilterableJob(NamedTuple):
|
||||
"""Event listener job to be executed with optional filter."""
|
||||
@@ -1156,13 +1140,6 @@ class State:
|
||||
self._as_dict: ReadOnlyDict[str, Collection[Any]] | None = None
|
||||
self._as_compressed_state: dict[str, Any] | None = None
|
||||
|
||||
def __hash__(self) -> int:
|
||||
"""Make the state hashable.
|
||||
|
||||
State objects are effectively immutable.
|
||||
"""
|
||||
return hash((id(self), self.last_updated))
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Name of this state."""
|
||||
@@ -1274,16 +1251,6 @@ class State:
|
||||
self.context.user_id, self.context.parent_id, self.context.id
|
||||
)
|
||||
|
||||
def __eq__(self, other: Any) -> bool:
|
||||
"""Return the comparison of the state."""
|
||||
return ( # type: ignore[no-any-return]
|
||||
self.__class__ == other.__class__
|
||||
and self.entity_id == other.entity_id
|
||||
and self.state == other.state
|
||||
and self.attributes == other.attributes
|
||||
and self.context == other.context
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
"""Return the representation of the states."""
|
||||
attrs = f"; {util.repr_helper(self.attributes)}" if self.attributes else ""
|
||||
|
||||
Reference in New Issue
Block a user