|
@ -14,124 +14,185 @@ bool _haEnabled = false; |
|
|
bool _haSendFlag = false; |
|
|
bool _haSendFlag = false; |
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
void _haWebSocketOnSend(JsonObject& root) { |
|
|
|
|
|
root["haVisible"] = 1; |
|
|
|
|
|
root["haPrefix"] = getSetting("haPrefix", HOMEASSISTANT_PREFIX); |
|
|
|
|
|
root["haEnabled"] = getSetting("haEnabled", HOMEASSISTANT_ENABLED).toInt() == 1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// SENSORS
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
#if SENSOR_SUPPORT
|
|
|
#if SENSOR_SUPPORT
|
|
|
|
|
|
|
|
|
void _haSendMagnitude(unsigned char i) { |
|
|
|
|
|
|
|
|
void _haSendMagnitude(unsigned char i, JsonObject& config) { |
|
|
|
|
|
|
|
|
String output; |
|
|
|
|
|
|
|
|
unsigned char type = magnitudeType(i); |
|
|
|
|
|
config["name"] = getSetting("hostname") + String(" ") + magnitudeTopic(type); |
|
|
|
|
|
config["platform"] = "mqtt"; |
|
|
|
|
|
config["device_class"] = "sensor"; |
|
|
|
|
|
config["state_topic"] = mqttTopic(magnitudeTopicIndex(i).c_str(), false); |
|
|
|
|
|
config["unit_of_measurement"] = magnitudeUnits(type); |
|
|
|
|
|
|
|
|
if (_haEnabled) { |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
DynamicJsonBuffer jsonBuffer; |
|
|
|
|
|
JsonObject& root = jsonBuffer.createObject(); |
|
|
|
|
|
|
|
|
void _haSendMagnitudes() { |
|
|
|
|
|
|
|
|
unsigned char type = magnitudeType(i); |
|
|
|
|
|
|
|
|
for (unsigned char i=0; i<magnitudeCount(); i++) { |
|
|
|
|
|
|
|
|
root["device_class"] = "sensor"; |
|
|
|
|
|
root["name"] = getSetting("hostname") + String(" ") + magnitudeTopic(type); |
|
|
|
|
|
root["state_topic"] = mqttTopic(magnitudeTopicIndex(i).c_str(), false); |
|
|
|
|
|
root["unit_of_measurement"] = magnitudeUnits(type); |
|
|
|
|
|
|
|
|
String topic = getSetting("haPrefix", HOMEASSISTANT_PREFIX) + |
|
|
|
|
|
"/sensor/" + |
|
|
|
|
|
getSetting("hostname") + "_" + String(i) + |
|
|
|
|
|
"/config"; |
|
|
|
|
|
|
|
|
|
|
|
String output; |
|
|
|
|
|
if (_haEnabled) { |
|
|
|
|
|
DynamicJsonBuffer jsonBuffer; |
|
|
|
|
|
JsonObject& config = jsonBuffer.createObject(); |
|
|
|
|
|
_haSendMagnitude(i, config); |
|
|
|
|
|
config.printTo(output); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
root.printTo(output); |
|
|
|
|
|
|
|
|
mqttSendRaw(topic.c_str(), output.c_str()); |
|
|
|
|
|
mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
String topic = getSetting("haPrefix", HOMEASSISTANT_PREFIX) + |
|
|
|
|
|
"/sensor/" + |
|
|
|
|
|
getSetting("hostname") + "_" + String(i) + |
|
|
|
|
|
"/config"; |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
mqttSendRaw(topic.c_str(), output.c_str()); |
|
|
|
|
|
mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true); |
|
|
|
|
|
|
|
|
#endif // SENSOR_SUPPORT
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
// SWITCHES & LIGHTS
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
void _haSendMagnitudes() { |
|
|
|
|
|
for (unsigned char i=0; i<magnitudeCount(); i++) { |
|
|
|
|
|
_haSendMagnitude(i); |
|
|
|
|
|
|
|
|
void _haSendSwitch(unsigned char i, JsonObject& config) { |
|
|
|
|
|
|
|
|
|
|
|
String name = getSetting("hostname"); |
|
|
|
|
|
if (relayCount() > 1) { |
|
|
|
|
|
name += String(" #") + String(i); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
config["name"] = name; |
|
|
|
|
|
config["platform"] = "mqtt"; |
|
|
|
|
|
|
|
|
|
|
|
if (relayCount()) { |
|
|
|
|
|
config["state_topic"] = mqttTopic(MQTT_TOPIC_RELAY, i, false); |
|
|
|
|
|
config["command_topic"] = mqttTopic(MQTT_TOPIC_RELAY, i, true); |
|
|
|
|
|
config["payload_on"] = String("1"); |
|
|
|
|
|
config["payload_off"] = String("0"); |
|
|
|
|
|
config["availability_topic"] = mqttTopic(MQTT_TOPIC_STATUS, false); |
|
|
|
|
|
config["payload_available"] = String("1"); |
|
|
|
|
|
config["payload_not_available"] = String("0"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
void _haSendSwitch(unsigned char i) { |
|
|
|
|
|
|
|
|
#if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
|
|
|
|
|
|
|
|
|
String output; |
|
|
|
|
|
|
|
|
if (i == 0) { |
|
|
|
|
|
|
|
|
if (_haEnabled) { |
|
|
|
|
|
|
|
|
if (lightHasColor()) { |
|
|
|
|
|
config["brightness_state_topic"] = mqttTopic(MQTT_TOPIC_BRIGHTNESS, false); |
|
|
|
|
|
config["brightness_command_topic"] = mqttTopic(MQTT_TOPIC_BRIGHTNESS, true); |
|
|
|
|
|
config["rgb_state_topic"] = mqttTopic(MQTT_TOPIC_COLOR_RGB, false); |
|
|
|
|
|
config["rgb_command_topic"] = mqttTopic(MQTT_TOPIC_COLOR_RGB, true); |
|
|
|
|
|
config["color_temp_command_topic"] = mqttTopic(MQTT_TOPIC_MIRED, true); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
DynamicJsonBuffer jsonBuffer; |
|
|
|
|
|
JsonObject& root = jsonBuffer.createObject(); |
|
|
|
|
|
|
|
|
if (lightChannels() > 3) { |
|
|
|
|
|
config["white_value_state_topic"] = mqttTopic(MQTT_TOPIC_CHANNEL, 3, false); |
|
|
|
|
|
config["white_value_command_topic"] = mqttTopic(MQTT_TOPIC_CHANNEL, 3, true); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
String name = getSetting("hostname"); |
|
|
|
|
|
if (relayCount() > 1) { |
|
|
|
|
|
name += String(" switch #") + String(i); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
root["name"] = name; |
|
|
|
|
|
root["platform"] = "mqtt"; |
|
|
|
|
|
|
|
|
|
|
|
if (relayCount()) { |
|
|
|
|
|
root["state_topic"] = mqttTopic(MQTT_TOPIC_RELAY, i, false); |
|
|
|
|
|
root["command_topic"] = mqttTopic(MQTT_TOPIC_RELAY, i, true); |
|
|
|
|
|
root["payload_on"] = String("1"); |
|
|
|
|
|
root["payload_off"] = String("0"); |
|
|
|
|
|
root["availability_topic"] = mqttTopic(MQTT_TOPIC_STATUS, false); |
|
|
|
|
|
root["payload_available"] = String("1"); |
|
|
|
|
|
root["payload_not_available"] = String("0"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
#endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
|
|
|
|
|
|
|
|
|
#if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (i == 0) { |
|
|
|
|
|
|
|
|
void _haSendSwitches() { |
|
|
|
|
|
|
|
|
if (lightHasColor()) { |
|
|
|
|
|
root["brightness_state_topic"] = mqttTopic(MQTT_TOPIC_BRIGHTNESS, false); |
|
|
|
|
|
root["brightness_command_topic"] = mqttTopic(MQTT_TOPIC_BRIGHTNESS, true); |
|
|
|
|
|
root["rgb_state_topic"] = mqttTopic(MQTT_TOPIC_COLOR_RGB, false); |
|
|
|
|
|
root["rgb_command_topic"] = mqttTopic(MQTT_TOPIC_COLOR_RGB, true); |
|
|
|
|
|
root["color_temp_command_topic"] = mqttTopic(MQTT_TOPIC_MIRED, true); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
#if (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE) || (defined(ITEAD_SLAMPHER))
|
|
|
|
|
|
String type = String("light"); |
|
|
|
|
|
#else
|
|
|
|
|
|
String type = String("switch"); |
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
if (lightChannels() > 3) { |
|
|
|
|
|
root["white_value_state_topic"] = mqttTopic(MQTT_TOPIC_CHANNEL, 3, false); |
|
|
|
|
|
root["white_value_command_topic"] = mqttTopic(MQTT_TOPIC_CHANNEL, 3, true); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
for (unsigned char i=0; i<relayCount(); i++) { |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
String topic = getSetting("haPrefix", HOMEASSISTANT_PREFIX) + |
|
|
|
|
|
"/" + type + |
|
|
|
|
|
"/" + getSetting("hostname") + "_" + String(i) + |
|
|
|
|
|
"/config"; |
|
|
|
|
|
|
|
|
|
|
|
String output; |
|
|
|
|
|
if (_haEnabled) { |
|
|
|
|
|
DynamicJsonBuffer jsonBuffer; |
|
|
|
|
|
JsonObject& config = jsonBuffer.createObject(); |
|
|
|
|
|
_haSendSwitch(i, config); |
|
|
|
|
|
config.printTo(output); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
#endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
|
|
|
|
|
|
|
|
|
mqttSendRaw(topic.c_str(), output.c_str()); |
|
|
|
|
|
mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true); |
|
|
|
|
|
|
|
|
root.printTo(output); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#if LIGHT_PROVIDER == LIGHT_PROVIDER_NONE
|
|
|
|
|
|
String component = String("switch"); |
|
|
|
|
|
#else
|
|
|
|
|
|
String component = String("light"); |
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
String topic = getSetting("haPrefix", HOMEASSISTANT_PREFIX) + |
|
|
|
|
|
"/" + component + |
|
|
|
|
|
"/" + getSetting("hostname") + "_" + String(i) + |
|
|
|
|
|
"/config"; |
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
mqttSendRaw(topic.c_str(), output.c_str()); |
|
|
|
|
|
mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true); |
|
|
|
|
|
|
|
|
String _haGetConfig() { |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
String output; |
|
|
|
|
|
|
|
|
|
|
|
#if (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE) || (defined(ITEAD_SLAMPHER))
|
|
|
|
|
|
String type = String("light"); |
|
|
|
|
|
#else
|
|
|
|
|
|
String type = String("switch"); |
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
void _haSendSwitches() { |
|
|
|
|
|
for (unsigned char i=0; i<relayCount(); i++) { |
|
|
for (unsigned char i=0; i<relayCount(); i++) { |
|
|
_haSendSwitch(i); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DynamicJsonBuffer jsonBuffer; |
|
|
|
|
|
JsonObject& config = jsonBuffer.createObject(); |
|
|
|
|
|
_haSendSwitch(i, config); |
|
|
|
|
|
|
|
|
|
|
|
output += type + ":\n"; |
|
|
|
|
|
bool first = true; |
|
|
|
|
|
for (auto kv : config) { |
|
|
|
|
|
if (first) { |
|
|
|
|
|
output += " - "; |
|
|
|
|
|
first = false; |
|
|
|
|
|
} else { |
|
|
|
|
|
output += " "; |
|
|
|
|
|
} |
|
|
|
|
|
output += kv.key + String(": ") + kv.value.as<String>() + String("\n"); |
|
|
|
|
|
} |
|
|
|
|
|
output += "\n"; |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#if SENSOR_SUPPORT
|
|
|
|
|
|
|
|
|
|
|
|
for (unsigned char i=0; i<magnitudeCount(); i++) { |
|
|
|
|
|
|
|
|
|
|
|
DynamicJsonBuffer jsonBuffer; |
|
|
|
|
|
JsonObject& config = jsonBuffer.createObject(); |
|
|
|
|
|
_haSendMagnitude(i, config); |
|
|
|
|
|
|
|
|
|
|
|
output += "sensor:\n"; |
|
|
|
|
|
bool first = true; |
|
|
|
|
|
for (auto kv : config) { |
|
|
|
|
|
if (first) { |
|
|
|
|
|
output += " - "; |
|
|
|
|
|
first = false; |
|
|
|
|
|
} else { |
|
|
|
|
|
output += " "; |
|
|
|
|
|
} |
|
|
|
|
|
output += kv.key + String(": ") + kv.value.as<String>() + String("\n"); |
|
|
|
|
|
} |
|
|
|
|
|
output += "\n"; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
return output; |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void _haSend() { |
|
|
void _haSend() { |
|
@ -161,6 +222,49 @@ void _haConfigure() { |
|
|
_haSend(); |
|
|
_haSend(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#if WEB_SUPPORT
|
|
|
|
|
|
|
|
|
|
|
|
void _haWebSocketOnSend(JsonObject& root) { |
|
|
|
|
|
root["haVisible"] = 1; |
|
|
|
|
|
root["haPrefix"] = getSetting("haPrefix", HOMEASSISTANT_PREFIX); |
|
|
|
|
|
root["haEnabled"] = getSetting("haEnabled", HOMEASSISTANT_ENABLED).toInt() == 1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void _haWebSocketOnAction(uint32_t client_id, const char * action, JsonObject& data) { |
|
|
|
|
|
if (strcmp(action, "haconfig") == 0) { |
|
|
|
|
|
String output = _haGetConfig(); |
|
|
|
|
|
output.replace(" ", " "); |
|
|
|
|
|
output.replace("\n", "<br />"); |
|
|
|
|
|
output = String("{\"haConfig\": \"") + output + String("\"}"); |
|
|
|
|
|
wsSend(client_id, output.c_str()); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#if TERMINAL_SUPPORT
|
|
|
|
|
|
|
|
|
|
|
|
void _haInitCommands() { |
|
|
|
|
|
settingsRegisterCommand(F("HA.CONFIG"), [](Embedis* e) { |
|
|
|
|
|
DEBUG_MSG(_haGetConfig().c_str()); |
|
|
|
|
|
DEBUG_MSG_P(PSTR("+OK\n")); |
|
|
|
|
|
}); |
|
|
|
|
|
settingsRegisterCommand(F("HA.SEND"), [](Embedis* e) { |
|
|
|
|
|
setSetting("haEnabled", "1"); |
|
|
|
|
|
_haConfigure(); |
|
|
|
|
|
wsSend(_haWebSocketOnSend); |
|
|
|
|
|
DEBUG_MSG_P(PSTR("+OK\n")); |
|
|
|
|
|
}); |
|
|
|
|
|
settingsRegisterCommand(F("HA.CLEAR"), [](Embedis* e) { |
|
|
|
|
|
setSetting("haEnabled", "0"); |
|
|
|
|
|
_haConfigure(); |
|
|
|
|
|
wsSend(_haWebSocketOnSend); |
|
|
|
|
|
DEBUG_MSG_P(PSTR("+OK\n")); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
void haSetup() { |
|
|
void haSetup() { |
|
@ -170,6 +274,7 @@ void haSetup() { |
|
|
#if WEB_SUPPORT
|
|
|
#if WEB_SUPPORT
|
|
|
wsOnSendRegister(_haWebSocketOnSend); |
|
|
wsOnSendRegister(_haWebSocketOnSend); |
|
|
wsOnAfterParseRegister(_haConfigure); |
|
|
wsOnAfterParseRegister(_haConfigure); |
|
|
|
|
|
wsOnActionRegister(_haWebSocketOnAction); |
|
|
#endif
|
|
|
#endif
|
|
|
|
|
|
|
|
|
// On MQTT connect check if we have something to send
|
|
|
// On MQTT connect check if we have something to send
|
|
@ -177,6 +282,11 @@ void haSetup() { |
|
|
if (type == MQTT_CONNECT_EVENT) _haSend(); |
|
|
if (type == MQTT_CONNECT_EVENT) _haSend(); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
#if TERMINAL_SUPPORT
|
|
|
|
|
|
_haInitCommands(); |
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#endif // HOMEASSISTANT_SUPPORT
|
|
|
#endif // HOMEASSISTANT_SUPPORT
|