1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Raise ConditionError for and/or/not errors (#46767)

This commit is contained in:
Anders Melchiorsen
2021-02-19 13:15:30 +01:00
committed by GitHub
parent e7e3e09063
commit bfea7d0baa
3 changed files with 63 additions and 22 deletions

View File

@@ -108,13 +108,19 @@ async def async_and_from_config(
hass: HomeAssistant, variables: TemplateVarsType = None
) -> bool:
"""Test and condition."""
try:
for check in checks:
errors = []
for check in checks:
try:
if not check(hass, variables):
return False
except Exception as ex: # pylint: disable=broad-except
_LOGGER.warning("Error during and-condition: %s", ex)
return False
except ConditionError as ex:
errors.append(str(ex))
except Exception as ex: # pylint: disable=broad-except
errors.append(str(ex))
# Raise the errors if no check was false
if errors:
raise ConditionError("Error in 'and' condition: " + ", ".join(errors))
return True
@@ -134,13 +140,20 @@ async def async_or_from_config(
def if_or_condition(
hass: HomeAssistant, variables: TemplateVarsType = None
) -> bool:
"""Test and condition."""
try:
for check in checks:
"""Test or condition."""
errors = []
for check in checks:
try:
if check(hass, variables):
return True
except Exception as ex: # pylint: disable=broad-except
_LOGGER.warning("Error during or-condition: %s", ex)
except ConditionError as ex:
errors.append(str(ex))
except Exception as ex: # pylint: disable=broad-except
errors.append(str(ex))
# Raise the errors if no check was true
if errors:
raise ConditionError("Error in 'or' condition: " + ", ".join(errors))
return False
@@ -161,12 +174,19 @@ async def async_not_from_config(
hass: HomeAssistant, variables: TemplateVarsType = None
) -> bool:
"""Test not condition."""
try:
for check in checks:
errors = []
for check in checks:
try:
if check(hass, variables):
return False
except Exception as ex: # pylint: disable=broad-except
_LOGGER.warning("Error during not-condition: %s", ex)
except ConditionError as ex:
errors.append(str(ex))
except Exception as ex: # pylint: disable=broad-except
errors.append(str(ex))
# Raise the errors if no check was true
if errors:
raise ConditionError("Error in 'not' condition: " + ", ".join(errors))
return True