core/tests/components/switch/test_init.py

106 lines
3.1 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""The tests for the Switch component."""
import pytest
2014-11-25 08:20:36 +00:00
from homeassistant import core
2015-09-13 05:56:49 +00:00
from homeassistant.components import switch
from homeassistant.const import CONF_PLATFORM
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
2014-11-25 08:20:36 +00:00
from . import common
from .common import MockSwitch
2014-11-25 08:20:36 +00:00
from tests.common import (
MockUser,
help_test_all,
import_and_test_deprecated_constant_enum,
setup_test_component_platform,
)
2014-11-25 08:20:36 +00:00
@pytest.fixture(autouse=True)
def entities(
hass: HomeAssistant, mock_switch_entities: list[MockSwitch]
) -> list[MockSwitch]:
"""Initialize the test switch."""
setup_test_component_platform(hass, switch.DOMAIN, mock_switch_entities)
return mock_switch_entities
2014-11-25 08:20:36 +00:00
@pytest.mark.usefixtures("enable_custom_integrations")
async def test_methods(hass: HomeAssistant, entities: list[MockSwitch]) -> None:
"""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)
2014-11-25 08:20:36 +00:00
await common.async_turn_off(hass, switch_1.entity_id)
await common.async_turn_on(hass, switch_2.entity_id)
2014-11-25 08:20:36 +00:00
assert not switch.is_on(hass, switch_1.entity_id)
assert switch.is_on(hass, switch_2.entity_id)
2014-11-25 08:20:36 +00:00
# Turn all off
await common.async_turn_off(hass)
2014-11-25 08:20:36 +00:00
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)
2014-11-25 08:20:36 +00:00
# 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:
"""Test that switch context works."""
2019-07-31 19:25:30 +00:00
assert await async_setup_component(hass, "switch", {"switch": {"platform": "test"}})
Load requirements and dependencies from manifests. Fallback to current `REQUIREMENTS` and `DEPENDENCIES` (#22717) * Load dependencies from manifests. Fallback to current DEPENDENCIES * Fix typing * Ignore typing correctly * Split out dependency processing to a new method * Fix tests * Only pull from manifest if dependencies is non empty * Inline temporary function * Fix light tests [skip ci] * Fix tests/common * Fix some mqtt tests [skip ci] * Fix tests and component manifests which have only one platform * Fix rflink tests * Fix more tests and manifests * Readability over shorthand format * Fix demo/notify tests * Load dependencies from manifests. Fallback to current DEPENDENCIES * Load requirements from manifests. Fallback to current REQUIREMENTS * Fix typing * Ignore typing correctly * Split out dependency processing to a new method * Only pull from manifest if dependencies is non empty * Inline temporary function * Fix tests and component manifests which have only one platform * Fix rflink tests * Readability over shorthand format * Clean up requirements * Use integration to resolve deps/reqs * Lint * Lint * revert a change * Revert a test change * Fix types * Fix types * Add back cache for load component * Fix test_component_not_found * Move light.test and device_tracker.test into test package instead with manifest to fix tests * Fix broken device_tracker tests * Add docstrings to __init__ * Fix all of the light tests that I broke earlier * Embed the test.switch platform to fix other tests * Embed and fix the test.imagimage_processing platform * Fix tests for nx584 * Add dependencies from platform file's DEPENDENCIES * Try to setup component when entity_platform is setting up Fix tests in helpers folder * Rewrite test_setup * Simplify * Lint * Disable demo component if running in test Temp workaround to unblock CI tests * Skip demo tests * Fix config entry test * Fix repeat test * Clarify doc * One extra guard * Fix import * Lint * Workaround google tts
2019-04-11 08:26:36 +00:00
await hass.async_block_till_done()
2019-07-31 19:25:30 +00:00
state = hass.states.get("switch.ac")
assert state is not None
2019-07-31 19:25:30 +00:00
await hass.services.async_call(
"switch",
"toggle",
{"entity_id": state.entity_id},
True,
core.Context(user_id=hass_admin_user.id),
)
2019-07-31 19:25:30 +00: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
def test_all() -> None:
"""Test module.__all__ is correctly set."""
help_test_all(switch)
@pytest.mark.parametrize(("enum"), list(switch.SwitchDeviceClass))
def test_deprecated_constants(
caplog: pytest.LogCaptureFixture,
enum: switch.SwitchDeviceClass,
) -> None:
"""Test deprecated constants."""
import_and_test_deprecated_constant_enum(
caplog, switch, enum, "DEVICE_CLASS_", "2025.1"
)