Revert setting communication delay in Risco init (#113497)

This commit is contained in:
On Freund 2024-03-15 12:42:53 +02:00 committed by GitHub
parent c69495b64f
commit 99e29b75cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 8 additions and 58 deletions

View File

@ -38,12 +38,10 @@ from homeassistant.helpers.storage import Store
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import (
CONF_COMMUNICATION_DELAY,
DATA_COORDINATOR,
DEFAULT_SCAN_INTERVAL,
DOMAIN,
EVENTS_COORDINATOR,
MAX_COMMUNICATION_DELAY,
TYPE_LOCAL,
)
@ -86,31 +84,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def _async_setup_local_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
data = entry.data
comm_delay = initial_delay = data.get(CONF_COMMUNICATION_DELAY, 0)
risco = RiscoLocal(data[CONF_HOST], data[CONF_PORT], data[CONF_PIN])
while True:
risco = RiscoLocal(
data[CONF_HOST],
data[CONF_PORT],
data[CONF_PIN],
communication_delay=comm_delay,
)
try:
await risco.connect()
except CannotConnectError as error:
if comm_delay >= MAX_COMMUNICATION_DELAY:
raise ConfigEntryNotReady() from error
comm_delay += 1
except UnauthorizedError:
_LOGGER.exception("Failed to login to Risco cloud")
return False
else:
break
if comm_delay > initial_delay:
new_data = data.copy()
new_data[CONF_COMMUNICATION_DELAY] = comm_delay
hass.config_entries.async_update_entry(entry, data=new_data)
try:
await risco.connect()
except CannotConnectError as error:
raise ConfigEntryNotReady() from error
except UnauthorizedError:
_LOGGER.exception("Failed to login to Risco cloud")
return False
async def _error(error: Exception) -> None:
_LOGGER.error("Error in Risco library: %s", error)

View File

@ -172,16 +172,6 @@ def connect_with_error(exception):
yield
@pytest.fixture
def connect_with_single_error(exception):
"""Fixture to simulate error on connect."""
with patch(
"homeassistant.components.risco.RiscoLocal.connect",
side_effect=[exception, None],
):
yield
@pytest.fixture
async def setup_risco_local(hass, local_config_entry):
"""Set up a local Risco integration for testing."""

View File

@ -1,22 +0,0 @@
"""Tests for the Risco initialization."""
import pytest
from homeassistant.components.risco import CannotConnectError
from homeassistant.components.risco.const import CONF_COMMUNICATION_DELAY
from homeassistant.core import HomeAssistant
@pytest.mark.parametrize("exception", [CannotConnectError])
async def test_single_error_on_connect(
hass: HomeAssistant, connect_with_single_error, local_config_entry
) -> None:
"""Test single error on connect to validate communication delay update from 0 (default) to 1."""
expected_data = {
**local_config_entry.data,
**{"type": "local", CONF_COMMUNICATION_DELAY: 1},
}
await hass.config_entries.async_setup(local_config_entry.entry_id)
await hass.async_block_till_done()
assert local_config_entry.data == expected_data