Repository: home-assistant/core
File: tests/components/airos/test_button.py (Python)

Code before:
from unittest.mock import AsyncMock

from airos.exceptions import AirOSDataMissingError, AirOSDeviceConnectionError
import pytest

from homeassistant.const import ATTR_ENTITY_ID, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er

from . import setup_integration

from tests.common import MockConfigEntry


@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_reboot_button_press_success(
    hass: HomeAssistant,
    mock_airos_client: AsyncMock,
    mock_config_entry: MockConfigEntry,
    entity_registry: er.EntityRegistry,
) -> None:
    """Test that pressing the reboot button utilizes the correct calls."""
    await setup_integration(hass, mock_config_entry, [Platform.BUTTON])

    entity_id = "button.nanostation_5ac_ap_name_restart"

    entity = entity_registry.async_get(entity_id)
    assert entity
    assert entity.unique_id == f"{mock_config_entry.unique_id}_reboot"

    await hass.services.async_call(
        "button",
        "press",
        {ATTR_ENTITY_ID: entity_id},
        blocking=True,
    )

    mock_airos_client.reboot.assert_awaited_once()


@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_reboot_button_press_fail(
    hass: HomeAssistant,
    mock_airos_client: AsyncMock,
    mock_config_entry: MockConfigEntry,
    caplog: pytest.LogCaptureFixture,
) -> None:
    """Test that pressing the reboot button utilizes the correct calls."""
    await setup_integration(hass, mock_config_entry, [Platform.BUTTON])


Review comment (suggestion):
Nitpicky, but if you define the `entity_id` outside the method, as a constant, you can share it across multiple tests afterwards. :)

Code after:
from unittest.mock import AsyncMock

from airos.exceptions import AirOSDataMissingError, AirOSDeviceConnectionError
import pytest

from homeassistant.const import ATTR_ENTITY_ID, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er

from . import setup_integration

from tests.common import MockConfigEntry

REBOOT_ENTITY_ID = "button.nanostation_5ac_ap_name_restart"


@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_reboot_button_press_success(
    hass: HomeAssistant,
    mock_airos_client: AsyncMock,
    mock_config_entry: MockConfigEntry,
    entity_registry: er.EntityRegistry,
) -> None:
    """Test that pressing the reboot button utilizes the correct calls."""
    await setup_integration(hass, mock_config_entry, [Platform.BUTTON])

    entity = entity_registry.async_get(REBOOT_ENTITY_ID)
    assert entity
    assert entity.unique_id == f"{mock_config_entry.unique_id}_reboot"

    await hass.services.async_call(
        "button",
        "press",
        {ATTR_ENTITY_ID: REBOOT_ENTITY_ID},
        blocking=True,
    )

    mock_airos_client.reboot.assert_awaited_once()


@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_reboot_button_press_fail(
    hass: HomeAssistant,
    mock_airos_client: AsyncMock,
    mock_config_entry: MockConfigEntry,
) -> None:
    """Test that pressing the reboot button utilizes the correct calls."""
    await setup_integration(hass, mock_config_entry, [Platform.BUTTON])

    mock_airos_client.reboot.return_value = False