Use new setup_test_component_platform helper instead of test fixture for light platform (#114200)

This commit is contained in:
Jan-Philipp Benecke 2024-03-26 00:17:16 +01:00 committed by GitHub
parent 6bb4e7d62c
commit 9580adfde9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 96 additions and 173 deletions

View file

@ -9,7 +9,7 @@ import pytest
from homeassistant.const import STATE_OFF, STATE_ON from homeassistant.const import STATE_OFF, STATE_ON
if TYPE_CHECKING: if TYPE_CHECKING:
from tests.components.light.common import MockLight, SetupLightPlatformCallable from tests.components.light.common import MockLight
@pytest.fixture(scope="session", autouse=True) @pytest.fixture(scope="session", autouse=True)
@ -118,11 +118,3 @@ def mock_light_entities() -> list["MockLight"]:
MockLight("Ceiling", STATE_OFF), MockLight("Ceiling", STATE_OFF),
MockLight(None, STATE_OFF), MockLight(None, STATE_OFF),
] ]
@pytest.fixture
def setup_light_platform() -> "SetupLightPlatformCallable":
"""Return a callable to set up the mock light entity component."""
from tests.components.light.common import setup_light_platform
return setup_light_platform

View file

@ -24,8 +24,13 @@ from homeassistant.setup import async_setup_component
from . import expose_entity, expose_new from . import expose_entity, expose_new
from tests.common import MockConfigEntry, MockUser, async_mock_service from tests.common import (
from tests.components.light.common import MockLight, SetupLightPlatformCallable MockConfigEntry,
MockUser,
async_mock_service,
setup_test_component_platform,
)
from tests.components.light.common import MockLight
from tests.typing import ClientSessionGenerator, WebSocketGenerator from tests.typing import ClientSessionGenerator, WebSocketGenerator
AGENT_ID_OPTIONS = [None, conversation.HOME_ASSISTANT_AGENT] AGENT_ID_OPTIONS = [None, conversation.HOME_ASSISTANT_AGENT]
@ -257,7 +262,6 @@ async def test_http_processing_intent_entity_renamed(
hass_client: ClientSessionGenerator, hass_client: ClientSessionGenerator,
hass_admin_user: MockUser, hass_admin_user: MockUser,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
setup_light_platform: SetupLightPlatformCallable,
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,
) -> None: ) -> None:
"""Test processing intent via HTTP API with entities renamed later. """Test processing intent via HTTP API with entities renamed later.
@ -268,7 +272,7 @@ async def test_http_processing_intent_entity_renamed(
entity = MockLight("kitchen light", "on") entity = MockLight("kitchen light", "on")
entity._attr_unique_id = "1234" entity._attr_unique_id = "1234"
entity.entity_id = "light.kitchen" entity.entity_id = "light.kitchen"
setup_light_platform(hass, [entity]) setup_test_component_platform(hass, LIGHT_DOMAIN, [entity])
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -346,7 +350,6 @@ async def test_http_processing_intent_entity_exposed(
hass_client: ClientSessionGenerator, hass_client: ClientSessionGenerator,
hass_admin_user: MockUser, hass_admin_user: MockUser,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
setup_light_platform: SetupLightPlatformCallable,
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,
) -> None: ) -> None:
"""Test processing intent via HTTP API with manual expose. """Test processing intent via HTTP API with manual expose.
@ -357,7 +360,7 @@ async def test_http_processing_intent_entity_exposed(
entity = MockLight("kitchen light", "on") entity = MockLight("kitchen light", "on")
entity._attr_unique_id = "1234" entity._attr_unique_id = "1234"
entity.entity_id = "light.kitchen" entity.entity_id = "light.kitchen"
setup_light_platform(hass, [entity]) setup_test_component_platform(hass, LIGHT_DOMAIN, [entity])
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -449,7 +452,6 @@ async def test_http_processing_intent_conversion_not_expose_new(
hass_client: ClientSessionGenerator, hass_client: ClientSessionGenerator,
hass_admin_user: MockUser, hass_admin_user: MockUser,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
setup_light_platform: SetupLightPlatformCallable,
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,
) -> None: ) -> None:
"""Test processing intent via HTTP API when not exposing new entities.""" """Test processing intent via HTTP API when not exposing new entities."""
@ -459,7 +461,7 @@ async def test_http_processing_intent_conversion_not_expose_new(
entity = MockLight("kitchen light", "on") entity = MockLight("kitchen light", "on")
entity._attr_unique_id = "1234" entity._attr_unique_id = "1234"
entity.entity_id = "light.kitchen" entity.entity_id = "light.kitchen"
setup_light_platform(hass, [entity]) setup_test_component_platform(hass, LIGHT_DOMAIN, [entity])
assert await async_setup_component( assert await async_setup_component(
hass, hass,

View file

@ -44,8 +44,12 @@ from homeassistant.core import Event, HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.common import async_capture_events, get_fixture_path from tests.common import (
from tests.components.light.common import MockLight, SetupLightPlatformCallable async_capture_events,
get_fixture_path,
setup_test_component_platform,
)
from tests.components.light.common import MockLight
async def test_default_state( async def test_default_state(
@ -261,15 +265,13 @@ async def test_state_reporting_all(hass: HomeAssistant) -> None:
assert hass.states.get("light.light_group").state == STATE_UNAVAILABLE assert hass.states.get("light.light_group").state == STATE_UNAVAILABLE
async def test_brightness( async def test_brightness(hass: HomeAssistant) -> None:
hass: HomeAssistant, setup_light_platform: SetupLightPlatformCallable
) -> None:
"""Test brightness reporting.""" """Test brightness reporting."""
entities = [ entities = [
MockLight("test1", STATE_ON), MockLight("test1", STATE_ON),
MockLight("test2", STATE_OFF), MockLight("test2", STATE_OFF),
] ]
setup_light_platform(hass, entities) setup_test_component_platform(hass, LIGHT_DOMAIN, entities)
entity0 = entities[0] entity0 = entities[0]
entity0.supported_color_modes = {ColorMode.BRIGHTNESS} entity0.supported_color_modes = {ColorMode.BRIGHTNESS}
@ -334,15 +336,13 @@ async def test_brightness(
assert state.attributes[ATTR_SUPPORTED_COLOR_MODES] == ["brightness"] assert state.attributes[ATTR_SUPPORTED_COLOR_MODES] == ["brightness"]
async def test_color_hs( async def test_color_hs(hass: HomeAssistant) -> None:
hass: HomeAssistant, setup_light_platform: SetupLightPlatformCallable
) -> None:
"""Test hs color reporting.""" """Test hs color reporting."""
entities = [ entities = [
MockLight("test1", STATE_ON), MockLight("test1", STATE_ON),
MockLight("test2", STATE_OFF), MockLight("test2", STATE_OFF),
] ]
setup_light_platform(hass, entities) setup_test_component_platform(hass, LIGHT_DOMAIN, entities)
entity0 = entities[0] entity0 = entities[0]
entity0.supported_color_modes = {ColorMode.HS} entity0.supported_color_modes = {ColorMode.HS}
@ -406,15 +406,13 @@ async def test_color_hs(
assert state.attributes[ATTR_SUPPORTED_FEATURES] == 0 assert state.attributes[ATTR_SUPPORTED_FEATURES] == 0
async def test_color_rgb( async def test_color_rgb(hass: HomeAssistant) -> None:
hass: HomeAssistant, setup_light_platform: SetupLightPlatformCallable
) -> None:
"""Test rgbw color reporting.""" """Test rgbw color reporting."""
entities = [ entities = [
MockLight("test1", STATE_ON), MockLight("test1", STATE_ON),
MockLight("test2", STATE_OFF), MockLight("test2", STATE_OFF),
] ]
setup_light_platform(hass, entities) setup_test_component_platform(hass, LIGHT_DOMAIN, entities)
entity0 = entities[0] entity0 = entities[0]
entity0.supported_color_modes = {ColorMode.RGB} entity0.supported_color_modes = {ColorMode.RGB}
@ -480,15 +478,13 @@ async def test_color_rgb(
assert state.attributes[ATTR_SUPPORTED_FEATURES] == 0 assert state.attributes[ATTR_SUPPORTED_FEATURES] == 0
async def test_color_rgbw( async def test_color_rgbw(hass: HomeAssistant) -> None:
hass: HomeAssistant, setup_light_platform: SetupLightPlatformCallable
) -> None:
"""Test rgbw color reporting.""" """Test rgbw color reporting."""
entities = [ entities = [
MockLight("test1", STATE_ON), MockLight("test1", STATE_ON),
MockLight("test2", STATE_OFF), MockLight("test2", STATE_OFF),
] ]
setup_light_platform(hass, entities) setup_test_component_platform(hass, LIGHT_DOMAIN, entities)
entity0 = entities[0] entity0 = entities[0]
entity0.supported_color_modes = {ColorMode.RGBW} entity0.supported_color_modes = {ColorMode.RGBW}
@ -554,15 +550,13 @@ async def test_color_rgbw(
assert state.attributes[ATTR_SUPPORTED_FEATURES] == 0 assert state.attributes[ATTR_SUPPORTED_FEATURES] == 0
async def test_color_rgbww( async def test_color_rgbww(hass: HomeAssistant) -> None:
hass: HomeAssistant, setup_light_platform: SetupLightPlatformCallable
) -> None:
"""Test rgbww color reporting.""" """Test rgbww color reporting."""
entities = [ entities = [
MockLight("test1", STATE_ON), MockLight("test1", STATE_ON),
MockLight("test2", STATE_OFF), MockLight("test2", STATE_OFF),
] ]
setup_light_platform(hass, entities) setup_test_component_platform(hass, LIGHT_DOMAIN, entities)
entity0 = entities[0] entity0 = entities[0]
entity0.supported_color_modes = {ColorMode.RGBWW} entity0.supported_color_modes = {ColorMode.RGBWW}
@ -628,15 +622,13 @@ async def test_color_rgbww(
assert state.attributes[ATTR_SUPPORTED_FEATURES] == 0 assert state.attributes[ATTR_SUPPORTED_FEATURES] == 0
async def test_white( async def test_white(hass: HomeAssistant) -> None:
hass: HomeAssistant, setup_light_platform: SetupLightPlatformCallable
) -> None:
"""Test white reporting.""" """Test white reporting."""
entities = [ entities = [
MockLight("test1", STATE_ON), MockLight("test1", STATE_ON),
MockLight("test2", STATE_ON), MockLight("test2", STATE_ON),
] ]
setup_light_platform(hass, entities) setup_test_component_platform(hass, LIGHT_DOMAIN, entities)
entity0 = entities[0] entity0 = entities[0]
entity0.supported_color_modes = {ColorMode.HS, ColorMode.WHITE} entity0.supported_color_modes = {ColorMode.HS, ColorMode.WHITE}
@ -687,15 +679,13 @@ async def test_white(
assert state.attributes[ATTR_SUPPORTED_COLOR_MODES] == ["hs", "white"] assert state.attributes[ATTR_SUPPORTED_COLOR_MODES] == ["hs", "white"]
async def test_color_temp( async def test_color_temp(hass: HomeAssistant) -> None:
hass: HomeAssistant, setup_light_platform: SetupLightPlatformCallable
) -> None:
"""Test color temp reporting.""" """Test color temp reporting."""
entities = [ entities = [
MockLight("test1", STATE_ON), MockLight("test1", STATE_ON),
MockLight("test2", STATE_OFF), MockLight("test2", STATE_OFF),
] ]
setup_light_platform(hass, entities) setup_test_component_platform(hass, LIGHT_DOMAIN, entities)
entity0 = entities[0] entity0 = entities[0]
entity0.supported_color_modes = {ColorMode.COLOR_TEMP} entity0.supported_color_modes = {ColorMode.COLOR_TEMP}
@ -758,16 +748,14 @@ async def test_color_temp(
assert state.attributes[ATTR_SUPPORTED_COLOR_MODES] == ["color_temp"] assert state.attributes[ATTR_SUPPORTED_COLOR_MODES] == ["color_temp"]
async def test_emulated_color_temp_group( async def test_emulated_color_temp_group(hass: HomeAssistant) -> None:
hass: HomeAssistant, setup_light_platform: SetupLightPlatformCallable
) -> None:
"""Test emulated color temperature in a group.""" """Test emulated color temperature in a group."""
entities = [ entities = [
MockLight("test1", STATE_ON), MockLight("test1", STATE_ON),
MockLight("test2", STATE_OFF), MockLight("test2", STATE_OFF),
MockLight("test3", STATE_OFF), MockLight("test3", STATE_OFF),
] ]
setup_light_platform(hass, entities) setup_test_component_platform(hass, LIGHT_DOMAIN, entities)
entity0 = entities[0] entity0 = entities[0]
entity0.supported_color_modes = {ColorMode.COLOR_TEMP} entity0.supported_color_modes = {ColorMode.COLOR_TEMP}
@ -824,9 +812,7 @@ async def test_emulated_color_temp_group(
assert state.attributes[ATTR_HS_COLOR] == (27.001, 19.243) assert state.attributes[ATTR_HS_COLOR] == (27.001, 19.243)
async def test_min_max_mireds( async def test_min_max_mireds(hass: HomeAssistant) -> None:
hass: HomeAssistant, setup_light_platform: SetupLightPlatformCallable
) -> None:
"""Test min/max mireds reporting. """Test min/max mireds reporting.
min/max mireds is reported both when light is on and off min/max mireds is reported both when light is on and off
@ -835,7 +821,7 @@ async def test_min_max_mireds(
MockLight("test1", STATE_ON), MockLight("test1", STATE_ON),
MockLight("test2", STATE_OFF), MockLight("test2", STATE_OFF),
] ]
setup_light_platform(hass, entities) setup_test_component_platform(hass, LIGHT_DOMAIN, entities)
entity0 = entities[0] entity0 = entities[0]
entity0.supported_color_modes = {ColorMode.COLOR_TEMP} entity0.supported_color_modes = {ColorMode.COLOR_TEMP}
@ -1005,16 +991,14 @@ async def test_effect(hass: HomeAssistant) -> None:
assert state.attributes[ATTR_EFFECT] == "Random" assert state.attributes[ATTR_EFFECT] == "Random"
async def test_supported_color_modes( async def test_supported_color_modes(hass: HomeAssistant) -> None:
hass: HomeAssistant, setup_light_platform: SetupLightPlatformCallable
) -> None:
"""Test supported_color_modes reporting.""" """Test supported_color_modes reporting."""
entities = [ entities = [
MockLight("test1", STATE_ON), MockLight("test1", STATE_ON),
MockLight("test2", STATE_OFF), MockLight("test2", STATE_OFF),
MockLight("test3", STATE_OFF), MockLight("test3", STATE_OFF),
] ]
setup_light_platform(hass, entities) setup_test_component_platform(hass, LIGHT_DOMAIN, entities)
entity0 = entities[0] entity0 = entities[0]
entity0.supported_color_modes = {ColorMode.COLOR_TEMP, ColorMode.HS} entity0.supported_color_modes = {ColorMode.COLOR_TEMP, ColorMode.HS}
@ -1055,16 +1039,14 @@ async def test_supported_color_modes(
} }
async def test_color_mode( async def test_color_mode(hass: HomeAssistant) -> None:
hass: HomeAssistant, setup_light_platform: SetupLightPlatformCallable
) -> None:
"""Test color_mode reporting.""" """Test color_mode reporting."""
entities = [ entities = [
MockLight("test1", STATE_ON), MockLight("test1", STATE_ON),
MockLight("test2", STATE_OFF), MockLight("test2", STATE_OFF),
MockLight("test3", STATE_OFF), MockLight("test3", STATE_OFF),
] ]
setup_light_platform(hass, entities) setup_test_component_platform(hass, LIGHT_DOMAIN, entities)
entity0 = entities[0] entity0 = entities[0]
entity0.supported_color_modes = {ColorMode.COLOR_TEMP, ColorMode.HS} entity0.supported_color_modes = {ColorMode.COLOR_TEMP, ColorMode.HS}
@ -1130,9 +1112,7 @@ async def test_color_mode(
assert state.attributes[ATTR_COLOR_MODE] == ColorMode.HS assert state.attributes[ATTR_COLOR_MODE] == ColorMode.HS
async def test_color_mode2( async def test_color_mode2(hass: HomeAssistant) -> None:
hass: HomeAssistant, setup_light_platform: SetupLightPlatformCallable
) -> None:
"""Test onoff color_mode and brightness are given lowest priority.""" """Test onoff color_mode and brightness are given lowest priority."""
entities = [ entities = [
MockLight("test1", STATE_ON), MockLight("test1", STATE_ON),
@ -1142,7 +1122,7 @@ async def test_color_mode2(
MockLight("test5", STATE_ON), MockLight("test5", STATE_ON),
MockLight("test6", STATE_ON), MockLight("test6", STATE_ON),
] ]
setup_light_platform(hass, entities) setup_test_component_platform(hass, LIGHT_DOMAIN, entities)
entity = entities[0] entity = entities[0]
entity.supported_color_modes = {ColorMode.COLOR_TEMP} entity.supported_color_modes = {ColorMode.COLOR_TEMP}
@ -1256,7 +1236,6 @@ async def test_supported_features(hass: HomeAssistant) -> None:
@pytest.mark.parametrize("supported_color_modes", [ColorMode.HS, ColorMode.RGB]) @pytest.mark.parametrize("supported_color_modes", [ColorMode.HS, ColorMode.RGB])
async def test_service_calls( async def test_service_calls(
hass: HomeAssistant, hass: HomeAssistant,
setup_light_platform: SetupLightPlatformCallable,
supported_color_modes, supported_color_modes,
) -> None: ) -> None:
"""Test service calls.""" """Test service calls."""
@ -1265,7 +1244,7 @@ async def test_service_calls(
MockLight("ceiling_lights", STATE_OFF), MockLight("ceiling_lights", STATE_OFF),
MockLight("kitchen_lights", STATE_OFF), MockLight("kitchen_lights", STATE_OFF),
] ]
setup_light_platform(hass, entities) setup_test_component_platform(hass, LIGHT_DOMAIN, entities)
entity0 = entities[0] entity0 = entities[0]
entity0.supported_color_modes = {supported_color_modes} entity0.supported_color_modes = {supported_color_modes}

View file

@ -4,7 +4,6 @@ All containing methods are legacy helpers that should not be used by new
components. Instead call the service directly. components. Instead call the service directly.
""" """
from collections.abc import Callable
from homeassistant.components.light import ( from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_BRIGHTNESS,
@ -33,12 +32,9 @@ from homeassistant.const import (
SERVICE_TURN_OFF, SERVICE_TURN_OFF,
SERVICE_TURN_ON, SERVICE_TURN_ON,
) )
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.loader import bind_hass from homeassistant.loader import bind_hass
from tests.common import MockPlatform, MockToggleEntity, mock_platform from tests.common import MockToggleEntity
@bind_hass @bind_hass
@ -288,25 +284,3 @@ class MockLight(MockToggleEntity, LightEntity):
setattr(self, "brightness", value) setattr(self, "brightness", value)
if key in TURN_ON_ARG_TO_COLOR_MODE: if key in TURN_ON_ARG_TO_COLOR_MODE:
self._attr_color_mode = TURN_ON_ARG_TO_COLOR_MODE[key] self._attr_color_mode = TURN_ON_ARG_TO_COLOR_MODE[key]
SetupLightPlatformCallable = Callable[[HomeAssistant, list[MockLight]], None]
def setup_light_platform(hass: HomeAssistant, entities: list[MockLight]) -> None:
"""Set up the mock light entity platform."""
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up test light platform."""
async_add_entities(entities)
mock_platform(
hass,
f"test.{DOMAIN}",
MockPlatform(async_setup_platform=async_setup_platform),
)

View file

@ -21,8 +21,9 @@ from tests.common import (
async_get_device_automation_capabilities, async_get_device_automation_capabilities,
async_get_device_automations, async_get_device_automations,
async_mock_service, async_mock_service,
setup_test_component_platform,
) )
from tests.components.light.common import MockLight, SetupLightPlatformCallable from tests.components.light.common import MockLight
@pytest.fixture(autouse=True, name="stub_blueprint_populate") @pytest.fixture(autouse=True, name="stub_blueprint_populate")
@ -324,7 +325,6 @@ async def test_if_fires_on_for_condition(
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
calls, calls,
setup_light_platform: SetupLightPlatformCallable,
mock_light_entities: list[MockLight], mock_light_entities: list[MockLight],
) -> None: ) -> None:
"""Test for firing if condition is on with delay.""" """Test for firing if condition is on with delay."""
@ -344,7 +344,7 @@ async def test_if_fires_on_for_condition(
point2 = point1 + timedelta(seconds=10) point2 = point1 + timedelta(seconds=10)
point3 = point2 + timedelta(seconds=10) point3 = point2 + timedelta(seconds=10)
setup_light_platform(hass, mock_light_entities) setup_test_component_platform(hass, DOMAIN, mock_light_entities)
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()

View file

@ -22,8 +22,13 @@ from homeassistant.exceptions import HomeAssistantError, Unauthorized
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
import homeassistant.util.color as color_util import homeassistant.util.color as color_util
from tests.common import MockEntityPlatform, MockUser, async_mock_service from tests.common import (
from tests.components.light.common import MockLight, SetupLightPlatformCallable MockEntityPlatform,
MockUser,
async_mock_service,
setup_test_component_platform,
)
from tests.components.light.common import MockLight
orig_Profiles = light.Profiles orig_Profiles = light.Profiles
@ -111,11 +116,10 @@ async def test_methods(hass: HomeAssistant) -> None:
async def test_services( async def test_services(
hass: HomeAssistant, hass: HomeAssistant,
mock_light_profiles, mock_light_profiles,
setup_light_platform: SetupLightPlatformCallable,
mock_light_entities: list[MockLight], mock_light_entities: list[MockLight],
) -> None: ) -> None:
"""Test the provided services.""" """Test the provided services."""
setup_light_platform(hass, mock_light_entities) setup_test_component_platform(hass, light.DOMAIN, mock_light_entities)
assert await async_setup_component( assert await async_setup_component(
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}} hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
@ -512,11 +516,10 @@ async def test_light_profiles(
profile_name, profile_name,
expected_data, expected_data,
last_call, last_call,
setup_light_platform: SetupLightPlatformCallable,
mock_light_entities: list[MockLight], mock_light_entities: list[MockLight],
) -> None: ) -> None:
"""Test light profiles.""" """Test light profiles."""
setup_light_platform(hass, mock_light_entities) setup_test_component_platform(hass, light.DOMAIN, mock_light_entities)
profile_mock_data = { profile_mock_data = {
"test": (0.4, 0.6, 100, 0), "test": (0.4, 0.6, 100, 0),
@ -561,11 +564,10 @@ async def test_light_profiles(
async def test_default_profiles_group( async def test_default_profiles_group(
hass: HomeAssistant, hass: HomeAssistant,
mock_light_profiles, mock_light_profiles,
setup_light_platform: SetupLightPlatformCallable,
mock_light_entities: list[MockLight], mock_light_entities: list[MockLight],
) -> None: ) -> None:
"""Test default turn-on light profile for all lights.""" """Test default turn-on light profile for all lights."""
setup_light_platform(hass, mock_light_entities) setup_test_component_platform(hass, light.DOMAIN, mock_light_entities)
assert await async_setup_component( assert await async_setup_component(
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}} hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
@ -787,11 +789,10 @@ async def test_default_profiles_light(
extra_call_params, extra_call_params,
expected_params_state_was_off, expected_params_state_was_off,
expected_params_state_was_on, expected_params_state_was_on,
setup_light_platform: SetupLightPlatformCallable,
mock_light_entities: list[MockLight], mock_light_entities: list[MockLight],
) -> None: ) -> None:
"""Test default turn-on light profile for a specific light.""" """Test default turn-on light profile for a specific light."""
setup_light_platform(hass, mock_light_entities) setup_test_component_platform(hass, light.DOMAIN, mock_light_entities)
assert await async_setup_component( assert await async_setup_component(
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}} hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
@ -857,11 +858,10 @@ async def test_default_profiles_light(
async def test_light_context( async def test_light_context(
hass: HomeAssistant, hass: HomeAssistant,
hass_admin_user: MockUser, hass_admin_user: MockUser,
setup_light_platform: SetupLightPlatformCallable,
mock_light_entities: list[MockLight], mock_light_entities: list[MockLight],
) -> None: ) -> None:
"""Test that light context works.""" """Test that light context works."""
setup_light_platform(hass, mock_light_entities) setup_test_component_platform(hass, light.DOMAIN, mock_light_entities)
assert await async_setup_component(hass, "light", {"light": {"platform": "test"}}) assert await async_setup_component(hass, "light", {"light": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -886,11 +886,10 @@ async def test_light_context(
async def test_light_turn_on_auth( async def test_light_turn_on_auth(
hass: HomeAssistant, hass: HomeAssistant,
hass_read_only_user: MockUser, hass_read_only_user: MockUser,
setup_light_platform: SetupLightPlatformCallable,
mock_light_entities: list[MockLight], mock_light_entities: list[MockLight],
) -> None: ) -> None:
"""Test that light context works.""" """Test that light context works."""
setup_light_platform(hass, mock_light_entities) setup_test_component_platform(hass, light.DOMAIN, mock_light_entities)
assert await async_setup_component(hass, "light", {"light": {"platform": "test"}}) assert await async_setup_component(hass, "light", {"light": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -910,16 +909,14 @@ async def test_light_turn_on_auth(
) )
async def test_light_brightness_step( async def test_light_brightness_step(hass: HomeAssistant) -> None:
hass: HomeAssistant, setup_light_platform: SetupLightPlatformCallable
) -> None:
"""Test that light context works.""" """Test that light context works."""
entities = [ entities = [
MockLight("Test_0", STATE_ON), MockLight("Test_0", STATE_ON),
MockLight("Test_1", STATE_ON), MockLight("Test_1", STATE_ON),
] ]
setup_light_platform(hass, entities) setup_test_component_platform(hass, light.DOMAIN, entities)
entity0 = entities[0] entity0 = entities[0]
entity0.supported_features = light.SUPPORT_BRIGHTNESS entity0.supported_features = light.SUPPORT_BRIGHTNESS
@ -986,11 +983,10 @@ async def test_light_brightness_step(
async def test_light_brightness_pct_conversion( async def test_light_brightness_pct_conversion(
hass: HomeAssistant, hass: HomeAssistant,
enable_custom_integrations: None, enable_custom_integrations: None,
setup_light_platform: SetupLightPlatformCallable,
mock_light_entities: list[MockLight], mock_light_entities: list[MockLight],
) -> None: ) -> None:
"""Test that light brightness percent conversion.""" """Test that light brightness percent conversion."""
setup_light_platform(hass, mock_light_entities) setup_test_component_platform(hass, light.DOMAIN, mock_light_entities)
entity = mock_light_entities[0] entity = mock_light_entities[0]
entity.supported_features = light.SUPPORT_BRIGHTNESS entity.supported_features = light.SUPPORT_BRIGHTNESS
@ -1147,7 +1143,7 @@ invalid_no_brightness_no_color_no_transition,,,
@pytest.mark.parametrize("light_state", [STATE_ON, STATE_OFF]) @pytest.mark.parametrize("light_state", [STATE_ON, STATE_OFF])
async def test_light_backwards_compatibility_supported_color_modes( async def test_light_backwards_compatibility_supported_color_modes(
hass: HomeAssistant, light_state, setup_light_platform: SetupLightPlatformCallable hass: HomeAssistant, light_state
) -> None: ) -> None:
"""Test supported_color_modes if not implemented by the entity.""" """Test supported_color_modes if not implemented by the entity."""
entities = [ entities = [
@ -1186,7 +1182,7 @@ async def test_light_backwards_compatibility_supported_color_modes(
entity4.supported_color_modes = None entity4.supported_color_modes = None
entity4.color_mode = None entity4.color_mode = None
setup_light_platform(hass, entities) setup_test_component_platform(hass, light.DOMAIN, entities)
assert await async_setup_component(hass, "light", {"light": {"platform": "test"}}) assert await async_setup_component(hass, "light", {"light": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -1230,9 +1226,7 @@ async def test_light_backwards_compatibility_supported_color_modes(
assert state.attributes["color_mode"] == light.ColorMode.UNKNOWN assert state.attributes["color_mode"] == light.ColorMode.UNKNOWN
async def test_light_backwards_compatibility_color_mode( async def test_light_backwards_compatibility_color_mode(hass: HomeAssistant) -> None:
hass: HomeAssistant, setup_light_platform: SetupLightPlatformCallable
) -> None:
"""Test color_mode if not implemented by the entity.""" """Test color_mode if not implemented by the entity."""
entities = [ entities = [
MockLight("Test_0", STATE_ON), MockLight("Test_0", STATE_ON),
@ -1275,7 +1269,7 @@ async def test_light_backwards_compatibility_color_mode(
entity4.hs_color = (240, 100) entity4.hs_color = (240, 100)
entity4.color_temp_kelvin = 10000 entity4.color_temp_kelvin = 10000
setup_light_platform(hass, entities) setup_test_component_platform(hass, light.DOMAIN, entities)
assert await async_setup_component(hass, "light", {"light": {"platform": "test"}}) assert await async_setup_component(hass, "light", {"light": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -1308,14 +1302,12 @@ async def test_light_backwards_compatibility_color_mode(
assert state.attributes["color_mode"] == light.ColorMode.HS assert state.attributes["color_mode"] == light.ColorMode.HS
async def test_light_service_call_rgbw( async def test_light_service_call_rgbw(hass: HomeAssistant) -> None:
hass: HomeAssistant, setup_light_platform: SetupLightPlatformCallable
) -> None:
"""Test rgbw functionality in service calls.""" """Test rgbw functionality in service calls."""
entity0 = MockLight("Test_rgbw", STATE_ON) entity0 = MockLight("Test_rgbw", STATE_ON)
entity0.supported_color_modes = {light.ColorMode.RGBW} entity0.supported_color_modes = {light.ColorMode.RGBW}
setup_light_platform(hass, [entity0]) setup_test_component_platform(hass, light.DOMAIN, [entity0])
assert await async_setup_component(hass, "light", {"light": {"platform": "test"}}) assert await async_setup_component(hass, "light", {"light": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -1338,9 +1330,7 @@ async def test_light_service_call_rgbw(
assert data == {"brightness": 255, "rgbw_color": (10, 20, 30, 40)} assert data == {"brightness": 255, "rgbw_color": (10, 20, 30, 40)}
async def test_light_state_off( async def test_light_state_off(hass: HomeAssistant) -> None:
hass: HomeAssistant, setup_light_platform: SetupLightPlatformCallable
) -> None:
"""Test rgbw color conversion in state updates.""" """Test rgbw color conversion in state updates."""
entities = [ entities = [
MockLight("Test_onoff", STATE_OFF), MockLight("Test_onoff", STATE_OFF),
@ -1348,7 +1338,7 @@ async def test_light_state_off(
MockLight("Test_ct", STATE_OFF), MockLight("Test_ct", STATE_OFF),
MockLight("Test_rgbw", STATE_OFF), MockLight("Test_rgbw", STATE_OFF),
] ]
setup_light_platform(hass, entities) setup_test_component_platform(hass, light.DOMAIN, entities)
entity0 = entities[0] entity0 = entities[0]
entity0.supported_color_modes = {light.ColorMode.ONOFF} entity0.supported_color_modes = {light.ColorMode.ONOFF}
@ -1411,12 +1401,10 @@ async def test_light_state_off(
} }
async def test_light_state_rgbw( async def test_light_state_rgbw(hass: HomeAssistant) -> None:
hass: HomeAssistant, setup_light_platform: SetupLightPlatformCallable
) -> None:
"""Test rgbw color conversion in state updates.""" """Test rgbw color conversion in state updates."""
entity0 = MockLight("Test_rgbw", STATE_ON) entity0 = MockLight("Test_rgbw", STATE_ON)
setup_light_platform(hass, [entity0]) setup_test_component_platform(hass, light.DOMAIN, [entity0])
entity0.brightness = 255 entity0.brightness = 255
entity0.supported_color_modes = {light.ColorMode.RGBW} entity0.supported_color_modes = {light.ColorMode.RGBW}
@ -1444,12 +1432,10 @@ async def test_light_state_rgbw(
} }
async def test_light_state_rgbww( async def test_light_state_rgbww(hass: HomeAssistant) -> None:
hass: HomeAssistant, setup_light_platform: SetupLightPlatformCallable
) -> None:
"""Test rgbww color conversion in state updates.""" """Test rgbww color conversion in state updates."""
entity0 = MockLight("Test_rgbww", STATE_ON) entity0 = MockLight("Test_rgbww", STATE_ON)
setup_light_platform(hass, [entity0]) setup_test_component_platform(hass, light.DOMAIN, [entity0])
entity0.supported_color_modes = {light.ColorMode.RGBWW} entity0.supported_color_modes = {light.ColorMode.RGBWW}
entity0.color_mode = light.ColorMode.RGBWW entity0.color_mode = light.ColorMode.RGBWW
@ -1477,9 +1463,7 @@ async def test_light_state_rgbww(
} }
async def test_light_service_call_color_conversion( async def test_light_service_call_color_conversion(hass: HomeAssistant) -> None:
hass: HomeAssistant, setup_light_platform: SetupLightPlatformCallable
) -> None:
"""Test color conversion in service calls.""" """Test color conversion in service calls."""
entities = [ entities = [
MockLight("Test_hs", STATE_ON), MockLight("Test_hs", STATE_ON),
@ -1491,7 +1475,7 @@ async def test_light_service_call_color_conversion(
MockLight("Test_rgbww", STATE_ON), MockLight("Test_rgbww", STATE_ON),
MockLight("Test_temperature", STATE_ON), MockLight("Test_temperature", STATE_ON),
] ]
setup_light_platform(hass, entities) setup_test_component_platform(hass, light.DOMAIN, entities)
entity0 = entities[0] entity0 = entities[0]
entity0.supported_color_modes = {light.ColorMode.HS} entity0.supported_color_modes = {light.ColorMode.HS}
@ -1923,7 +1907,7 @@ async def test_light_service_call_color_conversion(
async def test_light_service_call_color_conversion_named_tuple( async def test_light_service_call_color_conversion_named_tuple(
hass: HomeAssistant, setup_light_platform: SetupLightPlatformCallable hass: HomeAssistant,
) -> None: ) -> None:
"""Test a named tuple (RGBColor) is handled correctly.""" """Test a named tuple (RGBColor) is handled correctly."""
entities = [ entities = [
@ -1935,7 +1919,7 @@ async def test_light_service_call_color_conversion_named_tuple(
MockLight("Test_rgbw", STATE_ON), MockLight("Test_rgbw", STATE_ON),
MockLight("Test_rgbww", STATE_ON), MockLight("Test_rgbww", STATE_ON),
] ]
setup_light_platform(hass, entities) setup_test_component_platform(hass, light.DOMAIN, entities)
entity0 = entities[0] entity0 = entities[0]
entity0.supported_color_modes = {light.ColorMode.HS} entity0.supported_color_modes = {light.ColorMode.HS}
@ -2002,16 +1986,14 @@ async def test_light_service_call_color_conversion_named_tuple(
assert data == {"brightness": 64, "rgbww_color": (128, 0, 0, 0, 0)} assert data == {"brightness": 64, "rgbww_color": (128, 0, 0, 0, 0)}
async def test_light_service_call_color_temp_emulation( async def test_light_service_call_color_temp_emulation(hass: HomeAssistant) -> None:
hass: HomeAssistant, setup_light_platform: SetupLightPlatformCallable
) -> None:
"""Test color conversion in service calls.""" """Test color conversion in service calls."""
entities = [ entities = [
MockLight("Test_hs_ct", STATE_ON), MockLight("Test_hs_ct", STATE_ON),
MockLight("Test_hs", STATE_ON), MockLight("Test_hs", STATE_ON),
MockLight("Test_hs_white", STATE_ON), MockLight("Test_hs_white", STATE_ON),
] ]
setup_light_platform(hass, entities) setup_test_component_platform(hass, light.DOMAIN, entities)
entity0 = entities[0] entity0 = entities[0]
entity0.supported_color_modes = {light.ColorMode.COLOR_TEMP, light.ColorMode.HS} entity0.supported_color_modes = {light.ColorMode.COLOR_TEMP, light.ColorMode.HS}
@ -2062,15 +2044,13 @@ async def test_light_service_call_color_temp_emulation(
assert data == {"brightness": 255, "hs_color": (27.001, 19.243)} assert data == {"brightness": 255, "hs_color": (27.001, 19.243)}
async def test_light_service_call_color_temp_conversion( async def test_light_service_call_color_temp_conversion(hass: HomeAssistant) -> None:
hass: HomeAssistant, setup_light_platform: SetupLightPlatformCallable
) -> None:
"""Test color temp conversion in service calls.""" """Test color temp conversion in service calls."""
entities = [ entities = [
MockLight("Test_rgbww_ct", STATE_ON), MockLight("Test_rgbww_ct", STATE_ON),
MockLight("Test_rgbww", STATE_ON), MockLight("Test_rgbww", STATE_ON),
] ]
setup_light_platform(hass, entities) setup_test_component_platform(hass, light.DOMAIN, entities)
entity0 = entities[0] entity0 = entities[0]
entity0.supported_color_modes = { entity0.supported_color_modes = {
@ -2195,15 +2175,13 @@ async def test_light_service_call_color_temp_conversion(
assert data == {"brightness": 255, "rgbww_color": (0, 0, 0, 66, 189)} assert data == {"brightness": 255, "rgbww_color": (0, 0, 0, 66, 189)}
async def test_light_mired_color_temp_conversion( async def test_light_mired_color_temp_conversion(hass: HomeAssistant) -> None:
hass: HomeAssistant, setup_light_platform: SetupLightPlatformCallable
) -> None:
"""Test color temp conversion from K to legacy mired.""" """Test color temp conversion from K to legacy mired."""
entities = [ entities = [
MockLight("Test_rgbww_ct", STATE_ON), MockLight("Test_rgbww_ct", STATE_ON),
MockLight("Test_rgbww", STATE_ON), MockLight("Test_rgbww", STATE_ON),
] ]
setup_light_platform(hass, entities) setup_test_component_platform(hass, light.DOMAIN, entities)
entity0 = entities[0] entity0 = entities[0]
entity0.supported_color_modes = { entity0.supported_color_modes = {
@ -2243,13 +2221,11 @@ async def test_light_mired_color_temp_conversion(
assert state.attributes["color_temp_kelvin"] == 3500 assert state.attributes["color_temp_kelvin"] == 3500
async def test_light_service_call_white_mode( async def test_light_service_call_white_mode(hass: HomeAssistant) -> None:
hass: HomeAssistant, setup_light_platform: SetupLightPlatformCallable
) -> None:
"""Test color_mode white in service calls.""" """Test color_mode white in service calls."""
entity0 = MockLight("Test_white", STATE_ON) entity0 = MockLight("Test_white", STATE_ON)
entity0.supported_color_modes = {light.ColorMode.HS, light.ColorMode.WHITE} entity0.supported_color_modes = {light.ColorMode.HS, light.ColorMode.WHITE}
setup_light_platform(hass, [entity0]) setup_test_component_platform(hass, light.DOMAIN, [entity0])
assert await async_setup_component(hass, "light", {"light": {"platform": "test"}}) assert await async_setup_component(hass, "light", {"light": {"platform": "test"}})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -2344,9 +2320,7 @@ async def test_light_service_call_white_mode(
assert data == {"white": 128} assert data == {"white": 128}
async def test_light_state_color_conversion( async def test_light_state_color_conversion(hass: HomeAssistant) -> None:
hass: HomeAssistant, setup_light_platform: SetupLightPlatformCallable
) -> None:
"""Test color conversion in state updates.""" """Test color conversion in state updates."""
entities = [ entities = [
MockLight("Test_hs", STATE_ON), MockLight("Test_hs", STATE_ON),
@ -2354,7 +2328,7 @@ async def test_light_state_color_conversion(
MockLight("Test_xy", STATE_ON), MockLight("Test_xy", STATE_ON),
MockLight("Test_legacy", STATE_ON), MockLight("Test_legacy", STATE_ON),
] ]
setup_light_platform(hass, entities) setup_test_component_platform(hass, light.DOMAIN, entities)
entity0 = entities[0] entity0 = entities[0]
entity0.supported_color_modes = {light.ColorMode.HS} entity0.supported_color_modes = {light.ColorMode.HS}
@ -2415,11 +2389,10 @@ async def test_light_state_color_conversion(
async def test_services_filter_parameters( async def test_services_filter_parameters(
hass: HomeAssistant, hass: HomeAssistant,
mock_light_profiles, mock_light_profiles,
setup_light_platform: SetupLightPlatformCallable,
mock_light_entities: list[MockLight], mock_light_entities: list[MockLight],
) -> None: ) -> None:
"""Test turn_on and turn_off filters unsupported parameters.""" """Test turn_on and turn_off filters unsupported parameters."""
setup_light_platform(hass, mock_light_entities) setup_test_component_platform(hass, light.DOMAIN, mock_light_entities)
assert await async_setup_component( assert await async_setup_component(
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}} hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}

View file

@ -18,19 +18,22 @@ from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
from homeassistant.util.yaml import loader as yaml_loader from homeassistant.util.yaml import loader as yaml_loader
from tests.common import async_mock_service, mock_restore_cache from tests.common import (
from tests.components.light.common import MockLight, SetupLightPlatformCallable async_mock_service,
mock_restore_cache,
setup_test_component_platform,
)
from tests.components.light.common import MockLight
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def entities( def entities(
hass: HomeAssistant, hass: HomeAssistant,
setup_light_platform: SetupLightPlatformCallable,
mock_light_entities: list[MockLight], mock_light_entities: list[MockLight],
) -> list[MockLight]: ) -> list[MockLight]:
"""Initialize the test light.""" """Initialize the test light."""
entities = mock_light_entities[0:2] entities = mock_light_entities[0:2]
setup_light_platform(hass, entities) setup_test_component_platform(hass, light.DOMAIN, entities)
return entities return entities