From 389da5b1150b0042f59701f14996e1658328b85c Mon Sep 17 00:00:00 2001 From: Torrie Fischer Date: Sat, 23 Dec 2023 11:12:18 +0100 Subject: [PATCH] platform: arduino: mqtt: implement idle state support --- src/platform/arduino/MQTTTelemetry.cpp | 36 ++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/platform/arduino/MQTTTelemetry.cpp b/src/platform/arduino/MQTTTelemetry.cpp index 8daf1a8..131be55 100644 --- a/src/platform/arduino/MQTTTelemetry.cpp +++ b/src/platform/arduino/MQTTTelemetry.cpp @@ -96,6 +96,10 @@ const MQTTEntity Lightswitch { "light", Device, "lightswitch" }; +const MQTTEntity IdleSwitch { + "switch", Device, "idle" +}; + const MQTTEntity FPSSensor { "sensor", Device, "fps" }; @@ -145,6 +149,12 @@ MQTTTelemetry::onMQTTOnline() publishDoc(Lightswitch.configTopic().c_str(), true); m_mqtt.subscribe(Lightswitch.commandTopic().c_str()); + m_json.clear(); + IdleSwitch.toJson(m_json); + + publishDoc(IdleSwitch.configTopic().c_str(), true); + m_mqtt.subscribe(IdleSwitch.commandTopic().c_str()); + publishScenes(); m_json.clear(); @@ -174,6 +184,7 @@ MQTTTelemetry::handleEventOnline(const InputEvent& evt) } } else { String statTopic = Lightswitch.statTopic(); + String idleTopic = IdleSwitch.statTopic(); if (evt.intent == InputEvent::SetPower) { m_json.clear(); m_isOn = evt.asInt() ? true : false; @@ -196,6 +207,14 @@ MQTTTelemetry::handleEventOnline(const InputEvent& evt) m_json.clear(); m_json["state"] = m_isOn ? "ON" : "OFF"; publishDoc(statTopic.c_str()); + } else if (evt.intent == InputEvent::IdleStart) { + m_json.clear(); + m_json["state"] = "ON"; + publishDoc(idleTopic.c_str()); + } else if (evt.intent == InputEvent::IdleStop) { + m_json.clear(); + m_json["state"] = "OFF"; + publishDoc(idleTopic.c_str()); } } } @@ -282,6 +301,7 @@ MQTTTelemetry::publishHeartbeat() void MQTTTelemetry::callback(char* topic, const char* payload) { + Log.notice("MQTT: %s", topic); setEvent(InputEvent::NetworkActivity); if (Lightswitch.isCommandTopic(topic)) { deserializeJson(m_json, payload); @@ -292,7 +312,7 @@ MQTTTelemetry::callback(char* topic, const char* payload) setEvent(InputEvent{InputEvent::SetPower, true}); } else if (m_json["state"] == "OFF") { Log.notice("Turning off power"); - setEvent(InputEvent{InputEvent::SetScene, "Idle"}); + setEvent(InputEvent::IdleStart); setEvent(InputEvent{InputEvent::SetPower, false}); } } @@ -308,17 +328,23 @@ MQTTTelemetry::callback(char* topic, const char* payload) setEvent(InputEvent{InputEvent::SetBrightness, m_json["brightness"].as()}); } - Log.notice("Event done."); + } else if (IdleSwitch.isCommandTopic(topic)) { + if (strcmp(payload, "ON") == 0) { + Log.notice("Activating idle switch"); + setEvent(InputEvent::IdleStart); + } else if (strcmp(payload, "OFF") == 0) { + Log.notice("Deactivating idle switch"); + setEvent(InputEvent::IdleStop); + } } else { for(auto scene : Static::instance()->scenes()) { - String strName{scene.name}; MQTTEntity sceneObj { "scene", Device, - strName + scene.name }; if (sceneObj.isCommandTopic(topic)) { - setEvent(InputEvent{InputEvent::SetScene, scene.name}); + setEvent(InputEvent{InputEvent::SetScene, scene.name.c_str()}); return; } }