diff --git a/custom_components/miwifi/core/luci.py b/custom_components/miwifi/core/luci.py index 12c652d..2057b64 100644 --- a/custom_components/miwifi/core/luci.py +++ b/custom_components/miwifi/core/luci.py @@ -7,9 +7,6 @@ import uuid import json -import aiohttp -import async_timeout - from typing import Optional from datetime import timedelta @@ -38,7 +35,7 @@ class Luci(object): def __init__( self, hass: HomeAssistant, - session, + httpx_client, ip: str, password: str, options: dict = {} @@ -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 @@ -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"] @@ -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 @@ -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() diff --git a/custom_components/miwifi/core/luci_data.py b/custom_components/miwifi/core/luci_data.py index a9d55d8..a5068fc 100644 --- a/custom_components/miwifi/core/luci_data.py +++ b/custom_components/miwifi/core/luci_data.py @@ -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 @@ -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, { diff --git a/custom_components/miwifi/manifest.json b/custom_components/miwifi/manifest.json index e3a3509..6c23938 100644 --- a/custom_components/miwifi/manifest.json +++ b/custom_components/miwifi/manifest.json @@ -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,