Skip to content

Commit

Permalink
fix unclosed session on auth error
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-mc1 committed Feb 20, 2024
1 parent 31a74af commit 4466a41
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
4 changes: 2 additions & 2 deletions custom_components/qbittorrent_alt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
ServiceResponse,
SupportsResponse,
)
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers.entity import DeviceInfo

from .const import DOMAIN, PLATFORMS
Expand Down Expand Up @@ -58,7 +58,7 @@ async def service_resume_torrents(call: ServiceCall):
)
print(client.closed)
except LoginError as err:
raise ConfigEntryNotReady("Invalid credentials") from err
raise ConfigEntryAuthFailed("Invalid credentials") from err
except ClientConnectorError as err:
raise ConfigEntryNotReady("Failed to connect") from err

Expand Down
4 changes: 2 additions & 2 deletions custom_components/qbittorrent_alt/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from aioqbt.client import APIClient
from aioqbt.exc import LoginError
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryError
from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator

Expand Down Expand Up @@ -69,4 +69,4 @@ async def _async_update_data(self) -> dict[str, Any]:
"longest_eta": longest_eta,
}
except LoginError as exc:
raise ConfigEntryError("Invalid authentication") from exc
raise ConfigEntryAuthFailed("Invalid authentication") from exc
22 changes: 15 additions & 7 deletions custom_components/qbittorrent_alt/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ssl

from aiohttp import ClientSession, CookieJar
from aiohttp import ClientConnectorError, ClientSession, CookieJar
from aioqbt.api.types import TorrentInfo
from aioqbt.client import APIClient, create_client
from homeassistant.const import STATE_IDLE
Expand All @@ -18,12 +18,20 @@ async def setup_client(
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
client_session = ClientSession(cookie_jar=CookieJar(unsafe=True))
client = await create_client(
url, username=username, password=password, ssl=ssl_context, http=client_session
)
client._http_owner = True # Let aioqbt manage the ClientSession
await client.app.version()
return client
try:
client = await create_client(
url,
username=username,
password=password,
ssl=ssl_context,
http=client_session,
)
client._http_owner = True # Let aioqbt manage the ClientSession
await client.app.version()
return client
except ClientConnectorError as err:
await client_session.close() # Manuel close ClientSession when client setup fails
raise err


def get_qbittorrent_state(coordinator: QBittorrentDataCoordinator) -> str:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
homeassistant==2023.12.4
homeassistant==2024.2.2
aioqbt==0.7.0

0 comments on commit 4466a41

Please sign in to comment.