Use mock_platform for button entity component tests instead of hass.components (#113627)

This commit is contained in:
Jan-Philipp Benecke 2024-03-16 23:47:59 +01:00 committed by GitHub
parent 11c570ea7b
commit 73f11064d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 58 additions and 61 deletions

View File

@ -0,0 +1,50 @@
"""Fixtures for the button entity component tests."""
import logging
import pytest
from homeassistant.components.button import DOMAIN, ButtonEntity
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 MockButtonEntity(MockEntity, ButtonEntity):
"""Mock Button class."""
def press(self) -> None:
"""Press the button."""
_LOGGER.info("The button has been pressed")
@pytest.fixture
async def setup_platform(hass: HomeAssistant) -> None:
"""Set up the button entity platform."""
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up test button platform."""
async_add_entities(
[
MockButtonEntity(
name="button 1",
unique_id="unique_button_1",
),
]
)
mock_platform(
hass,
f"{TEST_DOMAIN}.{DOMAIN}",
MockPlatform(async_setup_platform=async_setup_platform),
)

View File

@ -0,0 +1,3 @@
"""Constants for the button entity component tests."""
TEST_DOMAIN = "test"

View File

@ -1,5 +1,4 @@
"""The tests for the Button component.""" """The tests for the Button component."""
from collections.abc import Generator from collections.abc import Generator
from datetime import timedelta from datetime import timedelta
from unittest.mock import MagicMock from unittest.mock import MagicMock
@ -26,6 +25,8 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
from .const import TEST_DOMAIN
from tests.common import ( from tests.common import (
MockConfigEntry, MockConfigEntry,
MockModule, MockModule,
@ -36,8 +37,6 @@ from tests.common import (
mock_restore_cache, mock_restore_cache,
) )
TEST_DOMAIN = "test"
async def test_button(hass: HomeAssistant) -> None: async def test_button(hass: HomeAssistant) -> None:
"""Test getting data from the mocked button entity.""" """Test getting data from the mocked button entity."""
@ -60,11 +59,9 @@ async def test_custom_integration(
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
enable_custom_integrations: None, enable_custom_integrations: None,
freezer: FrozenDateTimeFactory, freezer: FrozenDateTimeFactory,
setup_platform: None,
) -> None: ) -> None:
"""Test we integration.""" """Test we integration."""
platform = getattr(hass.components, f"test.{DOMAIN}")
platform.init()
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()
@ -98,14 +95,11 @@ async def test_custom_integration(
async def test_restore_state( async def test_restore_state(
hass: HomeAssistant, enable_custom_integrations: None hass: HomeAssistant, enable_custom_integrations: None, setup_platform: None
) -> None: ) -> None:
"""Test we restore state integration.""" """Test we restore state integration."""
mock_restore_cache(hass, (State("button.button_1", "2021-01-01T23:59:59+00:00"),)) mock_restore_cache(hass, (State("button.button_1", "2021-01-01T23:59:59+00:00"),))
platform = getattr(hass.components, f"test.{DOMAIN}")
platform.init()
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()
@ -113,14 +107,11 @@ async def test_restore_state(
async def test_restore_state_does_not_restore_unavailable( async def test_restore_state_does_not_restore_unavailable(
hass: HomeAssistant, enable_custom_integrations: None hass: HomeAssistant, enable_custom_integrations: None, setup_platform: None
) -> None: ) -> None:
"""Test we restore state integration except for unavailable.""" """Test we restore state integration except for unavailable."""
mock_restore_cache(hass, (State("button.button_1", STATE_UNAVAILABLE),)) mock_restore_cache(hass, (State("button.button_1", STATE_UNAVAILABLE),))
platform = getattr(hass.components, f"test.{DOMAIN}")
platform.init()
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

@ -1,47 +0,0 @@
"""Provide a mock button platform.
Call init before using it in your tests to ensure clean test data.
"""
import logging
from homeassistant.components.button import ButtonEntity
from tests.common import MockEntity
UNIQUE_BUTTON_1 = "unique_button_1"
ENTITIES = []
_LOGGER = logging.getLogger(__name__)
class MockButtonEntity(MockEntity, ButtonEntity):
"""Mock Button class."""
def press(self) -> None:
"""Press the button."""
_LOGGER.info("The button has been pressed")
def init(empty=False):
"""Initialize the platform with entities."""
global ENTITIES
ENTITIES = (
[]
if empty
else [
MockButtonEntity(
name="button 1",
unique_id="unique_button_1",
),
]
)
async def async_setup_platform(
hass, config, async_add_entities_callback, discovery_info=None
):
"""Return mock entities."""
async_add_entities_callback(ENTITIES)