Add async_get_hass_or_none (#118164)

This commit is contained in:
J. Nick Koston
2024-05-26 01:38:46 -10:00
committed by GitHub
parent 05c24e92d1
commit c368ffffd5
7 changed files with 30 additions and 44 deletions

View File

@ -268,8 +268,16 @@ def async_get_hass() -> HomeAssistant:
This should be used where it's very cumbersome or downright impossible to pass
hass to the code which needs it.
"""
if not _hass.hass:
if not (hass := async_get_hass_or_none()):
raise HomeAssistantError("async_get_hass called from the wrong thread")
return hass
def async_get_hass_or_none() -> HomeAssistant | None:
"""Return the HomeAssistant instance or None.
Returns None when called from the wrong thread.
"""
return _hass.hass