Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hooverd-uits committed Feb 5, 2022
0 parents commit a90fcab
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.venv
environment.sh
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.10.1
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.formatting.provider": "black"
}
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM python:3.10-alpine

WORKDIR /usr/src/app

COPY requests.txt .

RUN pip install -r requests.txt

COPY src .

CMD ["python", "main.py"]
13 changes: 13 additions & 0 deletions influxdb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

scriptPath="$( cd "$(dirname "$0")" && pwd -P )"

source "$scriptPath/../environment.sh"

docker run -p 8086:8086 \
-v influxdb2:/var/lib/influxdb2 \
-e DOCKER_INFLUXDB_INIT_USERNAME="$INFLUXDB_INIT_USERNAME" \
-e DOCKER_INFLUXDB_INIT_PASSWORD="$INFLUXDB_INIT_USERNAME" \
-e DOCKER_INFLUXDB_INIT_ORG="$INFLUXDB_INIT_ORG" \
-e DOCKER_INFLUXDB_INIT_BUCKET="$INFLUXDB_INIT_BUCKET" \
influxdb:2.0
2 changes: 2 additions & 0 deletions requests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
influxdb-client==1.25.0
websockets==10.1
Empty file added src/__init__.py
Empty file.
74 changes: 74 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import asyncio
from functools import partial
import json
import logging
import os
from typing import Optional
import websockets

from dataclasses import dataclass

from influxdb_client import InfluxDBClient, Point
from influxdb_client.client.write_api import SYNCHRONOUS

logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


@dataclass
class HubitatElevationEvent:
source: str
name: str
displayName: str
value: float
type: Optional[str]
unit: str
deviceId: int
hubId: int
installedAppId: int
descriptionText: str

def __post_init__(self):
self.value = float(self.value)


hs_ws_url = os.environ["HUBITAT_ELEVATION_WEBSOCKET_URL"]
influxdb_url = os.environ["INFLUXDB_URL"]
token = os.environ["INFLUXDB_TOKEN"]
org = os.environ["INFLUXDB_ORG"]
bucket = os.environ["INFLUXDB_BUCKET"]

client = InfluxDBClient(url=influxdb_url, token=token, org=org)
write_api = client.write_api(write_options=SYNCHRONOUS)


async def write_event(record):
return asyncio.get_running_loop().run_in_executor(
None, partial(write_api.write, bucket, record=record)
)


async def he_event():
async with websockets.connect(hs_ws_url) as websocket:
loop = asyncio.get_running_loop()

async for message in websocket:
try:
event = HubitatElevationEvent(**json.loads(message))
logger.info(
"%s (%s): %s"
% (event.displayName, event.deviceId, event.descriptionText)
)
except Exception as ex:
logger.error("Failed parsing message from HE websocket: %s" % message)

p = (
Point("home")
.tag("device", event.displayName)
.field(event.name, event.value)
)
await write_event(p)


asyncio.run(he_event())

0 comments on commit a90fcab

Please sign in to comment.