Skip to content

Commit

Permalink
Merge pull request #53 from chises/master
Browse files Browse the repository at this point in the history
fix aiohttp timeout error
  • Loading branch information
chises authored Feb 4, 2024
2 parents 9052c06 + 9d2bb0b commit 65babe4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
25 changes: 14 additions & 11 deletions custom_components/oilfox/OilFox.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import time
import logging
import aiohttp
from aiohttp import ClientTimeout

_LOGGER = logging.getLogger(__name__)

Expand All @@ -10,7 +11,7 @@ class OilFox:
"""OilFox Python Class"""

# https://github.com/foxinsights/customer-api
TIMEOUT = 15
TIMEOUT = 30
TOKEN_VALID = 900
hwid = None
password = None
Expand Down Expand Up @@ -59,12 +60,12 @@ async def update_stats(self):

if not_error:
headers = {"Authorization": "Bearer " + self.access_token}
async with aiohttp.ClientSession(timeout=self.TIMEOUT) as session:
async with aiohttp.ClientSession(
timeout=ClientTimeout(total=self.TIMEOUT)
) as session:
try:
async with session.get(
self.deviceUrl + self.hwid,
headers=headers,
timeout=self.TIMEOUT,
self.deviceUrl + self.hwid, headers=headers
) as response:
if response.status == 200:
self.state = await response.json()
Expand All @@ -84,9 +85,11 @@ async def get_tokens(self):
"email": self.email,
}

async with aiohttp.ClientSession() as session:
async with aiohttp.ClientSession(
timeout=ClientTimeout(total=self.TIMEOUT)
) as session:
async with session.post(
self.loginUrl, headers=headers, json=json_data, timeout=self.TIMEOUT
self.loginUrl, headers=headers, json=json_data
) as response:
if response.status == 200:
json_response = await response.json()
Expand All @@ -103,10 +106,10 @@ async def get_access_token(self):
data = {
"refresh_token": self.refresh_token,
}
async with aiohttp.ClientSession() as session:
async with session.post(
self.tokenUrl, data=data, timeout=self.TIMEOUT
) as response:
async with aiohttp.ClientSession(
timeout=ClientTimeout(total=self.TIMEOUT)
) as session:
async with session.post(self.tokenUrl, data=data) as response:
if response.status == 200:
json_response = await response.json()
self.access_token = json_response["access_token"]
Expand Down
2 changes: 1 addition & 1 deletion custom_components/oilfox/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
],
"iot_class": "cloud_polling",
"loggers": ["oilfox_component"],
"version": "0.1.14-beta"
"version": "0.1.16"
}

0 comments on commit 65babe4

Please sign in to comment.