Skip to content
This repository has been archived by the owner on Jun 20, 2022. It is now read-only.

Commit

Permalink
Merge pull request #5 from Unsigus/bugfix/async
Browse files Browse the repository at this point in the history
Merge bugfix/async into main
  • Loading branch information
DCSBL authored Nov 18, 2020
2 parents ba0a345 + 7a63953 commit 8a4153c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 39 deletions.
28 changes: 1 addition & 27 deletions custom_components/homewizard_energy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

async def async_setup(hass, config):
hass.data[const.DOMAIN] = {}
_LOGGER.info(f"---> Hello")
return True


Expand All @@ -32,29 +31,4 @@ async def async_setup_entry(hass, entry):
hass.config_entries.async_forward_entry_setup(entry, "sensor")
)

return True


# async def get_coordinator(hass):
# """Get the data update coordinator."""
# if DOMAIN in hass.data:
# return hass.data[DOMAIN]

# async def async_get_status():
# with async_timeout.timeout(10):
# return {
# case.country: case
# for case in await self._api.get_energy_data().get_cases(
# aiohttp_client.async_get_clientsession(hass)
# )
# }

# hass.data[DOMAIN] = update_coordinator.DataUpdateCoordinator(
# hass,
# logging.getLogger(__name__),
# name=DOMAIN,
# update_method=async_get_cases,
# update_interval=timedelta(hours=1),
# )
# await hass.data[DOMAIN].async_refresh()
# return hass.data[DOMAIN]
return True
20 changes: 8 additions & 12 deletions custom_components/homewizard_energy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
"""

import enum
import json
import logging

import requests
import aiohttp

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -42,8 +40,8 @@ async def get_energy_data(self):
Returns:
Dictionary containing device information.
"""
response = await self._call_api_endpoint(HWEP1Endpoints.ENERGY_DATA)
return response.json()
return await self._call_api_endpoint(HWEP1Endpoints.ENERGY_DATA)
# return response

# Helpers.
def _get_api_url(self, api_endpoint):
Expand Down Expand Up @@ -76,15 +74,13 @@ async def _call_api_endpoint(self, api_endpoint, payload=None):
}

if api_endpoint == HWEP1Endpoints.ENERGY_DATA:
response = requests.get(api_url, data=json.dumps(payload), verify=False)

async with aiohttp.ClientSession() as session:
async with session.get(api_url, headers=api_headers, timeout=30) as r:
response = await r.json()
else:
raise NotImplementedError("Unknown API endpoint.")

_LOGGER.debug(
f"Made {response.request.method} request to {response.request.url} "
f"with body: {response.request.body}"
)

_LOGGER.debug(f"{response}")
_LOGGER.debug(f"Received body: {response}")

return response

0 comments on commit 8a4153c

Please sign in to comment.