From 091c71b2eef2f220683cb30f080ceacf20bb6405 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Ole=20Hu=CC=88bner?= Date: Sat, 2 Sep 2023 18:15:26 +0200 Subject: [PATCH] initial --- .github/workflows/docker.yml | 34 ++++++++++++++ .gitignore | 2 + Dockerfile | 30 +++++++++++++ pulse.py | 86 ++++++++++++++++++++++++++++++++++++ requirenments.txt | 5 +++ sample.env | 5 +++ 6 files changed, 162 insertions(+) create mode 100644 .github/workflows/docker.yml create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 pulse.py create mode 100644 requirenments.txt create mode 100644 sample.env diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..a1de817 --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,34 @@ +name: Docker Build and Push + +on: + push: + branches: + - main + - dev + +jobs: + build-and-push: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Login to Docker Hub + run: echo ${{ secrets.DOCKERHUB_PASSWORD }} | docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin + + - name: Build and push Docker image + env: + DOCKER_IMAGE_NAME: your-docker-image-name + run: | + if [ "${{ github.ref }}" == "refs/heads/main" ]; then + DOCKER_TAG="latest" + elif [ "${{ github.ref }}" == "refs/heads/dev" ]; then + DOCKER_TAG="dev" + else + DOCKER_TAG="other" + fi + + docker build -t $DOCKER_IMAGE_NAME . + docker tag $DOCKER_IMAGE_NAME:latest ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKER_IMAGE_NAME:$DOCKER_TAG + docker push ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKER_IMAGE_NAME:$DOCKER_TAG diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e7d4a53 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +auth.env +.idea \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..26a93af --- /dev/null +++ b/Dockerfile @@ -0,0 +1,30 @@ +# Pull base image +#TODO better base image and structure +FROM ubuntu:22.04 + +# Labels +LABEL MAINTAINER="Øyvind Nilsen " + +# Setup external package-sources +RUN apt-get update && apt-get install -y \ + python3 \ + python3-dev \ + python3-setuptools \ + python3-pip \ + gcc \ + --no-install-recommends && \ + rm -rf /var/lib/apt/lists/* + +# Run pip install +RUN pip install pytz --upgrade +RUN pip install tzdata --upgrade +ADD requirenments.txt / +RUN pip3 install -r /requirenments.txt +# Environment +ENV PYTHONIOENCODING=utf-8 +ADD pulse.py / + +# Chmod +RUN chmod 755 /pulse.py + +CMD ["/bin/python3","/pulse.py"] \ No newline at end of file diff --git a/pulse.py b/pulse.py new file mode 100644 index 0000000..de645d4 --- /dev/null +++ b/pulse.py @@ -0,0 +1,86 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- +import os +import time + +from dateutil.parser import parse + + +print("tibberpulse-influxdb") +import tibber.const +import asyncio +import aiohttp +import tibber +# settings from EnvionmentValue +TOKEN=os.getenv('TOKEN', '') +TIBBERTOKEN=os.getenv('TIBBERTOKEN', '') +URL = os.getenv('URL',"" ) +BUCKET = os.getenv('BUCKET',"tibber" ) +ORG = os.getenv('ORG',"Default" ) + +from influxdb_client import InfluxDBClient, Point +from influxdb_client.client.write_api import SYNCHRONOUS + +bucket = "Default" + +client = InfluxDBClient(url=URL, token=TOKEN, org=ORG) + +write_api = client.write_api(write_options=SYNCHRONOUS) +query_api = client.query_api() + +def _callback(pkg): + try: + data = pkg.get("data") + if data is None: + return + lm=data.get("liveMeasurement") + print(lm) + write_to_db(data) + return lm + except Exception as e: + raise e + + +def write_to_db(data,bucket = BUCKET): + + measurement = data['liveMeasurement'] + timestamp = measurement['timestamp'] + power = measurement['power'] + lastMeterConsumption = measurement['lastMeterConsumption'] + + tags = {"adress":""} + fields = { + "power": float(power), + "lastMeterConsumption": float(lastMeterConsumption) + } + datapoint = {"fields": fields, + "tags": tags, + "measurement": "pulse", + time: timestamp + + } + p = Point(measurement_name="pulse").from_dict(datapoint) + write_api.write(record=p, bucket=bucket) + + + + + +async def run(): + async with aiohttp.ClientSession() as session: + tibber_connection = tibber.Tibber(TIBBERTOKEN, websession=session, user_agent="python") + await tibber_connection.update_info() + home = tibber_connection.get_homes()[0] + try: + await home.rt_subscribe(_callback) + except: + raise Exception("Session gone") + + while True: + await asyncio.sleep(4) + +loop = asyncio.get_event_loop() +try: + loop.run_until_complete(run()) +except: + exit(1) \ No newline at end of file diff --git a/requirenments.txt b/requirenments.txt new file mode 100644 index 0000000..91f7ee7 --- /dev/null +++ b/requirenments.txt @@ -0,0 +1,5 @@ +influxdb-client[ciso] +requests +pyTibber +tzdata +pytz \ No newline at end of file diff --git a/sample.env b/sample.env new file mode 100644 index 0000000..510cdff --- /dev/null +++ b/sample.env @@ -0,0 +1,5 @@ +TOKEN=pZ0GI2Y71UtgMe81VGwErZdzP06h9LwMhSeh9rgxqesu9WCcDxx3n84KsYFsVHOaktPf0JqA4d3OF0FIRvEVXw== +TIBBERTOKEN=g7DG1fqV7W74r--qdcUP_W2K-XnfE58-_Qt0K4qDsH4 +URL=https://influx.lina.jan-ole.de:443 +BUCKET=tibber +ORG=Default \ No newline at end of file