Skip to content

Commit

Permalink
Use mock_platform for button entity component tests instead of `has…
Browse files Browse the repository at this point in the history
…s.components` (home-assistant#113627)
  • Loading branch information
jpbede authored Mar 16, 2024
1 parent 11c570e commit 73f1106
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 61 deletions.
50 changes: 50 additions & 0 deletions tests/components/button/conftest.py
Original file line number Diff line number Diff line change
@@ -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),
)
3 changes: 3 additions & 0 deletions tests/components/button/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""Constants for the button entity component tests."""

TEST_DOMAIN = "test"
19 changes: 5 additions & 14 deletions tests/components/button/test_init.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""The tests for the Button component."""

from collections.abc import Generator
from datetime import timedelta
from unittest.mock import MagicMock
Expand All @@ -26,6 +25,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 @@ -36,8 +37,6 @@
mock_restore_cache,
)

TEST_DOMAIN = "test"


async def test_button(hass: HomeAssistant) -> None:
"""Test getting data from the mocked button entity."""
Expand All @@ -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()

Expand Down Expand Up @@ -98,29 +95,23 @@ 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()

assert hass.states.get("button.button_1").state == "2021-01-01T23:59:59+00:00"


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()

Expand Down
47 changes: 0 additions & 47 deletions tests/testing_config/custom_components/test/button.py

This file was deleted.

0 comments on commit 73f1106

Please sign in to comment.