2017-01-28 13:41:01 +00:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
xdrv_07_domoticz.ino - domoticz support for Tasmota
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Theo Arends
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-05-13 12:02:10 +01:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-05-13 12:02:10 +01:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-01-28 13:41:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_DOMOTICZ
|
|
|
|
|
2018-11-07 09:30:03 +00:00
|
|
|
#define XDRV_07 7
|
|
|
|
|
2020-07-24 14:30:21 +01:00
|
|
|
//#define D_PRFX_DOMOTICZ "Domoticz"
|
|
|
|
#define D_PRFX_DOMOTICZ "Dz"
|
2019-08-11 17:12:18 +01:00
|
|
|
#define D_CMND_IDX "Idx"
|
|
|
|
#define D_CMND_KEYIDX "KeyIdx"
|
|
|
|
#define D_CMND_SWITCHIDX "SwitchIdx"
|
|
|
|
#define D_CMND_SENSORIDX "SensorIdx"
|
|
|
|
#define D_CMND_UPDATETIMER "UpdateTimer"
|
2020-07-24 14:30:21 +01:00
|
|
|
#define D_CMND_DZSEND "Send"
|
2019-08-11 17:12:18 +01:00
|
|
|
|
|
|
|
const char kDomoticzCommands[] PROGMEM = D_PRFX_DOMOTICZ "|" // Prefix
|
2020-07-24 14:30:21 +01:00
|
|
|
D_CMND_IDX "|" D_CMND_KEYIDX "|" D_CMND_SWITCHIDX "|" D_CMND_SENSORIDX "|" D_CMND_UPDATETIMER "|" D_CMND_DZSEND ;
|
2019-08-01 16:18:49 +01:00
|
|
|
|
|
|
|
void (* const DomoticzCommand[])(void) PROGMEM = {
|
2020-07-24 14:30:21 +01:00
|
|
|
&CmndDomoticzIdx, &CmndDomoticzKeyIdx, &CmndDomoticzSwitchIdx, &CmndDomoticzSensorIdx, &CmndDomoticzUpdateTimer, &CmndDomoticzSend };
|
2017-10-29 17:18:46 +00:00
|
|
|
|
2019-08-11 17:12:18 +01:00
|
|
|
const char DOMOTICZ_MESSAGE[] PROGMEM = "{\"idx\":%d,\"nvalue\":%d,\"svalue\":\"%s\",\"Battery\":%d,\"RSSI\":%d}";
|
|
|
|
|
2017-12-22 13:55:24 +00:00
|
|
|
#if MAX_DOMOTICZ_SNS_IDX < DZ_MAX_SENSORS
|
2021-06-13 10:10:52 +01:00
|
|
|
#error "Domoticz: Too many sensors or change settings.h layout"
|
2017-12-22 13:55:24 +00:00
|
|
|
#endif
|
2017-10-25 13:27:30 +01:00
|
|
|
|
|
|
|
const char kDomoticzSensors[] PROGMEM =
|
2019-08-25 15:25:38 +01:00
|
|
|
D_DOMOTICZ_TEMP "|" D_DOMOTICZ_TEMP_HUM "|" D_DOMOTICZ_TEMP_HUM_BARO "|" D_DOMOTICZ_POWER_ENERGY "|" D_DOMOTICZ_ILLUMINANCE "|"
|
2019-09-29 17:00:01 +01:00
|
|
|
D_DOMOTICZ_COUNT "|" D_DOMOTICZ_VOLTAGE "|" D_DOMOTICZ_CURRENT "|" D_DOMOTICZ_AIRQUALITY "|" D_DOMOTICZ_P1_SMART_METER "|" D_DOMOTICZ_SHUTTER ;
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2020-07-24 14:30:21 +01:00
|
|
|
const char kDomoticzCommand[] PROGMEM = "switchlight|switchscene";
|
|
|
|
|
2017-06-06 22:23:23 +01:00
|
|
|
char domoticz_in_topic[] = DOMOTICZ_IN_TOPIC;
|
|
|
|
|
2018-12-06 14:03:42 +00:00
|
|
|
int domoticz_update_timer = 0;
|
2019-07-24 12:09:42 +01:00
|
|
|
uint32_t domoticz_fan_debounce = 0; // iFan02 state debounce timer
|
|
|
|
bool domoticz_subscribe = false;
|
|
|
|
bool domoticz_update_flag = true;
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2019-11-26 15:51:53 +00:00
|
|
|
#ifdef USE_SHUTTER
|
|
|
|
bool domoticz_is_shutter = false;
|
|
|
|
#endif // USE_SHUTTER
|
|
|
|
|
2020-07-24 14:30:21 +01:00
|
|
|
int DomoticzBatteryQuality(void) {
|
2018-04-01 20:37:00 +01:00
|
|
|
// Battery 0%: ESP 2.6V (minimum operating voltage is 2.5)
|
|
|
|
// Battery 100%: ESP 3.6V (maximum operating voltage is 3.6)
|
|
|
|
// Battery 101% to 200%: ESP over 3.6V (means over maximum operating voltage)
|
|
|
|
|
2019-07-08 11:52:26 +01:00
|
|
|
int quality = 100; // Voltage range from 2,6V > 0% to 3,6V > 100%
|
2018-04-01 20:37:00 +01:00
|
|
|
|
2020-08-03 17:21:34 +01:00
|
|
|
#ifdef ESP8266
|
2019-07-08 11:52:26 +01:00
|
|
|
#ifdef USE_ADC_VCC
|
2018-04-01 20:37:00 +01:00
|
|
|
uint16_t voltage = ESP.getVcc();
|
|
|
|
if (voltage <= 2600) {
|
|
|
|
quality = 0;
|
|
|
|
} else if (voltage >= 4600) {
|
|
|
|
quality = 200;
|
|
|
|
} else {
|
|
|
|
quality = (voltage - 2600) / 10;
|
|
|
|
}
|
2020-08-03 17:21:34 +01:00
|
|
|
#endif // USE_ADC_VCC
|
|
|
|
#endif // ESP8266
|
2018-04-01 20:37:00 +01:00
|
|
|
return quality;
|
|
|
|
}
|
|
|
|
|
2020-07-24 14:30:21 +01:00
|
|
|
int DomoticzRssiQuality(void) {
|
2018-04-01 20:37:00 +01:00
|
|
|
// RSSI range: 0% to 10% (12 means disable RSSI in Domoticz)
|
|
|
|
|
|
|
|
return WifiGetRssiAsQuality(WiFi.RSSI()) / 10;
|
|
|
|
}
|
|
|
|
|
2019-07-14 21:08:19 +01:00
|
|
|
#ifdef USE_SONOFF_IFAN
|
2020-07-24 14:30:21 +01:00
|
|
|
void MqttPublishDomoticzFanState(void) {
|
2021-06-11 17:14:12 +01:00
|
|
|
if (Settings->flag.mqtt_enabled && Settings->domoticz_relay_idx[1]) { // SetOption3 - Enable MQTT
|
2018-12-06 14:03:42 +00:00
|
|
|
char svalue[8]; // Fanspeed value
|
|
|
|
|
|
|
|
int fan_speed = GetFanspeed();
|
|
|
|
snprintf_P(svalue, sizeof(svalue), PSTR("%d"), fan_speed * 10);
|
2021-06-11 17:14:12 +01:00
|
|
|
Response_P(DOMOTICZ_MESSAGE, (int)Settings->domoticz_relay_idx[1], (0 == fan_speed) ? 0 : 2, svalue, DomoticzBatteryQuality(), DomoticzRssiQuality());
|
2018-12-06 14:03:42 +00:00
|
|
|
MqttPublish(domoticz_in_topic);
|
|
|
|
|
2019-07-24 12:09:42 +01:00
|
|
|
domoticz_fan_debounce = millis();
|
2018-12-06 14:03:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 14:30:21 +01:00
|
|
|
void DomoticzUpdateFanState(void) {
|
2018-12-06 14:03:42 +00:00
|
|
|
if (domoticz_update_flag) {
|
|
|
|
MqttPublishDomoticzFanState();
|
|
|
|
}
|
2019-07-24 12:09:42 +01:00
|
|
|
domoticz_update_flag = true;
|
2018-12-06 14:03:42 +00:00
|
|
|
}
|
2019-07-14 21:08:19 +01:00
|
|
|
#endif // USE_SONOFF_IFAN
|
2018-12-06 09:49:49 +00:00
|
|
|
|
2020-07-24 14:30:21 +01:00
|
|
|
void MqttPublishDomoticzPowerState(uint8_t device) {
|
2021-06-11 17:14:12 +01:00
|
|
|
if (Settings->flag.mqtt_enabled) { // SetOption3 - Enable MQTT
|
2020-05-08 11:04:52 +01:00
|
|
|
if (device < 1) { device = 1; }
|
2020-10-30 11:29:48 +00:00
|
|
|
if ((device > TasmotaGlobal.devices_present) || (device > MAX_DOMOTICZ_IDX)) { return; }
|
2021-06-11 17:14:12 +01:00
|
|
|
if (Settings->domoticz_relay_idx[device -1]) {
|
2019-11-26 15:51:53 +00:00
|
|
|
#ifdef USE_SHUTTER
|
|
|
|
if (domoticz_is_shutter) {
|
|
|
|
// Shutter is updated by sensor update - power state should not be sent
|
|
|
|
} else {
|
|
|
|
#endif // USE_SHUTTER
|
2019-07-14 21:08:19 +01:00
|
|
|
#ifdef USE_SONOFF_IFAN
|
2019-07-14 14:23:02 +01:00
|
|
|
if (IsModuleIfan() && (device > 1)) {
|
2018-12-06 14:03:42 +00:00
|
|
|
// Fan handled by MqttPublishDomoticzFanState
|
|
|
|
} else {
|
2019-07-14 21:08:19 +01:00
|
|
|
#endif // USE_SONOFF_IFAN
|
2018-12-06 14:03:42 +00:00
|
|
|
char svalue[8]; // Dimmer value
|
|
|
|
|
2021-06-11 17:14:12 +01:00
|
|
|
snprintf_P(svalue, sizeof(svalue), PSTR("%d"), Settings->light_dimmer);
|
|
|
|
Response_P(DOMOTICZ_MESSAGE, (int)Settings->domoticz_relay_idx[device -1], (TasmotaGlobal.power & (1 << (device -1))) ? 1 : 0, (TasmotaGlobal.light_type) ? svalue : "", DomoticzBatteryQuality(), DomoticzRssiQuality());
|
2018-12-06 09:49:49 +00:00
|
|
|
MqttPublish(domoticz_in_topic);
|
2019-07-14 21:08:19 +01:00
|
|
|
#ifdef USE_SONOFF_IFAN
|
2018-12-06 09:49:49 +00:00
|
|
|
}
|
2019-07-14 21:08:19 +01:00
|
|
|
#endif // USE_SONOFF_IFAN
|
2019-11-26 15:51:53 +00:00
|
|
|
#ifdef USE_SHUTTER
|
|
|
|
}
|
|
|
|
#endif //USE_SHUTTER
|
2018-12-06 09:49:49 +00:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 14:30:21 +01:00
|
|
|
void DomoticzUpdatePowerState(uint8_t device) {
|
2017-07-15 14:07:30 +01:00
|
|
|
if (domoticz_update_flag) {
|
2017-10-18 17:22:34 +01:00
|
|
|
MqttPublishDomoticzPowerState(device);
|
2017-07-15 14:07:30 +01:00
|
|
|
}
|
2019-07-24 12:09:42 +01:00
|
|
|
domoticz_update_flag = true;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 14:30:21 +01:00
|
|
|
void DomoticzMqttUpdate(void) {
|
2021-06-11 17:14:12 +01:00
|
|
|
if (domoticz_subscribe && (Settings->domoticz_update_timer || domoticz_update_timer)) {
|
2017-01-28 13:41:01 +00:00
|
|
|
domoticz_update_timer--;
|
|
|
|
if (domoticz_update_timer <= 0) {
|
2021-06-11 17:14:12 +01:00
|
|
|
domoticz_update_timer = Settings->domoticz_update_timer;
|
2020-10-30 11:29:48 +00:00
|
|
|
for (uint32_t i = 1; i <= TasmotaGlobal.devices_present; i++) {
|
2019-11-26 15:51:53 +00:00
|
|
|
#ifdef USE_SHUTTER
|
|
|
|
if (domoticz_is_shutter)
|
|
|
|
{
|
|
|
|
// no power state updates for shutters
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif // USE_SHUTTER
|
2019-07-14 21:08:19 +01:00
|
|
|
#ifdef USE_SONOFF_IFAN
|
2019-07-14 14:23:02 +01:00
|
|
|
if (IsModuleIfan() && (i > 1)) {
|
2018-12-06 14:03:42 +00:00
|
|
|
MqttPublishDomoticzFanState();
|
|
|
|
break;
|
|
|
|
} else {
|
2019-07-14 21:08:19 +01:00
|
|
|
#endif // USE_SONOFF_IFAN
|
2018-12-06 14:03:42 +00:00
|
|
|
MqttPublishDomoticzPowerState(i);
|
2019-07-14 21:08:19 +01:00
|
|
|
#ifdef USE_SONOFF_IFAN
|
2018-12-06 14:03:42 +00:00
|
|
|
}
|
2019-07-14 21:08:19 +01:00
|
|
|
#endif // USE_SONOFF_IFAN
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 14:30:21 +01:00
|
|
|
void DomoticzMqttSubscribe(void) {
|
2020-10-30 11:29:48 +00:00
|
|
|
uint8_t maxdev = (TasmotaGlobal.devices_present > MAX_DOMOTICZ_IDX) ? MAX_DOMOTICZ_IDX : TasmotaGlobal.devices_present;
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < maxdev; i++) {
|
2021-06-11 17:14:12 +01:00
|
|
|
if (Settings->domoticz_relay_idx[i]) {
|
2017-05-19 15:09:25 +01:00
|
|
|
domoticz_subscribe = true;
|
|
|
|
}
|
|
|
|
}
|
2019-11-26 15:51:53 +00:00
|
|
|
|
2017-06-06 22:23:23 +01:00
|
|
|
if (domoticz_subscribe) {
|
2017-01-28 13:41:01 +00:00
|
|
|
char stopic[TOPSZ];
|
2019-10-02 16:39:27 +01:00
|
|
|
snprintf_P(stopic, sizeof(stopic), PSTR(DOMOTICZ_OUT_TOPIC "/#")); // domoticz topic
|
2017-10-18 17:22:34 +01:00
|
|
|
MqttSubscribe(stopic);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 14:23:51 +01:00
|
|
|
bool DomoticzMqttData(void) {
|
2017-02-17 16:18:41 +00:00
|
|
|
/*
|
2020-09-24 14:23:51 +01:00
|
|
|
XdrvMailbox.topic = topic;
|
|
|
|
XdrvMailbox.index = strlen(topic);
|
|
|
|
XdrvMailbox.data = (char*)data;
|
|
|
|
XdrvMailbox.data_len = data_len;
|
2017-02-17 16:18:41 +00:00
|
|
|
*/
|
2019-07-24 12:09:42 +01:00
|
|
|
domoticz_update_flag = true;
|
2019-10-02 11:51:37 +01:00
|
|
|
|
2019-10-02 16:39:27 +01:00
|
|
|
if (strncasecmp_P(XdrvMailbox.topic, PSTR(DOMOTICZ_OUT_TOPIC), strlen(DOMOTICZ_OUT_TOPIC)) != 0) {
|
2019-10-02 11:51:37 +01:00
|
|
|
return false; // Process unchanged data
|
|
|
|
}
|
|
|
|
|
|
|
|
// topic is domoticz/out so try to analyse
|
|
|
|
if (XdrvMailbox.data_len < 20) {
|
|
|
|
return true; // No valid data
|
|
|
|
}
|
2020-10-10 14:42:27 +01:00
|
|
|
|
|
|
|
String domoticz_data = XdrvMailbox.data; // Copy the string into a new buffer that will be modified
|
|
|
|
JsonParser parser((char*)domoticz_data.c_str());
|
2020-09-24 14:23:51 +01:00
|
|
|
JsonParserObject domoticz = parser.getRootObject();
|
|
|
|
if (!domoticz) {
|
2019-10-02 11:51:37 +01:00
|
|
|
return true; // To much or invalid data
|
|
|
|
}
|
2017-04-29 13:40:53 +01:00
|
|
|
// if (strcmp_P(domoticz["dtype"],PSTR("Light/Switch"))) {
|
2019-07-24 12:09:42 +01:00
|
|
|
// return true;
|
2017-04-25 17:24:42 +01:00
|
|
|
// }
|
2020-09-24 14:23:51 +01:00
|
|
|
uint32_t idx = domoticz.getUInt(PSTR("idx"), 0);
|
|
|
|
int16_t nvalue = domoticz.getInt(PSTR("nvalue"), -1);
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2021-01-23 15:26:23 +00:00
|
|
|
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR(D_LOG_DOMOTICZ "idx %d, nvalue %d"), idx, nvalue);
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2019-10-02 11:51:37 +01:00
|
|
|
bool found = false;
|
|
|
|
if ((idx > 0) && (nvalue >= 0) && (nvalue <= 15)) {
|
2020-10-30 11:29:48 +00:00
|
|
|
uint8_t maxdev = (TasmotaGlobal.devices_present > MAX_DOMOTICZ_IDX) ? MAX_DOMOTICZ_IDX : TasmotaGlobal.devices_present;
|
2019-10-02 11:51:37 +01:00
|
|
|
for (uint32_t i = 0; i < maxdev; i++) {
|
2021-06-11 17:14:12 +01:00
|
|
|
if (idx == Settings->domoticz_relay_idx[i]) {
|
2020-09-24 14:23:51 +01:00
|
|
|
bool iscolordimmer = strcmp_P(domoticz.getStr(PSTR("dtype")), PSTR("Color Switch")) == 0;
|
|
|
|
bool isShutter = strcmp_P(domoticz.getStr(PSTR("dtype")), PSTR("Light/Switch")) == 0 & strncmp_P(domoticz.getStr(PSTR("switchType")),PSTR("Blinds"), 6) == 0;
|
2019-11-26 15:51:53 +00:00
|
|
|
|
2019-10-02 11:51:37 +01:00
|
|
|
char stemp1[10];
|
|
|
|
snprintf_P(stemp1, sizeof(stemp1), PSTR("%d"), i +1);
|
2019-07-14 21:08:19 +01:00
|
|
|
#ifdef USE_SONOFF_IFAN
|
2019-10-02 11:51:37 +01:00
|
|
|
if (IsModuleIfan() && (1 == i)) { // Idx 2 is fanspeed
|
2020-09-24 14:23:51 +01:00
|
|
|
JsonParserToken svalue_tok = domoticz[PSTR("svalue1")];
|
|
|
|
if (!svalue_tok) {
|
|
|
|
return true;
|
2019-10-02 11:51:37 +01:00
|
|
|
}
|
2020-09-24 14:23:51 +01:00
|
|
|
uint8_t svalue = svalue_tok.getUInt();
|
2020-09-24 16:13:32 +01:00
|
|
|
svalue = (2 == nvalue) ? svalue / 10 : 0;
|
2019-10-02 11:51:37 +01:00
|
|
|
if (GetFanspeed() == svalue) {
|
|
|
|
return true; // Stop loop as already set
|
|
|
|
}
|
|
|
|
if (TimePassedSince(domoticz_fan_debounce) < 1000) {
|
|
|
|
return true; // Stop loop if device in limbo
|
|
|
|
}
|
|
|
|
snprintf_P(XdrvMailbox.topic, XdrvMailbox.index, PSTR("/" D_CMND_FANSPEED));
|
|
|
|
snprintf_P(XdrvMailbox.data, XdrvMailbox.data_len, PSTR("%d"), svalue);
|
|
|
|
found = true;
|
|
|
|
} else
|
2019-07-14 21:08:19 +01:00
|
|
|
#endif // USE_SONOFF_IFAN
|
2019-11-26 15:51:53 +00:00
|
|
|
#ifdef USE_SHUTTER
|
2020-09-24 16:13:32 +01:00
|
|
|
if (isShutter) {
|
2020-09-24 14:23:51 +01:00
|
|
|
uint8_t position = domoticz.getUInt(PSTR("svalue1"), 0);
|
2019-11-26 15:51:53 +00:00
|
|
|
if (nvalue != 2) {
|
2020-09-24 16:13:32 +01:00
|
|
|
position = (0 == nvalue) ? 0 : 100;
|
2019-11-26 15:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
snprintf_P(XdrvMailbox.topic, TOPSZ, PSTR("/" D_PRFX_SHUTTER D_CMND_SHUTTER_POSITION));
|
|
|
|
snprintf_P(XdrvMailbox.data, XdrvMailbox.data_len, PSTR("%d"), position);
|
|
|
|
XdrvMailbox.data_len = position > 99 ? 3 : (position > 9 ? 2 : 1);
|
|
|
|
|
|
|
|
found = true;
|
|
|
|
} else
|
|
|
|
#endif // USE_SHUTTER
|
2020-02-23 19:49:54 +00:00
|
|
|
#ifdef USE_LIGHT
|
2019-10-02 11:51:37 +01:00
|
|
|
if (iscolordimmer && 10 == nvalue) { // Color_SetColor
|
2020-01-04 17:24:33 +00:00
|
|
|
// https://www.domoticz.com/wiki/Domoticz_API/JSON_URL%27s#Set_a_light_to_a_certain_color_or_color_temperature
|
2020-09-24 14:23:51 +01:00
|
|
|
JsonParserObject color = domoticz[PSTR("Color")].getObject();
|
|
|
|
// JsonObject& color = domoticz["Color"];
|
|
|
|
uint16_t level = nvalue = domoticz.getUInt(PSTR("svalue1"), 0);
|
|
|
|
uint16_t r = color.getUInt(PSTR("r"), 0) * level / 100;
|
|
|
|
uint16_t g = color.getUInt(PSTR("g"), 0) * level / 100;
|
|
|
|
uint16_t b = color.getUInt(PSTR("b"), 0) * level / 100;
|
|
|
|
uint16_t cw = color.getUInt(PSTR("cw"), 0) * level / 100;
|
|
|
|
uint16_t ww = color.getUInt(PSTR("ww"), 0) * level / 100;
|
|
|
|
uint16_t m = color.getUInt(PSTR("m"), 0);
|
|
|
|
uint16_t t = color.getUInt(PSTR("t"), 0);
|
2020-01-04 16:29:04 +00:00
|
|
|
if (2 == m) { // White with color temperature. Valid fields: t
|
2020-01-04 17:24:33 +00:00
|
|
|
snprintf_P(XdrvMailbox.topic, XdrvMailbox.index, PSTR("/" D_CMND_BACKLOG));
|
|
|
|
snprintf_P(XdrvMailbox.data, XdrvMailbox.data_len, PSTR(D_CMND_COLORTEMPERATURE " %d;" D_CMND_DIMMER " %d"), changeUIntScale(t, 0, 255, CT_MIN, CT_MAX), level);
|
2020-01-04 16:29:04 +00:00
|
|
|
} else {
|
|
|
|
snprintf_P(XdrvMailbox.topic, XdrvMailbox.index, PSTR("/" D_CMND_COLOR));
|
|
|
|
snprintf_P(XdrvMailbox.data, XdrvMailbox.data_len, PSTR("%02x%02x%02x%02x%02x"), r, g, b, cw, ww);
|
|
|
|
}
|
2019-10-02 11:51:37 +01:00
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
else if ((!iscolordimmer && 2 == nvalue) || // gswitch_sSetLevel
|
|
|
|
(iscolordimmer && 15 == nvalue)) { // Color_SetBrightnessLevel
|
2020-09-24 14:23:51 +01:00
|
|
|
if (domoticz[PSTR("svalue1")]) {
|
|
|
|
nvalue = domoticz.getUInt(PSTR("svalue1"), 0);
|
2019-10-02 11:51:37 +01:00
|
|
|
} else {
|
|
|
|
return true; // Invalid data
|
2018-12-06 09:49:49 +00:00
|
|
|
}
|
2021-06-11 17:14:12 +01:00
|
|
|
if (TasmotaGlobal.light_type && (Settings->light_dimmer == nvalue) && ((TasmotaGlobal.power >> i) &1)) {
|
2019-10-02 11:51:37 +01:00
|
|
|
return true; // State already set
|
2018-12-06 09:49:49 +00:00
|
|
|
}
|
2019-10-02 11:51:37 +01:00
|
|
|
snprintf_P(XdrvMailbox.topic, XdrvMailbox.index, PSTR("/" D_CMND_DIMMER));
|
|
|
|
snprintf_P(XdrvMailbox.data, XdrvMailbox.data_len, PSTR("%d"), nvalue);
|
|
|
|
found = true;
|
2020-02-23 19:49:54 +00:00
|
|
|
} else
|
|
|
|
#endif // USE_LIGHT
|
|
|
|
if (1 == nvalue || 0 == nvalue) {
|
2020-10-28 18:03:39 +00:00
|
|
|
if (((TasmotaGlobal.power >> i) &1) == (power_t)nvalue) {
|
2019-10-02 11:51:37 +01:00
|
|
|
return true; // Stop loop
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2020-10-30 11:29:48 +00:00
|
|
|
snprintf_P(XdrvMailbox.topic, XdrvMailbox.index, PSTR("/" D_CMND_POWER "%s"), (TasmotaGlobal.devices_present > 1) ? stemp1 : "");
|
2019-10-02 11:51:37 +01:00
|
|
|
snprintf_P(XdrvMailbox.data, XdrvMailbox.data_len, PSTR("%d"), nvalue);
|
|
|
|
found = true;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2019-10-02 11:51:37 +01:00
|
|
|
break;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-02 11:51:37 +01:00
|
|
|
}
|
|
|
|
if (!found) { return true; } // No command received
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2021-06-05 10:47:09 +01:00
|
|
|
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR(D_LOG_DOMOTICZ D_RECEIVED_TOPIC " %s, " D_DATA " %s"), XdrvMailbox.topic, XdrvMailbox.data);
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2019-10-02 11:51:37 +01:00
|
|
|
domoticz_update_flag = false;
|
|
|
|
return false; // Process new data
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 16:18:49 +01:00
|
|
|
/*********************************************************************************************/
|
2017-01-28 15:31:11 +00:00
|
|
|
|
2020-07-24 14:30:21 +01:00
|
|
|
void DomoticzSendSwitch(uint32_t type, uint32_t index, uint32_t state) {
|
|
|
|
char stemp[16]; // "switchlight" or "switchscene"
|
|
|
|
Response_P(PSTR("{\"command\":\"%s\",\"idx\":%d,\"switchcmd\":\"%s\"}"),
|
|
|
|
GetTextIndexed(stemp, sizeof(stemp), type, kDomoticzCommand), index, (state) ? (POWER_TOGGLE == state) ? "Toggle" : "On" : "Off"); // Domoticz case sensitive
|
|
|
|
MqttPublish(domoticz_in_topic);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DomoticzSendKey(uint8_t key, uint8_t device, uint8_t state, uint8_t svalflg) {
|
2019-07-24 12:09:42 +01:00
|
|
|
bool result = false;
|
2018-07-12 13:24:17 +01:00
|
|
|
|
|
|
|
if (device <= MAX_DOMOTICZ_IDX) {
|
2021-06-11 17:14:12 +01:00
|
|
|
if ((Settings->domoticz_key_idx[device -1] || Settings->domoticz_switch_idx[device -1]) && (svalflg)) {
|
|
|
|
DomoticzSendSwitch(0, (key) ? Settings->domoticz_switch_idx[device -1] : Settings->domoticz_key_idx[device -1], state);
|
2019-07-24 12:09:42 +01:00
|
|
|
result = true;
|
2018-07-12 13:24:17 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2018-07-12 13:24:17 +01:00
|
|
|
return result;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Sensors
|
2017-12-22 13:55:24 +00:00
|
|
|
*
|
|
|
|
* Source : https://www.domoticz.com/wiki/Domoticz_API/JSON_URL%27s
|
|
|
|
* https://www.domoticz.com/wiki/MQTT
|
|
|
|
*
|
|
|
|
* Percentage, Barometric, Air Quality:
|
|
|
|
* {\"idx\":%d,\"nvalue\":%s}, Idx, Value
|
|
|
|
*
|
|
|
|
* Humidity:
|
|
|
|
* {\"idx\":%d,\"nvalue\":%s,\"svalue\":\"%s\"}, Idx, Humidity, HumidityStatus
|
|
|
|
*
|
|
|
|
* All other:
|
|
|
|
* {\"idx\":%d,\"nvalue\":0,\"svalue\":\"%s\"}, Idx, Value(s)
|
|
|
|
*
|
2017-01-28 13:41:01 +00:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2020-07-24 14:30:21 +01:00
|
|
|
void DomoticzSendData(uint32_t sensor_idx, uint32_t idx, char *data) {
|
2021-05-19 16:21:50 +01:00
|
|
|
char payload[128];
|
2020-07-24 14:30:21 +01:00
|
|
|
if (DZ_AIRQUALITY == sensor_idx) {
|
2021-05-19 16:21:50 +01:00
|
|
|
snprintf_P(payload, sizeof(payload), PSTR("{\"idx\":%d,\"nvalue\":%s,\"Battery\":%d,\"RSSI\":%d}"),
|
2020-07-24 14:30:21 +01:00
|
|
|
idx, data, DomoticzBatteryQuality(), DomoticzRssiQuality());
|
|
|
|
} else {
|
|
|
|
uint8_t nvalue = 0;
|
|
|
|
#ifdef USE_SHUTTER
|
|
|
|
if (DZ_SHUTTER == sensor_idx) {
|
|
|
|
uint8_t position = atoi(data);
|
|
|
|
nvalue = position < 2 ? 0 : (position == 100 ? 1 : 2);
|
|
|
|
}
|
|
|
|
#endif // USE_SHUTTER
|
2021-05-19 16:21:50 +01:00
|
|
|
snprintf_P(payload, sizeof(payload), DOMOTICZ_MESSAGE, // "{\"idx\":%d,\"nvalue\":%d,\"svalue\":\"%s\",\"Battery\":%d,\"RSSI\":%d}"
|
2020-07-24 14:30:21 +01:00
|
|
|
idx, nvalue, data, DomoticzBatteryQuality(), DomoticzRssiQuality());
|
|
|
|
}
|
2021-05-19 16:21:50 +01:00
|
|
|
MqttPublishPayload(domoticz_in_topic, payload);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 14:30:21 +01:00
|
|
|
void DomoticzSensor(uint8_t idx, char *data) {
|
2021-06-11 17:14:12 +01:00
|
|
|
if (Settings->domoticz_sensor_idx[idx]) {
|
|
|
|
DomoticzSendData(idx, Settings->domoticz_sensor_idx[idx], data);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 14:30:21 +01:00
|
|
|
uint8_t DomoticzHumidityState(float h) {
|
|
|
|
return (!h) ? 0 : (h < 40) ? 2 : (h > 70) ? 3 : 1;
|
|
|
|
}
|
|
|
|
|
2021-03-27 14:15:15 +00:00
|
|
|
void DomoticzSensor(uint8_t idx, int value) {
|
2017-10-02 15:45:03 +01:00
|
|
|
char data[16];
|
|
|
|
snprintf_P(data, sizeof(data), PSTR("%d"), value);
|
2017-10-18 17:22:34 +01:00
|
|
|
DomoticzSensor(idx, data);
|
2017-10-02 15:45:03 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2021-01-25 15:25:17 +00:00
|
|
|
void DomoticzFloatSensor(uint8_t idx, float value) {
|
|
|
|
uint32_t resolution = 1;
|
|
|
|
/*
|
|
|
|
switch (idx) {
|
2021-06-11 17:14:12 +01:00
|
|
|
case DZ_TEMP: resolution = Settings->flag2.temperature_resolution; break;
|
|
|
|
case DZ_POWER_ENERGY: resolution = Settings->flag2.wattage_resolution; break;
|
|
|
|
case DZ_VOLTAGE: resolution = Settings->flag2.voltage_resolution; break;
|
|
|
|
case DZ_CURRENT: resolution = Settings->flag2.current_resolution; break;
|
2021-01-25 15:25:17 +00:00
|
|
|
}
|
|
|
|
*/
|
2021-06-11 17:14:12 +01:00
|
|
|
if (DZ_TEMP == idx) { resolution = Settings->flag2.temperature_resolution; }
|
|
|
|
else if (DZ_POWER_ENERGY == idx) { resolution = Settings->flag2.wattage_resolution; }
|
|
|
|
else if (DZ_VOLTAGE == idx) { resolution = Settings->flag2.voltage_resolution; }
|
|
|
|
else if (DZ_CURRENT == idx) { resolution = Settings->flag2.current_resolution; }
|
2021-01-25 15:25:17 +00:00
|
|
|
char data[FLOATSZ];
|
|
|
|
dtostrfd(value, resolution, data);
|
|
|
|
DomoticzSensor(idx, data);
|
|
|
|
}
|
|
|
|
|
2020-03-17 15:29:59 +00:00
|
|
|
//void DomoticzTempHumPressureSensor(float temp, float hum, float baro = -1);
|
2020-07-24 14:30:21 +01:00
|
|
|
void DomoticzTempHumPressureSensor(float temp, float hum, float baro) {
|
2020-03-17 15:29:59 +00:00
|
|
|
char temperature[FLOATSZ];
|
2021-06-11 17:14:12 +01:00
|
|
|
dtostrfd(temp, Settings->flag2.temperature_resolution, temperature);
|
2020-03-17 15:29:59 +00:00
|
|
|
char humidity[FLOATSZ];
|
2021-06-11 17:14:12 +01:00
|
|
|
dtostrfd(hum, Settings->flag2.humidity_resolution, humidity);
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
char data[32];
|
2020-03-17 15:29:59 +00:00
|
|
|
if (baro > -1) {
|
|
|
|
char pressure[FLOATSZ];
|
2021-06-11 17:14:12 +01:00
|
|
|
dtostrfd(baro, Settings->flag2.pressure_resolution, pressure);
|
2020-03-17 15:29:59 +00:00
|
|
|
|
|
|
|
snprintf_P(data, sizeof(data), PSTR("%s;%s;%d;%s;5"), temperature, humidity, DomoticzHumidityState(hum), pressure);
|
|
|
|
DomoticzSensor(DZ_TEMP_HUM_BARO, data);
|
|
|
|
} else {
|
|
|
|
snprintf_P(data, sizeof(data), PSTR("%s;%s;%d"), temperature, humidity, DomoticzHumidityState(hum));
|
|
|
|
DomoticzSensor(DZ_TEMP_HUM, data);
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 14:30:21 +01:00
|
|
|
void DomoticzSensorPowerEnergy(int power, char *energy) {
|
2017-01-28 13:41:01 +00:00
|
|
|
char data[16];
|
|
|
|
snprintf_P(data, sizeof(data), PSTR("%d;%s"), power, energy);
|
2017-10-18 17:22:34 +01:00
|
|
|
DomoticzSensor(DZ_POWER_ENERGY, data);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 14:30:21 +01:00
|
|
|
void DomoticzSensorP1SmartMeter(char *usage1, char *usage2, char *return1, char *return2, int power) {
|
2019-08-25 15:25:38 +01:00
|
|
|
//usage1 = energy usage meter tariff 1, This is an incrementing counter
|
|
|
|
//usage2 = energy usage meter tariff 2, This is an incrementing counter
|
|
|
|
//return1 = energy return meter tariff 1, This is an incrementing counter
|
|
|
|
//return2 = energy return meter tariff 2, This is an incrementing counter
|
2019-09-05 17:02:36 +01:00
|
|
|
//power = if >= 0 actual usage power. if < 0 actual return power (Watt)
|
|
|
|
int consumed = power;
|
|
|
|
int produced = 0;
|
|
|
|
if (power < 0) {
|
|
|
|
consumed = 0;
|
|
|
|
produced = -power;
|
|
|
|
}
|
2019-08-25 15:25:38 +01:00
|
|
|
char data[64];
|
|
|
|
snprintf_P(data, sizeof(data), PSTR("%s;%s;%s;%s;%d;%d"), usage1, usage2, return1, return2, consumed, produced);
|
|
|
|
DomoticzSensor(DZ_P1_SMART_METER, data);
|
|
|
|
}
|
|
|
|
|
2019-08-01 16:18:49 +01:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Commands
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2020-07-24 14:30:21 +01:00
|
|
|
void CmndDomoticzIdx(void) {
|
2019-08-01 16:18:49 +01:00
|
|
|
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_DOMOTICZ_IDX)) {
|
|
|
|
if (XdrvMailbox.payload >= 0) {
|
2021-06-11 17:14:12 +01:00
|
|
|
Settings->domoticz_relay_idx[XdrvMailbox.index -1] = XdrvMailbox.payload;
|
2020-10-29 11:21:24 +00:00
|
|
|
TasmotaGlobal.restart_flag = 2;
|
2019-08-01 16:18:49 +01:00
|
|
|
}
|
2021-06-11 17:14:12 +01:00
|
|
|
ResponseCmndIdxNumber(Settings->domoticz_relay_idx[XdrvMailbox.index -1]);
|
2019-08-01 16:18:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 14:30:21 +01:00
|
|
|
void CmndDomoticzKeyIdx(void) {
|
2019-08-01 16:18:49 +01:00
|
|
|
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_DOMOTICZ_IDX)) {
|
|
|
|
if (XdrvMailbox.payload >= 0) {
|
2021-06-11 17:14:12 +01:00
|
|
|
Settings->domoticz_key_idx[XdrvMailbox.index -1] = XdrvMailbox.payload;
|
2019-08-01 16:18:49 +01:00
|
|
|
}
|
2021-06-11 17:14:12 +01:00
|
|
|
ResponseCmndIdxNumber(Settings->domoticz_key_idx[XdrvMailbox.index -1]);
|
2019-08-01 16:18:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 14:30:21 +01:00
|
|
|
void CmndDomoticzSwitchIdx(void) {
|
2019-08-01 16:18:49 +01:00
|
|
|
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_DOMOTICZ_IDX)) {
|
|
|
|
if (XdrvMailbox.payload >= 0) {
|
2021-06-11 17:14:12 +01:00
|
|
|
Settings->domoticz_switch_idx[XdrvMailbox.index -1] = XdrvMailbox.payload;
|
2019-08-01 16:18:49 +01:00
|
|
|
}
|
2021-06-11 17:14:12 +01:00
|
|
|
ResponseCmndIdxNumber(Settings->domoticz_switch_idx[XdrvMailbox.index -1]);
|
2019-08-01 16:18:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 14:30:21 +01:00
|
|
|
void CmndDomoticzSensorIdx(void) {
|
2019-08-01 16:18:49 +01:00
|
|
|
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= DZ_MAX_SENSORS)) {
|
|
|
|
if (XdrvMailbox.payload >= 0) {
|
2021-06-11 17:14:12 +01:00
|
|
|
Settings->domoticz_sensor_idx[XdrvMailbox.index -1] = XdrvMailbox.payload;
|
2019-08-01 16:18:49 +01:00
|
|
|
}
|
2021-06-11 17:14:12 +01:00
|
|
|
ResponseCmndIdxNumber(Settings->domoticz_sensor_idx[XdrvMailbox.index -1]);
|
2019-08-01 16:18:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 14:30:21 +01:00
|
|
|
void CmndDomoticzUpdateTimer(void) {
|
2019-08-01 16:18:49 +01:00
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 3601)) {
|
2021-06-11 17:14:12 +01:00
|
|
|
Settings->domoticz_update_timer = XdrvMailbox.payload;
|
2019-08-01 16:18:49 +01:00
|
|
|
}
|
2021-06-11 17:14:12 +01:00
|
|
|
ResponseCmndNumber(Settings->domoticz_update_timer);
|
2019-08-01 16:18:49 +01:00
|
|
|
}
|
|
|
|
|
2020-07-24 14:30:21 +01:00
|
|
|
void CmndDomoticzSend(void) {
|
|
|
|
// DzSend1 <idx>,<values> - {\"idx\":<idx>,\"nvalue\":0,\"svalue\":\"<values>\",\"Battery\":xx,\"RSSI\":yy}
|
|
|
|
// DzSend2 <idx>,<values> - USE_SHUTTER only - {\"idx\":<idx>,\"nvalue\":<position>,\"svalue\":\"<values>\",\"Battery\":xx,\"RSSI\":yy}
|
|
|
|
// DzSend3 <idx>,<values> - {\"idx\":<idx>,\"nvalue\":<values>,\"Battery\":xx,\"RSSI\":yy}
|
|
|
|
// DzSend4 <idx>,<state> - {\"command\":\"switchlight\",\"idx\":<idx>,\"switchcmd\":\"<state>\"}
|
|
|
|
// DzSend5 <idx>,<state> - {\"command\":\"switchscene\",\"idx\":<idx>,\"switchcmd\":\"<state>\"}
|
|
|
|
|
|
|
|
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= 5)) {
|
|
|
|
if (XdrvMailbox.data_len > 0) {
|
2020-11-04 10:20:17 +00:00
|
|
|
if (strchr(XdrvMailbox.data, ',') != nullptr) { // Process parameter entry
|
2020-07-24 14:30:21 +01:00
|
|
|
char *data;
|
|
|
|
uint32_t index = strtoul(strtok_r(XdrvMailbox.data, ",", &data), nullptr, 10);
|
|
|
|
if ((index > 0) && (data != nullptr)) {
|
|
|
|
if (XdrvMailbox.index > 3) {
|
|
|
|
uint32_t state = strtoul(data, nullptr, 10); // 0, 1 or 2
|
|
|
|
DomoticzSendSwitch(XdrvMailbox.index -4, index, state);
|
|
|
|
} else {
|
|
|
|
uint32_t type = DZ_TEMP;
|
|
|
|
if (2 == XdrvMailbox.index) { type = DZ_SHUTTER; }
|
|
|
|
else if (3 == XdrvMailbox.index) { type = DZ_AIRQUALITY; }
|
|
|
|
DomoticzSendData(type, index, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Presentation
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2017-02-06 09:08:40 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
2018-10-10 21:21:44 +01:00
|
|
|
|
|
|
|
#define WEB_HANDLE_DOMOTICZ "dm"
|
|
|
|
|
|
|
|
const char HTTP_BTN_MENU_DOMOTICZ[] PROGMEM =
|
2019-02-13 15:05:25 +00:00
|
|
|
"<p><form action='" WEB_HANDLE_DOMOTICZ "' method='get'><button>" D_CONFIGURE_DOMOTICZ "</button></form></p>";
|
2018-10-10 21:21:44 +01:00
|
|
|
|
|
|
|
const char HTTP_FORM_DOMOTICZ[] PROGMEM =
|
2019-02-13 15:05:25 +00:00
|
|
|
"<fieldset><legend><b> " D_DOMOTICZ_PARAMETERS " </b></legend>"
|
|
|
|
"<form method='post' action='" WEB_HANDLE_DOMOTICZ "'>"
|
|
|
|
"<table>";
|
2018-10-10 21:21:44 +01:00
|
|
|
const char HTTP_FORM_DOMOTICZ_RELAY[] PROGMEM =
|
2019-05-31 18:51:24 +01:00
|
|
|
"<tr><td style='width:260px'><b>" D_DOMOTICZ_IDX " %d</b></td><td style='width:70px'><input id='r%d' placeholder='0' value='%d'></td></tr>"
|
|
|
|
"<tr><td style='width:260px'><b>" D_DOMOTICZ_KEY_IDX " %d</b></td><td style='width:70px'><input id='k%d' placeholder='0' value='%d'></td></tr>";
|
|
|
|
const char HTTP_FORM_DOMOTICZ_SWITCH[] PROGMEM =
|
|
|
|
"<tr><td style='width:260px'><b>" D_DOMOTICZ_SWITCH_IDX " %d</b></td><td style='width:70px'><input id='s%d' placeholder='0' value='%d'></td></tr>";
|
2018-10-10 21:21:44 +01:00
|
|
|
const char HTTP_FORM_DOMOTICZ_SENSOR[] PROGMEM =
|
2019-05-31 18:51:24 +01:00
|
|
|
"<tr><td style='width:260px'><b>" D_DOMOTICZ_SENSOR_IDX " %d</b> %s</td><td style='width:70px'><input id='l%d' placeholder='0' value='%d'></td></tr>";
|
2018-10-10 21:21:44 +01:00
|
|
|
const char HTTP_FORM_DOMOTICZ_TIMER[] PROGMEM =
|
2019-05-31 18:51:24 +01:00
|
|
|
"<tr><td style='width:260px'><b>" D_DOMOTICZ_UPDATE_TIMER "</b> (" STR(DOMOTICZ_UPDATE_TIMER) ")</td><td style='width:70px'><input id='ut' placeholder='" STR(DOMOTICZ_UPDATE_TIMER) "' value='%d'></td></tr>";
|
2018-10-10 21:21:44 +01:00
|
|
|
|
2020-07-24 14:30:21 +01:00
|
|
|
void HandleDomoticzConfiguration(void) {
|
2019-01-10 11:57:42 +00:00
|
|
|
if (!HttpCheckPriviledgedAccess()) { return; }
|
|
|
|
|
2021-01-23 15:26:23 +00:00
|
|
|
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_HTTP D_CONFIGURE_DOMOTICZ));
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2021-01-18 20:48:04 +00:00
|
|
|
if (Webserver->hasArg(F("save"))) {
|
2018-10-10 21:21:44 +01:00
|
|
|
DomoticzSaveSettings();
|
2018-10-12 10:42:52 +01:00
|
|
|
WebRestart(1);
|
2018-10-10 21:21:44 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-03 09:51:21 +01:00
|
|
|
char stemp[40];
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2020-11-06 16:09:13 +00:00
|
|
|
WSContentStart_P(PSTR(D_CONFIGURE_DOMOTICZ));
|
2019-03-04 17:16:07 +00:00
|
|
|
WSContentSendStyle();
|
2019-03-10 14:36:34 +00:00
|
|
|
WSContentSend_P(HTTP_FORM_DOMOTICZ);
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < MAX_DOMOTICZ_IDX; i++) {
|
2020-10-30 11:29:48 +00:00
|
|
|
if (i < TasmotaGlobal.devices_present) {
|
2019-03-04 17:16:07 +00:00
|
|
|
WSContentSend_P(HTTP_FORM_DOMOTICZ_RELAY,
|
2021-06-11 17:14:12 +01:00
|
|
|
i +1, i, Settings->domoticz_relay_idx[i],
|
|
|
|
i +1, i, Settings->domoticz_key_idx[i]);
|
2017-04-03 15:38:15 +01:00
|
|
|
}
|
2020-04-27 11:54:07 +01:00
|
|
|
if (PinUsed(GPIO_SWT1, i)) {
|
2019-03-04 17:16:07 +00:00
|
|
|
WSContentSend_P(HTTP_FORM_DOMOTICZ_SWITCH,
|
2021-06-11 17:14:12 +01:00
|
|
|
i +1, i, Settings->domoticz_switch_idx[i]);
|
2017-04-03 15:38:15 +01:00
|
|
|
}
|
2019-07-14 21:08:19 +01:00
|
|
|
#ifdef USE_SONOFF_IFAN
|
2019-07-14 14:23:02 +01:00
|
|
|
if (IsModuleIfan() && (1 == i)) { break; }
|
2019-07-14 21:08:19 +01:00
|
|
|
#endif // USE_SONOFF_IFAN
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < DZ_MAX_SENSORS; i++) {
|
2019-03-04 17:16:07 +00:00
|
|
|
WSContentSend_P(HTTP_FORM_DOMOTICZ_SENSOR,
|
2021-06-11 17:14:12 +01:00
|
|
|
i +1, GetTextIndexed(stemp, sizeof(stemp), i, kDomoticzSensors), i, Settings->domoticz_sensor_idx[i]);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2021-06-11 17:14:12 +01:00
|
|
|
WSContentSend_P(HTTP_FORM_DOMOTICZ_TIMER, Settings->domoticz_update_timer);
|
2019-03-10 14:36:34 +00:00
|
|
|
WSContentSend_P(PSTR("</table>"));
|
|
|
|
WSContentSend_P(HTTP_FORM_END);
|
2019-03-11 09:38:41 +00:00
|
|
|
WSContentSpaceButton(BUTTON_CONFIGURATION);
|
2019-03-16 15:23:41 +00:00
|
|
|
WSContentStop();
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 14:30:21 +01:00
|
|
|
void DomoticzSaveSettings(void) {
|
2021-05-03 15:15:47 +01:00
|
|
|
String cmnd = F(D_CMND_BACKLOG "0 ");
|
|
|
|
cmnd += AddWebCommand(PSTR(D_PRFX_DOMOTICZ D_CMND_UPDATETIMER), PSTR("ut"), STR(DOMOTICZ_UPDATE_TIMER));
|
|
|
|
char arg_idx[5];
|
|
|
|
char cmnd2[24];
|
2021-04-09 11:18:26 +01:00
|
|
|
for (uint32_t i = 1; i <= MAX_DOMOTICZ_IDX; i++) {
|
2021-05-03 15:15:47 +01:00
|
|
|
snprintf_P(cmnd2, sizeof(cmnd2), PSTR(D_PRFX_DOMOTICZ D_CMND_IDX "%d"), i);
|
2021-04-09 11:18:26 +01:00
|
|
|
snprintf_P(arg_idx, sizeof(arg_idx), PSTR("r%d"), i -1);
|
2021-05-03 15:15:47 +01:00
|
|
|
cmnd += AddWebCommand(cmnd2, arg_idx, PSTR("0"));
|
|
|
|
snprintf_P(cmnd2, sizeof(cmnd2), PSTR(D_PRFX_DOMOTICZ D_CMND_KEYIDX "%d"), i);
|
2021-04-09 11:18:26 +01:00
|
|
|
arg_idx[0] = 'k';
|
2021-05-03 15:15:47 +01:00
|
|
|
cmnd += AddWebCommand(cmnd2, arg_idx, PSTR("0"));
|
|
|
|
snprintf_P(cmnd2, sizeof(cmnd2), PSTR(D_PRFX_DOMOTICZ D_CMND_SWITCHIDX "%d"), i);
|
2021-04-09 11:18:26 +01:00
|
|
|
arg_idx[0] = 's';
|
2021-05-03 15:15:47 +01:00
|
|
|
cmnd += AddWebCommand(cmnd2, arg_idx, PSTR("0"));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2021-04-09 11:18:26 +01:00
|
|
|
for (uint32_t i = 1; i <= DZ_MAX_SENSORS; i++) {
|
2021-05-03 15:15:47 +01:00
|
|
|
snprintf_P(cmnd2, sizeof(cmnd2), PSTR(D_PRFX_DOMOTICZ D_CMND_SENSORIDX "%d"), i);
|
2021-04-09 11:18:26 +01:00
|
|
|
snprintf_P(arg_idx, sizeof(arg_idx), PSTR("l%d"), i -1);
|
2021-05-03 15:15:47 +01:00
|
|
|
cmnd += AddWebCommand(cmnd2, arg_idx, PSTR("0"));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2021-05-03 15:15:47 +01:00
|
|
|
ExecuteWebCommand((char*)cmnd.c_str()); // Note: beware of max number of commands in backlog currently 30 (MAX_BACKLOG)
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2018-01-13 14:53:02 +00:00
|
|
|
#endif // USE_WEBSERVER
|
2018-01-05 11:26:19 +00:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2020-07-24 14:30:21 +01:00
|
|
|
bool Xdrv07(uint8_t function) {
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2019-11-26 15:58:05 +00:00
|
|
|
|
2021-06-11 17:14:12 +01:00
|
|
|
if (Settings->flag.mqtt_enabled) { // SetOption3 - Enable MQTT
|
2018-01-05 11:26:19 +00:00
|
|
|
switch (function) {
|
2019-07-24 12:09:42 +01:00
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
DomoticzMqttUpdate();
|
|
|
|
break;
|
|
|
|
case FUNC_MQTT_DATA:
|
|
|
|
result = DomoticzMqttData();
|
|
|
|
break;
|
2018-10-10 21:21:44 +01:00
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
case FUNC_WEB_ADD_BUTTON:
|
2019-03-10 14:36:34 +00:00
|
|
|
WSContentSend_P(HTTP_BTN_MENU_DOMOTICZ);
|
2018-10-10 21:21:44 +01:00
|
|
|
break;
|
|
|
|
case FUNC_WEB_ADD_HANDLER:
|
2020-10-20 17:56:18 +01:00
|
|
|
WebServer_on(PSTR("/" WEB_HANDLE_DOMOTICZ), HandleDomoticzConfiguration);
|
2018-10-10 21:21:44 +01:00
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
2018-01-05 11:26:19 +00:00
|
|
|
case FUNC_MQTT_SUBSCRIBE:
|
|
|
|
DomoticzMqttSubscribe();
|
2019-11-26 15:51:53 +00:00
|
|
|
#ifdef USE_SHUTTER
|
2021-06-11 17:14:12 +01:00
|
|
|
if (Settings->domoticz_sensor_idx[DZ_SHUTTER]) { domoticz_is_shutter = true; }
|
2019-11-26 15:51:53 +00:00
|
|
|
#endif // USE_SHUTTER
|
2018-01-05 11:26:19 +00:00
|
|
|
break;
|
2018-01-18 15:19:28 +00:00
|
|
|
case FUNC_MQTT_INIT:
|
|
|
|
domoticz_update_timer = 2;
|
|
|
|
break;
|
2018-01-05 11:26:19 +00:00
|
|
|
case FUNC_SHOW_SENSOR:
|
|
|
|
// DomoticzSendSensor();
|
|
|
|
break;
|
2019-08-01 16:18:49 +01:00
|
|
|
case FUNC_COMMAND:
|
2019-08-11 17:12:18 +01:00
|
|
|
result = DecodeCommand(kDomoticzCommands, DomoticzCommand);
|
2019-08-01 16:18:49 +01:00
|
|
|
break;
|
2018-01-05 11:26:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
#endif // USE_DOMOTICZ
|