This commit is contained in:
Jan-Ole Hübner 2023-09-02 18:15:26 +02:00
parent 4738f63959
commit 091c71b2ee
6 changed files with 162 additions and 0 deletions

34
.github/workflows/docker.yml vendored Normal file
View file

@ -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

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
auth.env
.idea

30
Dockerfile Normal file
View file

@ -0,0 +1,30 @@
# Pull base image
#TODO better base image and structure
FROM ubuntu:22.04
# Labels
LABEL MAINTAINER="Øyvind Nilsen <oyvind.nilsen@gmail.com>"
# 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"]

86
pulse.py Normal file
View file

@ -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)

5
requirenments.txt Normal file
View file

@ -0,0 +1,5 @@
influxdb-client[ciso]
requests
pyTibber
tzdata
pytz

5
sample.env Normal file
View file

@ -0,0 +1,5 @@
TOKEN=pZ0GI2Y71UtgMe81VGwErZdzP06h9LwMhSeh9rgxqesu9WCcDxx3n84KsYFsVHOaktPf0JqA4d3OF0FIRvEVXw==
TIBBERTOKEN=g7DG1fqV7W74r--qdcUP_W2K-XnfE58-_Qt0K4qDsH4
URL=https://influx.lina.jan-ole.de:443
BUCKET=tibber
ORG=Default