Prepare MQTT platform tests part3 (#90106)

* Tests cover

* Tests fan
This commit is contained in:
Jan Bouwhuis 2023-03-23 19:14:08 +01:00 committed by GitHub
parent 8fd88d6703
commit 44add1dc11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 1133 additions and 975 deletions

File diff suppressed because it is too large Load diff

View file

@ -32,7 +32,6 @@ from homeassistant.const import (
Platform, Platform,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from .test_common import ( from .test_common import (
help_test_availability_when_connection_lost, help_test_availability_when_connection_lost,
@ -85,30 +84,24 @@ def fan_platform_only():
yield yield
@pytest.mark.parametrize("hass_config", [{mqtt.DOMAIN: {fan.DOMAIN: {"name": "test"}}}])
async def test_fail_setup_if_no_command_topic( async def test_fail_setup_if_no_command_topic(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None: ) -> None:
"""Test if command fails with command topic.""" """Test if command fails with command topic."""
assert not await async_setup_component( with pytest.raises(AssertionError):
hass, await mqtt_mock_entry_no_yaml_config()
mqtt.DOMAIN,
{mqtt.DOMAIN: {fan.DOMAIN: {"name": "test"}}},
)
assert ( assert (
"Invalid config for [mqtt]: required key not provided @ data['mqtt']['fan'][0]['command_topic']" "Invalid config for [mqtt]: required key not provided @ data['mqtt']['fan'][0]['command_topic']"
in caplog.text in caplog.text
) )
async def test_controlling_state_via_topic( @pytest.mark.parametrize(
hass: HomeAssistant, "hass_config",
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, [
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the controlling state via topic."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
fan.DOMAIN: { fan.DOMAIN: {
@ -139,10 +132,16 @@ async def test_controlling_state_via_topic(
"payload_reset_preset_mode": "rEset_preset_mode", "payload_reset_preset_mode": "rEset_preset_mode",
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_controlling_state_via_topic(
await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the controlling state via topic."""
await mqtt_mock_entry_no_yaml_config()
state = hass.states.get("fan.test") state = hass.states.get("fan.test")
assert state.state == STATE_UNKNOWN assert state.state == STATE_UNKNOWN
@ -224,15 +223,9 @@ async def test_controlling_state_via_topic(
assert state.state == STATE_UNKNOWN assert state.state == STATE_UNKNOWN
async def test_controlling_state_via_topic_with_different_speed_range( @pytest.mark.parametrize(
hass: HomeAssistant, "hass_config",
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, [
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the controlling state via topic using an alternate speed range."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
fan.DOMAIN: [ fan.DOMAIN: [
@ -262,10 +255,16 @@ async def test_controlling_state_via_topic_with_different_speed_range(
}, },
] ]
} }
}, }
],
) )
await hass.async_block_till_done() async def test_controlling_state_via_topic_with_different_speed_range(
await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the controlling state via topic using an alternate speed range."""
await mqtt_mock_entry_no_yaml_config()
async_fire_mqtt_message(hass, "percentage-state-topic1", "100") async_fire_mqtt_message(hass, "percentage-state-topic1", "100")
state = hass.states.get("fan.test1") state = hass.states.get("fan.test1")
@ -288,15 +287,9 @@ async def test_controlling_state_via_topic_with_different_speed_range(
caplog.clear() caplog.clear()
async def test_controlling_state_via_topic_no_percentage_topics( @pytest.mark.parametrize(
hass: HomeAssistant, "hass_config",
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, [
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the controlling state via topic without percentage topics."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
fan.DOMAIN: { fan.DOMAIN: {
@ -314,10 +307,16 @@ async def test_controlling_state_via_topic_no_percentage_topics(
], ],
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_controlling_state_via_topic_no_percentage_topics(
await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the controlling state via topic without percentage topics."""
await mqtt_mock_entry_no_yaml_config()
state = hass.states.get("fan.test") state = hass.states.get("fan.test")
assert state.state == STATE_UNKNOWN assert state.state == STATE_UNKNOWN
@ -347,15 +346,9 @@ async def test_controlling_state_via_topic_no_percentage_topics(
caplog.clear() caplog.clear()
async def test_controlling_state_via_topic_and_json_message( @pytest.mark.parametrize(
hass: HomeAssistant, "hass_config",
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, [
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the controlling state via topic and JSON message (percentage mode)."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
fan.DOMAIN: { fan.DOMAIN: {
@ -384,10 +377,16 @@ async def test_controlling_state_via_topic_and_json_message(
"speed_range_max": 100, "speed_range_max": 100,
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_controlling_state_via_topic_and_json_message(
await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the controlling state via topic and JSON message (percentage mode)."""
await mqtt_mock_entry_no_yaml_config()
state = hass.states.get("fan.test") state = hass.states.get("fan.test")
assert state.state == STATE_UNKNOWN assert state.state == STATE_UNKNOWN
@ -454,15 +453,9 @@ async def test_controlling_state_via_topic_and_json_message(
assert state.attributes.get("preset_mode") is None assert state.attributes.get("preset_mode") is None
async def test_controlling_state_via_topic_and_json_message_shared_topic( @pytest.mark.parametrize(
hass: HomeAssistant, "hass_config",
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, [
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the controlling state via topic and JSON message using a shared topic."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
fan.DOMAIN: { fan.DOMAIN: {
@ -491,10 +484,16 @@ async def test_controlling_state_via_topic_and_json_message_shared_topic(
"speed_range_max": 100, "speed_range_max": 100,
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_controlling_state_via_topic_and_json_message_shared_topic(
await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the controlling state via topic and JSON message using a shared topic."""
await mqtt_mock_entry_no_yaml_config()
state = hass.states.get("fan.test") state = hass.states.get("fan.test")
assert state.state == STATE_UNKNOWN assert state.state == STATE_UNKNOWN
@ -544,15 +543,9 @@ async def test_controlling_state_via_topic_and_json_message_shared_topic(
caplog.clear() caplog.clear()
async def test_sending_mqtt_commands_and_optimistic( @pytest.mark.parametrize(
hass: HomeAssistant, "hass_config",
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, [
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test optimistic mode without state topic."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
fan.DOMAIN: { fan.DOMAIN: {
@ -572,10 +565,15 @@ async def test_sending_mqtt_commands_and_optimistic(
], ],
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_sending_mqtt_commands_and_optimistic(
mqtt_mock = await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test optimistic mode without state topic."""
mqtt_mock = await mqtt_mock_entry_no_yaml_config()
state = hass.states.get("fan.test") state = hass.states.get("fan.test")
assert state.state == STATE_UNKNOWN assert state.state == STATE_UNKNOWN
@ -671,13 +669,9 @@ async def test_sending_mqtt_commands_and_optimistic(
assert state.attributes.get(ATTR_ASSUMED_STATE) assert state.attributes.get(ATTR_ASSUMED_STATE)
async def test_sending_mqtt_commands_with_alternate_speed_range( @pytest.mark.parametrize(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator "hass_config",
) -> None: [
"""Test the controlling state via topic using an alternate speed range."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
fan.DOMAIN: [ fan.DOMAIN: [
@ -707,10 +701,14 @@ async def test_sending_mqtt_commands_with_alternate_speed_range(
}, },
] ]
} }
}, }
],
) )
await hass.async_block_till_done() async def test_sending_mqtt_commands_with_alternate_speed_range(
mqtt_mock = await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test the controlling state via topic using an alternate speed range."""
mqtt_mock = await mqtt_mock_entry_no_yaml_config()
await common.async_set_percentage(hass, "fan.test1", 0) await common.async_set_percentage(hass, "fan.test1", 0)
mqtt_mock.async_publish.assert_called_once_with( mqtt_mock.async_publish.assert_called_once_with(
@ -777,15 +775,9 @@ async def test_sending_mqtt_commands_with_alternate_speed_range(
assert state.attributes.get(ATTR_ASSUMED_STATE) assert state.attributes.get(ATTR_ASSUMED_STATE)
async def test_sending_mqtt_commands_and_optimistic_no_legacy( @pytest.mark.parametrize(
hass: HomeAssistant, "hass_config",
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, [
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test optimistic mode without state topic without legacy speed command topic."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
fan.DOMAIN: { fan.DOMAIN: {
@ -800,10 +792,16 @@ async def test_sending_mqtt_commands_and_optimistic_no_legacy(
], ],
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_sending_mqtt_commands_and_optimistic_no_legacy(
mqtt_mock = await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test optimistic mode without state topic without legacy speed command topic."""
mqtt_mock = await mqtt_mock_entry_no_yaml_config()
state = hass.states.get("fan.test") state = hass.states.get("fan.test")
assert state.state == STATE_UNKNOWN assert state.state == STATE_UNKNOWN
@ -911,15 +909,9 @@ async def test_sending_mqtt_commands_and_optimistic_no_legacy(
await common.async_turn_on(hass, "fan.test", preset_mode="freaking-high") await common.async_turn_on(hass, "fan.test", preset_mode="freaking-high")
async def test_sending_mqtt_command_templates_( @pytest.mark.parametrize(
hass: HomeAssistant, "hass_config",
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, [
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test optimistic mode without state topic without legacy speed command topic."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
fan.DOMAIN: { fan.DOMAIN: {
@ -939,10 +931,15 @@ async def test_sending_mqtt_command_templates_(
], ],
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_sending_mqtt_command_templates_(
mqtt_mock = await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test optimistic mode without state topic without legacy speed command topic."""
mqtt_mock = await mqtt_mock_entry_no_yaml_config()
state = hass.states.get("fan.test") state = hass.states.get("fan.test")
assert state.state == STATE_UNKNOWN assert state.state == STATE_UNKNOWN
@ -1056,15 +1053,9 @@ async def test_sending_mqtt_command_templates_(
await common.async_turn_on(hass, "fan.test", preset_mode="low") await common.async_turn_on(hass, "fan.test", preset_mode="low")
async def test_sending_mqtt_commands_and_optimistic_no_percentage_topic( @pytest.mark.parametrize(
hass: HomeAssistant, "hass_config",
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, [
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test optimistic mode without state topic without percentage command topic."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
fan.DOMAIN: { fan.DOMAIN: {
@ -1080,10 +1071,15 @@ async def test_sending_mqtt_commands_and_optimistic_no_percentage_topic(
], ],
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_sending_mqtt_commands_and_optimistic_no_percentage_topic(
mqtt_mock = await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test optimistic mode without state topic without percentage command topic."""
mqtt_mock = await mqtt_mock_entry_no_yaml_config()
state = hass.states.get("fan.test") state = hass.states.get("fan.test")
assert state.state == STATE_UNKNOWN assert state.state == STATE_UNKNOWN
@ -1120,15 +1116,9 @@ async def test_sending_mqtt_commands_and_optimistic_no_percentage_topic(
assert state.attributes.get(ATTR_ASSUMED_STATE) assert state.attributes.get(ATTR_ASSUMED_STATE)
async def test_sending_mqtt_commands_and_explicit_optimistic( @pytest.mark.parametrize(
hass: HomeAssistant, "hass_config",
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, [
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test optimistic mode with state topic and turn on attributes."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
fan.DOMAIN: { fan.DOMAIN: {
@ -1149,10 +1139,15 @@ async def test_sending_mqtt_commands_and_explicit_optimistic(
"optimistic": True, "optimistic": True,
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_sending_mqtt_commands_and_explicit_optimistic(
mqtt_mock = await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test optimistic mode with state topic and turn on attributes."""
mqtt_mock = await mqtt_mock_entry_no_yaml_config()
state = hass.states.get("fan.test") state = hass.states.get("fan.test")
assert state.state == STATE_UNKNOWN assert state.state == STATE_UNKNOWN
@ -1395,15 +1390,9 @@ async def test_encoding_subscribable_topics(
) )
async def test_attributes( @pytest.mark.parametrize(
hass: HomeAssistant, "hass_config",
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, [
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test attributes."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
fan.DOMAIN: { fan.DOMAIN: {
@ -1418,10 +1407,16 @@ async def test_attributes(
], ],
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_attributes(
await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test attributes."""
await mqtt_mock_entry_no_yaml_config()
state = hass.states.get("fan.test") state = hass.states.get("fan.test")
assert state.state == STATE_UNKNOWN assert state.state == STATE_UNKNOWN
@ -1452,13 +1447,17 @@ async def test_attributes(
@pytest.mark.parametrize( @pytest.mark.parametrize(
("name", "config", "success", "features"), ("name", "hass_config", "success", "features"),
[ [
( (
"test1", "test1",
{ {
mqtt.DOMAIN: {
fan.DOMAIN: {
"name": "test1", "name": "test1",
"command_topic": "command-topic", "command_topic": "command-topic",
}
}
}, },
True, True,
0, 0,
@ -1466,29 +1465,41 @@ async def test_attributes(
( (
"test2", "test2",
{ {
mqtt.DOMAIN: {
fan.DOMAIN: {
"name": "test2", "name": "test2",
"command_topic": "command-topic", "command_topic": "command-topic",
"oscillation_command_topic": "oscillation-command-topic", "oscillation_command_topic": "oscillation-command-topic",
}
}
}, },
True, True,
fan.SUPPORT_OSCILLATE, fan.FanEntityFeature.OSCILLATE,
), ),
( (
"test3", "test3",
{ {
mqtt.DOMAIN: {
fan.DOMAIN: {
"name": "test3", "name": "test3",
"command_topic": "command-topic", "command_topic": "command-topic",
"percentage_command_topic": "percentage-command-topic", "percentage_command_topic": "percentage-command-topic",
}
}
}, },
True, True,
fan.SUPPORT_SET_SPEED, fan.FanEntityFeature.SET_SPEED,
), ),
( (
"test4", "test4",
{ {
mqtt.DOMAIN: {
fan.DOMAIN: {
"name": "test4", "name": "test4",
"command_topic": "command-topic", "command_topic": "command-topic",
"preset_mode_command_topic": "preset-mode-command-topic", "preset_mode_command_topic": "preset-mode-command-topic",
}
}
}, },
False, False,
None, None,
@ -1496,100 +1507,136 @@ async def test_attributes(
( (
"test5", "test5",
{ {
mqtt.DOMAIN: {
fan.DOMAIN: {
"name": "test5", "name": "test5",
"command_topic": "command-topic", "command_topic": "command-topic",
"preset_mode_command_topic": "preset-mode-command-topic", "preset_mode_command_topic": "preset-mode-command-topic",
"preset_modes": ["eco", "auto"], "preset_modes": ["eco", "auto"],
}
}
}, },
True, True,
fan.SUPPORT_PRESET_MODE, fan.FanEntityFeature.PRESET_MODE,
), ),
( (
"test6", "test6",
{ {
mqtt.DOMAIN: {
fan.DOMAIN: {
"name": "test6", "name": "test6",
"command_topic": "command-topic", "command_topic": "command-topic",
"preset_mode_command_topic": "preset-mode-command-topic", "preset_mode_command_topic": "preset-mode-command-topic",
"preset_modes": ["eco", "smart", "auto"], "preset_modes": ["eco", "smart", "auto"],
}
}
}, },
True, True,
fan.SUPPORT_PRESET_MODE, fan.FanEntityFeature.PRESET_MODE,
), ),
( (
"test7", "test7",
{ {
mqtt.DOMAIN: {
fan.DOMAIN: {
"name": "test7", "name": "test7",
"command_topic": "command-topic", "command_topic": "command-topic",
"percentage_command_topic": "percentage-command-topic", "percentage_command_topic": "percentage-command-topic",
}
}
}, },
True, True,
fan.SUPPORT_SET_SPEED, fan.FanEntityFeature.SET_SPEED,
), ),
( (
"test8", "test8",
{ {
mqtt.DOMAIN: {
fan.DOMAIN: {
"name": "test8", "name": "test8",
"command_topic": "command-topic", "command_topic": "command-topic",
"oscillation_command_topic": "oscillation-command-topic", "oscillation_command_topic": "oscillation-command-topic",
"percentage_command_topic": "percentage-command-topic", "percentage_command_topic": "percentage-command-topic",
}
}
}, },
True, True,
fan.SUPPORT_OSCILLATE | fan.SUPPORT_SET_SPEED, fan.FanEntityFeature.OSCILLATE | fan.FanEntityFeature.SET_SPEED,
), ),
( (
"test9", "test9",
{ {
mqtt.DOMAIN: {
fan.DOMAIN: {
"name": "test9", "name": "test9",
"command_topic": "command-topic", "command_topic": "command-topic",
"preset_mode_command_topic": "preset-mode-command-topic", "preset_mode_command_topic": "preset-mode-command-topic",
"preset_modes": ["Mode1", "Mode2", "Mode3"], "preset_modes": ["Mode1", "Mode2", "Mode3"],
}
}
}, },
True, True,
fan.SUPPORT_PRESET_MODE, fan.FanEntityFeature.PRESET_MODE,
), ),
( (
"test10", "test10",
{ {
mqtt.DOMAIN: {
fan.DOMAIN: {
"name": "test10", "name": "test10",
"command_topic": "command-topic", "command_topic": "command-topic",
"preset_mode_command_topic": "preset-mode-command-topic", "preset_mode_command_topic": "preset-mode-command-topic",
"preset_modes": ["whoosh", "silent", "auto"], "preset_modes": ["whoosh", "silent", "auto"],
}
}
}, },
True, True,
fan.SUPPORT_PRESET_MODE, fan.FanEntityFeature.PRESET_MODE,
), ),
( (
"test11", "test11",
{ {
mqtt.DOMAIN: {
fan.DOMAIN: {
"name": "test11", "name": "test11",
"command_topic": "command-topic", "command_topic": "command-topic",
"oscillation_command_topic": "oscillation-command-topic", "oscillation_command_topic": "oscillation-command-topic",
"preset_mode_command_topic": "preset-mode-command-topic", "preset_mode_command_topic": "preset-mode-command-topic",
"preset_modes": ["Mode1", "Mode2", "Mode3"], "preset_modes": ["Mode1", "Mode2", "Mode3"],
}
}
}, },
True, True,
fan.SUPPORT_PRESET_MODE | fan.SUPPORT_OSCILLATE, fan.FanEntityFeature.PRESET_MODE | fan.FanEntityFeature.OSCILLATE,
), ),
( (
"test12", "test12",
{ {
mqtt.DOMAIN: {
fan.DOMAIN: {
"name": "test12", "name": "test12",
"command_topic": "command-topic", "command_topic": "command-topic",
"percentage_command_topic": "percentage-command-topic", "percentage_command_topic": "percentage-command-topic",
"speed_range_min": 1, "speed_range_min": 1,
"speed_range_max": 40, "speed_range_max": 40,
}
}
}, },
True, True,
fan.SUPPORT_SET_SPEED, fan.FanEntityFeature.SET_SPEED,
), ),
( (
"test13", "test13",
{ {
mqtt.DOMAIN: {
fan.DOMAIN: {
"name": "test13", "name": "test13",
"command_topic": "command-topic", "command_topic": "command-topic",
"percentage_command_topic": "percentage-command-topic", "percentage_command_topic": "percentage-command-topic",
"speed_range_min": 50, "speed_range_min": 50,
"speed_range_max": 40, "speed_range_max": 40,
}
}
}, },
False, False,
None, None,
@ -1597,11 +1644,15 @@ async def test_attributes(
( (
"test14", "test14",
{ {
mqtt.DOMAIN: {
fan.DOMAIN: {
"name": "test14", "name": "test14",
"command_topic": "command-topic", "command_topic": "command-topic",
"percentage_command_topic": "percentage-command-topic", "percentage_command_topic": "percentage-command-topic",
"speed_range_min": 0, "speed_range_min": 0,
"speed_range_max": 40, "speed_range_max": 40,
}
}
}, },
False, False,
None, None,
@ -1609,10 +1660,14 @@ async def test_attributes(
( (
"test15", "test15",
{ {
mqtt.DOMAIN: {
fan.DOMAIN: {
"name": "test7reset_payload_in_preset_modes_a", "name": "test7reset_payload_in_preset_modes_a",
"command_topic": "command-topic", "command_topic": "command-topic",
"preset_mode_command_topic": "preset-mode-command-topic", "preset_mode_command_topic": "preset-mode-command-topic",
"preset_modes": ["auto", "smart", "normal", "None"], "preset_modes": ["auto", "smart", "normal", "None"],
}
}
}, },
False, False,
None, None,
@ -1620,39 +1675,37 @@ async def test_attributes(
( (
"test16", "test16",
{ {
mqtt.DOMAIN: {
fan.DOMAIN: {
"name": "test16", "name": "test16",
"command_topic": "command-topic", "command_topic": "command-topic",
"preset_mode_command_topic": "preset-mode-command-topic", "preset_mode_command_topic": "preset-mode-command-topic",
"preset_modes": ["whoosh", "silent", "auto", "None"], "preset_modes": ["whoosh", "silent", "auto", "None"],
"payload_reset_preset_mode": "normal", "payload_reset_preset_mode": "normal",
}
}
}, },
True, True,
fan.SUPPORT_PRESET_MODE, fan.FanEntityFeature.PRESET_MODE,
), ),
], ],
) )
async def test_supported_features( async def test_supported_features(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
name, name: str,
config, success: bool,
success,
features, features,
) -> None: ) -> None:
"""Test optimistic mode without state topic.""" """Test optimistic mode without state topic."""
assert (
await async_setup_component(
hass, mqtt.DOMAIN, {mqtt.DOMAIN: {fan.DOMAIN: config}}
)
is success
)
if success: if success:
await hass.async_block_till_done() await mqtt_mock_entry_no_yaml_config()
await mqtt_mock_entry_with_yaml_config()
state = hass.states.get(f"fan.{name}") state = hass.states.get(f"fan.{name}")
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == features assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == features
return
with pytest.raises(AssertionError):
await mqtt_mock_entry_no_yaml_config()
@pytest.mark.parametrize("hass_config", [DEFAULT_CONFIG]) @pytest.mark.parametrize("hass_config", [DEFAULT_CONFIG])