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

Add support for percentage speeds and preset modes to template fan (#45478)

This commit is contained in:
J. Nick Koston
2021-01-28 03:44:36 -06:00
committed by GitHub
parent 22e44e4ba4
commit babfef829d
2 changed files with 493 additions and 48 deletions

View File

@@ -6,12 +6,15 @@ from homeassistant import setup
from homeassistant.components.fan import (
ATTR_DIRECTION,
ATTR_OSCILLATING,
ATTR_PERCENTAGE,
ATTR_PRESET_MODE,
ATTR_SPEED,
DIRECTION_FORWARD,
DIRECTION_REVERSE,
SPEED_HIGH,
SPEED_LOW,
SPEED_MEDIUM,
SPEED_OFF,
)
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE
@@ -25,6 +28,10 @@ _STATE_INPUT_BOOLEAN = "input_boolean.state"
_STATE_AVAILABILITY_BOOLEAN = "availability_boolean.state"
# Represent for fan's speed
_SPEED_INPUT_SELECT = "input_select.speed"
# Represent for fan's preset mode
_PRESET_MODE_INPUT_SELECT = "input_select.preset_mode"
# Represent for fan's speed percentage
_PERCENTAGE_INPUT_NUMBER = "input_number.percentage"
# Represent for fan's oscillating
_OSC_INPUT = "input_select.osc"
# Represent for fan's direction
@@ -62,7 +69,7 @@ async def test_missing_optional_config(hass, calls):
await hass.async_start()
await hass.async_block_till_done()
_verify(hass, STATE_ON, None, None, None)
_verify(hass, STATE_ON, None, None, None, None, None)
async def test_missing_value_template_config(hass, calls):
@@ -191,9 +198,15 @@ async def test_templates_with_entities(hass, calls):
"fans": {
"test_fan": {
"value_template": value_template,
"percentage_template": "{{ states('input_number.percentage') }}",
"speed_template": "{{ states('input_select.speed') }}",
"preset_mode_template": "{{ states('input_select.preset_mode') }}",
"oscillating_template": "{{ states('input_select.osc') }}",
"direction_template": "{{ states('input_select.direction') }}",
"set_percentage": {
"service": "script.fans_set_speed",
"data_template": {"percentage": "{{ percentage }}"},
},
"turn_on": {"service": "script.fan_on"},
"turn_off": {"service": "script.fan_off"},
}
@@ -206,7 +219,7 @@ async def test_templates_with_entities(hass, calls):
await hass.async_start()
await hass.async_block_till_done()
_verify(hass, STATE_OFF, None, None, None)
_verify(hass, STATE_OFF, None, 0, None, None, None)
hass.states.async_set(_STATE_INPUT_BOOLEAN, True)
hass.states.async_set(_SPEED_INPUT_SELECT, SPEED_MEDIUM)
@@ -214,7 +227,128 @@ async def test_templates_with_entities(hass, calls):
hass.states.async_set(_DIRECTION_INPUT_SELECT, DIRECTION_FORWARD)
await hass.async_block_till_done()
_verify(hass, STATE_ON, SPEED_MEDIUM, True, DIRECTION_FORWARD)
_verify(hass, STATE_ON, SPEED_MEDIUM, 66, True, DIRECTION_FORWARD, None)
hass.states.async_set(_PERCENTAGE_INPUT_NUMBER, 33)
await hass.async_block_till_done()
_verify(hass, STATE_ON, SPEED_LOW, 33, True, DIRECTION_FORWARD, None)
hass.states.async_set(_PERCENTAGE_INPUT_NUMBER, 66)
await hass.async_block_till_done()
_verify(hass, STATE_ON, SPEED_MEDIUM, 66, True, DIRECTION_FORWARD, None)
hass.states.async_set(_PERCENTAGE_INPUT_NUMBER, 100)
await hass.async_block_till_done()
_verify(hass, STATE_ON, SPEED_HIGH, 100, True, DIRECTION_FORWARD, None)
hass.states.async_set(_PERCENTAGE_INPUT_NUMBER, "dog")
await hass.async_block_till_done()
_verify(hass, STATE_ON, None, 0, True, DIRECTION_FORWARD, None)
async def test_templates_with_entities_and_invalid_percentage(hass, calls):
"""Test templates with values from other entities."""
hass.states.async_set("sensor.percentage", "0")
with assert_setup_component(1, "fan"):
assert await setup.async_setup_component(
hass,
"fan",
{
"fan": {
"platform": "template",
"fans": {
"test_fan": {
"value_template": "{{ 'on' }}",
"percentage_template": "{{ states('sensor.percentage') }}",
"turn_on": {"service": "script.fan_on"},
"turn_off": {"service": "script.fan_off"},
},
},
}
},
)
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
_verify(hass, STATE_OFF, SPEED_OFF, 0, None, None, None)
hass.states.async_set("sensor.percentage", "33")
await hass.async_block_till_done()
_verify(hass, STATE_ON, SPEED_LOW, 33, None, None, None)
hass.states.async_set("sensor.percentage", "invalid")
await hass.async_block_till_done()
_verify(hass, STATE_ON, None, 0, None, None, None)
hass.states.async_set("sensor.percentage", "5000")
await hass.async_block_till_done()
_verify(hass, STATE_ON, None, 0, None, None, None)
hass.states.async_set("sensor.percentage", "100")
await hass.async_block_till_done()
_verify(hass, STATE_ON, SPEED_HIGH, 100, None, None, None)
hass.states.async_set("sensor.percentage", "0")
await hass.async_block_till_done()
_verify(hass, STATE_OFF, SPEED_OFF, 0, None, None, None)
async def test_templates_with_entities_and_preset_modes(hass, calls):
"""Test templates with values from other entities."""
hass.states.async_set("sensor.preset_mode", "0")
with assert_setup_component(1, "fan"):
assert await setup.async_setup_component(
hass,
"fan",
{
"fan": {
"platform": "template",
"fans": {
"test_fan": {
"value_template": "{{ 'on' }}",
"preset_modes": ["auto", "smart"],
"preset_mode_template": "{{ states('sensor.preset_mode') }}",
"turn_on": {"service": "script.fan_on"},
"turn_off": {"service": "script.fan_off"},
},
},
}
},
)
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
_verify(hass, STATE_ON, None, None, None, None, None)
hass.states.async_set("sensor.preset_mode", "invalid")
await hass.async_block_till_done()
_verify(hass, STATE_ON, None, None, None, None, None)
hass.states.async_set("sensor.preset_mode", "auto")
await hass.async_block_till_done()
_verify(hass, STATE_ON, "auto", None, None, None, "auto")
hass.states.async_set("sensor.preset_mode", "smart")
await hass.async_block_till_done()
_verify(hass, STATE_ON, "smart", None, None, None, "smart")
hass.states.async_set("sensor.preset_mode", "invalid")
await hass.async_block_till_done()
_verify(hass, STATE_ON, None, None, None, None, None)
async def test_template_with_unavailable_entities(hass, calls):
@@ -272,7 +406,7 @@ async def test_template_with_unavailable_parameters(hass, calls):
await hass.async_start()
await hass.async_block_till_done()
_verify(hass, STATE_ON, None, None, None)
_verify(hass, STATE_ON, None, 0, None, None, None)
async def test_availability_template_with_entities(hass, calls):
@@ -346,7 +480,7 @@ async def test_templates_with_valid_values(hass, calls):
await hass.async_start()
await hass.async_block_till_done()
_verify(hass, STATE_ON, SPEED_MEDIUM, True, DIRECTION_FORWARD)
_verify(hass, STATE_ON, SPEED_MEDIUM, 66, True, DIRECTION_FORWARD, None)
async def test_templates_invalid_values(hass, calls):
@@ -376,7 +510,7 @@ async def test_templates_invalid_values(hass, calls):
await hass.async_start()
await hass.async_block_till_done()
_verify(hass, STATE_OFF, None, None, None)
_verify(hass, STATE_OFF, None, 0, None, None, None)
async def test_invalid_availability_template_keeps_component_available(hass, caplog):
@@ -394,6 +528,7 @@ async def test_invalid_availability_template_keeps_component_available(hass, cap
"value_template": "{{ 'on' }}",
"availability_template": "{{ x - 12 }}",
"speed_template": "{{ states('input_select.speed') }}",
"preset_mode_template": "{{ states('input_select.preset_mode') }}",
"oscillating_template": "{{ states('input_select.osc') }}",
"direction_template": "{{ states('input_select.direction') }}",
"turn_on": {"service": "script.fan_on"},
@@ -427,14 +562,14 @@ async def test_on_off(hass, calls):
# verify
assert hass.states.get(_STATE_INPUT_BOOLEAN).state == STATE_ON
_verify(hass, STATE_ON, None, None, None)
_verify(hass, STATE_ON, None, 0, None, None, None)
# Turn off fan
await common.async_turn_off(hass, _TEST_FAN)
# verify
assert hass.states.get(_STATE_INPUT_BOOLEAN).state == STATE_OFF
_verify(hass, STATE_OFF, None, None, None)
_verify(hass, STATE_OFF, None, 0, None, None, None)
async def test_on_with_speed(hass, calls):
@@ -446,13 +581,13 @@ async def test_on_with_speed(hass, calls):
# verify
assert hass.states.get(_STATE_INPUT_BOOLEAN).state == STATE_ON
assert hass.states.get(_SPEED_INPUT_SELECT).state == SPEED_HIGH
_verify(hass, STATE_ON, SPEED_HIGH, None, None)
assert int(float(hass.states.get(_PERCENTAGE_INPUT_NUMBER).state)) == 100
_verify(hass, STATE_ON, SPEED_HIGH, 100, None, None, None)
async def test_set_speed(hass, calls):
"""Test set valid speed."""
await _register_components(hass)
await _register_components(hass, preset_modes=["auto", "smart"])
# Turn on fan
await common.async_turn_on(hass, _TEST_FAN)
@@ -462,14 +597,55 @@ async def test_set_speed(hass, calls):
# verify
assert hass.states.get(_SPEED_INPUT_SELECT).state == SPEED_HIGH
_verify(hass, STATE_ON, SPEED_HIGH, None, None)
_verify(hass, STATE_ON, SPEED_HIGH, 100, None, None, None)
# Set fan's speed to medium
await common.async_set_speed(hass, _TEST_FAN, SPEED_MEDIUM)
# verify
assert hass.states.get(_SPEED_INPUT_SELECT).state == SPEED_MEDIUM
_verify(hass, STATE_ON, SPEED_MEDIUM, None, None)
_verify(hass, STATE_ON, SPEED_MEDIUM, 66, None, None, None)
# Set fan's speed to off
await common.async_set_speed(hass, _TEST_FAN, SPEED_OFF)
# verify
assert hass.states.get(_SPEED_INPUT_SELECT).state == SPEED_OFF
_verify(hass, STATE_OFF, SPEED_OFF, 0, None, None, None)
async def test_set_percentage(hass, calls):
"""Test set valid speed percentage."""
await _register_components(hass)
# Turn on fan
await common.async_turn_on(hass, _TEST_FAN)
# Set fan's percentage speed to 100
await common.async_set_percentage(hass, _TEST_FAN, 100)
# verify
assert int(float(hass.states.get(_PERCENTAGE_INPUT_NUMBER).state)) == 100
_verify(hass, STATE_ON, SPEED_HIGH, 100, None, None, None)
# Set fan's percentage speed to 66
await common.async_set_percentage(hass, _TEST_FAN, 66)
assert int(float(hass.states.get(_PERCENTAGE_INPUT_NUMBER).state)) == 66
_verify(hass, STATE_ON, SPEED_MEDIUM, 66, None, None, None)
# Set fan's percentage speed to 0
await common.async_set_percentage(hass, _TEST_FAN, 0)
assert int(float(hass.states.get(_PERCENTAGE_INPUT_NUMBER).state)) == 0
_verify(hass, STATE_OFF, SPEED_OFF, 0, None, None, None)
# Set fan's percentage speed to 50
await common.async_turn_on(hass, _TEST_FAN, percentage=50)
assert int(float(hass.states.get(_PERCENTAGE_INPUT_NUMBER).state)) == 50
_verify(hass, STATE_ON, SPEED_MEDIUM, 50, None, None, None)
async def test_set_invalid_speed_from_initial_stage(hass, calls):
@@ -484,7 +660,7 @@ async def test_set_invalid_speed_from_initial_stage(hass, calls):
# verify speed is unchanged
assert hass.states.get(_SPEED_INPUT_SELECT).state == ""
_verify(hass, STATE_ON, None, None, None)
_verify(hass, STATE_ON, None, 0, None, None, None)
async def test_set_invalid_speed(hass, calls):
@@ -499,14 +675,14 @@ async def test_set_invalid_speed(hass, calls):
# verify
assert hass.states.get(_SPEED_INPUT_SELECT).state == SPEED_HIGH
_verify(hass, STATE_ON, SPEED_HIGH, None, None)
_verify(hass, STATE_ON, SPEED_HIGH, 100, None, None, None)
# Set fan's speed to 'invalid'
await common.async_set_speed(hass, _TEST_FAN, "invalid")
# verify speed is unchanged
assert hass.states.get(_SPEED_INPUT_SELECT).state == SPEED_HIGH
_verify(hass, STATE_ON, SPEED_HIGH, None, None)
_verify(hass, STATE_ON, SPEED_HIGH, 100, None, None, None)
async def test_custom_speed_list(hass, calls):
@@ -521,14 +697,48 @@ async def test_custom_speed_list(hass, calls):
# verify
assert hass.states.get(_SPEED_INPUT_SELECT).state == "1"
_verify(hass, STATE_ON, "1", None, None)
_verify(hass, STATE_ON, "1", 33, None, None, None)
# Set fan's speed to 'medium' which is invalid
await common.async_set_speed(hass, _TEST_FAN, SPEED_MEDIUM)
# verify that speed is unchanged
assert hass.states.get(_SPEED_INPUT_SELECT).state == "1"
_verify(hass, STATE_ON, "1", None, None)
_verify(hass, STATE_ON, "1", 33, None, None, None)
async def test_preset_modes(hass, calls):
"""Test preset_modes."""
await _register_components(
hass, ["off", "low", "medium", "high", "auto", "smart"], ["auto", "smart"]
)
# Turn on fan
await common.async_turn_on(hass, _TEST_FAN)
# Set fan's preset_mode to "auto"
await common.async_set_preset_mode(hass, _TEST_FAN, "auto")
# verify
assert hass.states.get(_PRESET_MODE_INPUT_SELECT).state == "auto"
# Set fan's preset_mode to "smart"
await common.async_set_preset_mode(hass, _TEST_FAN, "smart")
# Verify fan's preset_mode is "smart"
assert hass.states.get(_PRESET_MODE_INPUT_SELECT).state == "smart"
# Set fan's preset_mode to "invalid"
await common.async_set_preset_mode(hass, _TEST_FAN, "invalid")
# Verify fan's preset_mode is still "smart"
assert hass.states.get(_PRESET_MODE_INPUT_SELECT).state == "smart"
# Set fan's preset_mode to "auto"
await common.async_turn_on(hass, _TEST_FAN, preset_mode="auto")
# verify
assert hass.states.get(_PRESET_MODE_INPUT_SELECT).state == "auto"
async def test_set_osc(hass, calls):
@@ -543,14 +753,14 @@ async def test_set_osc(hass, calls):
# verify
assert hass.states.get(_OSC_INPUT).state == "True"
_verify(hass, STATE_ON, None, True, None)
_verify(hass, STATE_ON, None, 0, True, None, None)
# Set fan's osc to False
await common.async_oscillate(hass, _TEST_FAN, False)
# verify
assert hass.states.get(_OSC_INPUT).state == "False"
_verify(hass, STATE_ON, None, False, None)
_verify(hass, STATE_ON, None, 0, False, None, None)
async def test_set_invalid_osc_from_initial_state(hass, calls):
@@ -566,7 +776,7 @@ async def test_set_invalid_osc_from_initial_state(hass, calls):
# verify
assert hass.states.get(_OSC_INPUT).state == ""
_verify(hass, STATE_ON, None, None, None)
_verify(hass, STATE_ON, None, 0, None, None, None)
async def test_set_invalid_osc(hass, calls):
@@ -581,7 +791,7 @@ async def test_set_invalid_osc(hass, calls):
# verify
assert hass.states.get(_OSC_INPUT).state == "True"
_verify(hass, STATE_ON, None, True, None)
_verify(hass, STATE_ON, None, 0, True, None, None)
# Set fan's osc to None
with pytest.raises(vol.Invalid):
@@ -589,7 +799,7 @@ async def test_set_invalid_osc(hass, calls):
# verify osc is unchanged
assert hass.states.get(_OSC_INPUT).state == "True"
_verify(hass, STATE_ON, None, True, None)
_verify(hass, STATE_ON, None, 0, True, None, None)
async def test_set_direction(hass, calls):
@@ -604,14 +814,14 @@ async def test_set_direction(hass, calls):
# verify
assert hass.states.get(_DIRECTION_INPUT_SELECT).state == DIRECTION_FORWARD
_verify(hass, STATE_ON, None, None, DIRECTION_FORWARD)
_verify(hass, STATE_ON, None, 0, None, DIRECTION_FORWARD, None)
# Set fan's direction to reverse
await common.async_set_direction(hass, _TEST_FAN, DIRECTION_REVERSE)
# verify
assert hass.states.get(_DIRECTION_INPUT_SELECT).state == DIRECTION_REVERSE
_verify(hass, STATE_ON, None, None, DIRECTION_REVERSE)
_verify(hass, STATE_ON, None, 0, None, DIRECTION_REVERSE, None)
async def test_set_invalid_direction_from_initial_stage(hass, calls):
@@ -626,7 +836,7 @@ async def test_set_invalid_direction_from_initial_stage(hass, calls):
# verify direction is unchanged
assert hass.states.get(_DIRECTION_INPUT_SELECT).state == ""
_verify(hass, STATE_ON, None, None, None)
_verify(hass, STATE_ON, None, 0, None, None, None)
async def test_set_invalid_direction(hass, calls):
@@ -641,36 +851,61 @@ async def test_set_invalid_direction(hass, calls):
# verify
assert hass.states.get(_DIRECTION_INPUT_SELECT).state == DIRECTION_FORWARD
_verify(hass, STATE_ON, None, None, DIRECTION_FORWARD)
_verify(hass, STATE_ON, None, 0, None, DIRECTION_FORWARD, None)
# Set fan's direction to 'invalid'
await common.async_set_direction(hass, _TEST_FAN, "invalid")
# verify direction is unchanged
assert hass.states.get(_DIRECTION_INPUT_SELECT).state == DIRECTION_FORWARD
_verify(hass, STATE_ON, None, None, DIRECTION_FORWARD)
_verify(hass, STATE_ON, None, 0, None, DIRECTION_FORWARD, None)
def _verify(
hass, expected_state, expected_speed, expected_oscillating, expected_direction
hass,
expected_state,
expected_speed,
expected_percentage,
expected_oscillating,
expected_direction,
expected_preset_mode,
):
"""Verify fan's state, speed and osc."""
state = hass.states.get(_TEST_FAN)
attributes = state.attributes
assert state.state == str(expected_state)
assert attributes.get(ATTR_SPEED) == expected_speed
assert attributes.get(ATTR_PERCENTAGE) == expected_percentage
assert attributes.get(ATTR_OSCILLATING) == expected_oscillating
assert attributes.get(ATTR_DIRECTION) == expected_direction
assert attributes.get(ATTR_PRESET_MODE) == expected_preset_mode
async def _register_components(hass, speed_list=None):
async def _register_components(hass, speed_list=None, preset_modes=None):
"""Register basic components for testing."""
with assert_setup_component(1, "input_boolean"):
assert await setup.async_setup_component(
hass, "input_boolean", {"input_boolean": {"state": None}}
)
with assert_setup_component(3, "input_select"):
with assert_setup_component(1, "input_number"):
assert await setup.async_setup_component(
hass,
"input_number",
{
"input_number": {
"percentage": {
"min": 0.0,
"max": 100.0,
"name": "Percentage",
"step": 1.0,
"mode": "slider",
}
}
},
)
with assert_setup_component(4, "input_select"):
assert await setup.async_setup_component(
hass,
"input_select",
@@ -680,14 +915,21 @@ async def _register_components(hass, speed_list=None):
"name": "Speed",
"options": [
"",
SPEED_OFF,
SPEED_LOW,
SPEED_MEDIUM,
SPEED_HIGH,
"1",
"2",
"3",
"auto",
"smart",
],
},
"preset_mode": {
"name": "Preset Mode",
"options": ["auto", "smart"],
},
"osc": {"name": "oscillating", "options": ["", "True", "False"]},
"direction": {
"name": "Direction",
@@ -709,6 +951,8 @@ async def _register_components(hass, speed_list=None):
test_fan_config = {
"value_template": value_template,
"speed_template": "{{ states('input_select.speed') }}",
"preset_mode_template": "{{ states('input_select.preset_mode') }}",
"percentage_template": "{{ states('input_number.percentage') }}",
"oscillating_template": "{{ states('input_select.osc') }}",
"direction_template": "{{ states('input_select.direction') }}",
"turn_on": {
@@ -726,6 +970,20 @@ async def _register_components(hass, speed_list=None):
"option": "{{ speed }}",
},
},
"set_preset_mode": {
"service": "input_select.select_option",
"data_template": {
"entity_id": _PRESET_MODE_INPUT_SELECT,
"option": "{{ preset_mode }}",
},
},
"set_percentage": {
"service": "input_number.set_value",
"data_template": {
"entity_id": _PERCENTAGE_INPUT_NUMBER,
"value": "{{ percentage }}",
},
},
"set_oscillating": {
"service": "input_select.select_option",
"data_template": {
@@ -745,6 +1003,9 @@ async def _register_components(hass, speed_list=None):
if speed_list:
test_fan_config["speeds"] = speed_list
if preset_modes:
test_fan_config["preset_modes"] = preset_modes
assert await setup.async_setup_component(
hass,
"fan",