Use switch entities instead of toggle entities in tests (#114585)

This commit is contained in:
Jan-Philipp Benecke 2024-04-02 08:25:28 +02:00 committed by GitHub
parent ab2c88353b
commit c2ffed9b2d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 43 additions and 24 deletions

View file

@ -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

View file

@ -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"}}
)

View file

@ -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"}}
)

View file

@ -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),
]

View file

@ -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(