Make Throttle async aware (#13027)

* Make Throttle async aware

* Lint
This commit is contained in:
Paulus Schoutsen committed 2018-03-09 19:42:35 -08:00
1 parent 8f807a3006
commit 6ffc53b290
11 files changed
+54 -48

No files matched your search

+2 -3
View File
@@ -157,13 +157,12 @@ class FidoData(object):
REQUESTS_TIMEOUT, httpsession)
self.data = {}
@asyncio.coroutine
@Throttle(MIN_TIME_BETWEEN_UPDATES)
def async_update(self):
async def async_update(self):
"""Get the latest data from Fido."""
from pyfido.client import PyFidoError
try:
yield from self.client.fetch_data()
await self.client.fetch_data()
except PyFidoError as exp:
_LOGGER.error("Error on receive last Fido data: %s", exp)
return False
@@ -182,13 +182,12 @@ class HydroquebecData(object):
return self.client.get_contracts()
return []
@asyncio.coroutine
@Throttle(MIN_TIME_BETWEEN_UPDATES)
def _fetch_data(self):
async def _fetch_data(self):
"""Fetch latest data from HydroQuebec."""
from pyhydroquebec.client import PyHydroQuebecError
try:
yield from self.client.fetch_data()
await self.client.fetch_data()
except PyHydroQuebecError as exp:
_LOGGER.error("Error on receive last Hydroquebec data: %s", exp)
return False
+4 -9
View File
@@ -133,13 +133,9 @@ class LuftdatenSensor(Entity):
except KeyError:
return
@asyncio.coroutine
def async_update(self):
async def async_update(self):
"""Get the latest data from luftdaten.info and update the state."""
try:
yield from self.luftdaten.async_update()
except TypeError:
pass
await self.luftdaten.async_update()
class LuftdatenData(object):
@@ -150,12 +146,11 @@ class LuftdatenData(object):
self.data = data
@Throttle(MIN_TIME_BETWEEN_UPDATES)
@asyncio.coroutine
def async_update(self):
async def async_update(self):
"""Get the latest data from luftdaten.info."""
from luftdaten.exceptions import LuftdatenError
try:
yield from self.data.async_get_data()
await self.data.async_get_data()
except LuftdatenError:
_LOGGER.error("Unable to retrieve data from luftdaten.info")
+2 -3
View File
@@ -75,15 +75,14 @@ def setup_sabnzbd(base_url, apikey, name, config,
for variable in monitored])
@asyncio.coroutine
@Throttle(MIN_TIME_BETWEEN_UPDATES)
def async_update_queue(sab_api):
async def async_update_queue(sab_api):
"""
Throttled function to update SABnzbd queue.
This ensures that the queue info only gets updated once for all sensors
"""
yield from sab_api.refresh_queue()
await sab_api.refresh_queue()
def request_configuration(host, name, hass, config, async_add_devices,
+3 -4
View File
@@ -140,21 +140,20 @@ class StartcaData(object):
"""
return float(value) * 10 ** -9
@asyncio.coroutine
@Throttle(MIN_TIME_BETWEEN_UPDATES)
def async_update(self):
async def async_update(self):
"""Get the Start.ca bandwidth data from the web service."""
import xmltodict
_LOGGER.debug("Updating Start.ca usage data")
url = 'https://www.start.ca/support/usage/api?key=' + \
self.api_key
with async_timeout.timeout(REQUEST_TIMEOUT, loop=self.loop):
req = yield from self.websession.get(url)
req = await self.websession.get(url)
if req.status != 200:
_LOGGER.error("Request failed with status: %u", req.status)
return False
data = yield from req.text()
data = await req.text()
try:
xml_data = xmltodict.parse(data)
except ExpatError:
+3 -4
View File
@@ -132,22 +132,21 @@ class TekSavvyData(object):
self.data = {"limit": self.bandwidth_cap} if self.bandwidth_cap > 0 \
else {"limit": float('inf')}
@asyncio.coroutine
@Throttle(MIN_TIME_BETWEEN_UPDATES)
def async_update(self):
async def async_update(self):
"""Get the TekSavvy bandwidth data from the web service."""
headers = {"TekSavvy-APIKey": self.api_key}
_LOGGER.debug("Updating TekSavvy data")
url = "https://api.teksavvy.com/"\
"web/Usage/UsageSummaryRecords?$filter=IsCurrent%20eq%20true"
with async_timeout.timeout(REQUEST_TIMEOUT, loop=self.loop):
req = yield from self.websession.get(url, headers=headers)
req = await self.websession.get(url, headers=headers)
if req.status != 200:
_LOGGER.error("Request failed with status: %u", req.status)
return False
try:
data = yield from req.json()
data = await req.json()
for (api, ha_name) in API_HA_MAP:
self.data[ha_name] = float(data["value"][0][api])
on_peak_download = self.data["onpeak_download"]
@@ -777,14 +777,13 @@ class WUndergroundData(object):
return url + '.json'
@asyncio.coroutine
@Throttle(MIN_TIME_BETWEEN_UPDATES)
def async_update(self):
async def async_update(self):
"""Get the latest data from WUnderground."""
try:
with async_timeout.timeout(10, loop=self._hass.loop):
response = yield from self._session.get(self._build_url())
result = yield from response.json()
response = await self._session.get(self._build_url())
result = await response.json()
if "error" in result['response']:
raise ValueError(result['response']["error"]["description"])
self.data = result