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
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 30 additions and 44 deletions

View file

@ -93,8 +93,8 @@ from homeassistant.const import (
)
from homeassistant.core import (
DOMAIN as HOMEASSISTANT_DOMAIN,
HomeAssistant,
async_get_hass,
async_get_hass_or_none,
split_entity_id,
valid_entity_id,
)
@ -662,11 +662,7 @@ def template(value: Any | None) -> template_helper.Template:
if isinstance(value, (list, dict, template_helper.Template)):
raise vol.Invalid("template value should be a string")
hass: HomeAssistant | None = None
with contextlib.suppress(HomeAssistantError):
hass = async_get_hass()
template_value = template_helper.Template(str(value), hass)
template_value = template_helper.Template(str(value), async_get_hass_or_none())
try:
template_value.ensure_valid()
@ -684,11 +680,7 @@ def dynamic_template(value: Any | None) -> template_helper.Template:
if not template_helper.is_template_string(str(value)):
raise vol.Invalid("template value does not contain a dynamic template")
hass: HomeAssistant | None = None
with contextlib.suppress(HomeAssistantError):
hass = async_get_hass()
template_value = template_helper.Template(str(value), hass)
template_value = template_helper.Template(str(value), async_get_hass_or_none())
try:
template_value.ensure_valid()