|
| 1 | +"""Tests for the Vistapool button platform.""" |
| 2 | + |
| 3 | +from collections.abc import Generator |
| 4 | +from typing import Any |
| 5 | +from unittest.mock import AsyncMock, patch |
| 6 | + |
| 7 | +from aioaquarite import AquariteError |
| 8 | +import pytest |
| 9 | +from syrupy.assertion import SnapshotAssertion |
| 10 | + |
| 11 | +from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS |
| 12 | +from homeassistant.const import ATTR_ENTITY_ID, Platform |
| 13 | +from homeassistant.core import HomeAssistant |
| 14 | +from homeassistant.exceptions import HomeAssistantError |
| 15 | +from homeassistant.helpers import entity_registry as er |
| 16 | + |
| 17 | +from tests.common import MockConfigEntry, snapshot_platform |
| 18 | + |
| 19 | +_BUTTON = "button.my_pool_led_next_color" |
| 20 | +_LED_DATA = {"main": {"hasLED": 1, "version": 1}, "light": {"status": 0}} |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture(autouse=True) |
| 24 | +def _only_button_platform() -> Generator[None]: |
| 25 | + """Restrict integration setup to the button platform for these tests.""" |
| 26 | + with patch("homeassistant.components.vistapool.PLATFORMS", [Platform.BUTTON]): |
| 27 | + yield |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture(autouse=True) |
| 31 | +def _skip_pulse_delay() -> Generator[None]: |
| 32 | + """Skip the LED pulse delay so tests don't actually sleep.""" |
| 33 | + with patch("homeassistant.components.vistapool.button._LED_PULSE_DELAY_SECONDS", 0): |
| 34 | + yield |
| 35 | + |
| 36 | + |
| 37 | +async def test_all_entities( |
| 38 | + hass: HomeAssistant, |
| 39 | + snapshot: SnapshotAssertion, |
| 40 | + entity_registry: er.EntityRegistry, |
| 41 | + mock_config_entry: MockConfigEntry, |
| 42 | + mock_vistapool_client: AsyncMock, |
| 43 | +) -> None: |
| 44 | + """Test the LED-pulse button when hasLED is set.""" |
| 45 | + mock_vistapool_client.fetch_pool_data.return_value = _LED_DATA |
| 46 | + mock_config_entry.add_to_hass(hass) |
| 47 | + |
| 48 | + assert await hass.config_entries.async_setup(mock_config_entry.entry_id) |
| 49 | + await hass.async_block_till_done() |
| 50 | + |
| 51 | + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) |
| 52 | + |
| 53 | + |
| 54 | +async def test_button_not_created_without_led( |
| 55 | + hass: HomeAssistant, |
| 56 | + mock_config_entry: MockConfigEntry, |
| 57 | + mock_vistapool_client: AsyncMock, |
| 58 | + mock_pool_data: dict[str, Any], |
| 59 | +) -> None: |
| 60 | + """Test the LED-pulse button is not created when hasLED is 0.""" |
| 61 | + mock_vistapool_client.fetch_pool_data.return_value = mock_pool_data |
| 62 | + mock_config_entry.add_to_hass(hass) |
| 63 | + |
| 64 | + assert await hass.config_entries.async_setup(mock_config_entry.entry_id) |
| 65 | + await hass.async_block_till_done() |
| 66 | + |
| 67 | + assert hass.states.get(_BUTTON) is None |
| 68 | + |
| 69 | + |
| 70 | +async def test_button_press_when_light_off( |
| 71 | + hass: HomeAssistant, |
| 72 | + mock_config_entry: MockConfigEntry, |
| 73 | + mock_vistapool_client: AsyncMock, |
| 74 | +) -> None: |
| 75 | + """Test pressing the button when the light is off just turns it on.""" |
| 76 | + mock_vistapool_client.fetch_pool_data.return_value = _LED_DATA |
| 77 | + mock_config_entry.add_to_hass(hass) |
| 78 | + |
| 79 | + assert await hass.config_entries.async_setup(mock_config_entry.entry_id) |
| 80 | + await hass.async_block_till_done() |
| 81 | + |
| 82 | + await hass.services.async_call( |
| 83 | + BUTTON_DOMAIN, |
| 84 | + SERVICE_PRESS, |
| 85 | + {ATTR_ENTITY_ID: _BUTTON}, |
| 86 | + blocking=True, |
| 87 | + ) |
| 88 | + |
| 89 | + mock_vistapool_client.set_value.assert_awaited_once_with( |
| 90 | + "ABCDEF1234567890", "light.status", 1 |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +async def test_button_press_when_light_on( |
| 95 | + hass: HomeAssistant, |
| 96 | + mock_config_entry: MockConfigEntry, |
| 97 | + mock_vistapool_client: AsyncMock, |
| 98 | +) -> None: |
| 99 | + """Test pressing the button when the light is on power-cycles it.""" |
| 100 | + mock_vistapool_client.fetch_pool_data.return_value = { |
| 101 | + "main": {"hasLED": 1, "version": 1}, |
| 102 | + "light": {"status": 1}, |
| 103 | + } |
| 104 | + mock_config_entry.add_to_hass(hass) |
| 105 | + |
| 106 | + assert await hass.config_entries.async_setup(mock_config_entry.entry_id) |
| 107 | + await hass.async_block_till_done() |
| 108 | + |
| 109 | + await hass.services.async_call( |
| 110 | + BUTTON_DOMAIN, |
| 111 | + SERVICE_PRESS, |
| 112 | + {ATTR_ENTITY_ID: _BUTTON}, |
| 113 | + blocking=True, |
| 114 | + ) |
| 115 | + |
| 116 | + assert mock_vistapool_client.set_value.await_count == 2 |
| 117 | + assert mock_vistapool_client.set_value.await_args_list[0].args == ( |
| 118 | + "ABCDEF1234567890", |
| 119 | + "light.status", |
| 120 | + 0, |
| 121 | + ) |
| 122 | + assert mock_vistapool_client.set_value.await_args_list[1].args == ( |
| 123 | + "ABCDEF1234567890", |
| 124 | + "light.status", |
| 125 | + 1, |
| 126 | + ) |
| 127 | + |
| 128 | + |
| 129 | +async def test_button_press_rapid_repeat_after_off( |
| 130 | + hass: HomeAssistant, |
| 131 | + mock_config_entry: MockConfigEntry, |
| 132 | + mock_vistapool_client: AsyncMock, |
| 133 | +) -> None: |
| 134 | + """Test a second press lands the off/on pulse instead of repeating turn-on. |
| 135 | +
|
| 136 | + Without the optimistic update, the second press would read the stale |
| 137 | + off-state (the Firestore push hasn't round-tripped yet) and send another |
| 138 | + bare light.status=1 — a no-op on the wire that doesn't advance the color. |
| 139 | + """ |
| 140 | + mock_vistapool_client.fetch_pool_data.return_value = _LED_DATA |
| 141 | + mock_config_entry.add_to_hass(hass) |
| 142 | + |
| 143 | + assert await hass.config_entries.async_setup(mock_config_entry.entry_id) |
| 144 | + await hass.async_block_till_done() |
| 145 | + |
| 146 | + await hass.services.async_call( |
| 147 | + BUTTON_DOMAIN, |
| 148 | + SERVICE_PRESS, |
| 149 | + {ATTR_ENTITY_ID: _BUTTON}, |
| 150 | + blocking=True, |
| 151 | + ) |
| 152 | + await hass.services.async_call( |
| 153 | + BUTTON_DOMAIN, |
| 154 | + SERVICE_PRESS, |
| 155 | + {ATTR_ENTITY_ID: _BUTTON}, |
| 156 | + blocking=True, |
| 157 | + ) |
| 158 | + |
| 159 | + assert mock_vistapool_client.set_value.await_count == 3 |
| 160 | + assert mock_vistapool_client.set_value.await_args_list[0].args == ( |
| 161 | + "ABCDEF1234567890", |
| 162 | + "light.status", |
| 163 | + 1, |
| 164 | + ) |
| 165 | + assert mock_vistapool_client.set_value.await_args_list[1].args == ( |
| 166 | + "ABCDEF1234567890", |
| 167 | + "light.status", |
| 168 | + 0, |
| 169 | + ) |
| 170 | + assert mock_vistapool_client.set_value.await_args_list[2].args == ( |
| 171 | + "ABCDEF1234567890", |
| 172 | + "light.status", |
| 173 | + 1, |
| 174 | + ) |
| 175 | + |
| 176 | + |
| 177 | +async def test_button_press_raises_on_api_error( |
| 178 | + hass: HomeAssistant, |
| 179 | + mock_config_entry: MockConfigEntry, |
| 180 | + mock_vistapool_client: AsyncMock, |
| 181 | +) -> None: |
| 182 | + """Test the button re-raises HomeAssistantError when the library fails.""" |
| 183 | + mock_vistapool_client.fetch_pool_data.return_value = _LED_DATA |
| 184 | + mock_vistapool_client.set_value.side_effect = AquariteError("boom") |
| 185 | + mock_config_entry.add_to_hass(hass) |
| 186 | + |
| 187 | + assert await hass.config_entries.async_setup(mock_config_entry.entry_id) |
| 188 | + await hass.async_block_till_done() |
| 189 | + |
| 190 | + with pytest.raises(HomeAssistantError) as excinfo: |
| 191 | + await hass.services.async_call( |
| 192 | + BUTTON_DOMAIN, |
| 193 | + SERVICE_PRESS, |
| 194 | + {ATTR_ENTITY_ID: _BUTTON}, |
| 195 | + blocking=True, |
| 196 | + ) |
| 197 | + assert excinfo.value.translation_key == "set_failed" |
0 commit comments