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"
};
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<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 {
for(auto scene : Static<Sequencer>::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;
}
}