Only entity verify state writable once after success unless hass is missing (#118896)

This commit is contained in:
J. Nick Koston 2024-06-06 11:46:44 -05:00 committed by GitHub
parent ffea72f866
commit 62b1bde0e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 7 deletions

View File

@ -473,6 +473,10 @@ class Entity(
# Protect for multiple updates
_update_staged = False
# _verified_state_writable is set to True if the entity has been verified
# to be writable. This is used to avoid repeated checks.
_verified_state_writable = False
# Process updates in parallel
parallel_updates: asyncio.Semaphore | None = None
@ -986,16 +990,20 @@ class Entity(
f"No entity id specified for entity {self.name}"
)
self._verified_state_writable = True
@callback
def _async_write_ha_state_from_call_soon_threadsafe(self) -> None:
"""Write the state to the state machine from the event loop thread."""
self._async_verify_state_writable()
if not self.hass or not self._verified_state_writable:
self._async_verify_state_writable()
self._async_write_ha_state()
@callback
def async_write_ha_state(self) -> None:
"""Write the state to the state machine."""
self._async_verify_state_writable()
if not self.hass or not self._verified_state_writable:
self._async_verify_state_writable()
if self.hass.loop_thread_id != threading.get_ident():
report_non_thread_safe_operation("async_write_ha_state")
self._async_write_ha_state()

View File

@ -1662,11 +1662,6 @@ async def test_warn_no_platform(
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()
@ -1678,6 +1673,11 @@ async def test_warn_no_platform(
ent.async_write_ha_state()
assert error_message not in caplog.text
# No warning if the entity has a platform
caplog.clear()
ent.async_write_ha_state()
assert error_message not in caplog.text
async def test_invalid_state(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture