mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Increase Comed timeout since it sometimes takes a long time for the API to respond (#9536)
* Increase Comed timeout since it sometimes takes a long time for the API to respond * Rewrite ComEd sensor to use asyncio * Fix whitespace and build issues
This commit is contained in:
parent
cf8e6d8d86
commit
bf176c405a
@ -6,14 +6,17 @@ https://home-assistant.io/components/sensor.comed_hourly_pricing/
|
|||||||
"""
|
"""
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
import async_timeout
|
||||||
|
import aiohttp
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from requests import RequestException, get
|
|
||||||
|
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||||
from homeassistant.const import ATTR_ATTRIBUTION, STATE_UNKNOWN
|
from homeassistant.const import ATTR_ATTRIBUTION, STATE_UNKNOWN
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
_RESOURCE = 'https://hourlypricing.comed.com/api'
|
_RESOURCE = 'https://hourlypricing.comed.com/api'
|
||||||
@ -46,22 +49,27 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
@asyncio.coroutine
|
||||||
|
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
||||||
"""Set up the ComEd Hourly Pricing sensor."""
|
"""Set up the ComEd Hourly Pricing sensor."""
|
||||||
|
websession = async_get_clientsession(hass)
|
||||||
dev = []
|
dev = []
|
||||||
|
|
||||||
for variable in config[CONF_MONITORED_FEEDS]:
|
for variable in config[CONF_MONITORED_FEEDS]:
|
||||||
dev.append(ComedHourlyPricingSensor(
|
dev.append(ComedHourlyPricingSensor(
|
||||||
variable[CONF_SENSOR_TYPE], variable[CONF_OFFSET],
|
hass.loop, websession, variable[CONF_SENSOR_TYPE],
|
||||||
variable.get(CONF_NAME)))
|
variable[CONF_OFFSET], variable.get(CONF_NAME)))
|
||||||
|
|
||||||
add_devices(dev, True)
|
async_add_devices(dev, True)
|
||||||
|
|
||||||
|
|
||||||
class ComedHourlyPricingSensor(Entity):
|
class ComedHourlyPricingSensor(Entity):
|
||||||
"""Implementation of a ComEd Hourly Pricing sensor."""
|
"""Implementation of a ComEd Hourly Pricing sensor."""
|
||||||
|
|
||||||
def __init__(self, sensor_type, offset, name):
|
def __init__(self, loop, websession, sensor_type, offset, name):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
|
self.loop = loop
|
||||||
|
self.websession = websession
|
||||||
if name:
|
if name:
|
||||||
self._name = name
|
self._name = name
|
||||||
else:
|
else:
|
||||||
@ -92,20 +100,30 @@ class ComedHourlyPricingSensor(Entity):
|
|||||||
attrs = {ATTR_ATTRIBUTION: CONF_ATTRIBUTION}
|
attrs = {ATTR_ATTRIBUTION: CONF_ATTRIBUTION}
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
def update(self):
|
@asyncio.coroutine
|
||||||
|
def async_update(self):
|
||||||
"""Get the ComEd Hourly Pricing data from the web service."""
|
"""Get the ComEd Hourly Pricing data from the web service."""
|
||||||
try:
|
try:
|
||||||
|
if self.type == CONF_FIVE_MINUTE or \
|
||||||
|
self.type == CONF_CURRENT_HOUR_AVERAGE:
|
||||||
|
url_string = _RESOURCE
|
||||||
if self.type == CONF_FIVE_MINUTE:
|
if self.type == CONF_FIVE_MINUTE:
|
||||||
url_string = _RESOURCE + '?type=5minutefeed'
|
url_string += '?type=5minutefeed'
|
||||||
response = get(url_string, timeout=10)
|
else:
|
||||||
|
url_string += '?type=currenthouraverage'
|
||||||
|
|
||||||
|
with async_timeout.timeout(60, loop=self.loop):
|
||||||
|
response = yield from self.websession.get(url_string)
|
||||||
|
# The API responds with MIME type 'text/html'
|
||||||
|
text = yield from response.text()
|
||||||
|
data = json.loads(text)
|
||||||
self._state = round(
|
self._state = round(
|
||||||
float(response.json()[0]['price']) + self.offset, 2)
|
float(data[0]['price']) + self.offset, 2)
|
||||||
elif self.type == CONF_CURRENT_HOUR_AVERAGE:
|
|
||||||
url_string = _RESOURCE + '?type=currenthouraverage'
|
|
||||||
response = get(url_string, timeout=10)
|
|
||||||
self._state = round(
|
|
||||||
float(response.json()[0]['price']) + self.offset, 2)
|
|
||||||
else:
|
else:
|
||||||
self._state = STATE_UNKNOWN
|
self._state = STATE_UNKNOWN
|
||||||
except (RequestException, ValueError, KeyError):
|
|
||||||
|
except (asyncio.TimeoutError, aiohttp.ClientError) as err:
|
||||||
|
_LOGGER.error("Could not get data from ComEd API: %s", err)
|
||||||
|
except (ValueError, KeyError):
|
||||||
_LOGGER.warning("Could not update status for %s", self.name)
|
_LOGGER.warning("Could not update status for %s", self.name)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user