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

@ -1392,8 +1392,8 @@ async def test_translation_key(hass: HomeAssistant) -> None:
assert mock_entity2.translation_key == "from_entity_description"
async def test_repr_using_stringify_state() -> None:
"""Test that repr uses stringify state."""
async def test_repr(hass) -> None:
"""Test Entity.__repr__."""
class MyEntity(MockEntity):
"""Mock entity."""
@ -1403,9 +1403,20 @@ async def test_repr_using_stringify_state() -> None:
"""Return the state."""
raise ValueError("Boom")
platform = MockEntityPlatform(hass, domain="hello")
my_entity = MyEntity(entity_id="test.test", available=False)
# Not yet added
assert str(my_entity) == "<entity test.test=unknown>"
# Added
await platform.async_add_entities([my_entity])
assert str(my_entity) == "<entity test.test=unavailable>"
# Removed
await platform.async_remove_entity(my_entity.entity_id)
assert str(my_entity) == "<entity test.test=unknown>"
async def test_warn_using_async_update_ha_state(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture