Ensure it's safe to call Entity.__repr__ on non added entity (#106032)

This commit is contained in:
Erik Montnemery 2023-12-31 18:54:34 +01:00 committed by GitHub
parent c83388fd2d
commit c1f1b5c50b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 14 deletions

View file

@ -1476,7 +1476,12 @@ class Entity(
self.async_on_remove(self._async_unsubscribe_device_updates)
def __repr__(self) -> str:
"""Return the representation."""
"""Return the representation.
If the entity is not added to a platform it's not safe to call _stringify_state.
"""
if self._platform_state != EntityPlatformState.ADDED:
return f"<entity {self.entity_id}={STATE_UNKNOWN}>"
return f"<entity {self.entity_id}={self._stringify_state(self.available)}>"
async def async_request_call(self, coro: Coroutine[Any, Any, _T]) -> _T: