mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Remove unneeded checks for Entity.platform (#94321)
* Remove unneeded checks for Entity.platform * Update tests * Prevent breaking integrations without an EntityComponent * Warn when entity has no platform
This commit is contained in:
@@ -578,12 +578,14 @@ async def test_async_remove_no_platform(hass: HomeAssistant) -> None:
|
||||
|
||||
|
||||
async def test_async_remove_runs_callbacks(hass: HomeAssistant) -> None:
|
||||
"""Test async_remove method when no platform set."""
|
||||
"""Test async_remove runs on_remove callback."""
|
||||
result = []
|
||||
|
||||
platform = MockEntityPlatform(hass, domain="test")
|
||||
ent = entity.Entity()
|
||||
ent.hass = hass
|
||||
ent.entity_id = "test.test"
|
||||
await platform.async_add_entities([ent])
|
||||
ent.async_on_remove(lambda: result.append(1))
|
||||
await ent.async_remove()
|
||||
assert len(result) == 1
|
||||
@@ -593,11 +595,12 @@ async def test_async_remove_ignores_in_flight_polling(hass: HomeAssistant) -> No
|
||||
"""Test in flight polling is ignored after removing."""
|
||||
result = []
|
||||
|
||||
platform = MockEntityPlatform(hass, domain="test")
|
||||
ent = entity.Entity()
|
||||
ent.hass = hass
|
||||
ent.entity_id = "test.test"
|
||||
ent.async_on_remove(lambda: result.append(1))
|
||||
ent.async_write_ha_state()
|
||||
await platform.async_add_entities([ent])
|
||||
assert hass.states.get("test.test").state == STATE_UNKNOWN
|
||||
await ent.async_remove()
|
||||
assert len(result) == 1
|
||||
@@ -798,18 +801,18 @@ async def test_setup_source(hass: HomeAssistant) -> None:
|
||||
|
||||
async def test_removing_entity_unavailable(hass: HomeAssistant) -> None:
|
||||
"""Test removing an entity that is still registered creates an unavailable state."""
|
||||
entry = er.RegistryEntry(
|
||||
er.RegistryEntry(
|
||||
entity_id="hello.world",
|
||||
unique_id="test-unique-id",
|
||||
platform="test-platform",
|
||||
disabled_by=None,
|
||||
)
|
||||
|
||||
platform = MockEntityPlatform(hass, domain="hello")
|
||||
ent = entity.Entity()
|
||||
ent.hass = hass
|
||||
ent.entity_id = "hello.world"
|
||||
ent.registry_entry = entry
|
||||
ent.async_write_ha_state()
|
||||
ent._attr_unique_id = "test-unique-id"
|
||||
await platform.async_add_entities([ent])
|
||||
|
||||
state = hass.states.get("hello.world")
|
||||
assert state is not None
|
||||
@@ -1112,19 +1115,48 @@ async def test_warn_using_async_update_ha_state(
|
||||
"""Test we warn once when using async_update_ha_state without force_update."""
|
||||
ent = entity.Entity()
|
||||
ent.hass = hass
|
||||
ent.platform = MockEntityPlatform(hass)
|
||||
ent.entity_id = "hello.world"
|
||||
error_message = "is using self.async_update_ha_state()"
|
||||
|
||||
# When forcing, it should not trigger the warning
|
||||
caplog.clear()
|
||||
await ent.async_update_ha_state(force_refresh=True)
|
||||
assert "is using self.async_update_ha_state()" not in caplog.text
|
||||
assert error_message not in caplog.text
|
||||
|
||||
# When not forcing, it should trigger the warning
|
||||
caplog.clear()
|
||||
await ent.async_update_ha_state()
|
||||
assert "is using self.async_update_ha_state()" in caplog.text
|
||||
assert error_message in caplog.text
|
||||
|
||||
# When not forcing, it should not trigger the warning again
|
||||
caplog.clear()
|
||||
await ent.async_update_ha_state()
|
||||
assert "is using self.async_update_ha_state()" not in caplog.text
|
||||
assert error_message not in caplog.text
|
||||
|
||||
|
||||
async def test_warn_no_platform(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
"""Test we warn am entity does not have a platform."""
|
||||
ent = entity.Entity()
|
||||
ent.hass = hass
|
||||
ent.platform = MockEntityPlatform(hass)
|
||||
ent.entity_id = "hello.world"
|
||||
error_message = "does not have a platform"
|
||||
|
||||
# No warning if the entity has a platform
|
||||
caplog.clear()
|
||||
ent.async_write_ha_state()
|
||||
assert error_message not in caplog.text
|
||||
|
||||
# Without a platform, it should trigger the warning
|
||||
ent.platform = None
|
||||
caplog.clear()
|
||||
ent.async_write_ha_state()
|
||||
assert error_message in caplog.text
|
||||
|
||||
# Without a platform, it should not trigger the warning again
|
||||
caplog.clear()
|
||||
ent.async_write_ha_state()
|
||||
assert error_message not in caplog.text
|
||||
|
||||
Reference in New Issue
Block a user