VFD functionality added to HA
This commit is contained in:
124
gauge.py
124
gauge.py
@@ -293,6 +293,34 @@ _status_red_effect = [None] * num_gauges
|
|||||||
_status_green_effect= [None] * num_gauges
|
_status_green_effect= [None] * num_gauges
|
||||||
_bl_effect = [None] * num_gauges
|
_bl_effect = [None] * num_gauges
|
||||||
|
|
||||||
|
vfd_text = ""
|
||||||
|
vfd_decimal_point = False
|
||||||
|
vfd_alarm = False
|
||||||
|
|
||||||
|
|
||||||
|
def _build_vfd_command():
|
||||||
|
suffix = ""
|
||||||
|
if vfd_decimal_point:
|
||||||
|
suffix += "."
|
||||||
|
if vfd_alarm:
|
||||||
|
suffix += "!"
|
||||||
|
|
||||||
|
if vfd_text:
|
||||||
|
return f"VFD {vfd_text}{suffix}"
|
||||||
|
if suffix:
|
||||||
|
return f"VFD 0{suffix}"
|
||||||
|
return "VFD"
|
||||||
|
|
||||||
|
|
||||||
|
def send_vfd_state():
|
||||||
|
arduino_send(_build_vfd_command())
|
||||||
|
|
||||||
|
|
||||||
|
def publish_vfd_state(client):
|
||||||
|
client.publish(vfd_topics["state"], vfd_text, retain=True)
|
||||||
|
client.publish(vfd_topics["decimal_point_state"], b"ON" if vfd_decimal_point else b"OFF", retain=True)
|
||||||
|
client.publish(vfd_topics["alarm_state"], b"ON" if vfd_alarm else b"OFF", retain=True)
|
||||||
|
|
||||||
|
|
||||||
def _backlight_changed(gauge_idx, new_color, new_on, new_brightness):
|
def _backlight_changed(gauge_idx, new_color, new_on, new_brightness):
|
||||||
return (
|
return (
|
||||||
@@ -419,6 +447,18 @@ def make_gauge_topics(prefix, gauge_id):
|
|||||||
|
|
||||||
gauge_topics = [make_gauge_topics(MQTT_PREFIX, g["id"]) for g in gauges]
|
gauge_topics = [make_gauge_topics(MQTT_PREFIX, g["id"]) for g in gauges]
|
||||||
|
|
||||||
|
vfd_topics = {
|
||||||
|
"set": f"{MQTT_PREFIX}/vfd/set",
|
||||||
|
"state": f"{MQTT_PREFIX}/vfd/state",
|
||||||
|
"disc": f"homeassistant/text/{MQTT_CLIENT_ID}_vfd/config",
|
||||||
|
"decimal_point": f"{MQTT_PREFIX}/vfd/decimal_point/set",
|
||||||
|
"decimal_point_state": f"{MQTT_PREFIX}/vfd/decimal_point/state",
|
||||||
|
"decimal_point_disc": f"homeassistant/switch/{MQTT_CLIENT_ID}_vfd_decimal_point/config",
|
||||||
|
"alarm": f"{MQTT_PREFIX}/vfd/alarm/set",
|
||||||
|
"alarm_state": f"{MQTT_PREFIX}/vfd/alarm/state",
|
||||||
|
"alarm_disc": f"homeassistant/switch/{MQTT_CLIENT_ID}_vfd_alarm/config",
|
||||||
|
}
|
||||||
|
|
||||||
T_SET = f"{MQTT_PREFIX}/set"
|
T_SET = f"{MQTT_PREFIX}/set"
|
||||||
T_ZERO = f"{MQTT_PREFIX}/zero"
|
T_ZERO = f"{MQTT_PREFIX}/zero"
|
||||||
|
|
||||||
@@ -500,12 +540,35 @@ def check_wifi():
|
|||||||
|
|
||||||
|
|
||||||
def on_message(topic, payload):
|
def on_message(topic, payload):
|
||||||
|
global vfd_text, vfd_decimal_point, vfd_alarm
|
||||||
|
|
||||||
if client_ref is None:
|
if client_ref is None:
|
||||||
return
|
return
|
||||||
topic = topic.decode()
|
topic = topic.decode()
|
||||||
payload = payload.decode().strip()
|
payload = payload.decode().strip()
|
||||||
info(f"MQTT rx {topic} {payload}")
|
info(f"MQTT rx {topic} {payload}")
|
||||||
|
|
||||||
|
if topic == vfd_topics["set"]:
|
||||||
|
vfd_text = payload.upper()
|
||||||
|
send_vfd_state()
|
||||||
|
publish_vfd_state(client_ref)
|
||||||
|
info(f"VFD text -> {vfd_text}")
|
||||||
|
return
|
||||||
|
|
||||||
|
if topic == vfd_topics["decimal_point"]:
|
||||||
|
vfd_decimal_point = payload.upper() == "ON"
|
||||||
|
send_vfd_state()
|
||||||
|
publish_vfd_state(client_ref)
|
||||||
|
info(f"VFD decimal point -> {'ON' if vfd_decimal_point else 'OFF'}")
|
||||||
|
return
|
||||||
|
|
||||||
|
if topic == vfd_topics["alarm"]:
|
||||||
|
vfd_alarm = payload.upper() == "ON"
|
||||||
|
send_vfd_state()
|
||||||
|
publish_vfd_state(client_ref)
|
||||||
|
info(f"VFD alarm -> {'ON' if vfd_alarm else 'OFF'}")
|
||||||
|
return
|
||||||
|
|
||||||
for i, gt in enumerate(gauge_topics):
|
for i, gt in enumerate(gauge_topics):
|
||||||
if topic == gt["zero"]:
|
if topic == gt["zero"]:
|
||||||
info(f"Home command received for gauge {i}")
|
info(f"Home command received for gauge {i}")
|
||||||
@@ -723,6 +786,9 @@ def on_message(topic, payload):
|
|||||||
def _subscribe_all(c):
|
def _subscribe_all(c):
|
||||||
c.subscribe(f"{MQTT_PREFIX}/set")
|
c.subscribe(f"{MQTT_PREFIX}/set")
|
||||||
c.subscribe(f"{MQTT_PREFIX}/zero")
|
c.subscribe(f"{MQTT_PREFIX}/zero")
|
||||||
|
c.subscribe(vfd_topics["set"])
|
||||||
|
c.subscribe(vfd_topics["decimal_point"])
|
||||||
|
c.subscribe(vfd_topics["alarm"])
|
||||||
for i in range(num_gauges):
|
for i in range(num_gauges):
|
||||||
prefix = f"{MQTT_PREFIX}/gauge{i}"
|
prefix = f"{MQTT_PREFIX}/gauge{i}"
|
||||||
c.subscribe(f"{prefix}/set")
|
c.subscribe(f"{prefix}/set")
|
||||||
@@ -995,6 +1061,61 @@ def _append_backlight_status_discovery(entries, dev_ref):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _append_vfd_discovery(entries, dev_ref):
|
||||||
|
entries.append(
|
||||||
|
(
|
||||||
|
vfd_topics["disc"],
|
||||||
|
{
|
||||||
|
"name": "VFD Display",
|
||||||
|
"unique_id": f"{MQTT_CLIENT_ID}_vfd",
|
||||||
|
"cmd_t": vfd_topics["set"],
|
||||||
|
"stat_t": vfd_topics["state"],
|
||||||
|
"avty_t": gauge_topics[0]["status"],
|
||||||
|
"max": 4,
|
||||||
|
"mode": "text",
|
||||||
|
"icon": "mdi:alpha-box",
|
||||||
|
"pattern": "^[0-9A-Fa-f-]{0,4}$",
|
||||||
|
"dev": dev_ref,
|
||||||
|
},
|
||||||
|
"Discovery: VFD text",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
entries.append(
|
||||||
|
(
|
||||||
|
vfd_topics["decimal_point_disc"],
|
||||||
|
{
|
||||||
|
"name": "VFD Decimal Point",
|
||||||
|
"unique_id": f"{MQTT_CLIENT_ID}_vfd_decimal_point",
|
||||||
|
"cmd_t": vfd_topics["decimal_point"],
|
||||||
|
"stat_t": vfd_topics["decimal_point_state"],
|
||||||
|
"avty_t": gauge_topics[0]["status"],
|
||||||
|
"pl_on": "ON",
|
||||||
|
"pl_off": "OFF",
|
||||||
|
"icon": "mdi:circle-small",
|
||||||
|
"dev": dev_ref,
|
||||||
|
},
|
||||||
|
"Discovery: VFD decimal point",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
entries.append(
|
||||||
|
(
|
||||||
|
vfd_topics["alarm_disc"],
|
||||||
|
{
|
||||||
|
"name": "VFD Alarm",
|
||||||
|
"unique_id": f"{MQTT_CLIENT_ID}_vfd_alarm",
|
||||||
|
"cmd_t": vfd_topics["alarm"],
|
||||||
|
"stat_t": vfd_topics["alarm_state"],
|
||||||
|
"avty_t": gauge_topics[0]["status"],
|
||||||
|
"pl_on": "ON",
|
||||||
|
"pl_off": "OFF",
|
||||||
|
"icon": "mdi:alarm-bell",
|
||||||
|
"dev": dev_ref,
|
||||||
|
},
|
||||||
|
"Discovery: VFD alarm",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def schedule_discovery():
|
def schedule_discovery():
|
||||||
global _discovery_queue, _discovery_idx, _last_discovery_ms
|
global _discovery_queue, _discovery_idx, _last_discovery_ms
|
||||||
_dev_ref = _DEVICE
|
_dev_ref = _DEVICE
|
||||||
@@ -1005,6 +1126,7 @@ def schedule_discovery():
|
|||||||
_append_acceleration_discovery(entries, _dev_ref)
|
_append_acceleration_discovery(entries, _dev_ref)
|
||||||
_append_indicator_led_discovery(entries, _dev_ref)
|
_append_indicator_led_discovery(entries, _dev_ref)
|
||||||
_append_backlight_status_discovery(entries, _dev_ref)
|
_append_backlight_status_discovery(entries, _dev_ref)
|
||||||
|
_append_vfd_discovery(entries, _dev_ref)
|
||||||
_discovery_queue = entries
|
_discovery_queue = entries
|
||||||
_discovery_idx = 0
|
_discovery_idx = 0
|
||||||
_last_discovery_ms = 0
|
_last_discovery_ms = 0
|
||||||
@@ -1052,12 +1174,14 @@ def publish_state(client):
|
|||||||
client.publish(gt["state"], str(gauge_targets[i]))
|
client.publish(gt["state"], str(gauge_targets[i]))
|
||||||
client.publish(gt["speed_state"], str(gauge_speeds[i]), retain=True)
|
client.publish(gt["speed_state"], str(gauge_speeds[i]), retain=True)
|
||||||
client.publish(gt["acceleration_state"], str(gauge_accelerations[i]), retain=True)
|
client.publish(gt["acceleration_state"], str(gauge_accelerations[i]), retain=True)
|
||||||
|
publish_vfd_state(client)
|
||||||
|
|
||||||
|
|
||||||
def apply_motion_defaults():
|
def apply_motion_defaults():
|
||||||
for i in range(num_gauges):
|
for i in range(num_gauges):
|
||||||
gauge_set_speed(i, gauge_speeds[i])
|
gauge_set_speed(i, gauge_speeds[i])
|
||||||
gauge_set_acceleration(i, gauge_accelerations[i])
|
gauge_set_acceleration(i, gauge_accelerations[i])
|
||||||
|
send_vfd_state()
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user