Skip to content

Commit

Permalink
Allow MQTT selects to have a single or no options (#60281)
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery committed Nov 24, 2021
1 parent fd116fc commit 7c3edf2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 44 deletions.
18 changes: 2 additions & 16 deletions homeassistant/components/mqtt/select.py
Expand Up @@ -35,14 +35,6 @@
)


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,
Expand All @@ -53,15 +45,9 @@ def validate_config(config):
},
).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(
Expand Down
33 changes: 5 additions & 28 deletions tests/components/mqtt/test_select.py
Expand Up @@ -5,10 +5,7 @@
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,
Expand Down Expand Up @@ -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(
Expand All @@ -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):
Expand Down

0 comments on commit 7c3edf2

Please sign in to comment.