split code up into modules and add pubsub and timers
This commit is contained in:
parent
05c470197f
commit
001f99db7c
7 changed files with 228 additions and 137 deletions
73
CIRCUITPY/weather_station/bme280.py
Executable file
73
CIRCUITPY/weather_station/bme280.py
Executable file
|
|
@ -0,0 +1,73 @@
|
|||
import board
|
||||
import busio
|
||||
from adafruit_bme280 import basic as adafruit_bme280
|
||||
|
||||
class BME280():
|
||||
mqtt_state_topic = "homeassistant/sensor/balcony_weather_station/bme_280_001/state"
|
||||
mqtt_config_topic_temperature = "homeassistant/sensor/balcony_weather_station/bme_280_001_temp/config"
|
||||
mqtt_config_topic_humidity = "homeassistant/sensor/balcony_weather_station/bme_280_001_humi/config"
|
||||
mqtt_config_topic_pressure = "homeassistant/sensor/balcony_weather_station/bme_280_001_pres/config"
|
||||
config_msg_temperature = """{
|
||||
"name": "BEM 280 Temperature",
|
||||
"device_class": "temperature",
|
||||
"unit_of_measurement": "°C",
|
||||
"value_template": "{{ value_json.temperature}}",
|
||||
"state_topic": "homeassistant/sensor/balcony_weather_station/bme_280_001/state",
|
||||
"unique_id": "balcony_weather_station_bme_280_001_temperature",
|
||||
"device": {
|
||||
"identifiers": "balcony_weather_station_001",
|
||||
"name": "Balcony Weather Station"
|
||||
}
|
||||
}"""
|
||||
config_msg_pressure = """{
|
||||
"name": "BEM 280 Pressure",
|
||||
"device_class": "pressure",
|
||||
"unit_of_measurement": "hPa",
|
||||
"value_template": "{{ value_json.pressure}}",
|
||||
"state_topic": "homeassistant/sensor/balcony_weather_station/bme_280_001/state",
|
||||
"unique_id": "balcony_weather_station_bme_280_001_pressure",
|
||||
"device": {
|
||||
"identifiers": "balcony_weather_station_001",
|
||||
"name": "Balcony Weather Station"
|
||||
}
|
||||
}"""
|
||||
config_msg_humidity = """{
|
||||
"name": "BEM 280 Humidity",
|
||||
"device_class": "humidity",
|
||||
"unit_of_measurement": "%",
|
||||
"value_template": "{{ value_json.humidity}}",
|
||||
"state_topic": "homeassistant/sensor/balcony_weather_station/bme_280_001/state",
|
||||
"unique_id": "balcony_weather_station_bme_280_001_humidity",
|
||||
"device": {
|
||||
"identifiers": "balcony_weather_station_001",
|
||||
"name": "Balcony Weather Station"
|
||||
}
|
||||
}"""
|
||||
|
||||
def __init__(self, pubsub):
|
||||
self.pubsub = pubsub
|
||||
i2c = busio.I2C(scl=board.GP15, sda=board.GP14)
|
||||
self.bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c, 0x76)
|
||||
self.bme280.sea_level_pressure = 1016 # value from https://metar-taf.com/EDDM
|
||||
self.pubsub.publish('mqtt_pub', {
|
||||
'topic': self.mqtt_config_topic_temperature,
|
||||
'msg': self.config_msg_temperature
|
||||
})
|
||||
self.pubsub.publish('mqtt_pub', {
|
||||
'topic': self.mqtt_config_topic_humidity,
|
||||
'msg': self.config_msg_humidity
|
||||
})
|
||||
self.pubsub.publish('mqtt_pub', {
|
||||
'topic': self.mqtt_config_topic_pressure,
|
||||
'msg': self.config_msg_pressure
|
||||
})
|
||||
self.pubsub.subscribe('tick 120', self._send)
|
||||
|
||||
def _send(self, _):
|
||||
msg = f"""{{
|
||||
"temperature": {self.bme280.temperature:.1f},
|
||||
"humidity": {self.bme280.relative_humidity:.1f},
|
||||
"pressure": {self.bme280.pressure:.1f}
|
||||
}}"""
|
||||
self.pubsub.publish('mqtt_pub', {'topic': self.mqtt_state_topic, 'msg': msg})
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue