Skip to content

Commit

Permalink
Fix import and update async timeout (#567)
Browse files Browse the repository at this point in the history
  • Loading branch information
klaasnicolaas authored Dec 30, 2023
1 parent 35d263c commit 7caf86a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ addopts = "--cov"
asyncio_mode = "auto"

[tool.ruff]
target-version = "py311"
select = ["ALL"]
ignore = [
"ANN101", # Self... explanatory
Expand Down
18 changes: 7 additions & 11 deletions src/omnikinverter/omnikinverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import asyncio
import json
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any
from typing import Any, Self

import async_timeout
from aiohttp import BasicAuth, ClientError, ClientResponseError, ClientSession
from aiohttp.hdrs import METH_GET
from yarl import URL
Expand All @@ -19,9 +18,6 @@
)
from .models import Device, Inverter

if TYPE_CHECKING:
from typing_extensions import Self


@dataclass
class OmnikInverter:
Expand Down Expand Up @@ -88,7 +84,7 @@ async def request(
auth = BasicAuth(self.username, self.password)

try:
async with async_timeout.timeout(self.request_timeout):
async with asyncio.timeout(self.request_timeout):
response = await self.session.request(
method,
url,
Expand Down Expand Up @@ -140,17 +136,17 @@ async def tcp_request(self) -> dict[str, Any]:
raise OmnikInverterAuthError(msg)

try:
async with async_timeout.timeout(self.request_timeout):
async with asyncio.timeout(self.request_timeout):
reader, writer = await asyncio.open_connection(self.host, self.tcp_port)
except OSError as exception:
msg = "Failed to open a TCP connection to the Omnik Inverter device"
raise OmnikInverterConnectionError(msg) from exception
except asyncio.TimeoutError as exception: # pragma: no cover
msg = "Timeout occurred while connecting to the Omnik Inverter device"
raise OmnikInverterConnectionError(msg) from exception
except OSError as exception:
msg = "Failed to open a TCP connection to the Omnik Inverter device"
raise OmnikInverterConnectionError(msg) from exception

try:
async with async_timeout.timeout(self.request_timeout):
async with asyncio.timeout(self.request_timeout):
writer.write(tcp.create_information_request(self.serial_number))
await writer.drain()

Expand Down

0 comments on commit 7caf86a

Please sign in to comment.