mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
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
@ -21,7 +21,7 @@ from homeassistant.helpers.entity import Entity
|
|||||||
from homeassistant.util import Throttle
|
from homeassistant.util import Throttle
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
REQUIREMENTS = ['pyhydroquebec==2.0.2']
|
REQUIREMENTS = ['pyhydroquebec==2.1.0']
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_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)
|
password = config.get(CONF_PASSWORD)
|
||||||
contract = config.get(CONF_CONTRACT)
|
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()
|
contracts = yield from hydroquebec_data.get_contract_list()
|
||||||
|
if not contracts:
|
||||||
|
return
|
||||||
_LOGGER.info("Contract list: %s",
|
_LOGGER.info("Contract list: %s",
|
||||||
", ".join(contracts))
|
", ".join(contracts))
|
||||||
|
|
||||||
@ -161,11 +165,11 @@ class HydroQuebecSensor(Entity):
|
|||||||
class HydroquebecData(object):
|
class HydroquebecData(object):
|
||||||
"""Get data from HydroQuebec."""
|
"""Get data from HydroQuebec."""
|
||||||
|
|
||||||
def __init__(self, username, password, contract=None):
|
def __init__(self, username, password, httpsession, contract=None):
|
||||||
"""Initialize the data object."""
|
"""Initialize the data object."""
|
||||||
from pyhydroquebec import HydroQuebecClient
|
from pyhydroquebec import HydroQuebecClient
|
||||||
self.client = HydroQuebecClient(
|
self.client = HydroQuebecClient(
|
||||||
username, password, REQUESTS_TIMEOUT)
|
username, password, REQUESTS_TIMEOUT, httpsession)
|
||||||
self._contract = contract
|
self._contract = contract
|
||||||
self.data = {}
|
self.data = {}
|
||||||
|
|
||||||
@ -173,17 +177,22 @@ class HydroquebecData(object):
|
|||||||
def get_contract_list(self):
|
def get_contract_list(self):
|
||||||
"""Return the contract list."""
|
"""Return the contract list."""
|
||||||
# Fetch data
|
# Fetch data
|
||||||
yield from self._fetch_data()
|
ret = yield from self._fetch_data()
|
||||||
return self.client.get_contracts()
|
if ret:
|
||||||
|
return self.client.get_contracts()
|
||||||
|
return []
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||||
def _fetch_data(self):
|
def _fetch_data(self):
|
||||||
"""Fetch latest data from HydroQuebec."""
|
"""Fetch latest data from HydroQuebec."""
|
||||||
|
from pyhydroquebec.client import PyHydroQuebecError
|
||||||
try:
|
try:
|
||||||
yield from self.client.fetch_data()
|
yield from self.client.fetch_data()
|
||||||
except BaseException as exp:
|
except PyHydroQuebecError as exp:
|
||||||
_LOGGER.error("Error on receive last Hydroquebec data: %s", exp)
|
_LOGGER.error("Error on receive last Hydroquebec data: %s", exp)
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_update(self):
|
def async_update(self):
|
||||||
|
@ -717,7 +717,7 @@ pyhiveapi==0.2.10
|
|||||||
pyhomematic==0.1.36
|
pyhomematic==0.1.36
|
||||||
|
|
||||||
# homeassistant.components.sensor.hydroquebec
|
# homeassistant.components.sensor.hydroquebec
|
||||||
pyhydroquebec==2.0.2
|
pyhydroquebec==2.1.0
|
||||||
|
|
||||||
# homeassistant.components.alarm_control_panel.ialarm
|
# homeassistant.components.alarm_control_panel.ialarm
|
||||||
pyialarm==0.2
|
pyialarm==0.2
|
||||||
|
@ -15,7 +15,7 @@ CONTRACT = "123456789"
|
|||||||
class HydroQuebecClientMock():
|
class HydroQuebecClientMock():
|
||||||
"""Fake Hydroquebec client."""
|
"""Fake Hydroquebec client."""
|
||||||
|
|
||||||
def __init__(self, username, password, contract=None):
|
def __init__(self, username, password, contract=None, httpsession=None):
|
||||||
"""Fake Hydroquebec client init."""
|
"""Fake Hydroquebec client init."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -36,16 +36,32 @@ class HydroQuebecClientMock():
|
|||||||
class HydroQuebecClientMockError(HydroQuebecClientMock):
|
class HydroQuebecClientMockError(HydroQuebecClientMock):
|
||||||
"""Fake Hydroquebec client error."""
|
"""Fake Hydroquebec client error."""
|
||||||
|
|
||||||
|
def get_contracts(self):
|
||||||
|
"""Return fake hydroquebec contracts."""
|
||||||
|
return []
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def fetch_data(self):
|
def fetch_data(self):
|
||||||
"""Return fake fetching data."""
|
"""Return fake fetching data."""
|
||||||
raise hydroquebec.PyHydroQuebecError("Fake Error")
|
raise PyHydroQuebecErrorMock("Fake Error")
|
||||||
|
|
||||||
|
|
||||||
class PyHydroQuebecErrorMock(BaseException):
|
class PyHydroQuebecErrorMock(BaseException):
|
||||||
"""Fake PyHydroquebec Error."""
|
"""Fake PyHydroquebec Error."""
|
||||||
|
|
||||||
|
|
||||||
|
class PyHydroQuebecClientFakeModule():
|
||||||
|
"""Fake pyfido.client module."""
|
||||||
|
|
||||||
|
PyHydroQuebecError = PyHydroQuebecErrorMock
|
||||||
|
|
||||||
|
|
||||||
|
class PyHydroQuebecFakeModule():
|
||||||
|
"""Fake pyfido module."""
|
||||||
|
|
||||||
|
HydroQuebecClient = HydroQuebecClientMockError
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def test_hydroquebec_sensor(loop, hass):
|
def test_hydroquebec_sensor(loop, hass):
|
||||||
"""Test the Hydroquebec number sensor."""
|
"""Test the Hydroquebec number sensor."""
|
||||||
@ -79,11 +95,11 @@ def test_hydroquebec_sensor(loop, hass):
|
|||||||
def test_error(hass, caplog):
|
def test_error(hass, caplog):
|
||||||
"""Test the Hydroquebec sensor errors."""
|
"""Test the Hydroquebec sensor errors."""
|
||||||
caplog.set_level(logging.ERROR)
|
caplog.set_level(logging.ERROR)
|
||||||
sys.modules['pyhydroquebec'] = MagicMock()
|
sys.modules['pyhydroquebec'] = PyHydroQuebecFakeModule()
|
||||||
sys.modules['pyhydroquebec.client'] = MagicMock()
|
sys.modules['pyhydroquebec.client'] = PyHydroQuebecClientFakeModule()
|
||||||
import pyhydroquebec.client
|
|
||||||
pyhydroquebec.HydroQuebecClient = HydroQuebecClientMockError
|
config = {}
|
||||||
pyhydroquebec.client.PyHydroQuebecError = BaseException
|
fake_async_add_devices = MagicMock()
|
||||||
hydro_data = hydroquebec.HydroquebecData('username', 'password')
|
yield from hydroquebec.async_setup_platform(hass, config,
|
||||||
yield from hydro_data._fetch_data()
|
fake_async_add_devices)
|
||||||
assert "Error on receive last Hydroquebec data: " in caplog.text
|
assert fake_async_add_devices.called is False
|
||||||
|
Loading…
x
Reference in New Issue
Block a user