|
4 | 4 | from unittest.mock import DEFAULT, patch
|
5 | 5 |
|
6 | 6 | import pytest
|
7 |
| -from screenlogicpy import ScreenLogicGateway |
| 7 | +from screenlogicpy import ScreenLogicError, ScreenLogicGateway |
| 8 | +from screenlogicpy.const.common import ScreenLogicConnectionError |
8 | 9 |
|
9 | 10 | from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
|
10 | 11 | from homeassistant.components.number import DOMAIN as NUMBER_DOMAIN
|
11 | 12 | from homeassistant.components.screenlogic import DOMAIN
|
12 | 13 | from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
| 14 | +from homeassistant.config_entries import ConfigEntryState |
13 | 15 | from homeassistant.core import HomeAssistant
|
14 | 16 | from homeassistant.helpers import device_registry as dr, entity_registry as er
|
15 | 17 | from homeassistant.util import slugify
|
@@ -284,3 +286,35 @@ def stub_connect(*args, **kwargs):
|
284 | 286 |
|
285 | 287 | for entity_id in tested_entity_ids:
|
286 | 288 | assert hass.states.get(entity_id) is not None
|
| 289 | + |
| 290 | + |
| 291 | +@pytest.mark.parametrize( |
| 292 | + "exception", |
| 293 | + [ScreenLogicConnectionError, ScreenLogicError], |
| 294 | +) |
| 295 | +async def test_retry_on_connect_exception( |
| 296 | + hass: HomeAssistant, mock_config_entry: MockConfigEntry, exception: Exception |
| 297 | +) -> None: |
| 298 | + """Test setup retries on expected exceptions.""" |
| 299 | + |
| 300 | + def stub_connect(*args, **kwargs): |
| 301 | + raise exception |
| 302 | + |
| 303 | + mock_config_entry.add_to_hass(hass) |
| 304 | + |
| 305 | + with ( |
| 306 | + patch( |
| 307 | + GATEWAY_DISCOVERY_IMPORT_PATH, |
| 308 | + return_value={}, |
| 309 | + ), |
| 310 | + patch.multiple( |
| 311 | + ScreenLogicGateway, |
| 312 | + async_connect=stub_connect, |
| 313 | + is_connected=False, |
| 314 | + _async_connected_request=DEFAULT, |
| 315 | + ), |
| 316 | + ): |
| 317 | + assert not await hass.config_entries.async_setup(mock_config_entry.entry_id) |
| 318 | + await hass.async_block_till_done() |
| 319 | + |
| 320 | + assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY |
0 commit comments