def setup_platform(hass, config, add_devices, discovery_info=None): #from .old import PrixCarburantClient logging.basicConfig(stream=sys.stderr, level=logging.DEBUG) logging.info("[gazpart] start") """Setup the sensor platform.""" login = config.get(CONF_GRDF_LOGIN) password = config.get(CONF_GRDF_PASSWORD) # Try to log in Enedis API try: logging.info("logging in GRDF URI %s...", gazpar.API_BASE_URI) token = gazpar.login(login, password) logging.info("logged in successfully!") except: logging.error("unable to login on %s : %s", gazpar.API_BASE_URI, exc) add_devices([Gazpar(token, gazpar)])
params['influx']['port'], params['influx']['username'], params['influx']['password'], params['influx']['db'], ssl=params['influx']['ssl'], verify_ssl=params['influx']['verify_ssl']) logging.info("logged in InfluxDB Server Host %s succesfully", params['influx']['host']) except: logging.error("unable to login on %s", params['influx']['host']) sys.exit(1) # Try to log in Enedis API try: logging.info("logging in GRDF URI %s...", gazpar.API_BASE_URI) token = gazpar.login(params['grdf']['username'], params['grdf']['password']) logging.info("logged in successfully!") except: logging.error("unable to login on %s : %s", gazpar.API_BASE_URI, exc) sys.exit(1) # Calculate start/endDate and firstTS for data to request/parse if args.last: logging.info( "looking for last value date on InfluxDB 'conzo_gaz' on host %s...", params['influx']['host']) startDate = _getStartDateInfluxDb(client, "conso_gaz") logging.info( "found last fetch date %s on InfluxDB 'conzo_gaz' on host %s...", startDate[2] + "/" + startDate[1] + "/" + startDate[0], params['influx']['host'])
def main(): params = _openParams(PFILE) # Try to log in InfluxDB Server try: logging.info("logging in InfluxDB Server Host %s...", params['influx']['host']) client = InfluxDBClient(params['influx']['host'], params['influx']['port'], params['influx']['username'], params['influx']['password'], params['influx']['db'], ssl=params['influx']['ssl'], verify_ssl=params['influx']['verify_ssl']) logging.info("logged in InfluxDB Server Host %s succesfully", params['influx']['host']) except: logging.error("unable to login on %s", params['influx']['host']) sys.exit(1) # Try to log in GRDF API try: logging.info("logging in GRDF URI %s...", gazpar.API_BASE_URI) token = gazpar.login(params['grdf']['username'], params['grdf']['password']) logging.info("logged in successfully!") except: logging.error("unable to login on %s", gazpar.API_BASE_URI) sys.exit(1) # Calculate start/endDate and firstTS for data to request/parse if args.last: logging.info("looking for last value date on InfluxDB 'conzo_gaz' on host %s...", params['influx']['host']) startDate = _getStartDateInfluxDb(client,"conso_gaz") logging.info("found last fetch date %s on InfluxDB 'conzo_gaz' on host %s...", startDate[2]+"/"+startDate[1]+"/"+startDate[0], params['influx']['host']) firstTS = _getDateTS(int(startDate[0]),int(startDate[1]),int(startDate[2]),12,0) startDate = startDate[2]+"/"+startDate[1]+"/"+startDate[0] else : logging.warning("GRDF will perhaps has not all data for the last %s days ",args.days) startDate = _getStartDate(datetime.date.today(), args.days) firstTS = _getStartTS(args.days) logging.info("will use %s as firstDate and %s as startDate", firstTS, startDate) endDate = _dayToStr(datetime.date.today()) # Try to get data from GRDF API resGrdf = gazpar.get_data_per_day(token, startDate, endDate) try: logging.info("get Data from GRDF from {0} to {1}".format(startDate, endDate)) # Get result from GRDF by day resGrdf = gazpar.get_data_per_day(token, startDate, endDate) if (args.verbose): pp.pprint(resGrdf) except: logging.error("unable to get data from GRDF") sys.exit(1) # When we have all values let's start parse data and pushing it jsonInflux = [] i = 0 for d in resGrdf: t = datetime.datetime.strptime(d['date'] + " 12:00", '%d-%m-%Y %H:%M') logging.info(("found value : {0:3} kWh / {1:7.2f} m3 at {2}").format(d['kwh'], d['mcube'], t.strftime('%Y-%m-%dT%H:%M:%SZ'))) if t.timestamp() > firstTS: logging.info(("value added to jsonInflux as {0} > {1}").format(t.strftime('%Y-%m-%d %H:%M'), datetime.datetime.fromtimestamp(firstTS).strftime('%Y-%m-%d %H:%M'))) jsonInflux.append({ "measurement": "conso_gaz", "tags": { "fetch_date" : endDate }, "time": t.strftime('%Y-%m-%dT%H:%M:%SZ'), "fields": { "kwh": d['kwh'], "mcube": d['mcube'] } }) else: logging.info(("value NOT added to jsonInflux as {0} > {1}").format(t.timestamp(), firstTS)) i=+1 if (args.verbose): pp.pprint(jsonInflux) logging.info("trying to write {0} points to influxDB".format(len(jsonInflux))) try: client.write_points(jsonInflux) except: logging.info("unable to write data points to influxdb") else: logging.info("done")