diff --git a/pulse.py b/pulse.py index b896c39..da9da34 100644 --- a/pulse.py +++ b/pulse.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- import os import sys -import time import tibber.const import asyncio @@ -12,17 +11,16 @@ from influxdb_client.client.write_api import SYNCHRONOUS from DataPoints import Pulse import logging - -TOKEN=os.getenv('TOKEN', '') -TIBBERTOKEN=os.getenv('TIBBERTOKEN', '') -URL = os.getenv('URL',"" ) -BUCKET = os.getenv('BUCKET',"tibber" ) -ORG = os.getenv('ORG',"Default" ) +TOKEN = os.getenv('TOKEN', '') +TIBBERTOKEN = os.getenv('TIBBERTOKEN', '') +URL = os.getenv('URL', "") +BUCKET = os.getenv('BUCKET', "tibber") +ORG = os.getenv('ORG', "Default") #logging logger = logging.getLogger("TibberInflux") formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') -ch=logging.StreamHandler(sys.stdout) +ch = logging.StreamHandler(sys.stdout) #fh = logging.FileHandler('log.log') ch.setFormatter(formatter) #fh.setFormatter(formatter) @@ -31,52 +29,32 @@ logger.addHandler(ch) logger.setLevel(logging.INFO) -__version__ = "v0.1.6" +__version__ = "v0.1.7" logger.info(__version__) client = InfluxDBClient(url=URL, token=TOKEN, org=ORG) write_api = client.write_api(write_options=SYNCHRONOUS) query_api = client.query_api() + def _incoming(pkg): - try: - data = pkg.get("data") - if data is None: - exit(1) - p = Pulse(data).get_datapoint() - write_api.write(record=p, bucket=BUCKET) - logger.info(p) - return True - except: - exit(1) + data = pkg.get("data") + p = Pulse(data).get_datapoint() + write_api.write(record=p, bucket=BUCKET) + logger.info(p) + async def run(): conn = aiohttp.TCPConnector(limit_per_host=3) async with aiohttp.ClientSession(trust_env=True, connector=conn) as session: - - logger.info("connecting to tibber...") - if session.closed: - logger.error("session closed") - exit(1) - try: - tibber_connection = tibber.Tibber(TIBBERTOKEN, user_agent="python",websession=session) - - except Exception as e: - logger.info("error connecting to tibber...") - - logger.info(e) - raise e + tibber_connection = tibber.Tibber(TIBBERTOKEN, user_agent="python", websession=session) await tibber_connection.update_info() home = tibber_connection.get_homes()[0] await home.rt_subscribe(_incoming) - timeout = time.time() + 1800 # Set a timeout for 3600 seconds (1 hour) - while time.time() < timeout: + while True: await asyncio.sleep(2) - -loop = asyncio.get_event_loop() - +loop = asyncio.get_event_loop_policy().get_event_loop() loop.run_until_complete(run()) -exit(42)