mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +00:00
Numeric state condition can also accept input_number entity ID (#39680)
This commit is contained in:
parent
d7e471e244
commit
878347243d
@ -171,8 +171,8 @@ async def async_not_from_config(
|
|||||||
def numeric_state(
|
def numeric_state(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entity: Union[None, str, State],
|
entity: Union[None, str, State],
|
||||||
below: Optional[float] = None,
|
below: Optional[Union[float, str]] = None,
|
||||||
above: Optional[float] = None,
|
above: Optional[Union[float, str]] = None,
|
||||||
value_template: Optional[Template] = None,
|
value_template: Optional[Template] = None,
|
||||||
variables: TemplateVarsType = None,
|
variables: TemplateVarsType = None,
|
||||||
) -> bool:
|
) -> bool:
|
||||||
@ -192,8 +192,8 @@ def numeric_state(
|
|||||||
def async_numeric_state(
|
def async_numeric_state(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entity: Union[None, str, State],
|
entity: Union[None, str, State],
|
||||||
below: Optional[float] = None,
|
below: Optional[Union[float, str]] = None,
|
||||||
above: Optional[float] = None,
|
above: Optional[Union[float, str]] = None,
|
||||||
value_template: Optional[Template] = None,
|
value_template: Optional[Template] = None,
|
||||||
variables: TemplateVarsType = None,
|
variables: TemplateVarsType = None,
|
||||||
attribute: Optional[str] = None,
|
attribute: Optional[str] = None,
|
||||||
@ -233,11 +233,29 @@ def async_numeric_state(
|
|||||||
)
|
)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if below is not None and fvalue >= below:
|
if below is not None:
|
||||||
return False
|
if isinstance(below, str):
|
||||||
|
below_entity = hass.states.get(below)
|
||||||
|
if (
|
||||||
|
not below_entity
|
||||||
|
or below_entity.state in (STATE_UNAVAILABLE, STATE_UNKNOWN)
|
||||||
|
or fvalue >= float(below_entity.state)
|
||||||
|
):
|
||||||
|
return False
|
||||||
|
elif fvalue >= below:
|
||||||
|
return False
|
||||||
|
|
||||||
if above is not None and fvalue <= above:
|
if above is not None:
|
||||||
return False
|
if isinstance(above, str):
|
||||||
|
above_entity = hass.states.get(above)
|
||||||
|
if (
|
||||||
|
not above_entity
|
||||||
|
or above_entity.state in (STATE_UNAVAILABLE, STATE_UNKNOWN)
|
||||||
|
or fvalue <= float(above_entity.state)
|
||||||
|
):
|
||||||
|
return False
|
||||||
|
elif fvalue <= above:
|
||||||
|
return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -906,8 +906,12 @@ NUMERIC_STATE_CONDITION_SCHEMA = vol.All(
|
|||||||
vol.Required(CONF_CONDITION): "numeric_state",
|
vol.Required(CONF_CONDITION): "numeric_state",
|
||||||
vol.Required(CONF_ENTITY_ID): entity_ids,
|
vol.Required(CONF_ENTITY_ID): entity_ids,
|
||||||
vol.Optional(CONF_ATTRIBUTE): str,
|
vol.Optional(CONF_ATTRIBUTE): str,
|
||||||
CONF_BELOW: vol.Coerce(float),
|
CONF_BELOW: vol.Any(
|
||||||
CONF_ABOVE: vol.Coerce(float),
|
vol.Coerce(float), vol.All(str, entity_domain("input_number"))
|
||||||
|
),
|
||||||
|
CONF_ABOVE: vol.Any(
|
||||||
|
vol.Coerce(float), vol.All(str, entity_domain("input_number"))
|
||||||
|
),
|
||||||
vol.Optional(CONF_VALUE_TEMPLATE): template,
|
vol.Optional(CONF_VALUE_TEMPLATE): template,
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
|
@ -505,6 +505,62 @@ async def test_numberic_state_attribute(hass):
|
|||||||
assert not test(hass)
|
assert not test(hass)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_numeric_state_using_input_number(hass):
|
||||||
|
"""Test numeric_state conditions using input_number entities."""
|
||||||
|
await async_setup_component(
|
||||||
|
hass,
|
||||||
|
"input_number",
|
||||||
|
{
|
||||||
|
"input_number": {
|
||||||
|
"low": {"min": 0, "max": 255, "initial": 10},
|
||||||
|
"high": {"min": 0, "max": 255, "initial": 100},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
test = await condition.async_from_config(
|
||||||
|
hass,
|
||||||
|
{
|
||||||
|
"condition": "and",
|
||||||
|
"conditions": [
|
||||||
|
{
|
||||||
|
"condition": "numeric_state",
|
||||||
|
"entity_id": "sensor.temperature",
|
||||||
|
"below": "input_number.high",
|
||||||
|
"above": "input_number.low",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
hass.states.async_set("sensor.temperature", 42)
|
||||||
|
assert test(hass)
|
||||||
|
|
||||||
|
hass.states.async_set("sensor.temperature", 10)
|
||||||
|
assert not test(hass)
|
||||||
|
|
||||||
|
hass.states.async_set("sensor.temperature", 100)
|
||||||
|
assert not test(hass)
|
||||||
|
|
||||||
|
await hass.services.async_call(
|
||||||
|
"input_number",
|
||||||
|
"set_value",
|
||||||
|
{
|
||||||
|
"entity_id": "input_number.high",
|
||||||
|
"value": 101,
|
||||||
|
},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
assert test(hass)
|
||||||
|
|
||||||
|
assert not condition.async_numeric_state(
|
||||||
|
hass, entity="sensor.temperature", below="input_number.not_exist"
|
||||||
|
)
|
||||||
|
assert not condition.async_numeric_state(
|
||||||
|
hass, entity="sensor.temperature", above="input_number.not_exist"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_zone_multiple_entities(hass):
|
async def test_zone_multiple_entities(hass):
|
||||||
"""Test with multiple entities in condition."""
|
"""Test with multiple entities in condition."""
|
||||||
test = await condition.async_from_config(
|
test = await condition.async_from_config(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user