mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Add debug mode to catch unsafe thread operations using core helpers (#115390)
* adjust * adjust * fixes * one more * test * debug * move to config * cover * Update homeassistant/core.py * set debug from RuntimeConfig * reduce * fix message * raise * Update homeassistant/core.py * Update homeassistant/core.py * no flood check for raise * cover
This commit is contained in:
@@ -2594,3 +2594,24 @@ async def test_get_hassjob_type(hass: HomeAssistant) -> None:
|
||||
assert ent_1.get_hassjob_type("update") is HassJobType.Executor
|
||||
assert ent_1.get_hassjob_type("async_update") is HassJobType.Coroutinefunction
|
||||
assert ent_1.get_hassjob_type("update_callback") is HassJobType.Callback
|
||||
|
||||
|
||||
async def test_async_write_ha_state_thread_safety(hass: HomeAssistant) -> None:
|
||||
"""Test async_write_ha_state thread safety."""
|
||||
hass.config.debug = True
|
||||
|
||||
ent = entity.Entity()
|
||||
ent.entity_id = "test.any"
|
||||
ent.hass = hass
|
||||
ent.async_write_ha_state()
|
||||
assert hass.states.get(ent.entity_id)
|
||||
|
||||
ent2 = entity.Entity()
|
||||
ent2.entity_id = "test.any2"
|
||||
ent2.hass = hass
|
||||
with pytest.raises(
|
||||
RuntimeError,
|
||||
match="Detected code that calls async_write_ha_state from a thread.",
|
||||
):
|
||||
await hass.async_add_executor_job(ent2.async_write_ha_state)
|
||||
assert not hass.states.get(ent2.entity_id)
|
||||
|
||||
Reference in New Issue
Block a user