Files
core/tests/components/switch/test_init.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

85 lines
2.6 KiB
Python
Raw Normal View History

2016-03-09 10:25:50 +01:00
"""The tests for the Switch component."""
2020-08-19 14:57:38 +02:00
import pytest
2014-11-25 00:20:36 -08:00
2019-04-14 22:31:01 -07:00
from homeassistant import core
2015-09-12 22:56:49 -07:00
from homeassistant.components import switch
from homeassistant.const import CONF_PLATFORM
from homeassistant.core import HomeAssistant
2020-08-19 14:57:38 +02:00
from homeassistant.setup import async_setup_component
2014-11-25 00:20:36 -08:00
from . import common
from .common import MockSwitch
2014-11-25 00:20:36 -08:00
from tests.common import MockUser, setup_test_component_platform
2014-11-25 00:20:36 -08:00
2020-08-19 14:57:38 +02:00
@pytest.fixture(autouse=True)
def entities(
hass: HomeAssistant, mock_switch_entities: list[MockSwitch]
) -> list[MockSwitch]:
2020-08-19 14:57:38 +02:00
"""Initialize the test switch."""
setup_test_component_platform(hass, switch.DOMAIN, mock_switch_entities)
return mock_switch_entities
2018-07-29 01:53:37 +01:00
@pytest.mark.usefixtures("enable_custom_integrations")
async def test_methods(hass: HomeAssistant, entities: list[MockSwitch]) -> None:
2020-08-19 14:57:38 +02:00
"""Test is_on, turn_on, turn_off methods."""
switch_1, switch_2, switch_3 = entities
assert await async_setup_component(
hass, switch.DOMAIN, {switch.DOMAIN: {CONF_PLATFORM: "test"}}
)
await hass.async_block_till_done()
assert switch.is_on(hass, switch_1.entity_id)
assert not switch.is_on(hass, switch_2.entity_id)
assert not switch.is_on(hass, switch_3.entity_id)
await common.async_turn_off(hass, switch_1.entity_id)
await common.async_turn_on(hass, switch_2.entity_id)
assert not switch.is_on(hass, switch_1.entity_id)
assert switch.is_on(hass, switch_2.entity_id)
# Turn all off
await common.async_turn_off(hass)
assert not switch.is_on(hass, switch_1.entity_id)
assert not switch.is_on(hass, switch_2.entity_id)
assert not switch.is_on(hass, switch_3.entity_id)
# Turn all on
await common.async_turn_on(hass)
assert switch.is_on(hass, switch_1.entity_id)
assert switch.is_on(hass, switch_2.entity_id)
assert switch.is_on(hass, switch_3.entity_id)
@pytest.mark.usefixtures("enable_custom_integrations")
async def test_switch_context(
hass: HomeAssistant,
entities,
hass_admin_user: MockUser,
) -> None:
2018-07-29 01:53:37 +01:00
"""Test that switch context works."""
assert await async_setup_component(hass, "switch", {"switch": {"platform": "test"}})
await hass.async_block_till_done()
2018-07-29 01:53:37 +01:00
state = hass.states.get("switch.ac")
assert state is not None
await hass.services.async_call(
"switch",
"toggle",
{"entity_id": state.entity_id},
True,
core.Context(user_id=hass_admin_user.id),
)
2018-07-29 01:53:37 +01:00
state2 = hass.states.get("switch.ac")
assert state2 is not None
assert state.state != state2.state
assert state2.context.user_id == hass_admin_user.id