Add multi contracts support for Hydroquebec (#6392)

This commit is contained in:
Thibault Cohen 2017-03-04 03:11:58 -05:00 committed by Pascal Vizeli
parent 483556ac5b
commit aaa0944595
2 changed files with 26 additions and 12 deletions

View File

@ -21,13 +21,14 @@ 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==0.1.1'] REQUIREMENTS = ['pyhydroquebec==1.0.0']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
KILOWATT_HOUR = "kWh" # type: str KILOWATT_HOUR = "kWh" # type: str
PRICE = "CAD" # type: str PRICE = "CAD" # type: str
DAYS = "days" # type: str DAYS = "days" # type: str
CONF_CONTRACT = "contract" # type: str
DEFAULT_NAME = "HydroQuebec" DEFAULT_NAME = "HydroQuebec"
@ -64,6 +65,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]), vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
vol.Required(CONF_USERNAME): cv.string, vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string, vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_CONTRACT): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
}) })
@ -91,10 +93,12 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
username = config.get(CONF_USERNAME) username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD) password = config.get(CONF_PASSWORD)
contract = config.get(CONF_CONTRACT)
try: try:
hydroquebec_data = HydroquebecData(username, password) hydroquebec_data = HydroquebecData(username, password, contract)
hydroquebec_data.update() _LOGGER.info("Contract list: %s",
", ".join(hydroquebec_data.get_contract_list()))
except requests.exceptions.HTTPError as error: except requests.exceptions.HTTPError as error:
_LOGGER.error("Failt login: %s", error) _LOGGER.error("Failt login: %s", error)
return False return False
@ -105,7 +109,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
for variable in config[CONF_MONITORED_VARIABLES]: for variable in config[CONF_MONITORED_VARIABLES]:
sensors.append(HydroQuebecSensor(hydroquebec_data, variable, name)) sensors.append(HydroQuebecSensor(hydroquebec_data, variable, name))
add_devices(sensors) add_devices(sensors, True)
class HydroQuebecSensor(Entity): class HydroQuebecSensor(Entity):
@ -122,8 +126,6 @@ class HydroQuebecSensor(Entity):
self.hydroquebec_data = hydroquebec_data self.hydroquebec_data = hydroquebec_data
self._state = None self._state = None
self.update()
@property @property
def name(self): def name(self):
"""Return the name of the sensor.""" """Return the name of the sensor."""
@ -153,22 +155,34 @@ class HydroQuebecSensor(Entity):
class HydroquebecData(object): class HydroquebecData(object):
"""Get data from HydroQuebec.""" """Get data from HydroQuebec."""
def __init__(self, username, password): def __init__(self, username, password, contract=None):
"""Initialize the data object.""" """Initialize the data object."""
from pyhydroquebec import HydroQuebecClient from pyhydroquebec import HydroQuebecClient
self.client = HydroQuebecClient(username, self.client = HydroQuebecClient(username,
password, password,
REQUESTS_TIMEOUT) REQUESTS_TIMEOUT)
self._contract = contract
self.data = {} self.data = {}
@Throttle(MIN_TIME_BETWEEN_UPDATES) def get_contract_list(self):
def update(self): """Return the contract list."""
"""Get the latest data from HydroQuebec.""" # Fetch data
self._fetch_data()
return self.client.get_contracts()
def _fetch_data(self):
"""Fetch latest data from HydroQuebec."""
from pyhydroquebec.client import PyHydroQuebecError from pyhydroquebec.client import PyHydroQuebecError
try: try:
self.client.fetch_data() self.client.fetch_data()
except PyHydroQuebecError 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 return
@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self):
"""Return the latest collected data from HydroQuebec."""
# Fetch data
self._fetch_data()
# Update data # Update data
self.data = self.client.get_data() self.data = self.client.get_data(self._contract)[self._contract]

View File

@ -503,7 +503,7 @@ pyhik==0.1.0
pyhomematic==0.1.22 pyhomematic==0.1.22
# homeassistant.components.sensor.hydroquebec # homeassistant.components.sensor.hydroquebec
pyhydroquebec==0.1.1 pyhydroquebec==1.0.0
# homeassistant.components.device_tracker.icloud # homeassistant.components.device_tracker.icloud
pyicloud==0.9.1 pyicloud==0.9.1