def __init__(self, username, password, httpsession, contract=None): """Initialize the data object.""" from pyhydroquebec import HydroQuebecClient self.client = HydroQuebecClient( username, password, REQUESTS_TIMEOUT, httpsession) self._contract = contract self.data = {}
class HydroquebecData(object): """Get data from HydroQuebec.""" def __init__(self, username, password, contract=None): """Initialize the data object.""" from pyhydroquebec import HydroQuebecClient self.client = HydroQuebecClient(username, password, REQUESTS_TIMEOUT) self._contract = contract self.data = {} def get_contract_list(self): """Return the contract list.""" # Fetch data self._fetch_data() return self.client.get_contracts() def _fetch_data(self): """Fetch latest data from HydroQuebec.""" from pyhydroquebec.client import PyHydroQuebecError try: self.client.fetch_data() except PyHydroQuebecError as exp: _LOGGER.error("Error on receive last Hydroquebec data: %s", exp) return @Throttle(MIN_TIME_BETWEEN_UPDATES) def update(self): """Return the latest collected data from HydroQuebec.""" # Fetch data self._fetch_data() # Update data self.data = self.client.get_data(self._contract)[self._contract]
class HydroquebecData: """Get data from HydroQuebec.""" def __init__(self, username, password, httpsession, contract=None): """Initialize the data object.""" from pyhydroquebec import HydroQuebecClient self.client = HydroQuebecClient( username, password, REQUESTS_TIMEOUT, httpsession) self._contract = contract self.data = {} async def get_contract_list(self): """Return the contract list.""" # Fetch data ret = await self._fetch_data() if ret: return self.client.get_contracts() return [] @Throttle(MIN_TIME_BETWEEN_UPDATES) async def _fetch_data(self): """Fetch latest data from HydroQuebec.""" from pyhydroquebec.client import PyHydroQuebecError try: await self.client.fetch_data() except PyHydroQuebecError as exp: _LOGGER.error("Error on receive last Hydroquebec data: %s", exp) return False return True async def async_update(self): """Return the latest collected data from HydroQuebec.""" await self._fetch_data() self.data = self.client.get_data(self._contract)[self._contract]
class HydroquebecData: """Get data from HydroQuebec.""" def __init__(self, username, password, httpsession, contract=None): """Initialize the data object.""" from pyhydroquebec import HydroQuebecClient self.client = HydroQuebecClient(username, password, REQUESTS_TIMEOUT, httpsession) self._contract = contract self.data = {} async def get_contract_list(self): """Return the contract list.""" # Fetch data ret = await self._fetch_data() if ret: return self.client.get_contracts() return [] @Throttle(MIN_TIME_BETWEEN_UPDATES) async def _fetch_data(self): """Fetch latest data from HydroQuebec.""" from pyhydroquebec.client import PyHydroQuebecError try: await self.client.fetch_data() except PyHydroQuebecError as exp: _LOGGER.error("Error on receive last Hydroquebec data: %s", exp) return False return True async def async_update(self): """Return the latest collected data from HydroQuebec.""" await self._fetch_data() self.data = self.client.get_data(self._contract)[self._contract]
def main(): """Main function""" parser = argparse.ArgumentParser() parser.add_argument('-u', '--username', required=True, help='Hydro Quebec username') parser.add_argument('-p', '--password', required=True, help='Password') parser.add_argument('-j', '--json', action='store_true', default=False, help='Json output') parser.add_argument('-c', '--contract', default=None, help='Contract number') parser.add_argument('-l', '--list-contracts', action='store_true', default=False, help='List all your contracts') parser.add_argument('-H', '--hourly', action='store_true', default=False, help='Show yesterday hourly consumption') parser.add_argument('-t', '--timeout', default=REQUESTS_TIMEOUT, help='Request timeout') raw_group = parser.add_argument_group('Detailled-energy raw download option') raw_group.add_argument('--detailled-energy', action='store_true', default=False, help='Get raw json output download') raw_group.add_argument('--start-date', default=None, help='Start date for detailled-output') raw_group.add_argument('--end-date', default=datetime.datetime.now(HQ_TIMEZONE).strftime("%Y-%m-%d"), help="End date for detailled-output") args = parser.parse_args() client = HydroQuebecClient(args.username, args.password, args.timeout) loop = asyncio.get_event_loop() if args.detailled_energy is False: async_func = client.fetch_data() else: async_func = client.fetch_data_detailled_energy_use(args.contract, args.start_date, args.end_date) try: fut = asyncio.wait([async_func]) loop.run_until_complete(fut) except BaseException as exp: print(exp) return 1 finally: close_fut = asyncio.wait([client.close_session()]) loop.run_until_complete(close_fut) if not client.get_data(): return 2 if args.list_contracts: print("Contracts: {}".format(", ".join(client.get_contracts()))) elif args.json or args.detailled_energy: print(json.dumps(client.get_data(args.contract))) else: _format_output(args.username, client.get_data(args.contract), args.hourly)
def __init__(self, username, password): """Initialize the data object.""" from pyhydroquebec import HydroQuebecClient self.client = HydroQuebecClient(username, password, REQUESTS_TIMEOUT) self.data = {}
class HydroquebecData(object): """Get data from HydroQuebec.""" def __init__(self, username, password): """Initialize the data object.""" from pyhydroquebec import HydroQuebecClient self.client = HydroQuebecClient(username, password, REQUESTS_TIMEOUT) self.data = {} @Throttle(MIN_TIME_BETWEEN_UPDATES) def update(self): """Get the latest data from HydroQuebec.""" from pyhydroquebec.client import PyHydroQuebecError try: self.client.fetch_data() except PyHydroQuebecError as exp: _LOGGER.error("Error on receive last Hydroquebec data: %s", exp) return # Update data self.data = self.client.get_data()
def main(): """Main function""" parser = argparse.ArgumentParser() parser.add_argument('-u', '--username', required=True, help='Hydro Quebec username') parser.add_argument('-p', '--password', required=True, help='Password') parser.add_argument('-j', '--json', action='store_true', default=False, help='Json output') parser.add_argument('-c', '--contract', default=None, help='Contract number') parser.add_argument('-l', '--list-contracts', action='store_true', default=False, help='List all your contracts') parser.add_argument('-t', '--timeout', default=REQUESTS_TIMEOUT, help='Request timeout') args = parser.parse_args() client = HydroQuebecClient(args.username, args.password, args.timeout) loop = asyncio.get_event_loop() try: fut = asyncio.wait([client.fetch_data()]) loop.run_until_complete(fut) except BaseException as exp: print(exp) return 1 finally: client.close_session() if not client.get_data(): return 2 if args.list_contracts: print("Contracts: {}".format(", ".join(client.get_contracts()))) elif args.json: print(json.dumps(client.get_data(args.contract))) else: _format_output(args.username, client.get_data(args.contract))
def main(): """Entrypoint function.""" parser = argparse.ArgumentParser() parser.add_argument('-u', '--username', help='Hydro Quebec username') parser.add_argument('-p', '--password', help='Password') parser.add_argument('-j', '--json', action='store_true', default=False, help='Json output') parser.add_argument('-i', '--influxdb', action='store_true', default=False, help='InfluxDb output') parser.add_argument('-c', '--contract', default=None, help='Contract number') parser.add_argument('-l', '--list-contracts', action='store_true', default=False, help='List all your contracts') parser.add_argument('-H', '--hourly', action='store_true', default=False, help='Show yesterday hourly consumption') parser.add_argument('-t', '--timeout', default=REQUESTS_TIMEOUT, help='Request timeout') parser.add_argument('-V', '--version', action='store_true', default=False, help='Show version') raw_group = parser.add_argument_group( 'Detailled-energy raw download option') raw_group.add_argument('--detailled-energy', action='store_true', default=False, help='Get raw json output download') raw_group.add_argument( '--start-date', default=(datetime.datetime.now(HQ_TIMEZONE) - datetime.timedelta(days=1)).strftime("%Y-%m-%d"), help='Start date for detailled-output') raw_group.add_argument( '--end-date', default=datetime.datetime.now(HQ_TIMEZONE).strftime("%Y-%m-%d"), help="End date for detailled-output") args = parser.parse_args() if args.version: print(VERSION) return 0 if not args.username or not args.password: parser.print_usage() print("pyhydroquebec: error: the following arguments are required: " "-u/--username, -p/--password") return 3 client = HydroQuebecClient(args.username, args.password, args.timeout) loop = asyncio.get_event_loop() if args.detailled_energy is False: async_func = client.fetch_data() else: start_date = datetime.datetime.strptime(args.start_date, '%Y-%m-%d') end_date = datetime.datetime.strptime(args.end_date, '%Y-%m-%d') async_func = client.fetch_data_detailled_energy_use( start_date, end_date) try: fut = asyncio.wait([async_func]) loop.run_until_complete(fut) except BaseException as exp: print(exp) return 1 finally: close_fut = asyncio.wait([client.close_session()]) loop.run_until_complete(close_fut) if not client.get_data(): return 2 if args.list_contracts: print("Contracts: {}".format(", ".join(client.get_contracts()))) elif args.influxdb: output_influx(client.get_data(args.contract)) elif args.json or args.detailled_energy: output_json(client.get_data(args.contract)) else: output_text(args.username, client.get_data(args.contract), args.hourly) return 0