diff --git a/tests/components/button/conftest.py b/tests/components/button/conftest.py new file mode 100644 index 00000000000..fa7b153def5 --- /dev/null +++ b/tests/components/button/conftest.py @@ -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), + ) diff --git a/tests/components/button/const.py b/tests/components/button/const.py new file mode 100644 index 00000000000..9cb21e53550 --- /dev/null +++ b/tests/components/button/const.py @@ -0,0 +1,3 @@ +"""Constants for the button entity component tests.""" + +TEST_DOMAIN = "test" diff --git a/tests/components/button/test_init.py b/tests/components/button/test_init.py index 5290d145d69..caddfe93814 100644 --- a/tests/components/button/test_init.py +++ b/tests/components/button/test_init.py @@ -1,5 +1,4 @@ """The tests for the Button component.""" - from collections.abc import Generator from datetime import timedelta 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.util import dt as dt_util +from .const import TEST_DOMAIN + from tests.common import ( MockConfigEntry, MockModule, @@ -36,8 +37,6 @@ from tests.common import ( mock_restore_cache, ) -TEST_DOMAIN = "test" - async def test_button(hass: HomeAssistant) -> None: """Test getting data from the mocked button entity.""" @@ -60,11 +59,9 @@ async def test_custom_integration( caplog: pytest.LogCaptureFixture, enable_custom_integrations: None, freezer: FrozenDateTimeFactory, + setup_platform: None, ) -> None: """Test we integration.""" - 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() @@ -98,14 +95,11 @@ async def test_custom_integration( async def test_restore_state( - hass: HomeAssistant, enable_custom_integrations: None + hass: HomeAssistant, enable_custom_integrations: None, setup_platform: None ) -> None: """Test we restore state integration.""" 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"}}) await hass.async_block_till_done() @@ -113,14 +107,11 @@ async def test_restore_state( 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: """Test we restore state integration except for 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"}}) await hass.async_block_till_done() diff --git a/tests/testing_config/custom_components/test/button.py b/tests/testing_config/custom_components/test/button.py deleted file mode 100644 index 8daf3a741a1..00000000000 --- a/tests/testing_config/custom_components/test/button.py +++ /dev/null @@ -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)