From 25c4ab070b4e771bba0f1edc72cbd87220c12051 Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Sun, 17 Mar 2024 17:32:16 +0100 Subject: [PATCH] Use `mock_platform` for event entity component tests instead of `hass.components` (#113667) --- tests/components/event/conftest.py | 52 +++++++++++++++++++ tests/components/event/const.py | 3 ++ tests/components/event/test_init.py | 24 +++------ .../custom_components/test/event.py | 43 --------------- 4 files changed, 61 insertions(+), 61 deletions(-) create mode 100644 tests/components/event/conftest.py create mode 100644 tests/components/event/const.py delete mode 100644 tests/testing_config/custom_components/test/event.py diff --git a/tests/components/event/conftest.py b/tests/components/event/conftest.py new file mode 100644 index 00000000000..38c59af117b --- /dev/null +++ b/tests/components/event/conftest.py @@ -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), + ) diff --git a/tests/components/event/const.py b/tests/components/event/const.py new file mode 100644 index 00000000000..323f32fa04b --- /dev/null +++ b/tests/components/event/const.py @@ -0,0 +1,3 @@ +"""Constants for the event entity component tests.""" + +TEST_DOMAIN = "test" diff --git a/tests/components/event/test_init.py b/tests/components/event/test_init.py index 25cb5af3e08..fd3cf0eaf9b 100644 --- a/tests/components/event/test_init.py +++ b/tests/components/event/test_init.py @@ -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() diff --git a/tests/testing_config/custom_components/test/event.py b/tests/testing_config/custom_components/test/event.py deleted file mode 100644 index 7dee8390148..00000000000 --- a/tests/testing_config/custom_components/test/event.py +++ /dev/null @@ -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)