Reworked some JSON building using the json.dumps() function
ci/woodpecker/push/woodpecker Pipeline failed
ci/woodpecker/push/woodpecker Pipeline failed
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import time
|
||||
from machine import Pin, SoftI2C
|
||||
import json
|
||||
import network
|
||||
from simple_mqtt import SimpleMQTT
|
||||
|
||||
@@ -31,14 +32,12 @@ else:
|
||||
# Home Assistant MQTT Discovery
|
||||
# -------------------------------------------------------------
|
||||
def register_ha_discovery(mqtt, sensor_type):
|
||||
device_info = (
|
||||
f'"device":{{'
|
||||
f'"identifiers":["{Config.NODE_ID}"],'
|
||||
f'"name":"{NODE_NAME}",'
|
||||
f'"model":"Pico 2 W ({sensor_type.upper()})",'
|
||||
f'"manufacturer":"Raspberry Pi"'
|
||||
f"}}"
|
||||
)
|
||||
device_info = {
|
||||
"identifiers": [Config.NODE_ID],
|
||||
"name": NODE_NAME,
|
||||
"model": f"Pico 2 W ({sensor_type.upper()})",
|
||||
"manufacturer": "Raspberry Pi",
|
||||
}
|
||||
|
||||
configs = [
|
||||
{
|
||||
@@ -58,31 +57,29 @@ def register_ha_discovery(mqtt, sensor_type):
|
||||
]
|
||||
|
||||
if sensor_type == "bme280":
|
||||
configs.append(
|
||||
{
|
||||
configs.append({
|
||||
"id": "pressure",
|
||||
"name": "Pressure",
|
||||
"unit": "hPa",
|
||||
"cls": "atmospheric_pressure",
|
||||
"val_tpl": "{{ value_json.pressure }}",
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
for c in configs:
|
||||
topic = f"homeassistant/sensor/{Config.NODE_ID}/{c['id']}/config"
|
||||
unique_id = f"{Config.NODE_ID}_{c['id']}"
|
||||
payload = (
|
||||
f'{{"name":"{c["name"]}",'
|
||||
f'"has_entity_name":true,'
|
||||
f'"unique_id":"{unique_id}",'
|
||||
f'"device_class":"{c["cls"]}",'
|
||||
f'"state_class":"measurement",'
|
||||
f'"unit_of_measurement":"{c["unit"]}",'
|
||||
f'"state_topic":"{STATE_TOPIC}",'
|
||||
f'"value_template":"{c["val_tpl"]}",'
|
||||
f"{device_info}}}"
|
||||
)
|
||||
mqtt.publish(topic, payload, retain=True)
|
||||
payload = {
|
||||
"name": c["name"],
|
||||
"has_entity_name": True,
|
||||
"unique_id": f"{Config.NODE_ID}_{c['id']}",
|
||||
"device_class": c["cls"],
|
||||
"state_class": "measurement",
|
||||
"unit_of_measurement": c["unit"],
|
||||
"state_topic": STATE_TOPIC,
|
||||
"value_template": c["val_tpl"],
|
||||
"device": device_info,
|
||||
}
|
||||
|
||||
mqtt.publish(topic, json.dumps(payload), retain=True)
|
||||
print(f"Registered HA discovery: {c['id']}")
|
||||
|
||||
|
||||
@@ -148,17 +145,8 @@ def main():
|
||||
try:
|
||||
data = sensor.read_values()
|
||||
|
||||
if data.get("pressure") is not None:
|
||||
state_json = (
|
||||
f'{{"temperature":{data["temperature"]},'
|
||||
f'"humidity":{data["humidity"]},'
|
||||
f'"pressure":{data["pressure"]}}}'
|
||||
)
|
||||
else:
|
||||
state_json = (
|
||||
f'{{"temperature":{data["temperature"]},'
|
||||
f'"humidity":{data["humidity"]}}}'
|
||||
)
|
||||
payload = {k: v for k, v in data.items() if v is not None}
|
||||
state_json = json.dumps(payload)
|
||||
|
||||
mqtt.publish(STATE_TOPIC, state_json)
|
||||
print(f"[{NODE_NAME}] Published: {state_json}")
|
||||
|
||||
Reference in New Issue
Block a user