Move to ChatGPT canvas for improvements
This commit is contained in:
@@ -12,16 +12,17 @@ MQTT_USERNAME = os.getenv('MQTT_USERNAME', 'your_username')
|
|||||||
MQTT_PASSWORD = os.getenv('MQTT_PASSWORD', 'your_password')
|
MQTT_PASSWORD = os.getenv('MQTT_PASSWORD', 'your_password')
|
||||||
MQTT_SNAPSHOT_TOPIC = "/frigate/patiocam/person/snapshot"
|
MQTT_SNAPSHOT_TOPIC = "/frigate/patiocam/person/snapshot"
|
||||||
MQTT_TOPIC_PUBLISH = "homeassistant/sensor/dayglo_rating/state"
|
MQTT_TOPIC_PUBLISH = "homeassistant/sensor/dayglo_rating/state"
|
||||||
DAYGLO_THRESHOLD_TOPIC = "homeassistant/sensor/dayglo_threshold/state"
|
|
||||||
DISCOVERY_PREFIX = "homeassistant"
|
DISCOVERY_PREFIX = "homeassistant"
|
||||||
|
|
||||||
# Default threshold
|
# Default rating
|
||||||
dayglo_threshold = 50
|
last_rating = 0
|
||||||
|
|
||||||
def on_connect(client, userdata, flags, rc):
|
def on_connect(client, userdata, flags, rc):
|
||||||
print("Connected with result code", rc)
|
print("Connected with result code", rc)
|
||||||
client.subscribe(MQTT_SNAPSHOT_TOPIC)
|
client.subscribe(MQTT_SNAPSHOT_TOPIC)
|
||||||
publish_discovery_configurations()
|
publish_discovery_configurations()
|
||||||
|
# Publish initial rating of 0
|
||||||
|
publish_rating(0)
|
||||||
|
|
||||||
def publish_discovery_configurations():
|
def publish_discovery_configurations():
|
||||||
rating_config = {
|
rating_config = {
|
||||||
@@ -32,26 +33,17 @@ def publish_discovery_configurations():
|
|||||||
"icon": "mdi:brush",
|
"icon": "mdi:brush",
|
||||||
"unique_id": "mqtt_dayglo_rating"
|
"unique_id": "mqtt_dayglo_rating"
|
||||||
}
|
}
|
||||||
threshold_config = {
|
|
||||||
"name": "Dayglo Threshold",
|
|
||||||
"state_topic": DAYGLO_THRESHOLD_TOPIC,
|
|
||||||
"unit_of_measurement": "%",
|
|
||||||
"value_template": "{{ value_json.threshold }}",
|
|
||||||
"icon": "mdi:tune-vertical",
|
|
||||||
"unique_id": "mqtt_dayglo_threshold"
|
|
||||||
}
|
|
||||||
|
|
||||||
client.publish(f"{DISCOVERY_PREFIX}/sensor/dayglo_rating/config", json.dumps(rating_config), retain=True)
|
client.publish(f"{DISCOVERY_PREFIX}/sensor/dayglo_rating/config", json.dumps(rating_config), retain=True)
|
||||||
client.publish(f"{DISCOVERY_PREFIX}/sensor/dayglo_threshold/config", json.dumps(threshold_config), retain=True)
|
|
||||||
|
def publish_rating(rating):
|
||||||
|
global last_rating
|
||||||
|
last_rating = rating
|
||||||
|
client.publish(MQTT_TOPIC_PUBLISH, json.dumps({"rating": rating}))
|
||||||
|
|
||||||
def on_message(client, userdata, msg):
|
def on_message(client, userdata, msg):
|
||||||
if msg.topic == MQTT_SNAPSHOT_TOPIC:
|
if msg.topic == MQTT_SNAPSHOT_TOPIC:
|
||||||
print("Snapshot received")
|
print("Snapshot received")
|
||||||
process_snapshot(msg.payload)
|
process_snapshot(msg.payload)
|
||||||
elif msg.topic == DAYGLO_THRESHOLD_TOPIC:
|
|
||||||
global dayglo_threshold
|
|
||||||
dayglo_threshold = float(msg.payload.decode('utf-8'))
|
|
||||||
print("Dayglo threshold updated to:", dayglo_threshold)
|
|
||||||
|
|
||||||
def process_snapshot(payload):
|
def process_snapshot(payload):
|
||||||
image_data = base64.b64decode(payload)
|
image_data = base64.b64decode(payload)
|
||||||
@@ -61,7 +53,9 @@ def process_snapshot(payload):
|
|||||||
if image is not None:
|
if image is not None:
|
||||||
rating = calculate_dayglo_rating(image)
|
rating = calculate_dayglo_rating(image)
|
||||||
print("Dayglo Rating calculated:", rating)
|
print("Dayglo Rating calculated:", rating)
|
||||||
client.publish(MQTT_TOPIC_PUBLISH, json.dumps({"rating": rating}))
|
publish_rating(rating)
|
||||||
|
else:
|
||||||
|
print("Invalid image received")
|
||||||
|
|
||||||
def calculate_dayglo_rating(image):
|
def calculate_dayglo_rating(image):
|
||||||
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
|
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
|
||||||
@@ -73,7 +67,6 @@ def calculate_dayglo_rating(image):
|
|||||||
rating = (dayglo_pixels / total_pixels) * 100
|
rating = (dayglo_pixels / total_pixels) * 100
|
||||||
return rating
|
return rating
|
||||||
|
|
||||||
#client = mqtt.Client()
|
|
||||||
client = mqtt.Client(protocol=mqtt.MQTTv311)
|
client = mqtt.Client(protocol=mqtt.MQTTv311)
|
||||||
client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)
|
client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)
|
||||||
client.on_connect = on_connect
|
client.on_connect = on_connect
|
||||||
|
|||||||
Reference in New Issue
Block a user