Use Home Assistant aiohttp session for Reolink (#125948)

This commit is contained in:
starkillerOG 2024-09-16 11:53:13 +02:00 committed by GitHub
parent b32f40c0fe
commit 15bf6222f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 3 deletions

View File

@ -25,6 +25,7 @@ from homeassistant.const import (
)
from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback
from homeassistant.helpers import issue_registry as ir
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.device_registry import format_mac
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.event import async_call_later
@ -64,10 +65,12 @@ class ReolinkHost:
) -> None:
"""Initialize Reolink Host. Could be either NVR, or Camera."""
self._hass: HomeAssistant = hass
self._clientsession: aiohttp.ClientSession | None = None
self._unique_id: str = ""
def get_aiohttp_session() -> aiohttp.ClientSession:
"""Return the HA aiohttp session."""
return async_get_clientsession(hass, verify_ssl=False)
self._api = Host(
config[CONF_HOST],
config[CONF_USERNAME],
@ -76,6 +79,7 @@ class ReolinkHost:
use_https=config.get(CONF_USE_HTTPS),
protocol=options[CONF_PROTOCOL],
timeout=DEFAULT_TIMEOUT,
aiohttp_get_session_callback=get_aiohttp_session,
)
self.last_wake: float = 0

View File

@ -2,8 +2,9 @@
import json
from typing import Any
from unittest.mock import AsyncMock, MagicMock, call
from unittest.mock import ANY, AsyncMock, MagicMock, call
from aiohttp import ClientSession
from freezegun.api import FrozenDateTimeFactory
import pytest
from reolink_aio.exceptions import ApiError, CredentialsInvalidError, ReolinkError
@ -492,11 +493,14 @@ async def test_dhcp_ip_update(
use_https=TEST_USE_HTTPS,
protocol=DEFAULT_PROTOCOL,
timeout=DEFAULT_TIMEOUT,
aiohttp_get_session_callback=ANY,
)
assert expected_call in reolink_connect_class.call_args_list
for exc_call in reolink_connect_class.call_args_list:
assert exc_call[0][0] in host_call_list
get_session = exc_call[1]["aiohttp_get_session_callback"]
assert isinstance(get_session(), ClientSession)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"