|
| 1 | +"""Test Velux button entities.""" |
| 2 | + |
| 3 | +from unittest.mock import AsyncMock, MagicMock |
| 4 | + |
| 5 | +import pytest |
| 6 | +from pyvlx import PyVLXException |
| 7 | +from syrupy.assertion import SnapshotAssertion |
| 8 | + |
| 9 | +from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS |
| 10 | +from homeassistant.components.velux import DOMAIN |
| 11 | +from homeassistant.const import ATTR_ENTITY_ID, Platform |
| 12 | +from homeassistant.core import HomeAssistant |
| 13 | +from homeassistant.exceptions import HomeAssistantError |
| 14 | +from homeassistant.helpers import device_registry as dr, entity_registry as er |
| 15 | + |
| 16 | +from tests.common import MockConfigEntry, snapshot_platform |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def platform() -> Platform: |
| 21 | + """Fixture to specify platform to test.""" |
| 22 | + return Platform.BUTTON |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.usefixtures("setup_integration") |
| 26 | +async def test_button_snapshot( |
| 27 | + hass: HomeAssistant, |
| 28 | + mock_config_entry: MockConfigEntry, |
| 29 | + entity_registry: er.EntityRegistry, |
| 30 | + device_registry: dr.DeviceRegistry, |
| 31 | + snapshot: SnapshotAssertion, |
| 32 | +) -> None: |
| 33 | + """Snapshot the button entity (registry + state).""" |
| 34 | + await snapshot_platform( |
| 35 | + hass, |
| 36 | + entity_registry, |
| 37 | + snapshot, |
| 38 | + mock_config_entry.entry_id, |
| 39 | + ) |
| 40 | + |
| 41 | + # Get the button entity setup and test device association |
| 42 | + entity_entries = er.async_entries_for_config_entry( |
| 43 | + entity_registry, mock_config_entry.entry_id |
| 44 | + ) |
| 45 | + assert len(entity_entries) == 1 |
| 46 | + entry = entity_entries[0] |
| 47 | + |
| 48 | + assert entry.device_id is not None |
| 49 | + device_entry = device_registry.async_get(entry.device_id) |
| 50 | + assert device_entry is not None |
| 51 | + assert (DOMAIN, f"gateway_{mock_config_entry.entry_id}") in device_entry.identifiers |
| 52 | + assert device_entry.via_device_id is None |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.usefixtures("setup_integration") |
| 56 | +async def test_button_press_success( |
| 57 | + hass: HomeAssistant, |
| 58 | + mock_pyvlx: MagicMock, |
| 59 | +) -> None: |
| 60 | + """Test successful button press.""" |
| 61 | + |
| 62 | + # Configure the mock method to be async and return a coroutine |
| 63 | + mock_pyvlx.reboot_gateway.return_value = AsyncMock()() |
| 64 | + |
| 65 | + # Press the button |
| 66 | + await hass.services.async_call( |
| 67 | + BUTTON_DOMAIN, |
| 68 | + SERVICE_PRESS, |
| 69 | + {ATTR_ENTITY_ID: "button.klf_200_gateway_restart"}, |
| 70 | + blocking=True, |
| 71 | + ) |
| 72 | + |
| 73 | + # Verify the reboot method was called |
| 74 | + mock_pyvlx.reboot_gateway.assert_called_once() |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.usefixtures("setup_integration") |
| 78 | +async def test_button_press_failure( |
| 79 | + hass: HomeAssistant, |
| 80 | + mock_pyvlx: MagicMock, |
| 81 | +) -> None: |
| 82 | + """Test button press failure handling.""" |
| 83 | + |
| 84 | + # Mock reboot failure |
| 85 | + mock_pyvlx.reboot_gateway.side_effect = PyVLXException("Connection failed") |
| 86 | + |
| 87 | + # Press the button and expect HomeAssistantError |
| 88 | + with pytest.raises( |
| 89 | + HomeAssistantError, |
| 90 | + match="Failed to reboot gateway. Try again in a few moments or power cycle the device manually", |
| 91 | + ): |
| 92 | + await hass.services.async_call( |
| 93 | + BUTTON_DOMAIN, |
| 94 | + SERVICE_PRESS, |
| 95 | + {ATTR_ENTITY_ID: "button.klf_200_gateway_restart"}, |
| 96 | + blocking=True, |
| 97 | + ) |
| 98 | + |
| 99 | + # Verify the reboot method was called |
| 100 | + mock_pyvlx.reboot_gateway.assert_called_once() |
0 commit comments