init home-assistant
This commit is contained in:
1
home-assistant/tests/components/select/__init__.py
Normal file
1
home-assistant/tests/components/select/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""The tests for the Select integration."""
|
||||
23
home-assistant/tests/components/select/common.py
Normal file
23
home-assistant/tests/components/select/common.py
Normal file
@@ -0,0 +1,23 @@
|
||||
"""Common helpers for select entity component tests."""
|
||||
|
||||
from homeassistant.components.select import SelectEntity
|
||||
|
||||
from tests.common import MockEntity
|
||||
|
||||
|
||||
class MockSelectEntity(MockEntity, SelectEntity):
|
||||
"""Mock Select class."""
|
||||
|
||||
@property
|
||||
def current_option(self):
|
||||
"""Return the current option of this select."""
|
||||
return self._handle("current_option")
|
||||
|
||||
@property
|
||||
def options(self) -> list:
|
||||
"""Return the list of available options of this select."""
|
||||
return self._handle("options")
|
||||
|
||||
def select_option(self, option: str) -> None:
|
||||
"""Change the selected option."""
|
||||
self._values["current_option"] = option
|
||||
24
home-assistant/tests/components/select/conftest.py
Normal file
24
home-assistant/tests/components/select/conftest.py
Normal file
@@ -0,0 +1,24 @@
|
||||
"""Fixtures for the select entity component tests."""
|
||||
|
||||
import pytest
|
||||
|
||||
from .common import MockSelectEntity
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_select_entities() -> list[MockSelectEntity]:
|
||||
"""Return a list of mock select entities."""
|
||||
return [
|
||||
MockSelectEntity(
|
||||
name="select 1",
|
||||
unique_id="unique_select_1",
|
||||
options=["option 1", "option 2", "option 3"],
|
||||
current_option="option 1",
|
||||
),
|
||||
MockSelectEntity(
|
||||
name="select 2",
|
||||
unique_id="unique_select_2",
|
||||
options=["option 1", "option 2", "option 3"],
|
||||
current_option=None,
|
||||
),
|
||||
]
|
||||
511
home-assistant/tests/components/select/test_device_action.py
Normal file
511
home-assistant/tests/components/select/test_device_action.py
Normal file
@@ -0,0 +1,511 @@
|
||||
"""The tests for Select device actions."""
|
||||
|
||||
import pytest
|
||||
from pytest_unordered import unordered
|
||||
import voluptuous_serialize
|
||||
|
||||
from homeassistant.components import automation
|
||||
from homeassistant.components.device_automation import DeviceAutomationType
|
||||
from homeassistant.components.select import DOMAIN
|
||||
from homeassistant.components.select.device_action import async_get_action_capabilities
|
||||
from homeassistant.const import EntityCategory
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import (
|
||||
config_validation as cv,
|
||||
device_registry as dr,
|
||||
entity_registry as er,
|
||||
)
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import (
|
||||
MockConfigEntry,
|
||||
async_get_device_automations,
|
||||
async_mock_service,
|
||||
)
|
||||
|
||||
|
||||
async def test_get_actions(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test we get the expected actions from a select."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_entry = entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
expected_actions = [
|
||||
{
|
||||
"domain": DOMAIN,
|
||||
"type": action,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entity_entry.id,
|
||||
"metadata": {"secondary": False},
|
||||
}
|
||||
for action in (
|
||||
"select_first",
|
||||
"select_last",
|
||||
"select_next",
|
||||
"select_option",
|
||||
"select_previous",
|
||||
)
|
||||
]
|
||||
actions = await async_get_device_automations(
|
||||
hass, DeviceAutomationType.ACTION, device_entry.id
|
||||
)
|
||||
assert actions == unordered(expected_actions)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("hidden_by", "entity_category"),
|
||||
[
|
||||
(er.RegistryEntryHider.INTEGRATION, None),
|
||||
(er.RegistryEntryHider.USER, None),
|
||||
(None, EntityCategory.CONFIG),
|
||||
(None, EntityCategory.DIAGNOSTIC),
|
||||
],
|
||||
)
|
||||
async def test_get_actions_hidden_auxiliary(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
hidden_by,
|
||||
entity_category,
|
||||
) -> None:
|
||||
"""Test we get the expected actions from a hidden or auxiliary entity."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_entry = entity_registry.async_get_or_create(
|
||||
DOMAIN,
|
||||
"test",
|
||||
"5678",
|
||||
device_id=device_entry.id,
|
||||
entity_category=entity_category,
|
||||
hidden_by=hidden_by,
|
||||
)
|
||||
expected_actions = []
|
||||
expected_actions += [
|
||||
{
|
||||
"domain": DOMAIN,
|
||||
"type": action,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entity_entry.id,
|
||||
"metadata": {"secondary": True},
|
||||
}
|
||||
for action in (
|
||||
"select_first",
|
||||
"select_last",
|
||||
"select_next",
|
||||
"select_option",
|
||||
"select_previous",
|
||||
)
|
||||
]
|
||||
actions = await async_get_device_automations(
|
||||
hass, DeviceAutomationType.ACTION, device_entry.id
|
||||
)
|
||||
assert actions == unordered(expected_actions)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("action_type", ["select_first", "select_last"])
|
||||
async def test_action_select_first_last(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
action_type: str,
|
||||
) -> None:
|
||||
"""Test for select_first and select_last actions."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entry = entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger": {
|
||||
"platform": "event",
|
||||
"event_type": "test_event",
|
||||
},
|
||||
"action": {
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.id,
|
||||
"type": action_type,
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
select_calls = async_mock_service(hass, DOMAIN, action_type)
|
||||
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(select_calls) == 1
|
||||
assert select_calls[0].domain == DOMAIN
|
||||
assert select_calls[0].service == action_type
|
||||
assert select_calls[0].data == {"entity_id": entry.entity_id}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("action_type", ["select_first", "select_last"])
|
||||
async def test_action_select_first_last_legacy(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
action_type: str,
|
||||
) -> None:
|
||||
"""Test for select_first and select_last actions."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entry = entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger": {
|
||||
"platform": "event",
|
||||
"event_type": "test_event",
|
||||
},
|
||||
"action": {
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.entity_id,
|
||||
"type": action_type,
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
select_calls = async_mock_service(hass, DOMAIN, action_type)
|
||||
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(select_calls) == 1
|
||||
assert select_calls[0].domain == DOMAIN
|
||||
assert select_calls[0].service == action_type
|
||||
assert select_calls[0].data == {"entity_id": entry.entity_id}
|
||||
|
||||
|
||||
async def test_action_select_option(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test for select_option action."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entry = entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger": {
|
||||
"platform": "event",
|
||||
"event_type": "test_event",
|
||||
},
|
||||
"action": {
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.id,
|
||||
"type": "select_option",
|
||||
"option": "option1",
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
select_calls = async_mock_service(hass, DOMAIN, "select_option")
|
||||
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(select_calls) == 1
|
||||
assert select_calls[0].domain == DOMAIN
|
||||
assert select_calls[0].service == "select_option"
|
||||
assert select_calls[0].data == {"entity_id": entry.entity_id, "option": "option1"}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("action_type", ["select_next", "select_previous"])
|
||||
async def test_action_select_next_previous(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
action_type: str,
|
||||
) -> None:
|
||||
"""Test for select_next and select_previous actions."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entry = entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger": {
|
||||
"platform": "event",
|
||||
"event_type": "test_event",
|
||||
},
|
||||
"action": {
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.id,
|
||||
"type": action_type,
|
||||
"cycle": False,
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
select_calls = async_mock_service(hass, DOMAIN, action_type)
|
||||
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(select_calls) == 1
|
||||
assert select_calls[0].domain == DOMAIN
|
||||
assert select_calls[0].service == action_type
|
||||
assert select_calls[0].data == {"entity_id": entry.entity_id, "cycle": False}
|
||||
|
||||
|
||||
async def test_get_action_capabilities(
|
||||
hass: HomeAssistant, entity_registry: er.EntityRegistry
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a select action."""
|
||||
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
|
||||
|
||||
config = {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": "select_option",
|
||||
"entity_id": entry.id,
|
||||
"option": "option1",
|
||||
}
|
||||
|
||||
# Test when entity doesn't exists
|
||||
capabilities = await async_get_action_capabilities(hass, config)
|
||||
assert capabilities
|
||||
assert "extra_fields" in capabilities
|
||||
assert voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
) == [
|
||||
{
|
||||
"name": "option",
|
||||
"required": True,
|
||||
"type": "select",
|
||||
"options": [],
|
||||
},
|
||||
]
|
||||
|
||||
# Mock an entity
|
||||
hass.states.async_set(
|
||||
entry.entity_id, "option1", {"options": ["option1", "option2"]}
|
||||
)
|
||||
|
||||
# Test if we get the right capabilities now
|
||||
capabilities = await async_get_action_capabilities(hass, config)
|
||||
assert capabilities
|
||||
assert "extra_fields" in capabilities
|
||||
assert voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
) == [
|
||||
{
|
||||
"name": "option",
|
||||
"required": True,
|
||||
"type": "select",
|
||||
"options": [("option1", "option1"), ("option2", "option2")],
|
||||
},
|
||||
]
|
||||
|
||||
# Test next/previous actions
|
||||
config = {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": "select_next",
|
||||
"entity_id": entry.id,
|
||||
}
|
||||
capabilities = await async_get_action_capabilities(hass, config)
|
||||
assert capabilities
|
||||
assert "extra_fields" in capabilities
|
||||
assert voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
) == [
|
||||
{
|
||||
"name": "cycle",
|
||||
"optional": True,
|
||||
"type": "boolean",
|
||||
"default": True,
|
||||
},
|
||||
]
|
||||
|
||||
config["type"] = "select_previous"
|
||||
capabilities = await async_get_action_capabilities(hass, config)
|
||||
assert capabilities
|
||||
assert "extra_fields" in capabilities
|
||||
assert voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
) == [
|
||||
{
|
||||
"name": "cycle",
|
||||
"optional": True,
|
||||
"type": "boolean",
|
||||
"default": True,
|
||||
},
|
||||
]
|
||||
|
||||
# Test action types without extra fields
|
||||
config = {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": "select_first",
|
||||
"entity_id": entry.id,
|
||||
}
|
||||
capabilities = await async_get_action_capabilities(hass, config)
|
||||
assert capabilities == {}
|
||||
|
||||
config["type"] = "select_last"
|
||||
capabilities = await async_get_action_capabilities(hass, config)
|
||||
assert capabilities == {}
|
||||
|
||||
|
||||
async def test_get_action_capabilities_legacy(
|
||||
hass: HomeAssistant, entity_registry: er.EntityRegistry
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a select action."""
|
||||
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
|
||||
|
||||
config = {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": "select_option",
|
||||
"entity_id": entry.entity_id,
|
||||
"option": "option1",
|
||||
}
|
||||
|
||||
# Test when entity doesn't exists
|
||||
capabilities = await async_get_action_capabilities(hass, config)
|
||||
assert capabilities
|
||||
assert "extra_fields" in capabilities
|
||||
assert voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
) == [
|
||||
{
|
||||
"name": "option",
|
||||
"required": True,
|
||||
"type": "select",
|
||||
"options": [],
|
||||
},
|
||||
]
|
||||
|
||||
# Mock an entity
|
||||
hass.states.async_set(
|
||||
entry.entity_id, "option1", {"options": ["option1", "option2"]}
|
||||
)
|
||||
|
||||
# Test if we get the right capabilities now
|
||||
capabilities = await async_get_action_capabilities(hass, config)
|
||||
assert capabilities
|
||||
assert "extra_fields" in capabilities
|
||||
assert voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
) == [
|
||||
{
|
||||
"name": "option",
|
||||
"required": True,
|
||||
"type": "select",
|
||||
"options": [("option1", "option1"), ("option2", "option2")],
|
||||
},
|
||||
]
|
||||
|
||||
# Test next/previous actions
|
||||
config = {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": "select_next",
|
||||
"entity_id": entry.entity_id,
|
||||
}
|
||||
capabilities = await async_get_action_capabilities(hass, config)
|
||||
assert capabilities
|
||||
assert "extra_fields" in capabilities
|
||||
assert voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
) == [
|
||||
{
|
||||
"name": "cycle",
|
||||
"optional": True,
|
||||
"type": "boolean",
|
||||
"default": True,
|
||||
},
|
||||
]
|
||||
|
||||
config["type"] = "select_previous"
|
||||
capabilities = await async_get_action_capabilities(hass, config)
|
||||
assert capabilities
|
||||
assert "extra_fields" in capabilities
|
||||
assert voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
) == [
|
||||
{
|
||||
"name": "cycle",
|
||||
"optional": True,
|
||||
"type": "boolean",
|
||||
"default": True,
|
||||
},
|
||||
]
|
||||
|
||||
# Test action types without extra fields
|
||||
config = {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": "select_first",
|
||||
"entity_id": entry.entity_id,
|
||||
}
|
||||
capabilities = await async_get_action_capabilities(hass, config)
|
||||
assert capabilities == {}
|
||||
|
||||
config["type"] = "select_last"
|
||||
capabilities = await async_get_action_capabilities(hass, config)
|
||||
assert capabilities == {}
|
||||
366
home-assistant/tests/components/select/test_device_condition.py
Normal file
366
home-assistant/tests/components/select/test_device_condition.py
Normal file
@@ -0,0 +1,366 @@
|
||||
"""The tests for Select device conditions."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
from pytest_unordered import unordered
|
||||
import voluptuous_serialize
|
||||
|
||||
from homeassistant.components import automation
|
||||
from homeassistant.components.device_automation import DeviceAutomationType
|
||||
from homeassistant.components.select import DOMAIN
|
||||
from homeassistant.components.select.device_condition import (
|
||||
async_get_condition_capabilities,
|
||||
)
|
||||
from homeassistant.const import EntityCategory
|
||||
from homeassistant.core import HomeAssistant, ServiceCall
|
||||
from homeassistant.helpers import (
|
||||
config_validation as cv,
|
||||
device_registry as dr,
|
||||
entity_registry as er,
|
||||
)
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import MockConfigEntry, async_get_device_automations
|
||||
|
||||
|
||||
async def test_get_conditions(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test we get the expected conditions from a select."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_entry = entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
expected_conditions = [
|
||||
{
|
||||
"condition": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": "selected_option",
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entity_entry.id,
|
||||
"metadata": {"secondary": False},
|
||||
}
|
||||
]
|
||||
conditions = await async_get_device_automations(
|
||||
hass, DeviceAutomationType.CONDITION, device_entry.id
|
||||
)
|
||||
assert conditions == unordered(expected_conditions)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("hidden_by", "entity_category"),
|
||||
[
|
||||
(er.RegistryEntryHider.INTEGRATION, None),
|
||||
(er.RegistryEntryHider.USER, None),
|
||||
(None, EntityCategory.CONFIG),
|
||||
(None, EntityCategory.DIAGNOSTIC),
|
||||
],
|
||||
)
|
||||
async def test_get_conditions_hidden_auxiliary(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
hidden_by,
|
||||
entity_category,
|
||||
) -> None:
|
||||
"""Test we get the expected conditions from a hidden or auxiliary entity."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_entry = entity_registry.async_get_or_create(
|
||||
DOMAIN,
|
||||
"test",
|
||||
"5678",
|
||||
device_id=device_entry.id,
|
||||
entity_category=entity_category,
|
||||
hidden_by=hidden_by,
|
||||
)
|
||||
expected_conditions = [
|
||||
{
|
||||
"condition": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": condition,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entity_entry.id,
|
||||
"metadata": {"secondary": True},
|
||||
}
|
||||
for condition in ("selected_option",)
|
||||
]
|
||||
conditions = await async_get_device_automations(
|
||||
hass, DeviceAutomationType.CONDITION, device_entry.id
|
||||
)
|
||||
assert conditions == unordered(expected_conditions)
|
||||
|
||||
|
||||
async def test_if_selected_option(
|
||||
hass: HomeAssistant,
|
||||
service_calls: list[ServiceCall],
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test for selected_option conditions."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entry = entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger": {"platform": "event", "event_type": "test_event1"},
|
||||
"condition": [
|
||||
{
|
||||
"condition": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.id,
|
||||
"type": "selected_option",
|
||||
"option": "option1",
|
||||
}
|
||||
],
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data": {
|
||||
"result": "option1 - {{ trigger.platform }} - {{ trigger.event.event_type }}"
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"trigger": {"platform": "event", "event_type": "test_event2"},
|
||||
"condition": [
|
||||
{
|
||||
"condition": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.id,
|
||||
"type": "selected_option",
|
||||
"option": "option2",
|
||||
}
|
||||
],
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data": {
|
||||
"result": "option2 - {{ trigger.platform }} - {{ trigger.event.event_type }}"
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
# Test with non existing entity
|
||||
hass.bus.async_fire("test_event1")
|
||||
hass.bus.async_fire("test_event2")
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 0
|
||||
|
||||
hass.states.async_set(
|
||||
entry.entity_id, "option1", {"options": ["option1", "option2"]}
|
||||
)
|
||||
hass.bus.async_fire("test_event1")
|
||||
hass.bus.async_fire("test_event2")
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 1
|
||||
assert service_calls[0].data["result"] == "option1 - event - test_event1"
|
||||
|
||||
hass.states.async_set(
|
||||
entry.entity_id, "option2", {"options": ["option1", "option2"]}
|
||||
)
|
||||
hass.bus.async_fire("test_event1")
|
||||
hass.bus.async_fire("test_event2")
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 2
|
||||
assert service_calls[1].data["result"] == "option2 - event - test_event2"
|
||||
|
||||
|
||||
async def test_if_selected_option_legacy(
|
||||
hass: HomeAssistant,
|
||||
service_calls: list[ServiceCall],
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test for selected_option conditions."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entry = entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger": {"platform": "event", "event_type": "test_event1"},
|
||||
"condition": [
|
||||
{
|
||||
"condition": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.entity_id,
|
||||
"type": "selected_option",
|
||||
"option": "option1",
|
||||
}
|
||||
],
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data": {
|
||||
"result": "option1 - {{ trigger.platform }} - {{ trigger.event.event_type }}"
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set(
|
||||
entry.entity_id, "option1", {"options": ["option1", "option2"]}
|
||||
)
|
||||
hass.bus.async_fire("test_event1")
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 1
|
||||
assert service_calls[0].data["result"] == "option1 - event - test_event1"
|
||||
|
||||
|
||||
async def test_get_condition_capabilities(
|
||||
hass: HomeAssistant, entity_registry: er.EntityRegistry
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a select condition."""
|
||||
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
|
||||
|
||||
config = {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": "selected_option",
|
||||
"entity_id": entry.id,
|
||||
"option": "option1",
|
||||
}
|
||||
|
||||
# Test when entity doesn't exists
|
||||
capabilities = await async_get_condition_capabilities(hass, config)
|
||||
assert capabilities
|
||||
assert "extra_fields" in capabilities
|
||||
assert voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
) == [
|
||||
{
|
||||
"name": "option",
|
||||
"required": True,
|
||||
"type": "select",
|
||||
"options": [],
|
||||
},
|
||||
{
|
||||
"name": "for",
|
||||
"optional": True,
|
||||
"type": "positive_time_period_dict",
|
||||
},
|
||||
]
|
||||
|
||||
# Mock an entity
|
||||
hass.states.async_set(
|
||||
entry.entity_id, "option1", {"options": ["option1", "option2"]}
|
||||
)
|
||||
|
||||
# Test if we get the right capabilities now
|
||||
capabilities = await async_get_condition_capabilities(hass, config)
|
||||
assert capabilities
|
||||
assert "extra_fields" in capabilities
|
||||
assert voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
) == [
|
||||
{
|
||||
"name": "option",
|
||||
"required": True,
|
||||
"type": "select",
|
||||
"options": [("option1", "option1"), ("option2", "option2")],
|
||||
},
|
||||
{
|
||||
"name": "for",
|
||||
"optional": True,
|
||||
"type": "positive_time_period_dict",
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
async def test_get_condition_capabilities_legacy(
|
||||
hass: HomeAssistant, entity_registry: er.EntityRegistry
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a select condition."""
|
||||
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
|
||||
|
||||
config = {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": "selected_option",
|
||||
"entity_id": entry.entity_id,
|
||||
"option": "option1",
|
||||
}
|
||||
|
||||
# Test when entity doesn't exists
|
||||
capabilities = await async_get_condition_capabilities(hass, config)
|
||||
assert capabilities
|
||||
assert "extra_fields" in capabilities
|
||||
assert voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
) == [
|
||||
{
|
||||
"name": "option",
|
||||
"required": True,
|
||||
"type": "select",
|
||||
"options": [],
|
||||
},
|
||||
{
|
||||
"name": "for",
|
||||
"optional": True,
|
||||
"type": "positive_time_period_dict",
|
||||
},
|
||||
]
|
||||
|
||||
# Mock an entity
|
||||
hass.states.async_set(
|
||||
entry.entity_id, "option1", {"options": ["option1", "option2"]}
|
||||
)
|
||||
|
||||
# Test if we get the right capabilities now
|
||||
capabilities = await async_get_condition_capabilities(hass, config)
|
||||
assert capabilities
|
||||
assert "extra_fields" in capabilities
|
||||
assert voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
) == [
|
||||
{
|
||||
"name": "option",
|
||||
"required": True,
|
||||
"type": "select",
|
||||
"options": [("option1", "option1"), ("option2", "option2")],
|
||||
},
|
||||
{
|
||||
"name": "for",
|
||||
"optional": True,
|
||||
"type": "positive_time_period_dict",
|
||||
},
|
||||
]
|
||||
469
home-assistant/tests/components/select/test_device_trigger.py
Normal file
469
home-assistant/tests/components/select/test_device_trigger.py
Normal file
@@ -0,0 +1,469 @@
|
||||
"""The tests for Select device triggers."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
from pytest_unordered import unordered
|
||||
import voluptuous_serialize
|
||||
|
||||
from homeassistant.components import automation
|
||||
from homeassistant.components.device_automation import DeviceAutomationType
|
||||
from homeassistant.components.select import DOMAIN
|
||||
from homeassistant.components.select.device_trigger import (
|
||||
async_get_trigger_capabilities,
|
||||
)
|
||||
from homeassistant.const import EntityCategory
|
||||
from homeassistant.core import HomeAssistant, ServiceCall
|
||||
from homeassistant.helpers import (
|
||||
config_validation as cv,
|
||||
device_registry as dr,
|
||||
entity_registry as er,
|
||||
)
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import MockConfigEntry, async_get_device_automations
|
||||
|
||||
|
||||
async def test_get_triggers(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test we get the expected triggers from a select."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_entry = entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
expected_triggers = [
|
||||
{
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": "current_option_changed",
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entity_entry.id,
|
||||
"metadata": {"secondary": False},
|
||||
}
|
||||
]
|
||||
triggers = await async_get_device_automations(
|
||||
hass, DeviceAutomationType.TRIGGER, device_entry.id
|
||||
)
|
||||
assert triggers == unordered(expected_triggers)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("hidden_by", "entity_category"),
|
||||
[
|
||||
(er.RegistryEntryHider.INTEGRATION, None),
|
||||
(er.RegistryEntryHider.USER, None),
|
||||
(None, EntityCategory.CONFIG),
|
||||
(None, EntityCategory.DIAGNOSTIC),
|
||||
],
|
||||
)
|
||||
async def test_get_triggers_hidden_auxiliary(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
hidden_by,
|
||||
entity_category,
|
||||
) -> None:
|
||||
"""Test we get the expected triggers from a hidden or auxiliary entity."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_entry = entity_registry.async_get_or_create(
|
||||
DOMAIN,
|
||||
"test",
|
||||
"5678",
|
||||
device_id=device_entry.id,
|
||||
entity_category=entity_category,
|
||||
hidden_by=hidden_by,
|
||||
)
|
||||
expected_triggers = [
|
||||
{
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": trigger,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entity_entry.id,
|
||||
"metadata": {"secondary": True},
|
||||
}
|
||||
for trigger in ("current_option_changed",)
|
||||
]
|
||||
triggers = await async_get_device_automations(
|
||||
hass, DeviceAutomationType.TRIGGER, device_entry.id
|
||||
)
|
||||
assert triggers == unordered(expected_triggers)
|
||||
|
||||
|
||||
async def test_if_fires_on_state_change(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
service_calls: list[ServiceCall],
|
||||
) -> None:
|
||||
"""Test for turn_on and turn_off triggers firing."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entry = entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
|
||||
hass.states.async_set(
|
||||
entry.entity_id, "option1", {"options": ["option1", "option2", "option3"]}
|
||||
)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger": {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.id,
|
||||
"type": "current_option_changed",
|
||||
"to": "option2",
|
||||
},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data": {
|
||||
"some": (
|
||||
"to - {{ trigger.platform}} - "
|
||||
"{{ trigger.entity_id}} - {{ trigger.from_state.state}} - "
|
||||
"{{ trigger.to_state.state}} - {{ trigger.for }} - "
|
||||
"{{ trigger.id}}"
|
||||
)
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"trigger": {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.id,
|
||||
"type": "current_option_changed",
|
||||
"from": "option2",
|
||||
},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data": {
|
||||
"some": (
|
||||
"from - {{ trigger.platform}} - "
|
||||
"{{ trigger.entity_id}} - {{ trigger.from_state.state}} - "
|
||||
"{{ trigger.to_state.state}} - {{ trigger.for }} - "
|
||||
"{{ trigger.id}}"
|
||||
)
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"trigger": {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.id,
|
||||
"type": "current_option_changed",
|
||||
"from": "option3",
|
||||
"to": "option1",
|
||||
},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data": {
|
||||
"some": (
|
||||
"from-to - {{ trigger.platform}} - "
|
||||
"{{ trigger.entity_id}} - {{ trigger.from_state.state}} - "
|
||||
"{{ trigger.to_state.state}} - {{ trigger.for }} - "
|
||||
"{{ trigger.id}}"
|
||||
)
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
# Test triggering device trigger with a to state
|
||||
hass.states.async_set(entry.entity_id, "option2")
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 1
|
||||
assert (
|
||||
service_calls[0].data["some"]
|
||||
== f"to - device - {entry.entity_id} - option1 - option2 - None - 0"
|
||||
)
|
||||
|
||||
# Test triggering device trigger with a from state
|
||||
hass.states.async_set(entry.entity_id, "option3")
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 2
|
||||
assert (
|
||||
service_calls[1].data["some"]
|
||||
== f"from - device - {entry.entity_id} - option2 - option3 - None - 0"
|
||||
)
|
||||
|
||||
# Test triggering device trigger with both a from and to state
|
||||
hass.states.async_set(entry.entity_id, "option1")
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 3
|
||||
assert (
|
||||
service_calls[2].data["some"]
|
||||
== f"from-to - device - {entry.entity_id} - option3 - option1 - None - 0"
|
||||
)
|
||||
|
||||
|
||||
async def test_if_fires_on_state_change_legacy(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
service_calls: list[ServiceCall],
|
||||
) -> None:
|
||||
"""Test for turn_on and turn_off triggers firing."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entry = entity_registry.async_get_or_create(
|
||||
DOMAIN, "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
|
||||
hass.states.async_set(
|
||||
entry.entity_id, "option1", {"options": ["option1", "option2", "option3"]}
|
||||
)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger": {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entry.entity_id,
|
||||
"type": "current_option_changed",
|
||||
"to": "option2",
|
||||
},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data": {
|
||||
"some": (
|
||||
"to - {{ trigger.platform}} - "
|
||||
"{{ trigger.entity_id}} - {{ trigger.from_state.state}} - "
|
||||
"{{ trigger.to_state.state}} - {{ trigger.for }} - "
|
||||
"{{ trigger.id}}"
|
||||
)
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
# Test triggering device trigger with a to state
|
||||
hass.states.async_set(entry.entity_id, "option2")
|
||||
await hass.async_block_till_done()
|
||||
assert len(service_calls) == 1
|
||||
assert (
|
||||
service_calls[0].data["some"]
|
||||
== f"to - device - {entry.entity_id} - option1 - option2 - None - 0"
|
||||
)
|
||||
|
||||
|
||||
async def test_get_trigger_capabilities(
|
||||
hass: HomeAssistant, entity_registry: er.EntityRegistry
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a select trigger."""
|
||||
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
|
||||
|
||||
config = {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": "current_option_changed",
|
||||
"entity_id": entry.id,
|
||||
"to": "option1",
|
||||
}
|
||||
|
||||
# Test when entity doesn't exists
|
||||
capabilities = await async_get_trigger_capabilities(hass, config)
|
||||
assert capabilities
|
||||
assert "extra_fields" in capabilities
|
||||
assert voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
) == [
|
||||
{
|
||||
"name": "from",
|
||||
"optional": True,
|
||||
"type": "select",
|
||||
"options": [],
|
||||
},
|
||||
{
|
||||
"name": "to",
|
||||
"optional": True,
|
||||
"type": "select",
|
||||
"options": [],
|
||||
},
|
||||
{
|
||||
"name": "for",
|
||||
"optional": True,
|
||||
"type": "positive_time_period_dict",
|
||||
},
|
||||
]
|
||||
|
||||
# Mock an entity
|
||||
hass.states.async_set(
|
||||
entry.entity_id, "option1", {"options": ["option1", "option2"]}
|
||||
)
|
||||
|
||||
# Test if we get the right capabilities now
|
||||
capabilities = await async_get_trigger_capabilities(hass, config)
|
||||
assert capabilities
|
||||
assert "extra_fields" in capabilities
|
||||
assert voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
) == [
|
||||
{
|
||||
"name": "from",
|
||||
"optional": True,
|
||||
"type": "select",
|
||||
"options": [("option1", "option1"), ("option2", "option2")],
|
||||
},
|
||||
{
|
||||
"name": "to",
|
||||
"optional": True,
|
||||
"type": "select",
|
||||
"options": [("option1", "option1"), ("option2", "option2")],
|
||||
},
|
||||
{
|
||||
"name": "for",
|
||||
"optional": True,
|
||||
"type": "positive_time_period_dict",
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
async def test_get_trigger_capabilities_unknown(
|
||||
hass: HomeAssistant, entity_registry: er.EntityRegistry
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a select trigger."""
|
||||
config = {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": "current_option_changed",
|
||||
"entity_id": "12345",
|
||||
"to": "option1",
|
||||
}
|
||||
|
||||
# Test when entity doesn't exists
|
||||
capabilities = await async_get_trigger_capabilities(hass, config)
|
||||
assert capabilities
|
||||
assert "extra_fields" in capabilities
|
||||
assert voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
) == [
|
||||
{
|
||||
"name": "from",
|
||||
"optional": True,
|
||||
"type": "select",
|
||||
"options": [],
|
||||
},
|
||||
{
|
||||
"name": "to",
|
||||
"optional": True,
|
||||
"type": "select",
|
||||
"options": [],
|
||||
},
|
||||
{
|
||||
"name": "for",
|
||||
"optional": True,
|
||||
"type": "positive_time_period_dict",
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
async def test_get_trigger_capabilities_legacy(
|
||||
hass: HomeAssistant, entity_registry: er.EntityRegistry
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a select trigger."""
|
||||
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
|
||||
|
||||
config = {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": "current_option_changed",
|
||||
"entity_id": entry.entity_id,
|
||||
"to": "option1",
|
||||
}
|
||||
|
||||
# Test when entity doesn't exists
|
||||
capabilities = await async_get_trigger_capabilities(hass, config)
|
||||
assert capabilities
|
||||
assert "extra_fields" in capabilities
|
||||
assert voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
) == [
|
||||
{
|
||||
"name": "from",
|
||||
"optional": True,
|
||||
"type": "select",
|
||||
"options": [],
|
||||
},
|
||||
{
|
||||
"name": "to",
|
||||
"optional": True,
|
||||
"type": "select",
|
||||
"options": [],
|
||||
},
|
||||
{
|
||||
"name": "for",
|
||||
"optional": True,
|
||||
"type": "positive_time_period_dict",
|
||||
},
|
||||
]
|
||||
|
||||
# Mock an entity
|
||||
hass.states.async_set(
|
||||
entry.entity_id, "option1", {"options": ["option1", "option2"]}
|
||||
)
|
||||
|
||||
# Test if we get the right capabilities now
|
||||
capabilities = await async_get_trigger_capabilities(hass, config)
|
||||
assert capabilities
|
||||
assert "extra_fields" in capabilities
|
||||
assert voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
) == [
|
||||
{
|
||||
"name": "from",
|
||||
"optional": True,
|
||||
"type": "select",
|
||||
"options": [("option1", "option1"), ("option2", "option2")],
|
||||
},
|
||||
{
|
||||
"name": "to",
|
||||
"optional": True,
|
||||
"type": "select",
|
||||
"options": [("option1", "option1"), ("option2", "option2")],
|
||||
},
|
||||
{
|
||||
"name": "for",
|
||||
"optional": True,
|
||||
"type": "positive_time_period_dict",
|
||||
},
|
||||
]
|
||||
204
home-assistant/tests/components/select/test_init.py
Normal file
204
home-assistant/tests/components/select/test_init.py
Normal file
@@ -0,0 +1,204 @@
|
||||
"""The tests for the Select component."""
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
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,
|
||||
)
|
||||
from homeassistant.const import ATTR_ENTITY_ID, CONF_PLATFORM, STATE_UNKNOWN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ServiceValidationError
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import setup_test_component_platform
|
||||
|
||||
|
||||
class MockSelectEntity(SelectEntity):
|
||||
"""Mock SelectEntity to use in tests."""
|
||||
|
||||
_attr_current_option = "option_one"
|
||||
_attr_options = ["option_one", "option_two", "option_three"]
|
||||
|
||||
|
||||
async def test_select(hass: HomeAssistant) -> None:
|
||||
"""Test getting data from the mocked select entity."""
|
||||
select = MockSelectEntity()
|
||||
assert select.current_option == "option_one"
|
||||
assert select.state == "option_one"
|
||||
assert select.options == ["option_one", "option_two", "option_three"]
|
||||
|
||||
# Test none selected
|
||||
select._attr_current_option = None
|
||||
assert select.current_option is None
|
||||
assert select.state is None
|
||||
|
||||
# Test none existing selected
|
||||
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_first()
|
||||
|
||||
with pytest.raises(NotImplementedError):
|
||||
await select.async_last()
|
||||
|
||||
with pytest.raises(NotImplementedError):
|
||||
await select.async_next(cycle=False)
|
||||
|
||||
with pytest.raises(NotImplementedError):
|
||||
await select.async_previous(cycle=False)
|
||||
|
||||
with pytest.raises(NotImplementedError):
|
||||
await select.async_select_option("option_one")
|
||||
|
||||
select.select_option = MagicMock()
|
||||
select._attr_current_option = None
|
||||
|
||||
await select.async_first()
|
||||
assert select.select_option.call_args[0][0] == "option_one"
|
||||
|
||||
await select.async_last()
|
||||
assert select.select_option.call_args[0][0] == "option_three"
|
||||
|
||||
await select.async_next(cycle=False)
|
||||
assert select.select_option.call_args[0][0] == "option_one"
|
||||
|
||||
await select.async_previous(cycle=False)
|
||||
assert select.select_option.call_args[0][0] == "option_three"
|
||||
|
||||
await select.async_select_option("option_two")
|
||||
assert select.select_option.call_args[0][0] == "option_two"
|
||||
|
||||
assert select.select_option.call_count == 5
|
||||
|
||||
assert select.capability_attributes[ATTR_OPTIONS] == [
|
||||
"option_one",
|
||||
"option_two",
|
||||
"option_three",
|
||||
]
|
||||
|
||||
|
||||
async def test_custom_integration_and_validation(
|
||||
hass: HomeAssistant,
|
||||
mock_select_entities: list[MockSelectEntity],
|
||||
) -> None:
|
||||
"""Test we can only select valid options."""
|
||||
setup_test_component_platform(hass, DOMAIN, mock_select_entities)
|
||||
|
||||
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 ServiceValidationError trigger
|
||||
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"},
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert exc.value.translation_domain == DOMAIN
|
||||
assert exc.value.translation_key == "not_valid_option"
|
||||
|
||||
assert hass.states.get("select.select_1").state == "option 2"
|
||||
|
||||
assert hass.states.get("select.select_2").state == STATE_UNKNOWN
|
||||
|
||||
with pytest.raises(ServiceValidationError):
|
||||
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"
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_SELECT_FIRST,
|
||||
{ATTR_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"},
|
||||
blocking=True,
|
||||
)
|
||||
assert hass.states.get("select.select_2").state == "option 3"
|
||||
|
||||
# Do no cycle
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_SELECT_NEXT,
|
||||
{ATTR_ENTITY_ID: "select.select_2", ATTR_CYCLE: False},
|
||||
blocking=True,
|
||||
)
|
||||
assert hass.states.get("select.select_2").state == "option 3"
|
||||
|
||||
# Do cycle (default behavior)
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_SELECT_NEXT,
|
||||
{ATTR_ENTITY_ID: "select.select_2"},
|
||||
blocking=True,
|
||||
)
|
||||
assert hass.states.get("select.select_2").state == "option 1"
|
||||
|
||||
# Do not cycle
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_SELECT_PREVIOUS,
|
||||
{ATTR_ENTITY_ID: "select.select_2", ATTR_CYCLE: False},
|
||||
blocking=True,
|
||||
)
|
||||
assert hass.states.get("select.select_2").state == "option 1"
|
||||
|
||||
# Do cycle (default behavior)
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_SELECT_PREVIOUS,
|
||||
{ATTR_ENTITY_ID: "select.select_2"},
|
||||
blocking=True,
|
||||
)
|
||||
assert hass.states.get("select.select_2").state == "option 3"
|
||||
52
home-assistant/tests/components/select/test_recorder.py
Normal file
52
home-assistant/tests/components/select/test_recorder.py
Normal file
@@ -0,0 +1,52 @@
|
||||
"""The tests for select recorder."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
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.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
from tests.components.recorder.common import async_wait_recording_done
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
async def select_only() -> None:
|
||||
"""Enable only the select platform."""
|
||||
with patch(
|
||||
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
|
||||
[Platform.SELECT],
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
async def test_exclude_attributes(recorder_mock: Recorder, hass: HomeAssistant) -> None:
|
||||
"""Test select registered attributes to be excluded."""
|
||||
now = dt_util.utcnow()
|
||||
assert await async_setup_component(hass, "homeassistant", {})
|
||||
await async_setup_component(
|
||||
hass, select.DOMAIN, {select.DOMAIN: {"platform": "demo"}}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5))
|
||||
await hass.async_block_till_done()
|
||||
await async_wait_recording_done(hass)
|
||||
|
||||
states = await hass.async_add_executor_job(
|
||||
get_significant_states, hass, now, None, hass.states.async_entity_ids()
|
||||
)
|
||||
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
|
||||
@@ -0,0 +1,57 @@
|
||||
"""Test reproduce state for select entities."""
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.select.const import (
|
||||
ATTR_OPTION,
|
||||
ATTR_OPTIONS,
|
||||
DOMAIN,
|
||||
SERVICE_SELECT_OPTION,
|
||||
)
|
||||
from homeassistant.const import ATTR_ENTITY_ID
|
||||
from homeassistant.core import HomeAssistant, State
|
||||
from homeassistant.helpers.state import async_reproduce_state
|
||||
|
||||
from tests.common import async_mock_service
|
||||
|
||||
|
||||
async def test_reproducing_states(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
"""Test reproducing select states."""
|
||||
calls = async_mock_service(hass, DOMAIN, SERVICE_SELECT_OPTION)
|
||||
hass.states.async_set(
|
||||
"select.test",
|
||||
"option_one",
|
||||
{ATTR_OPTIONS: ["option_one", "option_two", "option_three"]},
|
||||
)
|
||||
|
||||
await async_reproduce_state(
|
||||
hass,
|
||||
[
|
||||
State("select.test", "option_two"),
|
||||
],
|
||||
)
|
||||
|
||||
assert len(calls) == 1
|
||||
assert calls[0].domain == DOMAIN
|
||||
assert calls[0].data == {ATTR_ENTITY_ID: "select.test", ATTR_OPTION: "option_two"}
|
||||
|
||||
# Calling it again should not do anything
|
||||
await async_reproduce_state(
|
||||
hass,
|
||||
[
|
||||
State("select.test", "option_one"),
|
||||
],
|
||||
)
|
||||
assert len(calls) == 1
|
||||
|
||||
# Restoring an invalid state should not work either
|
||||
await async_reproduce_state(hass, [State("select.test", "option_four")])
|
||||
assert len(calls) == 1
|
||||
assert "Invalid state specified" in caplog.text
|
||||
|
||||
# Restoring an state for an invalid entity ID logs a warning
|
||||
await async_reproduce_state(hass, [State("select.non_existing", "option_three")])
|
||||
assert len(calls) == 1
|
||||
assert "Unable to find entity" in caplog.text
|
||||
@@ -0,0 +1,21 @@
|
||||
"""Test the select significant change platform."""
|
||||
|
||||
from homeassistant.components.select.significant_change import (
|
||||
async_check_significant_change,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
|
||||
async def test_significant_change(hass: HomeAssistant) -> None:
|
||||
"""Detect select significant change."""
|
||||
attrs1 = {"options": ["option1", "option2"]}
|
||||
attrs2 = {"options": ["option1", "option2", "option3"]}
|
||||
|
||||
assert not async_check_significant_change(
|
||||
hass, "option1", attrs1, "option1", attrs1
|
||||
)
|
||||
assert not async_check_significant_change(
|
||||
hass, "option1", attrs1, "option1", attrs2
|
||||
)
|
||||
assert async_check_significant_change(hass, "option1", attrs1, "option2", attrs1)
|
||||
assert async_check_significant_change(hass, "option1", attrs1, "option2", attrs2)
|
||||
Reference in New Issue
Block a user