Use mock_platform
for event entity component tests instead of hass.components
(#113667)
This commit is contained in:
parent
75a489deb9
commit
25c4ab070b
4 changed files with 61 additions and 61 deletions
52
tests/components/event/conftest.py
Normal file
52
tests/components/event/conftest.py
Normal file
|
@ -0,0 +1,52 @@
|
|||
"""Fixtures for the event entity component tests."""
|
||||
import logging
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.event import DOMAIN, EventEntity
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
from .const import TEST_DOMAIN
|
||||
|
||||
from tests.common import MockEntity, MockPlatform, mock_platform
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class MockEventEntity(MockEntity, EventEntity):
|
||||
"""Mock EventEntity class."""
|
||||
|
||||
@property
|
||||
def event_types(self) -> list[str]:
|
||||
"""Return a list of possible events."""
|
||||
return self._handle("event_types")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def mock_event_platform(hass: HomeAssistant) -> None:
|
||||
"""Mock the event entity platform."""
|
||||
|
||||
async def async_setup_platform(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Set up test event platform."""
|
||||
async_add_entities(
|
||||
[
|
||||
MockEventEntity(
|
||||
name="doorbell",
|
||||
unique_id="unique_doorbell",
|
||||
event_types=["short_press", "long_press"],
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
mock_platform(
|
||||
hass,
|
||||
f"{TEST_DOMAIN}.{DOMAIN}",
|
||||
MockPlatform(async_setup_platform=async_setup_platform),
|
||||
)
|
3
tests/components/event/const.py
Normal file
3
tests/components/event/const.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
"""Constants for the event entity component tests."""
|
||||
|
||||
TEST_DOMAIN = "test"
|
|
@ -22,6 +22,8 @@ from homeassistant.helpers.restore_state import STORAGE_KEY as RESTORE_STATE_KEY
|
|||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from .const import TEST_DOMAIN
|
||||
|
||||
from tests.common import (
|
||||
MockConfigEntry,
|
||||
MockModule,
|
||||
|
@ -34,8 +36,6 @@ from tests.common import (
|
|||
mock_restore_cache_with_extra_data,
|
||||
)
|
||||
|
||||
TEST_DOMAIN = "test"
|
||||
|
||||
|
||||
async def test_event() -> None:
|
||||
"""Test the event entity."""
|
||||
|
@ -96,7 +96,7 @@ async def test_event() -> None:
|
|||
event._trigger_event("unknown_event")
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_custom_integrations")
|
||||
@pytest.mark.usefixtures("enable_custom_integrations", "mock_event_platform")
|
||||
async def test_restore_state(hass: HomeAssistant) -> None:
|
||||
"""Test we restore state integration."""
|
||||
mock_restore_cache_with_extra_data(
|
||||
|
@ -128,9 +128,6 @@ async def test_restore_state(hass: HomeAssistant) -> None:
|
|||
),
|
||||
)
|
||||
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
@ -142,7 +139,7 @@ async def test_restore_state(hass: HomeAssistant) -> None:
|
|||
assert state.attributes["hello"] == "world"
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_custom_integrations")
|
||||
@pytest.mark.usefixtures("enable_custom_integrations", "mock_event_platform")
|
||||
async def test_invalid_extra_restore_state(hass: HomeAssistant) -> None:
|
||||
"""Test we restore state integration."""
|
||||
mock_restore_cache_with_extra_data(
|
||||
|
@ -163,9 +160,6 @@ async def test_invalid_extra_restore_state(hass: HomeAssistant) -> None:
|
|||
),
|
||||
)
|
||||
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
@ -177,7 +171,7 @@ async def test_invalid_extra_restore_state(hass: HomeAssistant) -> None:
|
|||
assert "hello" not in state.attributes
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_custom_integrations")
|
||||
@pytest.mark.usefixtures("enable_custom_integrations", "mock_event_platform")
|
||||
async def test_no_extra_restore_state(hass: HomeAssistant) -> None:
|
||||
"""Test we restore state integration."""
|
||||
mock_restore_cache(
|
||||
|
@ -198,9 +192,6 @@ async def test_no_extra_restore_state(hass: HomeAssistant) -> None:
|
|||
),
|
||||
)
|
||||
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
@ -212,7 +203,7 @@ async def test_no_extra_restore_state(hass: HomeAssistant) -> None:
|
|||
assert "hello" not in state.attributes
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_custom_integrations")
|
||||
@pytest.mark.usefixtures("enable_custom_integrations", "mock_event_platform")
|
||||
async def test_saving_state(hass: HomeAssistant, hass_storage: dict[str, Any]) -> None:
|
||||
"""Test we restore state integration."""
|
||||
restore_data = {"last_event_type": "double_press", "last_event_attributes": None}
|
||||
|
@ -230,9 +221,6 @@ async def test_saving_state(hass: HomeAssistant, hass_storage: dict[str, Any]) -
|
|||
),
|
||||
)
|
||||
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
|
|
@ -1,43 +0,0 @@
|
|||
"""Provide a mock event platform.
|
||||
|
||||
Call init before using it in your tests to ensure clean test data.
|
||||
"""
|
||||
|
||||
from homeassistant.components.event import EventEntity
|
||||
|
||||
from tests.common import MockEntity
|
||||
|
||||
ENTITIES = []
|
||||
|
||||
|
||||
class MockEventEntity(MockEntity, EventEntity):
|
||||
"""Mock EventEntity class."""
|
||||
|
||||
@property
|
||||
def event_types(self) -> list[str]:
|
||||
"""Return a list of possible events."""
|
||||
return self._handle("event_types")
|
||||
|
||||
|
||||
def init(empty=False):
|
||||
"""Initialize the platform with entities."""
|
||||
global ENTITIES
|
||||
|
||||
ENTITIES = (
|
||||
[]
|
||||
if empty
|
||||
else [
|
||||
MockEventEntity(
|
||||
name="doorbell",
|
||||
unique_id="unique_doorbell",
|
||||
event_types=["short_press", "long_press"],
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_platform(
|
||||
hass, config, async_add_entities_callback, discovery_info=None
|
||||
):
|
||||
"""Return mock entities."""
|
||||
async_add_entities_callback(ENTITIES)
|
Loading…
Add table
Reference in a new issue