Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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