Skip to content

Commit

Permalink
Fix hass warning
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Mamontov committed Dec 18, 2021
1 parent e67d656 commit 9c06a1b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 30 deletions.
46 changes: 21 additions & 25 deletions custom_components/miwifi/core/luci.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
import uuid
import json

import aiohttp
import async_timeout

from typing import Optional
from datetime import timedelta

Expand Down Expand Up @@ -38,7 +35,7 @@ class Luci(object):
def __init__(
self,
hass: HomeAssistant,
session,
httpx_client,
ip: str,
password: str,
options: dict = {}
Expand All @@ -48,8 +45,7 @@ def __init__(

self.hass = hass

self._loop = hass.loop
self._session = session
self.httpx_client = httpx_client
self._ip = ip
self._token = None

Expand Down Expand Up @@ -94,26 +90,26 @@ async def login(self) -> dict:
nonce = self.generate_nonce()

try:
with async_timeout.timeout(self.timeout, loop = self._loop):
response = await self._session.post(
async with self.httpx_client as client:
response = await client.post(
"{}/api/xqsystem/login".format(self.base_url),
data = {
data={
"username": DEFAULT_USERNAME,
"logtype": str(LOGIN_TYPE),
"password": self.generate_password_hash(nonce, self.password),
"nonce": nonce,
}
},
timeout = self.timeout
)

data = json.loads(await response.read())
except asyncio.TimeoutError as e:
_LOGGER.debug("ERROR MiWifi: Timeout connection error %r", e)
raise exceptions.LuciConnectionError()
except aiohttp.ClientError as e:
_LOGGER.debug(response)

data = json.loads(response.content)
except Exception as e:
_LOGGER.debug("ERROR MiWifi: Connection error %r", e)
raise exceptions.LuciConnectionError()

if response.status != 200 or "token" not in data:
if response.status_code != 200 or "token" not in data:
raise exceptions.LuciTokenError()

self._token = data["token"]
Expand All @@ -122,8 +118,8 @@ async def login(self) -> dict:

async def logout(self) -> None:
try:
with async_timeout.timeout(self.timeout, loop = self._loop):
await self._session.get("{}/;stok={}/web/logout".format(self.base_url, self._token))
async with self.httpx_client as client:
await client.get("{}/;stok={}/web/logout".format(self.base_url, self._token), timeout = self.timeout)
except:
return

Expand Down Expand Up @@ -321,16 +317,16 @@ async def get_entities_map(self) -> dict:

async def get(self, path: str):
try:
with async_timeout.timeout(self.timeout, loop = self._loop):
response = await self._session.get(
async with self.httpx_client as client:
response = await client.get(
"{}/;stok={}/api/{}".format(self.base_url, self._token, path),
timeout = self.timeout
)

data = json.loads(await response.read())
except asyncio.TimeoutError as e:
_LOGGER.debug("ERROR MiWifi: Timeout connection error %r", e)
raise exceptions.LuciConnectionError()
except aiohttp.ClientError as e:
_LOGGER.debug(response)

data = json.loads(response.content)
except Exception as e:
_LOGGER.debug("ERROR MiWifi: Connection error %r", e)
raise exceptions.LuciConnectionError()

Expand Down
6 changes: 2 additions & 4 deletions custom_components/miwifi/core/luci_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
CONF_TIMEOUT
)
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.httpx_client import get_async_client
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.storage import Store
Expand All @@ -42,11 +42,9 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry, store: Store,
self.config_entry = config_entry
self.store = store

session = async_get_clientsession(hass, False)

self.api = Luci(
hass,
session,
get_async_client(hass, False),
self.ip,
self.password,
{
Expand Down
2 changes: 1 addition & 1 deletion custom_components/miwifi/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "miwifi",
"name": "MiWiFi",
"version": "1.2.9",
"version": "1.3.0",
"documentation": "https://github.com/dmamontov/hass-miwifi",
"issue_tracker": "https://github.com/dmamontov/hass-miwifi/issues",
"config_flow": true,
Expand Down

0 comments on commit 9c06a1b

Please sign in to comment.