Last active
October 23, 2019 19:22
-
-
Save LinuxChristian/c00486eaee5a55daa790122ac4236c11 to your computer and use it in GitHub Desktop.
This example reading sensor information from Home assistant and sending it to the brewers friend API
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
# Example of reading sensor information from Home assistant | |
# and sending it to the brewers friend API | |
# | |
# Set this script to transmit data to Brewers friend every 30 minutes by adding | |
# this line to your crontab, | |
# | |
# */30 * * * * /usr/bin/python3 /<PATH TO YOUR SCRIPT>/send_to_brewersfriend.py | |
# | |
from requests import get, post | |
import logging as lg | |
lg.basicConfig(level=lg.INFO) | |
LOG = lg.getLogger() | |
# Create handlers | |
c_handler = lg.StreamHandler() | |
f_handler = lg.FileHandler('/tmp/brewers.log') | |
c_handler.setLevel(lg.DEBUG) | |
f_handler.setLevel(lg.INFO) | |
# Create formatters and add it to handlers | |
c_format = lg.Formatter('%(name)s - %(levelname)s - %(message)s') | |
f_format = lg.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | |
c_handler.setFormatter(c_format) | |
f_handler.setFormatter(f_format) | |
# Add handlers to the logger | |
LOG.addHandler(c_handler) | |
LOG.addHandler(f_handler) | |
def get_sensor(sensor_name): | |
url = 'http://localhost:8123/api/states/sensor.{}'.format(sensor_name) | |
headers = { | |
'Authorization': 'Bearer <YOUR HASS TOKEN HERE> | |
'content-type': 'application/json', | |
} | |
response = get(url, headers=headers) | |
jresp = response.json() | |
return jresp["state"] | |
# Get parameters | |
gravity = get_sensor("tilt_orange_gravity") | |
temp = get_sensor("tilt_orange_temperature") | |
ambient = 23.1 | |
# Information about the brewers friend API | |
# https://github.com/thorrak/tiltbridge/blob/5a813337c9b0686d2d9e16801daa6e5add59efc3/src/sendData.cpp#L281 | |
data = {"app_name":"hass", | |
"name": "hass", | |
"device_source": "Tilt", | |
"gravity_unit": "G", | |
"temp": temp, | |
"temp_unit": "C", | |
"gravity": gravity, | |
"report_source":"hass", | |
"ambient":ambient} | |
resp = post("https://log.brewersfriend.com/stream/<YOUR SENSOR KEY HERE>", headers = {"Content-Type": "application/json"}, json=data) | |
LOG.info("Message sent") | |
LOG.info("Response {}".format(response.status_code)) | |
LOG.info("Response {}".format(response.text)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment