1
0
Fork 0

Compare commits

..

No commits in common. "master" and "0.2" have entirely different histories.
master ... 0.2

2 changed files with 9 additions and 42 deletions

View File

@ -1,6 +1,6 @@
[tool.poetry]
name = "thermostat"
version = "0.3.0dev2"
version = "0.2.0"
description = ""
authors = ["Dustin C. Hatch <dustin@hatch.name>"]

View File

@ -1,9 +1,7 @@
import json
import logging
import os
import select
import signal
import threading
import time
from contextlib import closing
from types import FrameType
@ -22,45 +20,30 @@ PORT = 8883
USERNAME = os.environ.get("MQTT_USERNAME", "")
PASSWORD = os.environ.get("MQTT_PASSWORD", "")
TOPIC = "homeassistant/sensor/thermostat"
AVAILABILITY_TOPIC = f"{TOPIC}/availability"
I2CPORT = 1
SENSOR_ADDR = 0x77
DEVICE = {
"manufacturer": "Dustin C. Hatch",
"name": "RPi Thermostat Display",
"model": "RPi Thermostat Display",
"identifiers": [
os.uname().nodename,
],
}
SENSOR_CONFIG = {
"thermostat_temperature": {
"device_class": "temperature",
"name": "Thermostat Temperature",
"device": DEVICE,
"state_topic": TOPIC,
"availability_topic": AVAILABILITY_TOPIC,
"unit_of_measurement": "°C",
"value_template": r"{{ value_json.temperature }}",
},
"thermostat_pressure": {
"device_class": "pressure",
"name": "Thermostat Pressure",
"device": DEVICE,
"state_topic": TOPIC,
"availability_topic": AVAILABILITY_TOPIC,
"unit_of_measurement": "hPa",
"value_template": r"{{ value_json.pressure }}",
},
"thermostat_humidity": {
"device_class": "humidity",
"name": "Thermostat Humidity",
"device": DEVICE,
"state_topic": TOPIC,
"availability_topic": AVAILABILITY_TOPIC,
"unit_of_measurement": "%",
"value_template": r"{{ value_json.humidity }}",
},
@ -69,20 +52,18 @@ SENSOR_CONFIG = {
class Daemon:
def __init__(self) -> None:
self.quitpipe = os.pipe()
self._ready = threading.Event()
self.running = True
def on_signal(self, signum: int, frame: FrameType) -> None:
log.debug("Got signal %d at %s", signum, frame)
log.info("Stopping")
os.close(self.quitpipe[1])
self.running = False
def run(self):
signal.signal(signal.SIGINT, self.on_signal)
signal.signal(signal.SIGTERM, self.on_signal)
client = mqtt.Client()
client.will_set(AVAILABILITY_TOPIC, "offline", retain=True)
client.on_connect = self.on_connect
client.on_message = self.on_message
client.on_disconnect = self.on_disconnect
@ -92,20 +73,15 @@ class Daemon:
client.loop_start()
with closing(smbus2.SMBus(I2CPORT)) as bus:
params = bme280.load_calibration_params(bus, SENSOR_ADDR)
self._ready.wait()
while 1:
while self.running:
data = bme280.sample(bus, SENSOR_ADDR, params)
values = {
"temperature": adj(data.temperature),
"pressure": adj(data.pressure),
"humidity": adj(data.humidity),
"temperature": int(data.temperature),
"pressure": int(data.pressure),
"humidity": int(data.humidity),
}
client.publish(TOPIC, json.dumps(values))
ready = select.select((self.quitpipe[0],), (), (), 10)[0]
if self.quitpipe[0] in ready:
os.close(self.quitpipe[0])
break
client.publish(AVAILABILITY_TOPIC, "offline", retain=True)
time.sleep(10)
client.disconnect()
client.loop_stop()
@ -118,14 +94,9 @@ class Daemon:
):
log.info("Successfully connected to MQTT broker")
for key, value in SENSOR_CONFIG.items():
value["unique_id"] = f"sensor.{key}"
client.publish(
f"homeassistant/sensor/{key}/config",
json.dumps(value),
retain=True,
f"homeassistant/sensor/{key}/config", json.dumps(value)
)
client.publish(AVAILABILITY_TOPIC, "online", retain=True)
self._ready.set()
def on_disconnect(self, client, userdata, rc):
log.error("Lost connection to MQTT broker")
@ -142,10 +113,6 @@ class Daemon:
print("Message", client, userdata, msg)
def adj(value, p=10):
return int(value * p) / p
def main():
logging.basicConfig(level=logging.DEBUG)
daemon = Daemon()