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 4c89f7b commit 331ac6c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 44 deletions.
63 changes: 25 additions & 38 deletions custom_components/ledfx/core/ledfx.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import logging

import asyncio
import json
import aiohttp
import async_timeout

from . import exceptions
from .const import (
Expand All @@ -18,17 +15,15 @@
class LedFx(object):
def __init__(
self,
loop,
session,
httpx_client,
ip: str,
port: str,
options: dict = {}
) -> None:
if ip.endswith("/"):
ip = ip[:-1]

self._loop = loop
self._session = session
self.httpx_client = httpx_client

self.base_url = BASE_RESOURCE.format(ip = ip, port = port)
_LOGGER.debug("Debug LedFx: Init {}".format(self.base_url))
Expand All @@ -37,16 +32,14 @@ def __init__(

async def get(self, path: str, is_check_status: bool = True):
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(
"{}/{}".format(self.base_url, path),
timeout = self.timeout
)

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

Expand All @@ -58,17 +51,15 @@ async def get(self, path: str, is_check_status: bool = True):

async def post(self, path: str, data: dict, is_check_status: bool = True):
try:
with async_timeout.timeout(POST_TIMEOUT, loop = self._loop):
response = await self._session.post(
async with self.httpx_client as client:
response = await client.post(
"{}/{}".format(self.base_url, path),
json = data
json = data,
timeout = POST_TIMEOUT
)

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

Expand All @@ -80,17 +71,15 @@ async def post(self, path: str, data: dict, is_check_status: bool = True):

async def put(self, path: str, data: dict, is_check_status: bool = True):
try:
with async_timeout.timeout(POST_TIMEOUT, loop = self._loop):
response = await self._session.put(
async with self.httpx_client as client:
response = await client.put(
"{}/{}".format(self.base_url, path),
json = data
json = data,
timeout = POST_TIMEOUT
)

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

Expand All @@ -102,16 +91,14 @@ async def put(self, path: str, data: dict, is_check_status: bool = True):

async def delete(self, path: str, is_check_status: bool = True):
try:
with async_timeout.timeout(POST_TIMEOUT, loop = self._loop):
response = await self._session.delete(
"{}/{}".format(self.base_url, path)
async with self.httpx_client as client:
response = await client.delete(
"{}/{}".format(self.base_url, path),
timeout = POST_TIMEOUT
)

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

Expand Down
7 changes: 2 additions & 5 deletions custom_components/ledfx/core/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
CONF_TIMEOUT
)

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

Expand Down Expand Up @@ -49,11 +49,8 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry):
self.hass = hass
self.config_entry = config_entry

session = async_get_clientsession(hass, False)

self.api = LedFx(
hass.loop,
session,
get_async_client(hass, False),
self.ip,
self.port,
{"timeout": self.timeout}
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ledfx/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "ledfx",
"name": "LedFx",
"version": "1.0.2",
"version": "1.1.0",
"documentation": "https://github.com/dmamontov/hass-ledfx",
"issue_tracker": "https://github.com/dmamontov/hass-ledfx/issues",
"config_flow": true,
Expand Down

0 comments on commit 331ac6c

Please sign in to comment.