From d501d8cb289606b8df4b2dcbd3b68e8bbdf46035 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Thu, 26 Mar 2026 11:32:39 +0100 Subject: [PATCH] Adjust some trigger and condition schemas (#166568) --- homeassistant/components/text/condition.py | 12 ++---- .../components/water_heater/condition.py | 11 +---- homeassistant/helpers/condition.py | 42 +++++++------------ homeassistant/helpers/trigger.py | 39 +++++++---------- 4 files changed, 35 insertions(+), 69 deletions(-) diff --git a/homeassistant/components/text/condition.py b/homeassistant/components/text/condition.py index 3945488d666..7fe4ee44568 100644 --- a/homeassistant/components/text/condition.py +++ b/homeassistant/components/text/condition.py @@ -5,14 +5,12 @@ from typing import TYPE_CHECKING import voluptuous as vol from homeassistant.components.input_text import DOMAIN as INPUT_TEXT_DOMAIN -from homeassistant.const import CONF_OPTIONS, CONF_TARGET +from homeassistant.const import CONF_OPTIONS from homeassistant.core import HomeAssistant, State from homeassistant.helpers import config_validation as cv from homeassistant.helpers.automation import DomainSpec from homeassistant.helpers.condition import ( - ATTR_BEHAVIOR, - BEHAVIOR_ALL, - BEHAVIOR_ANY, + ENTITY_STATE_CONDITION_SCHEMA_ANY_ALL, Condition, ConditionConfig, EntityConditionBase, @@ -22,13 +20,9 @@ from .const import DOMAIN CONF_VALUE = "value" -_TEXT_CONDITION_SCHEMA = vol.Schema( +_TEXT_CONDITION_SCHEMA = ENTITY_STATE_CONDITION_SCHEMA_ANY_ALL.extend( { - vol.Required(CONF_TARGET): cv.TARGET_FIELDS, vol.Required(CONF_OPTIONS): { - vol.Required(ATTR_BEHAVIOR, default=BEHAVIOR_ANY): vol.In( - [BEHAVIOR_ANY, BEHAVIOR_ALL] - ), vol.Required(CONF_VALUE): cv.string, }, } diff --git a/homeassistant/components/water_heater/condition.py b/homeassistant/components/water_heater/condition.py index 64f4d128954..ce1f36c5269 100644 --- a/homeassistant/components/water_heater/condition.py +++ b/homeassistant/components/water_heater/condition.py @@ -9,7 +9,6 @@ import voluptuous as vol from homeassistant.const import ( ATTR_TEMPERATURE, CONF_OPTIONS, - CONF_TARGET, STATE_OFF, UnitOfTemperature, ) @@ -17,9 +16,7 @@ from homeassistant.core import HomeAssistant, State from homeassistant.helpers import config_validation as cv from homeassistant.helpers.automation import DomainSpec, NumericalDomainSpec from homeassistant.helpers.condition import ( - ATTR_BEHAVIOR, - BEHAVIOR_ALL, - BEHAVIOR_ANY, + ENTITY_STATE_CONDITION_SCHEMA_ANY_ALL, Condition, ConditionConfig, EntityConditionBase, @@ -33,13 +30,9 @@ from .const import DOMAIN ATTR_OPERATION_MODE = "operation_mode" -_OPERATION_MODE_CONDITION_SCHEMA = vol.Schema( +_OPERATION_MODE_CONDITION_SCHEMA = ENTITY_STATE_CONDITION_SCHEMA_ANY_ALL.extend( { - vol.Required(CONF_TARGET): cv.TARGET_FIELDS, vol.Required(CONF_OPTIONS): { - vol.Required(ATTR_BEHAVIOR, default=BEHAVIOR_ANY): vol.In( - [BEHAVIOR_ANY, BEHAVIOR_ALL] - ), vol.Required(ATTR_OPERATION_MODE): vol.All( cv.ensure_list, vol.Length(min=1), [str] ), diff --git a/homeassistant/helpers/condition.py b/homeassistant/helpers/condition.py index 967ddefe1b8..19537a20f0b 100644 --- a/homeassistant/helpers/condition.py +++ b/homeassistant/helpers/condition.py @@ -462,19 +462,13 @@ def make_entity_state_condition( return CustomCondition -NUMERICAL_CONDITION_SCHEMA = vol.Schema( +NUMERICAL_CONDITION_SCHEMA = ENTITY_STATE_CONDITION_SCHEMA_ANY_ALL.extend( { - vol.Required(CONF_TARGET): cv.TARGET_FIELDS, - vol.Required(CONF_OPTIONS): vol.All( - { - vol.Required(ATTR_BEHAVIOR, default=BEHAVIOR_ANY): vol.In( - [BEHAVIOR_ANY, BEHAVIOR_ALL] - ), - vol.Required("threshold"): NumericThresholdSelector( - NumericThresholdSelectorConfig(mode=NumericThresholdMode.IS) - ), - }, - ), + vol.Required(CONF_OPTIONS): { + vol.Required("threshold"): NumericThresholdSelector( + NumericThresholdSelectorConfig(mode=NumericThresholdMode.IS) + ), + }, } ) @@ -588,22 +582,16 @@ def _make_numerical_condition_with_unit_schema( unit_converter: type[BaseUnitConverter], ) -> vol.Schema: """Factory for numerical condition schema with unit option.""" - return vol.Schema( + return ENTITY_STATE_CONDITION_SCHEMA_ANY_ALL.extend( { - vol.Required(CONF_TARGET): cv.TARGET_FIELDS, - vol.Required(CONF_OPTIONS): vol.All( - { - vol.Required(ATTR_BEHAVIOR, default=BEHAVIOR_ANY): vol.In( - [BEHAVIOR_ANY, BEHAVIOR_ALL] - ), - vol.Required("threshold"): NumericThresholdSelector( - NumericThresholdSelectorConfig( - mode=NumericThresholdMode.IS, - unit_of_measurement=list(unit_converter.VALID_UNITS), - ) - ), - }, - ), + vol.Required(CONF_OPTIONS): { + vol.Required("threshold"): NumericThresholdSelector( + NumericThresholdSelectorConfig( + mode=NumericThresholdMode.IS, + unit_of_measurement=list(unit_converter.VALID_UNITS), + ) + ), + }, } ) diff --git a/homeassistant/helpers/trigger.py b/homeassistant/helpers/trigger.py index fefbc416cb1..975c3dddc3c 100644 --- a/homeassistant/helpers/trigger.py +++ b/homeassistant/helpers/trigger.py @@ -336,7 +336,6 @@ ENTITY_STATE_TRIGGER_SCHEMA_FIRST_LAST = ENTITY_STATE_TRIGGER_SCHEMA.extend( [BEHAVIOR_FIRST, BEHAVIOR_LAST, BEHAVIOR_ANY] ), }, - vol.Required(CONF_TARGET): cv.TARGET_FIELDS, } ) @@ -746,19 +745,16 @@ class EntityNumericalStateChangedTriggerWithUnitBase( cls._schema = make_numerical_state_changed_with_unit_schema(cls._unit_converter) -NUMERICAL_ATTRIBUTE_CROSSED_THRESHOLD_SCHEMA = ENTITY_STATE_TRIGGER_SCHEMA.extend( - { - vol.Required(CONF_OPTIONS): vol.All( - { - vol.Required(ATTR_BEHAVIOR, default=BEHAVIOR_ANY): vol.In( - [BEHAVIOR_FIRST, BEHAVIOR_LAST, BEHAVIOR_ANY] - ), +NUMERICAL_ATTRIBUTE_CROSSED_THRESHOLD_SCHEMA = ( + ENTITY_STATE_TRIGGER_SCHEMA_FIRST_LAST.extend( + { + vol.Required(CONF_OPTIONS): { vol.Required("threshold"): NumericThresholdSelector( NumericThresholdSelectorConfig(mode=NumericThresholdMode.CROSSED) ), }, - ) - } + } + ) ) @@ -787,21 +783,16 @@ def _make_numerical_state_crossed_threshold_with_unit_schema( This trigger only fires when the observed attribute changes from not within to within the defined threshold. """ - return ENTITY_STATE_TRIGGER_SCHEMA.extend( + return ENTITY_STATE_TRIGGER_SCHEMA_FIRST_LAST.extend( { - vol.Required(CONF_OPTIONS, default={}): vol.All( - { - vol.Required(ATTR_BEHAVIOR, default=BEHAVIOR_ANY): vol.In( - [BEHAVIOR_FIRST, BEHAVIOR_LAST, BEHAVIOR_ANY] - ), - vol.Required("threshold"): NumericThresholdSelector( - NumericThresholdSelectorConfig( - mode=NumericThresholdMode.CROSSED, - unit_of_measurement=list(unit_converter.VALID_UNITS), - ) - ), - }, - ) + vol.Required(CONF_OPTIONS, default={}): { + vol.Required("threshold"): NumericThresholdSelector( + NumericThresholdSelectorConfig( + mode=NumericThresholdMode.CROSSED, + unit_of_measurement=list(unit_converter.VALID_UNITS), + ) + ), + }, } )