mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Use setup_test_component_platform
helper for fan entity component tests instead of hass.components
(#114409)
This commit is contained in:
parent
a5b609f081
commit
ae635a5586
@ -17,6 +17,7 @@ from homeassistant.components.fan import (
|
||||
SERVICE_SET_DIRECTION,
|
||||
SERVICE_SET_PERCENTAGE,
|
||||
SERVICE_SET_PRESET_MODE,
|
||||
FanEntity,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
@ -25,6 +26,8 @@ from homeassistant.const import (
|
||||
SERVICE_TURN_ON,
|
||||
)
|
||||
|
||||
from tests.common import MockEntity
|
||||
|
||||
|
||||
async def async_turn_on(
|
||||
hass,
|
||||
@ -146,3 +149,27 @@ async def async_set_direction(
|
||||
|
||||
await hass.services.async_call(DOMAIN, SERVICE_SET_DIRECTION, data, blocking=True)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
class MockFan(MockEntity, FanEntity):
|
||||
"""Mock Fan class."""
|
||||
|
||||
@property
|
||||
def preset_mode(self) -> str | None:
|
||||
"""Return preset mode."""
|
||||
return self._handle("preset_mode")
|
||||
|
||||
@property
|
||||
def preset_modes(self) -> list[str] | None:
|
||||
"""Return preset mode."""
|
||||
return self._handle("preset_modes")
|
||||
|
||||
@property
|
||||
def supported_features(self):
|
||||
"""Return the class of this fan."""
|
||||
return self._handle("supported_features")
|
||||
|
||||
async def async_set_preset_mode(self, preset_mode: str) -> None:
|
||||
"""Set preset mode."""
|
||||
self._attr_preset_mode = preset_mode
|
||||
self.async_write_ha_state()
|
||||
|
@ -16,8 +16,12 @@ from homeassistant.core import HomeAssistant
|
||||
import homeassistant.helpers.entity_registry as er
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import help_test_all, import_and_test_deprecated_constant_enum
|
||||
from tests.testing_config.custom_components.test.fan import MockFan
|
||||
from tests.common import (
|
||||
help_test_all,
|
||||
import_and_test_deprecated_constant_enum,
|
||||
setup_test_component_platform,
|
||||
)
|
||||
from tests.components.fan.common import MockFan
|
||||
|
||||
|
||||
class BaseFan(FanEntity):
|
||||
@ -103,21 +107,21 @@ async def test_preset_mode_validation(
|
||||
hass: HomeAssistant,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
entity_registry: er.EntityRegistry,
|
||||
enable_custom_integrations: None,
|
||||
) -> None:
|
||||
"""Test preset mode validation."""
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
platform = getattr(hass.components, "test.fan")
|
||||
platform.init(empty=False)
|
||||
test_fan = MockFan(
|
||||
name="Support fan with preset_mode support",
|
||||
supported_features=FanEntityFeature.PRESET_MODE,
|
||||
unique_id="unique_support_preset_mode",
|
||||
preset_modes=["auto", "eco"],
|
||||
)
|
||||
setup_test_component_platform(hass, "fan", [test_fan])
|
||||
|
||||
assert await async_setup_component(hass, "fan", {"fan": {"platform": "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
test_fan: MockFan = platform.ENTITIES["support_preset_mode"]
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("fan.support_fan_with_preset_mode_support")
|
||||
assert state.attributes.get(ATTR_PRESET_MODES) == ["auto", "eco"]
|
||||
|
||||
|
@ -1,65 +0,0 @@
|
||||
"""Provide a mock fan platform.
|
||||
|
||||
Call init before using it in your tests to ensure clean test data.
|
||||
"""
|
||||
|
||||
from homeassistant.components.fan import FanEntity, FanEntityFeature
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
from tests.common import MockEntity
|
||||
|
||||
ENTITIES = {}
|
||||
|
||||
|
||||
def init(empty=False):
|
||||
"""Initialize the platform with entities."""
|
||||
global ENTITIES
|
||||
|
||||
ENTITIES = (
|
||||
{}
|
||||
if empty
|
||||
else {
|
||||
"support_preset_mode": MockFan(
|
||||
name="Support fan with preset_mode support",
|
||||
supported_features=FanEntityFeature.PRESET_MODE,
|
||||
unique_id="unique_support_preset_mode",
|
||||
preset_modes=["auto", "eco"],
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_platform(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
async_add_entities_callback: AddEntitiesCallback,
|
||||
discovery_info: DiscoveryInfoType | None = None,
|
||||
):
|
||||
"""Return mock entities."""
|
||||
async_add_entities_callback(list(ENTITIES.values()))
|
||||
|
||||
|
||||
class MockFan(MockEntity, FanEntity):
|
||||
"""Mock Fan class."""
|
||||
|
||||
@property
|
||||
def preset_mode(self) -> str | None:
|
||||
"""Return preset mode."""
|
||||
return self._handle("preset_mode")
|
||||
|
||||
@property
|
||||
def preset_modes(self) -> list[str] | None:
|
||||
"""Return preset mode."""
|
||||
return self._handle("preset_modes")
|
||||
|
||||
@property
|
||||
def supported_features(self):
|
||||
"""Return the class of this fan."""
|
||||
return self._handle("supported_features")
|
||||
|
||||
async def async_set_preset_mode(self, preset_mode: str) -> None:
|
||||
"""Set preset mode."""
|
||||
self._attr_preset_mode = preset_mode
|
||||
await self.async_update_ha_state()
|
Loading…
x
Reference in New Issue
Block a user