From 7c3edf24f2235d0deceffd633666ea0d1285a4a1 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 24 Nov 2021 20:06:12 +0100 Subject: [PATCH] Allow MQTT selects to have a single or no options (#60281) --- homeassistant/components/mqtt/select.py | 18 ++------------ tests/components/mqtt/test_select.py | 33 ++++--------------------- 2 files changed, 7 insertions(+), 44 deletions(-) diff --git a/homeassistant/components/mqtt/select.py b/homeassistant/components/mqtt/select.py index 45304d68079..7b374ba8955 100644 --- a/homeassistant/components/mqtt/select.py +++ b/homeassistant/components/mqtt/select.py @@ -35,14 +35,6 @@ MQTT_SELECT_ATTRIBUTES_BLOCKED = frozenset( ) -def validate_config(config): - """Validate that the configuration is valid, throws if it isn't.""" - if len(config[CONF_OPTIONS]) < 2: - raise vol.Invalid(f"'{CONF_OPTIONS}' must include at least 2 options") - - return config - - _PLATFORM_SCHEMA_BASE = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend( { vol.Optional(CONF_COMMAND_TEMPLATE): cv.template, @@ -53,15 +45,9 @@ _PLATFORM_SCHEMA_BASE = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend( }, ).extend(MQTT_ENTITY_COMMON_SCHEMA.schema) -PLATFORM_SCHEMA = vol.All( - _PLATFORM_SCHEMA_BASE, - validate_config, -) +PLATFORM_SCHEMA = vol.All(_PLATFORM_SCHEMA_BASE) -DISCOVERY_SCHEMA = vol.All( - _PLATFORM_SCHEMA_BASE.extend({}, extra=vol.REMOVE_EXTRA), - validate_config, -) +DISCOVERY_SCHEMA = vol.All(_PLATFORM_SCHEMA_BASE.extend({}, extra=vol.REMOVE_EXTRA)) async def async_setup_platform( diff --git a/tests/components/mqtt/test_select.py b/tests/components/mqtt/test_select.py index 731aca2e178..2f31ce788fd 100644 --- a/tests/components/mqtt/test_select.py +++ b/tests/components/mqtt/test_select.py @@ -5,10 +5,7 @@ from unittest.mock import patch import pytest from homeassistant.components import select -from homeassistant.components.mqtt.select import ( - CONF_OPTIONS, - MQTT_SELECT_ATTRIBUTES_BLOCKED, -) +from homeassistant.components.mqtt.select import MQTT_SELECT_ATTRIBUTES_BLOCKED from homeassistant.components.select import ( ATTR_OPTION, ATTR_OPTIONS, @@ -478,7 +475,8 @@ async def test_entity_debug_info_message(hass, mqtt_mock): ) -async def test_options_attributes(hass, mqtt_mock): +@pytest.mark.parametrize("options", [["milk", "beer"], ["milk"], []]) +async def test_options_attributes(hass, mqtt_mock, options): """Test options attribute.""" topic = "test/select" await async_setup_component( @@ -490,35 +488,14 @@ async def test_options_attributes(hass, mqtt_mock): "state_topic": topic, "command_topic": topic, "name": "Test select", - "options": ["milk", "beer"], + "options": options, } }, ) await hass.async_block_till_done() state = hass.states.get("select.test_select") - assert state.attributes.get(ATTR_OPTIONS) == ["milk", "beer"] - - -async def test_invalid_options(hass, caplog, mqtt_mock): - """Test invalid options.""" - topic = "test/select" - await async_setup_component( - hass, - "select", - { - "select": { - "platform": "mqtt", - "state_topic": topic, - "command_topic": topic, - "name": "Test Select", - "options": "beer", - } - }, - ) - await hass.async_block_till_done() - - assert f"'{CONF_OPTIONS}' must include at least 2 options" in caplog.text + assert state.attributes.get(ATTR_OPTIONS) == options async def test_mqtt_payload_not_an_option_warning(hass, caplog, mqtt_mock):