-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a90fcab
Showing
8 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.venv | ||
environment.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3.10.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"python.formatting.provider": "black" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
influxdb-client==1.25.0 | ||
websockets==10.1 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |