mirror of
https://github.com/home-assistant/core.git
synced 2025-07-29 18:28:14 +02:00
Add async_get_hass_or_none (#118164)
This commit is contained in:
@ -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
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user