Use correct state in Entity repr and output similar value to State class (#87519)

* Use correct state in Entity repr

* Test Entity.__repr__

* Align with State class
This commit is contained in:
Paulus Schoutsen 2023-02-06 13:35:36 -05:00 committed by GitHub
parent 815ee58ea3
commit a090652560
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 1 deletions

View File

@ -957,7 +957,7 @@ class Entity(ABC):
def __repr__(self) -> str:
"""Return the representation."""
return f"<Entity {self.name}: {self.state}>"
return f"<entity {self.entity_id}={self._stringify_state(self.available)}>"
async def async_request_call(self, coro: Coroutine[Any, Any, Any]) -> None:
"""Process request batched."""

View File

@ -953,3 +953,18 @@ async def test_translation_key(hass):
)
mock_entity2.entity_id = "hello.world"
assert mock_entity2.translation_key == "from_entity_description"
async def test_repr_using_stringify_state():
"""Test that repr uses stringify state."""
class MyEntity(MockEntity):
"""Mock entity."""
@property
def state(self):
"""Return the state."""
raise ValueError("Boom")
entity = MyEntity(entity_id="test.test", available=False)
assert str(entity) == "<entity test.test=unavailable>"