Repository: home-assistant/core
File: tests/helpers/test_trigger.py (Python)

Code before:
    # Reset the above sensor state to a valid numeric value
    hass.states.async_set("sensor.above", "10")

    # Test the trigger does not fire when the below sensor does not exist
    hass.states.async_remove("sensor.below")
    hass.states.async_set("test.test_entity", "on", {"test_attribute": None})
    hass.states.async_set("test.test_entity", "on", {"test_attribute": 50})
    await hass.async_block_till_done()
    assert len(service_calls) == 0

    # Test the trigger does not fire when the below sensor state is not numeric
    for invalid_value in ("cat", None):
        hass.states.async_set("sensor.below", invalid_value)
        hass.states.async_set("test.test_entity", "on", {"test_attribute": None})
        hass.states.async_set("test.test_entity", "on", {"test_attribute": 50})
        await hass.async_block_till_done()
        assert len(service_calls) == 0


@pytest.mark.parametrize(
    ("trigger_options", "expected_result"),
    [
        # Valid configurations
        (
            {CONF_THRESHOLD_TYPE: "above", CONF_LOWER_LIMIT: 10},
            does_not_raise(),
        ),
        (
            {CONF_THRESHOLD_TYPE: "above", CONF_LOWER_LIMIT: "sensor.test"},
            does_not_raise(),
        ),
        (
            {CONF_THRESHOLD_TYPE: "below", CONF_UPPER_LIMIT: 90},
            does_not_raise(),
        ),
        (
            {CONF_THRESHOLD_TYPE: "below", CONF_UPPER_LIMIT: "sensor.test"},
            does_not_raise(),
        ),
        (
            {
                CONF_THRESHOLD_TYPE: "between",
                CONF_LOWER_LIMIT: 10,
                CONF_UPPER_LIMIT: 90,
            },
            does_not_raise(),
        ),
        (
            {
                CONF_THRESHOLD_TYPE: "between",
                CONF_LOWER_LIMIT: 10,

Review comment (suggestion):
Add a comment saying we intentionally do not use the enum because we want to make sure the validator accepts strings when source is JSON or YAML.

Code after:
    # Reset the above sensor state to a valid numeric value
    hass.states.async_set("sensor.above", "10")

    # Test the trigger does not fire when the below sensor does not exist
    hass.states.async_remove("sensor.below")
    hass.states.async_set("test.test_entity", "on", {"test_attribute": None})
    hass.states.async_set("test.test_entity", "on", {"test_attribute": 50})
    await hass.async_block_till_done()
    assert len(service_calls) == 0

    # Test the trigger does not fire when the below sensor state is not numeric
    for invalid_value in ("cat", None):
        hass.states.async_set("sensor.below", invalid_value)
        hass.states.async_set("test.test_entity", "on", {"test_attribute": None})
        hass.states.async_set("test.test_entity", "on", {"test_attribute": 50})
        await hass.async_block_till_done()
        assert len(service_calls) == 0


@pytest.mark.parametrize(
    ("trigger_options", "expected_result"),
    [
        # Valid configurations
        # Don't use the enum in tests to allow testing validation of strings when the source is JSON or YAML
        (
            {CONF_THRESHOLD_TYPE: "above", CONF_LOWER_LIMIT: 10},
            does_not_raise(),
        ),
        (
            {CONF_THRESHOLD_TYPE: "above", CONF_LOWER_LIMIT: "sensor.test"},
            does_not_raise(),
        ),
        (
            {CONF_THRESHOLD_TYPE: "below", CONF_UPPER_LIMIT: 90},
            does_not_raise(),
        ),
        (
            {CONF_THRESHOLD_TYPE: "below", CONF_UPPER_LIMIT: "sensor.test"},
            does_not_raise(),
        ),
        (
            {
                CONF_THRESHOLD_TYPE: "between",
                CONF_LOWER_LIMIT: 10,
                CONF_UPPER_LIMIT: 90,
            },
            does_not_raise(),
        ),
        (
            {
                CONF_THRESHOLD_TYPE: "between",