Allow MQTT selects to have a single or no options (#60281)

This commit is contained in:
Erik Montnemery 2021-11-24 20:06:12 +01:00 committed by GitHub
parent fd116fc408
commit 7c3edf24f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 44 deletions

View File

@ -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( _PLATFORM_SCHEMA_BASE = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend(
{ {
vol.Optional(CONF_COMMAND_TEMPLATE): cv.template, 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) ).extend(MQTT_ENTITY_COMMON_SCHEMA.schema)
PLATFORM_SCHEMA = vol.All( PLATFORM_SCHEMA = vol.All(_PLATFORM_SCHEMA_BASE)
_PLATFORM_SCHEMA_BASE,
validate_config,
)
DISCOVERY_SCHEMA = vol.All( DISCOVERY_SCHEMA = vol.All(_PLATFORM_SCHEMA_BASE.extend({}, extra=vol.REMOVE_EXTRA))
_PLATFORM_SCHEMA_BASE.extend({}, extra=vol.REMOVE_EXTRA),
validate_config,
)
async def async_setup_platform( async def async_setup_platform(

View File

@ -5,10 +5,7 @@ from unittest.mock import patch
import pytest import pytest
from homeassistant.components import select from homeassistant.components import select
from homeassistant.components.mqtt.select import ( from homeassistant.components.mqtt.select import MQTT_SELECT_ATTRIBUTES_BLOCKED
CONF_OPTIONS,
MQTT_SELECT_ATTRIBUTES_BLOCKED,
)
from homeassistant.components.select import ( from homeassistant.components.select import (
ATTR_OPTION, ATTR_OPTION,
ATTR_OPTIONS, 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.""" """Test options attribute."""
topic = "test/select" topic = "test/select"
await async_setup_component( await async_setup_component(
@ -490,35 +488,14 @@ async def test_options_attributes(hass, mqtt_mock):
"state_topic": topic, "state_topic": topic,
"command_topic": topic, "command_topic": topic,
"name": "Test select", "name": "Test select",
"options": ["milk", "beer"], "options": options,
} }
}, },
) )
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get("select.test_select") state = hass.states.get("select.test_select")
assert state.attributes.get(ATTR_OPTIONS) == ["milk", "beer"] assert state.attributes.get(ATTR_OPTIONS) == options
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
async def test_mqtt_payload_not_an_option_warning(hass, caplog, mqtt_mock): async def test_mqtt_payload_not_an_option_warning(hass, caplog, mqtt_mock):