Update hydroquebec component to use hass httpsession (#11412)
* Update hydroquebec component to use hass httpsession * Remove blank line
This commit is contained in:
parent
1490ccf7fb
commit
ebbdce1f42
3 changed files with 43 additions and 18 deletions
|
@ -21,7 +21,7 @@ from homeassistant.helpers.entity import Entity
|
|||
from homeassistant.util import Throttle
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
REQUIREMENTS = ['pyhydroquebec==2.0.2']
|
||||
REQUIREMENTS = ['pyhydroquebec==2.1.0']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -103,8 +103,12 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
|||
password = config.get(CONF_PASSWORD)
|
||||
contract = config.get(CONF_CONTRACT)
|
||||
|
||||
hydroquebec_data = HydroquebecData(username, password, contract)
|
||||
httpsession = hass.helpers.aiohttp_client.async_get_clientsession()
|
||||
hydroquebec_data = HydroquebecData(username, password, httpsession,
|
||||
contract)
|
||||
contracts = yield from hydroquebec_data.get_contract_list()
|
||||
if not contracts:
|
||||
return
|
||||
_LOGGER.info("Contract list: %s",
|
||||
", ".join(contracts))
|
||||
|
||||
|
@ -161,11 +165,11 @@ class HydroQuebecSensor(Entity):
|
|||
class HydroquebecData(object):
|
||||
"""Get data from HydroQuebec."""
|
||||
|
||||
def __init__(self, username, password, contract=None):
|
||||
def __init__(self, username, password, httpsession, contract=None):
|
||||
"""Initialize the data object."""
|
||||
from pyhydroquebec import HydroQuebecClient
|
||||
self.client = HydroQuebecClient(
|
||||
username, password, REQUESTS_TIMEOUT)
|
||||
username, password, REQUESTS_TIMEOUT, httpsession)
|
||||
self._contract = contract
|
||||
self.data = {}
|
||||
|
||||
|
@ -173,17 +177,22 @@ class HydroquebecData(object):
|
|||
def get_contract_list(self):
|
||||
"""Return the contract list."""
|
||||
# Fetch data
|
||||
yield from self._fetch_data()
|
||||
return self.client.get_contracts()
|
||||
ret = yield from self._fetch_data()
|
||||
if ret:
|
||||
return self.client.get_contracts()
|
||||
return []
|
||||
|
||||
@asyncio.coroutine
|
||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||
def _fetch_data(self):
|
||||
"""Fetch latest data from HydroQuebec."""
|
||||
from pyhydroquebec.client import PyHydroQuebecError
|
||||
try:
|
||||
yield from self.client.fetch_data()
|
||||
except BaseException as exp:
|
||||
except PyHydroQuebecError as exp:
|
||||
_LOGGER.error("Error on receive last Hydroquebec data: %s", exp)
|
||||
return False
|
||||
return True
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_update(self):
|
||||
|
|
|
@ -717,7 +717,7 @@ pyhiveapi==0.2.10
|
|||
pyhomematic==0.1.36
|
||||
|
||||
# homeassistant.components.sensor.hydroquebec
|
||||
pyhydroquebec==2.0.2
|
||||
pyhydroquebec==2.1.0
|
||||
|
||||
# homeassistant.components.alarm_control_panel.ialarm
|
||||
pyialarm==0.2
|
||||
|
|
|
@ -15,7 +15,7 @@ CONTRACT = "123456789"
|
|||
class HydroQuebecClientMock():
|
||||
"""Fake Hydroquebec client."""
|
||||
|
||||
def __init__(self, username, password, contract=None):
|
||||
def __init__(self, username, password, contract=None, httpsession=None):
|
||||
"""Fake Hydroquebec client init."""
|
||||
pass
|
||||
|
||||
|
@ -36,16 +36,32 @@ class HydroQuebecClientMock():
|
|||
class HydroQuebecClientMockError(HydroQuebecClientMock):
|
||||
"""Fake Hydroquebec client error."""
|
||||
|
||||
def get_contracts(self):
|
||||
"""Return fake hydroquebec contracts."""
|
||||
return []
|
||||
|
||||
@asyncio.coroutine
|
||||
def fetch_data(self):
|
||||
"""Return fake fetching data."""
|
||||
raise hydroquebec.PyHydroQuebecError("Fake Error")
|
||||
raise PyHydroQuebecErrorMock("Fake Error")
|
||||
|
||||
|
||||
class PyHydroQuebecErrorMock(BaseException):
|
||||
"""Fake PyHydroquebec Error."""
|
||||
|
||||
|
||||
class PyHydroQuebecClientFakeModule():
|
||||
"""Fake pyfido.client module."""
|
||||
|
||||
PyHydroQuebecError = PyHydroQuebecErrorMock
|
||||
|
||||
|
||||
class PyHydroQuebecFakeModule():
|
||||
"""Fake pyfido module."""
|
||||
|
||||
HydroQuebecClient = HydroQuebecClientMockError
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_hydroquebec_sensor(loop, hass):
|
||||
"""Test the Hydroquebec number sensor."""
|
||||
|
@ -79,11 +95,11 @@ def test_hydroquebec_sensor(loop, hass):
|
|||
def test_error(hass, caplog):
|
||||
"""Test the Hydroquebec sensor errors."""
|
||||
caplog.set_level(logging.ERROR)
|
||||
sys.modules['pyhydroquebec'] = MagicMock()
|
||||
sys.modules['pyhydroquebec.client'] = MagicMock()
|
||||
import pyhydroquebec.client
|
||||
pyhydroquebec.HydroQuebecClient = HydroQuebecClientMockError
|
||||
pyhydroquebec.client.PyHydroQuebecError = BaseException
|
||||
hydro_data = hydroquebec.HydroquebecData('username', 'password')
|
||||
yield from hydro_data._fetch_data()
|
||||
assert "Error on receive last Hydroquebec data: " in caplog.text
|
||||
sys.modules['pyhydroquebec'] = PyHydroQuebecFakeModule()
|
||||
sys.modules['pyhydroquebec.client'] = PyHydroQuebecClientFakeModule()
|
||||
|
||||
config = {}
|
||||
fake_async_add_devices = MagicMock()
|
||||
yield from hydroquebec.async_setup_platform(hass, config,
|
||||
fake_async_add_devices)
|
||||
assert fake_async_add_devices.called is False
|
||||
|
|
Loading…
Add table
Reference in a new issue