Skip to content

Commit

Permalink
Use mock_platform for event entity component tests instead of `hass…
Browse files Browse the repository at this point in the history
….components` (home-assistant#113667)
  • Loading branch information
jpbede authored Mar 17, 2024
1 parent 75a489d commit 25c4ab0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 61 deletions.
52 changes: 52 additions & 0 deletions tests/components/event/conftest.py
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions tests/components/event/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""Constants for the event entity component tests."""

TEST_DOMAIN = "test"
24 changes: 6 additions & 18 deletions tests/components/event/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
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,
Expand All @@ -34,8 +36,6 @@
mock_restore_cache_with_extra_data,
)

TEST_DOMAIN = "test"


async def test_event() -> None:
"""Test the event entity."""
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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()

Expand All @@ -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(
Expand All @@ -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()

Expand All @@ -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(
Expand All @@ -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()

Expand All @@ -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}
Expand All @@ -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()

Expand Down
43 changes: 0 additions & 43 deletions tests/testing_config/custom_components/test/event.py

This file was deleted.

0 comments on commit 25c4ab0

Please sign in to comment.