Hydroquebec component use now asyncio (#10795)

* Hydroquebec component use now asyncio

* Add tests

* Improve coverage

* fix tests

* Remove useless try/except and associated tests
This commit is contained in:
Thibault Cohen authored and Martin Hjelmare committed 2017-12-17 22:11:48 +01:00
1 parent 0664bf31a2
commit 8742ce035a
4 files changed
+112 -24

No files matched your search

+22 -22
View File
@@ -7,10 +7,10 @@ https://www.hydroquebec.com/portail/en/group/clientele/portrait-de-consommation
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.hydroquebec/
"""
import asyncio
import logging
from datetime import timedelta
import requests
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
@@ -21,7 +21,7 @@ from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle
import homeassistant.helpers.config_validation as cv
REQUIREMENTS = ['pyhydroquebec==1.3.1']
REQUIREMENTS = ['pyhydroquebec==2.0.2']
_LOGGER = logging.getLogger(__name__)
@@ -93,7 +93,8 @@ DAILY_MAP = (('yesterday_total_consumption', 'consoTotalQuot'),
('yesterday_higher_price_consumption', 'consoHautQuot'))
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 HydroQuebec sensor."""
# Create a data fetcher to support all of the configured sensors. Then make
# the first call to init the data.
@@ -102,13 +103,10 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
password = config.get(CONF_PASSWORD)
contract = config.get(CONF_CONTRACT)
try:
hydroquebec_data = HydroquebecData(username, password, contract)
_LOGGER.info("Contract list: %s",
", ".join(hydroquebec_data.get_contract_list()))
except requests.exceptions.HTTPError as error:
_LOGGER.error("Failt login: %s", error)
return False
hydroquebec_data = HydroquebecData(username, password, contract)
contracts = yield from hydroquebec_data.get_contract_list()
_LOGGER.info("Contract list: %s",
", ".join(contracts))
name = config.get(CONF_NAME)
@@ -116,7 +114,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
for variable in config[CONF_MONITORED_VARIABLES]:
sensors.append(HydroQuebecSensor(hydroquebec_data, variable, name))
add_devices(sensors)
async_add_devices(sensors, True)
class HydroQuebecSensor(Entity):
@@ -152,10 +150,11 @@ class HydroQuebecSensor(Entity):
"""Icon to use in the frontend, if any."""
return self._icon
def update(self):
@asyncio.coroutine
def async_update(self):
"""Get the latest data from Hydroquebec and update the state."""
self.hydroquebec_data.update()
if self.type in self.hydroquebec_data.data:
yield from self.hydroquebec_data.async_update()
if self.hydroquebec_data.data.get(self.type) is not None:
self._state = round(self.hydroquebec_data.data[self.type], 2)
@@ -170,23 +169,24 @@ class HydroquebecData(object):
self._contract = contract
self.data = {}
@asyncio.coroutine
def get_contract_list(self):
"""Return the contract list."""
# Fetch data
self._fetch_data()
yield from self._fetch_data()
return self.client.get_contracts()
@asyncio.coroutine
@Throttle(MIN_TIME_BETWEEN_UPDATES)
def _fetch_data(self):
"""Fetch latest data from HydroQuebec."""
from pyhydroquebec.client import PyHydroQuebecError
try:
self.client.fetch_data()
except PyHydroQuebecError as exp:
yield from self.client.fetch_data()
except BaseException as exp:
_LOGGER.error("Error on receive last Hydroquebec data: %s", exp)
return
@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self):
@asyncio.coroutine
def async_update(self):
"""Return the latest collected data from HydroQuebec."""
self._fetch_data()
yield from self._fetch_data()
self.data = self.client.get_data(self._contract)[self._contract]