Use the select enums in the select tests (#193925)

This commit is contained in:
epenet
2026-11-23 11:50:45 +01:00
committed by GitHub
parent abb5b9fb26
commit a05013fd76
3 changed files with 73 additions and 46 deletions
+50 -37
View File
@@ -6,18 +6,13 @@ import pytest
from pytest_homeassistant.common import setup_test_component_platform
from homeassistant.components.select import (
ATTR_CYCLE,
ATTR_OPTION,
ATTR_OPTIONS,
DOMAIN,
SERVICE_SELECT_FIRST,
SERVICE_SELECT_LAST,
SERVICE_SELECT_NEXT,
SERVICE_SELECT_OPTION,
SERVICE_SELECT_PREVIOUS,
SelectEntity,
SelectEntityCapabilityAttribute,
SelectService,
SelectServiceArgument,
)
from homeassistant.const import ATTR_ENTITY_ID, CONF_PLATFORM, STATE_UNKNOWN
from homeassistant.const import CONF_PLATFORM, STATE_UNKNOWN, TargetKey
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
from homeassistant.setup import async_setup_component
@@ -91,7 +86,7 @@ async def test_select(hass: HomeAssistant) -> None:
assert select.select_option.call_count == 5
assert select.capability_attributes[ATTR_OPTIONS] == [
assert select.capability_attributes[SelectEntityCapabilityAttribute.OPTIONS] == [
"option_one",
"option_two",
"option_three",
@@ -112,8 +107,11 @@ async def test_custom_integration_and_validation(
await hass.services.async_call(
DOMAIN,
SERVICE_SELECT_OPTION,
{ATTR_OPTION: "option 2", ATTR_ENTITY_ID: "select.select_1"},
SelectService.SELECT_OPTION,
{
SelectServiceArgument.OPTION: "option 2",
TargetKey.ENTITY_ID: "select.select_1",
},
blocking=True,
)
@@ -125,8 +123,11 @@ async def test_custom_integration_and_validation(
with pytest.raises(ServiceValidationError) as exc:
await hass.services.async_call(
DOMAIN,
SERVICE_SELECT_OPTION,
{ATTR_OPTION: "option invalid", ATTR_ENTITY_ID: "select.select_1"},
SelectService.SELECT_OPTION,
{
SelectServiceArgument.OPTION: "option invalid",
TargetKey.ENTITY_ID: "select.select_1",
},
blocking=True,
)
await hass.async_block_till_done()
@@ -140,8 +141,11 @@ async def test_custom_integration_and_validation(
with pytest.raises(ServiceValidationError):
await hass.services.async_call(
DOMAIN,
SERVICE_SELECT_OPTION,
{ATTR_OPTION: "option invalid", ATTR_ENTITY_ID: "select.select_2"},
SelectService.SELECT_OPTION,
{
SelectServiceArgument.OPTION: "option invalid",
TargetKey.ENTITY_ID: "select.select_2",
},
blocking=True,
)
await hass.async_block_till_done()
@@ -149,8 +153,11 @@ async def test_custom_integration_and_validation(
await hass.services.async_call(
DOMAIN,
SERVICE_SELECT_OPTION,
{ATTR_OPTION: "option 3", ATTR_ENTITY_ID: "select.select_2"},
SelectService.SELECT_OPTION,
{
SelectServiceArgument.OPTION: "option 3",
TargetKey.ENTITY_ID: "select.select_2",
},
blocking=True,
)
await hass.async_block_till_done()
@@ -159,16 +166,16 @@ async def test_custom_integration_and_validation(
await hass.services.async_call(
DOMAIN,
SERVICE_SELECT_FIRST,
{ATTR_ENTITY_ID: "select.select_2"},
SelectService.SELECT_FIRST,
{TargetKey.ENTITY_ID: "select.select_2"},
blocking=True,
)
assert hass.states.get("select.select_2").state == "option 1"
await hass.services.async_call(
DOMAIN,
SERVICE_SELECT_LAST,
{ATTR_ENTITY_ID: "select.select_2"},
SelectService.SELECT_LAST,
{TargetKey.ENTITY_ID: "select.select_2"},
blocking=True,
)
assert hass.states.get("select.select_2").state == "option 3"
@@ -176,8 +183,8 @@ async def test_custom_integration_and_validation(
# Do no cycle
await hass.services.async_call(
DOMAIN,
SERVICE_SELECT_NEXT,
{ATTR_ENTITY_ID: "select.select_2", ATTR_CYCLE: False},
SelectService.SELECT_NEXT,
{TargetKey.ENTITY_ID: "select.select_2", SelectServiceArgument.CYCLE: False},
blocking=True,
)
assert hass.states.get("select.select_2").state == "option 3"
@@ -185,8 +192,8 @@ async def test_custom_integration_and_validation(
# Do cycle (default behavior)
await hass.services.async_call(
DOMAIN,
SERVICE_SELECT_NEXT,
{ATTR_ENTITY_ID: "select.select_2"},
SelectService.SELECT_NEXT,
{TargetKey.ENTITY_ID: "select.select_2"},
blocking=True,
)
assert hass.states.get("select.select_2").state == "option 1"
@@ -194,8 +201,8 @@ async def test_custom_integration_and_validation(
# Do not cycle
await hass.services.async_call(
DOMAIN,
SERVICE_SELECT_PREVIOUS,
{ATTR_ENTITY_ID: "select.select_2", ATTR_CYCLE: False},
SelectService.SELECT_PREVIOUS,
{TargetKey.ENTITY_ID: "select.select_2", SelectServiceArgument.CYCLE: False},
blocking=True,
)
assert hass.states.get("select.select_2").state == "option 1"
@@ -203,8 +210,8 @@ async def test_custom_integration_and_validation(
# Do cycle (default behavior)
await hass.services.async_call(
DOMAIN,
SERVICE_SELECT_PREVIOUS,
{ATTR_ENTITY_ID: "select.select_2"},
SelectService.SELECT_PREVIOUS,
{TargetKey.ENTITY_ID: "select.select_2"},
blocking=True,
)
assert hass.states.get("select.select_2").state == "option 3"
@@ -213,13 +220,19 @@ async def test_custom_integration_and_validation(
@pytest.mark.parametrize(
("service", "service_data"),
[
pytest.param(SERVICE_SELECT_FIRST, {}, id="first"),
pytest.param(SERVICE_SELECT_LAST, {}, id="last"),
pytest.param(SERVICE_SELECT_NEXT, {}, id="next_cycle"),
pytest.param(SERVICE_SELECT_NEXT, {ATTR_CYCLE: False}, id="next_no_cycle"),
pytest.param(SERVICE_SELECT_PREVIOUS, {}, id="previous_cycle"),
pytest.param(SelectService.SELECT_FIRST, {}, id="first"),
pytest.param(SelectService.SELECT_LAST, {}, id="last"),
pytest.param(SelectService.SELECT_NEXT, {}, id="next_cycle"),
pytest.param(
SERVICE_SELECT_PREVIOUS, {ATTR_CYCLE: False}, id="previous_no_cycle"
SelectService.SELECT_NEXT,
{SelectServiceArgument.CYCLE: False},
id="next_no_cycle",
),
pytest.param(SelectService.SELECT_PREVIOUS, {}, id="previous_cycle"),
pytest.param(
SelectService.SELECT_PREVIOUS,
{SelectServiceArgument.CYCLE: False},
id="previous_no_cycle",
),
],
)
@@ -249,7 +262,7 @@ async def test_navigation_services_without_options(
await hass.services.async_call(
DOMAIN,
service,
{ATTR_ENTITY_ID: "select.select_without_options"} | service_data,
{TargetKey.ENTITY_ID: "select.select_without_options"} | service_data,
blocking=True,
)
assert exc.value.translation_domain == DOMAIN
+4 -4
View File
@@ -10,8 +10,8 @@ from pytest_homeassistant.components.recorder.common import async_wait_recording
from homeassistant.components import select
from homeassistant.components.recorder import Recorder
from homeassistant.components.recorder.history import get_significant_states
from homeassistant.components.select import ATTR_OPTIONS
from homeassistant.const import ATTR_FRIENDLY_NAME, Platform
from homeassistant.components.select import SelectEntityCapabilityAttribute
from homeassistant.const import EntityStateAttribute, Platform
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
@@ -45,5 +45,5 @@ async def test_exclude_attributes(recorder_mock: Recorder, hass: HomeAssistant)
assert len(states) >= 1
for entity_states in states.values():
for state in entity_states:
assert ATTR_OPTIONS not in state.attributes
assert ATTR_FRIENDLY_NAME in state.attributes
assert SelectEntityCapabilityAttribute.OPTIONS not in state.attributes
assert EntityStateAttribute.FRIENDLY_NAME in state.attributes
@@ -3,8 +3,13 @@
import pytest
from pytest_homeassistant.common import async_mock_service
from homeassistant.components.select.const import ATTR_OPTIONS, DOMAIN
from homeassistant.const import ATTR_ENTITY_ID, ATTR_OPTION, SERVICE_SELECT_OPTION
from homeassistant.components.select import (
SelectEntityCapabilityAttribute,
SelectService,
SelectServiceArgument,
)
from homeassistant.components.select.const import DOMAIN
from homeassistant.const import TargetKey
from homeassistant.core import HomeAssistant, State
from homeassistant.helpers.state import async_reproduce_state
@@ -13,11 +18,17 @@ async def test_reproducing_states(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test reproducing select states."""
calls = async_mock_service(hass, DOMAIN, SERVICE_SELECT_OPTION)
calls = async_mock_service(hass, DOMAIN, SelectService.SELECT_OPTION)
hass.states.async_set(
"select.test",
"option_one",
{ATTR_OPTIONS: ["option_one", "option_two", "option_three"]},
{
SelectEntityCapabilityAttribute.OPTIONS: [
"option_one",
"option_two",
"option_three",
]
},
)
await async_reproduce_state(
@@ -29,7 +40,10 @@ async def test_reproducing_states(
assert len(calls) == 1
assert calls[0].domain == DOMAIN
assert calls[0].data == {ATTR_ENTITY_ID: "select.test", ATTR_OPTION: "option_two"}
assert calls[0].data == {
TargetKey.ENTITY_ID: "select.test",
SelectServiceArgument.OPTION: "option_two",
}
# Calling it again should not do anything
await async_reproduce_state(