Working sound setup for raspis
This commit is contained in:
@@ -4,20 +4,20 @@
|
||||
import requests
|
||||
import json
|
||||
import time
|
||||
import board
|
||||
import adafruit_dht
|
||||
import Adafruit_DHT
|
||||
|
||||
config = json.load(open("/etc/dht22_sensing.json"))
|
||||
dht_device = adafruit_dht.DHT22(getattr(board, config['dht_pin']))
|
||||
|
||||
|
||||
def send_to_home_assistant(temperature, humidity):
|
||||
# print(f"Sending temperature {temperature}, humidity {humidity}")
|
||||
headers = {
|
||||
'x-ha-access': config['token'],
|
||||
'Authorization': "Bearer {}".format(config['token'])
|
||||
}
|
||||
|
||||
temperature_url = "{}/api/states/sensor.{}".format(config['ha_url'], config['ha_temp_sensor_name'])
|
||||
temperature_url = "{}/api/states/sensor.{}".format(
|
||||
config['ha_url'], config['ha_temp_sensor_name'])
|
||||
temperatur_data = {
|
||||
"state": str(temperature),
|
||||
"attributes": {
|
||||
@@ -28,7 +28,8 @@ def send_to_home_assistant(temperature, humidity):
|
||||
}
|
||||
requests.post(temperature_url, json=temperatur_data, headers=headers)
|
||||
|
||||
humidity_url = "{}/api/states/sensor.{}".format(config['ha_url'], config['ha_humidity_sensor_name'])
|
||||
humidity_url = "{}/api/states/sensor.{}".format(
|
||||
config['ha_url'], config['ha_humidity_sensor_name'])
|
||||
humidity_data = {
|
||||
"state": str(humidity),
|
||||
"attributes": {
|
||||
@@ -40,14 +41,9 @@ def send_to_home_assistant(temperature, humidity):
|
||||
requests.post(humidity_url, json=humidity_data, headers=headers)
|
||||
|
||||
|
||||
def sense():
|
||||
try:
|
||||
send_to_home_assistant(dht_device.temperature, dht_device.humidity)
|
||||
except RuntimeError as error:
|
||||
# Errors happen fairly often, DHT's are hard to read, just keep going
|
||||
print(error.args[0])
|
||||
time.sleep(config['polling_sleep_time_seconds'])
|
||||
|
||||
|
||||
while True:
|
||||
sense()
|
||||
if __name__ == "__main__":
|
||||
while True:
|
||||
sensor = Adafruit_DHT.DHT22
|
||||
humidity, temperature = Adafruit_DHT.read_retry(sensor, config['dht_pin'])
|
||||
send_to_home_assistant(temperature, humidity)
|
||||
time.sleep(float(config['polling_sleep_time_seconds']))
|
||||
|
||||
Reference in New Issue
Block a user