chore: cleanup after pytibber fix

This commit is contained in:
Jan-Ole Hübner 2024-05-10 13:25:47 +02:00
parent 230391cb32
commit 60858603d9

View file

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