From c2ffed9b2defdc9861b9f0e51987ae7aadd12659 Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Tue, 2 Apr 2024 08:25:28 +0200 Subject: [PATCH] Use switch entities instead of toggle entities in tests (#114585) --- tests/components/conftest.py | 8 ++--- .../generic_hygrostat/test_humidifier.py | 8 ++--- .../generic_thermostat/test_climate.py | 8 ++--- tests/components/switch/common.py | 35 ++++++++++++++----- tests/components/switch/test_init.py | 8 ++--- 5 files changed, 43 insertions(+), 24 deletions(-) diff --git a/tests/components/conftest.py b/tests/components/conftest.py index 958c7fe3c86..bde8cad5ea4 100644 --- a/tests/components/conftest.py +++ b/tests/components/conftest.py @@ -9,13 +9,13 @@ import pytest from homeassistant.const import STATE_OFF, STATE_ON from homeassistant.core import HomeAssistant -from tests.common import MockToggleEntity from tests.components.conversation import MockAgent if TYPE_CHECKING: from tests.components.device_tracker.common import MockScanner from tests.components.light.common import MockLight from tests.components.sensor.common import MockSensor + from tests.components.switch.common import MockSwitch @pytest.fixture(scope="session", autouse=True) @@ -145,11 +145,11 @@ def mock_sensor_entities() -> dict[str, "MockSensor"]: @pytest.fixture -def mock_toggle_entities() -> list[MockToggleEntity]: +def mock_switch_entities() -> list["MockSwitch"]: """Return mocked toggle entities.""" - from tests.components.switch.common import get_mock_toggle_entities + from tests.components.switch.common import get_mock_switch_entities - return get_mock_toggle_entities() + return get_mock_switch_entities() @pytest.fixture diff --git a/tests/components/generic_hygrostat/test_humidifier.py b/tests/components/generic_hygrostat/test_humidifier.py index 528418b9974..ef7a2c90aa9 100644 --- a/tests/components/generic_hygrostat/test_humidifier.py +++ b/tests/components/generic_hygrostat/test_humidifier.py @@ -37,12 +37,12 @@ from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util from tests.common import ( - MockToggleEntity, assert_setup_component, async_fire_time_changed, mock_restore_cache, setup_test_component_platform, ) +from tests.components.switch.common import MockSwitch ENTITY = "humidifier.test" ENT_SENSOR = "sensor.test" @@ -129,11 +129,11 @@ async def test_humidifier_input_boolean(hass: HomeAssistant, setup_comp_1) -> No async def test_humidifier_switch( - hass: HomeAssistant, setup_comp_1, mock_toggle_entities: list[MockToggleEntity] + hass: HomeAssistant, setup_comp_1, mock_switch_entities: list[MockSwitch] ) -> None: """Test humidifier switching test switch.""" - setup_test_component_platform(hass, switch.DOMAIN, mock_toggle_entities) - switch_1 = mock_toggle_entities[1] + setup_test_component_platform(hass, switch.DOMAIN, mock_switch_entities) + switch_1 = mock_switch_entities[1] assert await async_setup_component( hass, switch.DOMAIN, {"switch": {"platform": "test"}} ) diff --git a/tests/components/generic_thermostat/test_climate.py b/tests/components/generic_thermostat/test_climate.py index f6424f894cf..8903f4b6606 100644 --- a/tests/components/generic_thermostat/test_climate.py +++ b/tests/components/generic_thermostat/test_climate.py @@ -50,7 +50,6 @@ from homeassistant.util import dt as dt_util from homeassistant.util.unit_system import METRIC_SYSTEM, US_CUSTOMARY_SYSTEM from tests.common import ( - MockToggleEntity, assert_setup_component, async_fire_time_changed, async_mock_service, @@ -59,6 +58,7 @@ from tests.common import ( setup_test_component_platform, ) from tests.components.climate import common +from tests.components.switch.common import MockSwitch ENTITY = "climate.test" ENT_SENSOR = "sensor.test" @@ -142,11 +142,11 @@ async def test_heater_input_boolean(hass: HomeAssistant, setup_comp_1) -> None: async def test_heater_switch( - hass: HomeAssistant, setup_comp_1, mock_toggle_entities: list[MockToggleEntity] + hass: HomeAssistant, setup_comp_1, mock_switch_entities: list[MockSwitch] ) -> None: """Test heater switching test switch.""" - setup_test_component_platform(hass, switch.DOMAIN, mock_toggle_entities) - switch_1 = mock_toggle_entities[1] + setup_test_component_platform(hass, switch.DOMAIN, mock_switch_entities) + switch_1 = mock_switch_entities[1] assert await async_setup_component( hass, switch.DOMAIN, {"switch": {"platform": "test"}} ) diff --git a/tests/components/switch/common.py b/tests/components/switch/common.py index cb30efe47d0..e9764d59d7c 100644 --- a/tests/components/switch/common.py +++ b/tests/components/switch/common.py @@ -4,7 +4,9 @@ All containing methods are legacy helpers that should not be used by new components. Instead call the service directly. """ -from homeassistant.components.switch import DOMAIN +from typing import Any + +from homeassistant.components.switch import DOMAIN, SwitchDeviceClass, SwitchEntity from homeassistant.const import ( ATTR_ENTITY_ID, ENTITY_MATCH_ALL, @@ -15,8 +17,6 @@ from homeassistant.const import ( ) from homeassistant.loader import bind_hass -from tests.common import MockToggleEntity - @bind_hass def turn_on(hass, entity_id=ENTITY_MATCH_ALL): @@ -42,10 +42,29 @@ async def async_turn_off(hass, entity_id=ENTITY_MATCH_ALL): await hass.services.async_call(DOMAIN, SERVICE_TURN_OFF, data, blocking=True) -def get_mock_toggle_entities() -> list[MockToggleEntity]: - """Return a list of mock toggle entities.""" +class MockSwitch(SwitchEntity): + """Mocked switch entity.""" + + _attr_device_class = SwitchDeviceClass.SWITCH + + def __init__(self, name: str | None, state: str) -> None: + """Initialize the mock switch entity.""" + self._attr_name = name + self._attr_is_on = state == STATE_ON + + def turn_on(self, **kwargs: Any) -> None: + """Turn the entity on.""" + self._attr_is_on = True + + def turn_off(self, **kwargs: Any) -> None: + """Turn the entity off.""" + self._attr_is_on = False + + +def get_mock_switch_entities() -> list[MockSwitch]: + """Return a list of mock switch entities.""" return [ - MockToggleEntity("AC", STATE_ON), - MockToggleEntity("AC", STATE_OFF), - MockToggleEntity(None, STATE_OFF), + MockSwitch("AC", STATE_ON), + MockSwitch("AC", STATE_OFF), + MockSwitch(None, STATE_OFF), ] diff --git a/tests/components/switch/test_init.py b/tests/components/switch/test_init.py index 28e9d273570..aa3e4ccce58 100644 --- a/tests/components/switch/test_init.py +++ b/tests/components/switch/test_init.py @@ -9,9 +9,9 @@ from homeassistant.core import HomeAssistant from homeassistant.setup import async_setup_component from . import common +from .common import MockSwitch from tests.common import ( - MockToggleEntity, MockUser, help_test_all, import_and_test_deprecated_constant_enum, @@ -20,10 +20,10 @@ from tests.common import ( @pytest.fixture(autouse=True) -def entities(hass: HomeAssistant, mock_toggle_entities: list[MockToggleEntity]): +def entities(hass: HomeAssistant, mock_switch_entities: list[MockSwitch]): """Initialize the test switch.""" - setup_test_component_platform(hass, switch.DOMAIN, mock_toggle_entities) - return mock_toggle_entities + setup_test_component_platform(hass, switch.DOMAIN, mock_switch_entities) + return mock_switch_entities async def test_methods(