import tibber.const import tibber import asyncio import time import logging import datetime import threading logger = logging.getLogger('main') TAXFEE=0.1088 TAX_RATE=1.21 SURCHARGE_PER_KWH=0.017520661 class client: def __init__(self, key): self.key = key self.lasthour =-1 self.current_price = None self.tibberupdate = threading.Thread(target=self.__updateTibber) def start(self): self.tibberupdate.start() async def __getTibberData(self): try: tibber_connection = tibber.Tibber(self.key) await tibber_connection.update_info() logger.debug(tibber_connection.name) home = tibber_connection.get_homes()[0] await home.fetch_consumption_data() await home.update_info() logger.debug(home.address1) await home.update_price_info() logger.debug(home.current_price_info) newrate=(home.current_price_info['energy'] + SURCHARGE_PER_KWH) * TAX_RATE logger.info("Tibber price updated to {}".format(newrate)) await tibber_connection.close_connection() return newrate except Exception as e: logger.error("Unable to refresh Tibber data: %s", repr(e)) def getPrice(self): return self.current_price def __updateTibber(self): while True: now = datetime.datetime.now() if now.hour != self.lasthour: logger.info("Tibber price needs updating") self.current_price = asyncio.run(self.__getTibberData()) self.lasthour= now.hour time.sleep(1)