platform: arduino: mqtt: implement idle state support

This commit is contained in:
Torrie Fischer 2023-12-23 11:12:18 +01:00
parent 83b3af58f7
commit 389da5b115

View File

@ -96,6 +96,10 @@ const MQTTEntity Lightswitch {
"light", Device, "lightswitch" "light", Device, "lightswitch"
}; };
const MQTTEntity IdleSwitch {
"switch", Device, "idle"
};
const MQTTEntity FPSSensor { const MQTTEntity FPSSensor {
"sensor", Device, "fps" "sensor", Device, "fps"
}; };
@ -145,6 +149,12 @@ MQTTTelemetry::onMQTTOnline()
publishDoc(Lightswitch.configTopic().c_str(), true); publishDoc(Lightswitch.configTopic().c_str(), true);
m_mqtt.subscribe(Lightswitch.commandTopic().c_str()); 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(); publishScenes();
m_json.clear(); m_json.clear();
@ -174,6 +184,7 @@ MQTTTelemetry::handleEventOnline(const InputEvent& evt)
} }
} else { } else {
String statTopic = Lightswitch.statTopic(); String statTopic = Lightswitch.statTopic();
String idleTopic = IdleSwitch.statTopic();
if (evt.intent == InputEvent::SetPower) { if (evt.intent == InputEvent::SetPower) {
m_json.clear(); m_json.clear();
m_isOn = evt.asInt() ? true : false; m_isOn = evt.asInt() ? true : false;
@ -196,6 +207,14 @@ MQTTTelemetry::handleEventOnline(const InputEvent& evt)
m_json.clear(); m_json.clear();
m_json["state"] = m_isOn ? "ON" : "OFF"; m_json["state"] = m_isOn ? "ON" : "OFF";
publishDoc(statTopic.c_str()); 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 void
MQTTTelemetry::callback(char* topic, const char* payload) MQTTTelemetry::callback(char* topic, const char* payload)
{ {
Log.notice("MQTT: %s", topic);
setEvent(InputEvent::NetworkActivity); setEvent(InputEvent::NetworkActivity);
if (Lightswitch.isCommandTopic(topic)) { if (Lightswitch.isCommandTopic(topic)) {
deserializeJson(m_json, payload); deserializeJson(m_json, payload);
@ -292,7 +312,7 @@ MQTTTelemetry::callback(char* topic, const char* payload)
setEvent(InputEvent{InputEvent::SetPower, true}); setEvent(InputEvent{InputEvent::SetPower, true});
} else if (m_json["state"] == "OFF") { } else if (m_json["state"] == "OFF") {
Log.notice("Turning off power"); Log.notice("Turning off power");
setEvent(InputEvent{InputEvent::SetScene, "Idle"}); setEvent(InputEvent::IdleStart);
setEvent(InputEvent{InputEvent::SetPower, false}); setEvent(InputEvent{InputEvent::SetPower, false});
} }
} }
@ -308,17 +328,23 @@ MQTTTelemetry::callback(char* topic, const char* payload)
setEvent(InputEvent{InputEvent::SetBrightness, m_json["brightness"].as<int>()}); setEvent(InputEvent{InputEvent::SetBrightness, m_json["brightness"].as<int>()});
} }
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 { } else {
for(auto scene : Static<Sequencer>::instance()->scenes()) { for(auto scene : Static<Sequencer>::instance()->scenes()) {
String strName{scene.name};
MQTTEntity sceneObj { MQTTEntity sceneObj {
"scene", "scene",
Device, Device,
strName scene.name
}; };
if (sceneObj.isCommandTopic(topic)) { if (sceneObj.isCommandTopic(topic)) {
setEvent(InputEvent{InputEvent::SetScene, scene.name}); setEvent(InputEvent{InputEvent::SetScene, scene.name.c_str()});
return; return;
} }
} }