diff --git a/tests/components/mqtt/test_siren.py b/tests/components/mqtt/test_siren.py index 9837a3cc8a6..7b20e802a5c 100644 --- a/tests/components/mqtt/test_siren.py +++ b/tests/components/mqtt/test_siren.py @@ -19,9 +19,9 @@ from homeassistant.const import ( Platform, ) from homeassistant.core import HomeAssistant -from homeassistant.setup import async_setup_component from .test_common import ( + help_custom_config, help_test_availability_when_connection_lost, help_test_availability_without_topic, help_test_custom_availability_payload, @@ -86,13 +86,9 @@ async def async_turn_off( await hass.services.async_call(siren.DOMAIN, SERVICE_TURN_OFF, data, blocking=True) -async def test_controlling_state_via_topic( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the controlling state via topic.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { siren.DOMAIN: { @@ -103,10 +99,14 @@ async def test_controlling_state_via_topic( "payload_off": 0, } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_controlling_state_via_topic( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the controlling state via topic.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("siren.test") assert state.state == STATE_UNKNOWN @@ -123,13 +123,9 @@ async def test_controlling_state_via_topic( assert state.state == STATE_OFF -async def test_sending_mqtt_commands_and_optimistic( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the sending MQTT commands in optimistic mode.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { siren.DOMAIN: { @@ -140,10 +136,15 @@ async def test_sending_mqtt_commands_and_optimistic( "qos": "2", } } - }, - ) + } + ], +) +async def test_sending_mqtt_commands_and_optimistic( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the sending MQTT commands in optimistic mode.""" await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state = hass.states.get("siren.test") assert state.state == STATE_OFF @@ -167,15 +168,9 @@ async def test_sending_mqtt_commands_and_optimistic( assert state.state == STATE_OFF -async def test_controlling_state_via_topic_and_json_message( - hass: HomeAssistant, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, - caplog: pytest.LogCaptureFixture, -) -> None: - """Test the controlling state via topic and JSON message.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { siren.DOMAIN: { @@ -187,10 +182,16 @@ async def test_controlling_state_via_topic_and_json_message( "state_value_template": "{{ value_json.val }}", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_controlling_state_via_topic_and_json_message( + hass: HomeAssistant, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test the controlling state via topic and JSON message.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("siren.test") assert state.state == STATE_UNKNOWN @@ -210,15 +211,9 @@ async def test_controlling_state_via_topic_and_json_message( assert state.state == STATE_OFF -async def test_controlling_state_and_attributes_with_json_message_without_template( - hass: HomeAssistant, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, - caplog: pytest.LogCaptureFixture, -) -> None: - """Test the controlling state via topic and JSON message without a value template.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { siren.DOMAIN: { @@ -230,10 +225,16 @@ async def test_controlling_state_and_attributes_with_json_message_without_templa "available_tones": ["ping", "siren", "bell"], } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_controlling_state_and_attributes_with_json_message_without_template( + hass: HomeAssistant, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test the controlling state via topic and JSON message without a value template.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("siren.test") assert state.state == STATE_UNKNOWN @@ -292,31 +293,41 @@ async def test_controlling_state_and_attributes_with_json_message_without_templa assert state.attributes.get(siren.ATTR_VOLUME_LEVEL) == 0.6 +@pytest.mark.parametrize( + "hass_config", + [ + help_custom_config( + siren.DOMAIN, + { + mqtt.DOMAIN: { + siren.DOMAIN: { + "command_topic": "command-topic", + } + } + }, + ( + { + "name": "test1", + "available_tones": ["ping", "siren", "bell"], + "support_duration": False, + }, + { + "name": "test2", + "available_tones": ["ping", "siren", "bell"], + "support_volume_set": False, + }, + { + "name": "test3", + }, + ), + ) + ], +) async def test_filtering_not_supported_attributes_optimistic( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test setting attributes with support flags optimistic.""" - config = { - "command_topic": "command-topic", - "available_tones": ["ping", "siren", "bell"], - } - config1 = copy.deepcopy(config) - config1["name"] = "test1" - config1["support_duration"] = False - config2 = copy.deepcopy(config) - config2["name"] = "test2" - config2["support_volume_set"] = False - config3 = copy.deepcopy(config) - config3["name"] = "test3" - del config3["available_tones"] - - assert await async_setup_component( - hass, - mqtt.DOMAIN, - {mqtt.DOMAIN: {siren.DOMAIN: [config1, config2, config3]}}, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + await mqtt_mock_entry_no_yaml_config() state1 = hass.states.get("siren.test1") assert state1.state == STATE_OFF @@ -377,34 +388,44 @@ async def test_filtering_not_supported_attributes_optimistic( assert state3.attributes.get(siren.ATTR_VOLUME_LEVEL) == 0.88 +@pytest.mark.parametrize( + "hass_config", + [ + help_custom_config( + siren.DOMAIN, + { + mqtt.DOMAIN: { + siren.DOMAIN: { + "command_topic": "command-topic", + } + } + }, + ( + { + "name": "test1", + "state_topic": "state-topic1", + "available_tones": ["ping", "siren", "bell"], + "support_duration": False, + }, + { + "name": "test2", + "state_topic": "state-topic2", + "available_tones": ["ping", "siren", "bell"], + "support_volume_set": False, + }, + { + "name": "test3", + "state_topic": "state-topic3", + }, + ), + ) + ], +) async def test_filtering_not_supported_attributes_via_state( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test setting attributes with support flags via state.""" - config = { - "command_topic": "command-topic", - "available_tones": ["ping", "siren", "bell"], - } - config1 = copy.deepcopy(config) - config1["name"] = "test1" - config1["state_topic"] = "state-topic1" - config1["support_duration"] = False - config2 = copy.deepcopy(config) - config2["name"] = "test2" - config2["state_topic"] = "state-topic2" - config2["support_volume_set"] = False - config3 = copy.deepcopy(config) - config3["name"] = "test3" - config3["state_topic"] = "state-topic3" - del config3["available_tones"] - - assert await async_setup_component( - hass, - mqtt.DOMAIN, - {mqtt.DOMAIN: {siren.DOMAIN: [config1, config2, config3]}}, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + await mqtt_mock_entry_no_yaml_config() state1 = hass.states.get("siren.test1") assert state1.state == STATE_UNKNOWN @@ -529,13 +550,9 @@ async def test_custom_availability_payload( ) -async def test_custom_state_payload( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator -) -> None: - """Test the state payload.""" - assert await async_setup_component( - hass, - mqtt.DOMAIN, +@pytest.mark.parametrize( + "hass_config", + [ { mqtt.DOMAIN: { siren.DOMAIN: { @@ -548,10 +565,14 @@ async def test_custom_state_payload( "state_off": "LOW", } } - }, - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + } + ], +) +async def test_custom_state_payload( + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator +) -> None: + """Test the state payload.""" + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("siren.test") assert state.state == STATE_UNKNOWN @@ -762,30 +783,36 @@ async def test_discovery_update_siren_template( ) +@pytest.mark.parametrize( + "hass_config", + [ + help_custom_config( + siren.DOMAIN, + DEFAULT_CONFIG, + ( + { + "name": "Beer", + "available_tones": ["ping", "chimes"], + "command_template": "CMD: {{ value }}, DURATION: {{ duration }}," + " TONE: {{ tone }}, VOLUME: {{ volume_level }}", + }, + { + "name": "Milk", + "available_tones": ["ping", "chimes"], + "command_template": "CMD: {{ value }}, DURATION: {{ duration }}," + " TONE: {{ tone }}, VOLUME: {{ volume_level }}", + "command_off_template": "CMD_OFF: {{ value }}", + }, + ), + ) + ], +) async def test_command_templates( hass: HomeAssistant, - mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, - caplog: pytest.LogCaptureFixture, + mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, ) -> None: """Test siren with command templates optimistic.""" - config1 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][siren.DOMAIN]) - config1["name"] = "Beer" - config1["available_tones"] = ["ping", "chimes"] - config1[ - "command_template" - ] = "CMD: {{ value }}, DURATION: {{ duration }}, TONE: {{ tone }}, VOLUME: {{ volume_level }}" - - config2 = copy.deepcopy(config1) - config2["name"] = "Milk" - config2["command_off_template"] = "CMD_OFF: {{ value }}" - - assert await async_setup_component( - hass, - mqtt.DOMAIN, - {mqtt.DOMAIN: {siren.DOMAIN: [config1, config2]}}, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() state1 = hass.states.get("siren.beer") assert state1.state == STATE_OFF diff --git a/tests/components/mqtt/test_state_vacuum.py b/tests/components/mqtt/test_state_vacuum.py index a5c83813100..dffaebca172 100644 --- a/tests/components/mqtt/test_state_vacuum.py +++ b/tests/components/mqtt/test_state_vacuum.py @@ -29,9 +29,9 @@ from homeassistant.components.vacuum import ( ) from homeassistant.const import CONF_NAME, ENTITY_MATCH_ALL, STATE_UNKNOWN, Platform from homeassistant.core import HomeAssistant -from homeassistant.setup import async_setup_component from .test_common import ( + help_custom_config, help_test_availability_when_connection_lost, help_test_availability_without_topic, help_test_custom_availability_payload, @@ -83,6 +83,18 @@ DEFAULT_CONFIG = { DEFAULT_CONFIG_2 = {mqtt.DOMAIN: {vacuum.DOMAIN: {"schema": "state", "name": "test"}}} +CONFIG_ALL_SERVICES = help_custom_config( + vacuum.DOMAIN, + DEFAULT_CONFIG, + ( + { + mqttvacuum.CONF_SUPPORTED_FEATURES: services_to_strings( + mqttvacuum.ALL_SERVICES, SERVICE_TO_STRING + ) + }, + ), +) + @pytest.fixture(autouse=True) def vacuum_platform_only(): @@ -91,13 +103,12 @@ def vacuum_platform_only(): yield +@pytest.mark.parametrize("hass_config", [DEFAULT_CONFIG]) async def test_default_supported_features( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test that the correct supported features.""" - assert await async_setup_component(hass, mqtt.DOMAIN, DEFAULT_CONFIG) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + await mqtt_mock_entry_no_yaml_config() entity = hass.states.get("vacuum.mqtttest") entity_features = entity.attributes.get(mqttvacuum.CONF_SUPPORTED_FEATURES, 0) assert sorted(services_to_strings(entity_features, SERVICE_TO_STRING)) == sorted( @@ -105,20 +116,12 @@ async def test_default_supported_features( ) +@pytest.mark.parametrize("hass_config", [CONFIG_ALL_SERVICES]) async def test_all_commands( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test simple commands send to the vacuum.""" - config = deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][vacuum.DOMAIN]) - config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings( - mqttvacuum.ALL_SERVICES, SERVICE_TO_STRING - ) - - assert await async_setup_component( - hass, mqtt.DOMAIN, {mqtt.DOMAIN: {vacuum.DOMAIN: config}} - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() await hass.services.async_call( DOMAIN, SERVICE_START, {"entity_id": ENTITY_MATCH_ALL}, blocking=True @@ -181,21 +184,27 @@ async def test_all_commands( } +@pytest.mark.parametrize( + "hass_config", + [ + help_custom_config( + vacuum.DOMAIN, + DEFAULT_CONFIG, + ( + { + mqttvacuum.CONF_SUPPORTED_FEATURES: services_to_strings( + mqttvacuum.STRING_TO_SERVICE["status"], SERVICE_TO_STRING + ) + }, + ), + ) + ], +) async def test_commands_without_supported_features( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test commands which are not supported by the vacuum.""" - config = deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][vacuum.DOMAIN]) - services = mqttvacuum.STRING_TO_SERVICE["status"] - config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings( - services, SERVICE_TO_STRING - ) - - assert await async_setup_component( - hass, mqtt.DOMAIN, {mqtt.DOMAIN: {vacuum.DOMAIN: config}} - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + mqtt_mock = await mqtt_mock_entry_no_yaml_config() await hass.services.async_call( DOMAIN, SERVICE_START, {"entity_id": ENTITY_MATCH_ALL}, blocking=True @@ -243,20 +252,12 @@ async def test_commands_without_supported_features( mqtt_mock.async_publish.assert_not_called() +@pytest.mark.parametrize("hass_config", [CONFIG_ALL_SERVICES]) async def test_status( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test status updates from the vacuum.""" - config = deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][vacuum.DOMAIN]) - config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings( - mqttvacuum.ALL_SERVICES, SERVICE_TO_STRING - ) - - assert await async_setup_component( - hass, mqtt.DOMAIN, {mqtt.DOMAIN: {vacuum.DOMAIN: config}} - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + await mqtt_mock_entry_no_yaml_config() state = hass.states.get("vacuum.mqtttest") assert state.state == STATE_UNKNOWN @@ -292,21 +293,27 @@ async def test_status( assert state.state == STATE_UNKNOWN +@pytest.mark.parametrize( + "hass_config", + [ + help_custom_config( + vacuum.DOMAIN, + DEFAULT_CONFIG, + ( + { + mqttvacuum.CONF_SUPPORTED_FEATURES: services_to_strings( + mqttvacuum.DEFAULT_SERVICES, SERVICE_TO_STRING + ) + }, + ), + ) + ], +) async def test_no_fan_vacuum( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test status updates from the vacuum when fan is not supported.""" - config = deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][vacuum.DOMAIN]) - del config[mqttvacuum.CONF_FAN_SPEED_LIST] - config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings( - mqttvacuum.DEFAULT_SERVICES, SERVICE_TO_STRING - ) - - assert await async_setup_component( - hass, mqtt.DOMAIN, {mqtt.DOMAIN: {vacuum.DOMAIN: config}} - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + await mqtt_mock_entry_no_yaml_config() message = """{ "battery_level": 54, @@ -347,21 +354,13 @@ async def test_no_fan_vacuum( assert state.attributes.get(ATTR_BATTERY_LEVEL) == 61 +@pytest.mark.parametrize("hass_config", [CONFIG_ALL_SERVICES]) @pytest.mark.no_fail_on_log_exception async def test_status_invalid_json( - hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator + hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator ) -> None: """Test to make sure nothing breaks if the vacuum sends bad JSON.""" - config = deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][vacuum.DOMAIN]) - config[mqttvacuum.CONF_SUPPORTED_FEATURES] = services_to_strings( - mqttvacuum.ALL_SERVICES, SERVICE_TO_STRING - ) - - assert await async_setup_component( - hass, mqtt.DOMAIN, {mqtt.DOMAIN: {vacuum.DOMAIN: config}} - ) - await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() + await mqtt_mock_entry_no_yaml_config() async_fire_mqtt_message(hass, "vacuum/state", '{"asdfasas false}') state = hass.states.get("vacuum.mqtttest")