Validate Select option before calling entity method (#52352)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
Co-authored-by: Franck Nijhof <frenck@frenck.nl>
Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
Jan Bouwhuis 2021-08-04 11:12:42 +02:00 committed by GitHub
parent ff307a802e
commit 8299d0a7c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 164 additions and 12 deletions

View file

@ -1,6 +1,18 @@
"""The tests for the Select component."""
from homeassistant.components.select import SelectEntity
from unittest.mock import MagicMock
import pytest
from homeassistant.components.select import ATTR_OPTIONS, DOMAIN, SelectEntity
from homeassistant.const import (
ATTR_ENTITY_ID,
ATTR_OPTION,
CONF_PLATFORM,
SERVICE_SELECT_OPTION,
STATE_UNKNOWN,
)
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
class MockSelectEntity(SelectEntity):
@ -26,3 +38,75 @@ async def test_select(hass: HomeAssistant) -> None:
select._attr_current_option = "option_four"
assert select.current_option == "option_four"
assert select.state is None
select.hass = hass
with pytest.raises(NotImplementedError):
await select.async_select_option("option_one")
select.select_option = MagicMock()
await select.async_select_option("option_one")
assert select.select_option.called
assert select.select_option.call_args[0][0] == "option_one"
assert select.capability_attributes[ATTR_OPTIONS] == [
"option_one",
"option_two",
"option_three",
]
async def test_custom_integration_and_validation(hass, enable_custom_integrations):
"""Test we can only select valid options."""
platform = getattr(hass.components, f"test.{DOMAIN}")
platform.init()
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
await hass.async_block_till_done()
assert hass.states.get("select.select_1").state == "option 1"
await hass.services.async_call(
DOMAIN,
SERVICE_SELECT_OPTION,
{ATTR_OPTION: "option 2", ATTR_ENTITY_ID: "select.select_1"},
blocking=True,
)
hass.states.async_set("select.select_1", "option 2")
await hass.async_block_till_done()
assert hass.states.get("select.select_1").state == "option 2"
# test ValueError trigger
with pytest.raises(ValueError):
await hass.services.async_call(
DOMAIN,
SERVICE_SELECT_OPTION,
{ATTR_OPTION: "option invalid", ATTR_ENTITY_ID: "select.select_1"},
blocking=True,
)
await hass.async_block_till_done()
assert hass.states.get("select.select_1").state == "option 2"
assert hass.states.get("select.select_2").state == STATE_UNKNOWN
with pytest.raises(ValueError):
await hass.services.async_call(
DOMAIN,
SERVICE_SELECT_OPTION,
{ATTR_OPTION: "option invalid", ATTR_ENTITY_ID: "select.select_2"},
blocking=True,
)
await hass.async_block_till_done()
assert hass.states.get("select.select_2").state == STATE_UNKNOWN
await hass.services.async_call(
DOMAIN,
SERVICE_SELECT_OPTION,
{ATTR_OPTION: "option 3", ATTR_ENTITY_ID: "select.select_2"},
blocking=True,
)
await hass.async_block_till_done()
assert hass.states.get("select.select_2").state == "option 3"