Use async_timeout instead of asyncio.wait_for (#90496)
* Use async_timeout instead of asyncio.wait_for * fix imports * fix imports * break out Event.wait patch * Update tests/components/reolink/conftest.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Simplify --------- Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
9cab05c4b9
commit
b47ac524ea
4 changed files with 29 additions and 14 deletions
|
@ -8,6 +8,7 @@ from typing import Any
|
|||
|
||||
import aiohttp
|
||||
from aiohttp.web import Request
|
||||
import async_timeout
|
||||
from reolink_aio.api import Host
|
||||
from reolink_aio.exceptions import ReolinkError, SubscriptionError
|
||||
|
||||
|
@ -23,6 +24,7 @@ from .const import CONF_PROTOCOL, CONF_USE_HTTPS, DOMAIN
|
|||
from .exceptions import ReolinkSetupException, ReolinkWebhookException, UserNotAdmin
|
||||
|
||||
DEFAULT_TIMEOUT = 60
|
||||
FIRST_ONVIF_TIMEOUT = 15
|
||||
SUBSCRIPTION_RENEW_THRESHOLD = 300
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -146,11 +148,13 @@ class ReolinkHost:
|
|||
"Waiting for initial ONVIF state on webhook '%s'", self._webhook_url
|
||||
)
|
||||
try:
|
||||
await asyncio.wait_for(self._webhook_reachable.wait(), timeout=15)
|
||||
async with async_timeout.timeout(FIRST_ONVIF_TIMEOUT):
|
||||
await self._webhook_reachable.wait()
|
||||
except asyncio.TimeoutError:
|
||||
_LOGGER.debug(
|
||||
"Did not receive initial ONVIF state on webhook '%s' after 15 seconds",
|
||||
"Did not receive initial ONVIF state on webhook '%s' after %i seconds",
|
||||
self._webhook_url,
|
||||
FIRST_ONVIF_TIMEOUT,
|
||||
)
|
||||
ir.async_create_issue(
|
||||
self._hass,
|
||||
|
|
|
@ -39,8 +39,6 @@ def reolink_connect(mock_get_source_ip: None) -> Generator[MagicMock, None, None
|
|||
with patch(
|
||||
"homeassistant.components.reolink.host.webhook.async_register",
|
||||
return_value=True,
|
||||
), patch(
|
||||
"homeassistant.components.reolink.host.asyncio.Event.wait", AsyncMock()
|
||||
), patch(
|
||||
"homeassistant.components.reolink.host.Host", autospec=True
|
||||
) as host_mock_class:
|
||||
|
@ -65,6 +63,13 @@ def reolink_connect(mock_get_source_ip: None) -> Generator[MagicMock, None, None
|
|||
yield host_mock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def reolink_ONVIF_wait() -> Generator[None, None, None]:
|
||||
"""Mock reolink connection."""
|
||||
with patch("homeassistant.components.reolink.host.asyncio.Event.wait", AsyncMock()):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def reolink_platforms(mock_get_source_ip: None) -> Generator[None, None, None]:
|
||||
"""Mock reolink entry setup."""
|
||||
|
|
|
@ -28,7 +28,9 @@ from .conftest import (
|
|||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
pytestmark = pytest.mark.usefixtures("mock_setup_entry", "reolink_connect")
|
||||
pytestmark = pytest.mark.usefixtures(
|
||||
"mock_setup_entry", "reolink_connect", "reolink_ONVIF_wait"
|
||||
)
|
||||
|
||||
|
||||
async def test_config_flow_manual_success(hass: HomeAssistant) -> None:
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
"""Test the Reolink init."""
|
||||
import asyncio
|
||||
from typing import Any
|
||||
from unittest.mock import AsyncMock, MagicMock, Mock, patch
|
||||
|
||||
|
@ -55,6 +54,7 @@ pytestmark = pytest.mark.usefixtures("reolink_connect", "reolink_platforms")
|
|||
async def test_failures_parametrized(
|
||||
hass: HomeAssistant,
|
||||
reolink_connect: MagicMock,
|
||||
reolink_ONVIF_wait: MagicMock,
|
||||
config_entry: MockConfigEntry,
|
||||
attr: str,
|
||||
value: Any,
|
||||
|
@ -71,7 +71,10 @@ async def test_failures_parametrized(
|
|||
|
||||
|
||||
async def test_entry_reloading(
|
||||
hass: HomeAssistant, config_entry: MockConfigEntry, reolink_connect: MagicMock
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
reolink_connect: MagicMock,
|
||||
reolink_ONVIF_wait: MagicMock,
|
||||
) -> None:
|
||||
"""Test the entry is reloaded correctly when settings change."""
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
|
@ -88,7 +91,7 @@ async def test_entry_reloading(
|
|||
|
||||
|
||||
async def test_no_repair_issue(
|
||||
hass: HomeAssistant, config_entry: MockConfigEntry
|
||||
hass: HomeAssistant, config_entry: MockConfigEntry, reolink_ONVIF_wait: MagicMock
|
||||
) -> None:
|
||||
"""Test no repairs issue is raised when http local url is used."""
|
||||
await async_process_ha_core_config(
|
||||
|
@ -106,7 +109,7 @@ async def test_no_repair_issue(
|
|||
|
||||
|
||||
async def test_https_repair_issue(
|
||||
hass: HomeAssistant, config_entry: MockConfigEntry
|
||||
hass: HomeAssistant, config_entry: MockConfigEntry, reolink_ONVIF_wait: MagicMock
|
||||
) -> None:
|
||||
"""Test repairs issue is raised when https local url is used."""
|
||||
await async_process_ha_core_config(
|
||||
|
@ -125,6 +128,7 @@ async def test_port_repair_issue(
|
|||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
reolink_connect: MagicMock,
|
||||
reolink_ONVIF_wait: MagicMock,
|
||||
protocol: str,
|
||||
) -> None:
|
||||
"""Test repairs issue is raised when auto enable of ports fails."""
|
||||
|
@ -144,10 +148,7 @@ async def test_webhook_repair_issue(
|
|||
hass: HomeAssistant, config_entry: MockConfigEntry
|
||||
) -> None:
|
||||
"""Test repairs issue is raised when the webhook url is unreachable."""
|
||||
with patch(
|
||||
"homeassistant.components.reolink.host.asyncio.Event.wait",
|
||||
AsyncMock(side_effect=asyncio.TimeoutError()),
|
||||
):
|
||||
with patch("homeassistant.components.reolink.host.FIRST_ONVIF_TIMEOUT", new=0):
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
@ -156,7 +157,10 @@ async def test_webhook_repair_issue(
|
|||
|
||||
|
||||
async def test_firmware_repair_issue(
|
||||
hass: HomeAssistant, config_entry: MockConfigEntry, reolink_connect: MagicMock
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
reolink_connect: MagicMock,
|
||||
reolink_ONVIF_wait: MagicMock,
|
||||
) -> None:
|
||||
"""Test firmware issue is raised when too old firmware is used."""
|
||||
reolink_connect.sw_version_update_required = True
|
||||
|
|
Loading…
Add table
Reference in a new issue