diff --git a/sonoff/sonoff.h b/sonoff/sonoff.h index 9dd173167..62dce5e2f 100644 --- a/sonoff/sonoff.h +++ b/sonoff/sonoff.h @@ -270,7 +270,7 @@ enum XsnsFunctions {FUNC_SETTINGS_OVERRIDE, FUNC_PIN_STATE, FUNC_MODULE_INIT, FU FUNC_SAVE_AT_MIDNIGHT, FUNC_SAVE_BEFORE_RESTART, FUNC_PREP_BEFORE_TELEPERIOD, FUNC_AFTER_TELEPERIOD, FUNC_JSON_APPEND, FUNC_WEB_SENSOR, FUNC_COMMAND, FUNC_COMMAND_SENSOR, FUNC_COMMAND_DRIVER, FUNC_MQTT_SUBSCRIBE, FUNC_MQTT_INIT, FUNC_MQTT_DATA, - FUNC_SET_POWER, FUNC_SET_DEVICE_POWER, FUNC_SHOW_SENSOR, + FUNC_SET_POWER, FUNC_SET_DEVICE_POWER, FUNC_SHOW_SENSOR, FUNC_ANY_KEY, FUNC_ENERGY_EVERY_SECOND, FUNC_ENERGY_RESET, FUNC_RULES_PROCESS, FUNC_SERIAL, FUNC_FREE_MEM, FUNC_BUTTON_PRESSED, FUNC_WEB_ADD_BUTTON, FUNC_WEB_ADD_MAIN_BUTTON, FUNC_WEB_ADD_HANDLER, FUNC_SET_CHANNELS, FUNC_SET_SCHEME}; diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index 18afb76c7..ef42e85eb 100755 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -537,9 +537,10 @@ bool SendKey(uint32_t key, uint32_t device, uint32_t state) Response_P(PSTR("{\"%s%d\":{\"State\":%d}}"), (key) ? "Switch" : "Button", device, state); result = XdrvRulesProcess(); } -#ifdef USE_KNX - KnxSendButtonPower(key, device, state); -#endif // USE_KNX + int32_t payload_save = XdrvMailbox.payload; + XdrvMailbox.payload = key << 16 | state << 8 | device; + XsnsCall(FUNC_ANY_KEY); + XdrvMailbox.payload = payload_save; return result; } diff --git a/sonoff/xdrv_11_knx.ino b/sonoff/xdrv_11_knx.ino index 6fa6231c0..a1b45800e 100644 --- a/sonoff/xdrv_11_knx.ino +++ b/sonoff/xdrv_11_knx.ino @@ -672,8 +672,13 @@ void KnxUpdatePowerState(uint8_t device, power_t state) } -void KnxSendButtonPower(uint8_t key, uint8_t device, uint8_t state) +void KnxSendButtonPower(void) { + if (!(Settings.flag.knx_enabled)) { return; } + + uint32_t key = (XdrvMailbox.payload >> 16) & 0xFF; + uint32_t device = XdrvMailbox.payload & 0xFF; + uint32_t state = (XdrvMailbox.payload >> 8) & 0xFF; // key 0 = button_topic // key 1 = switch_topic // state 0 = off @@ -681,9 +686,6 @@ void KnxSendButtonPower(uint8_t key, uint8_t device, uint8_t state) // state 2 = toggle // state 3 = hold // state 9 = clear retain flag - if (!(Settings.flag.knx_enabled)) { return; } -// if (key) -// { // Search all the registered GA that has that output (variable: device) as parameter uint8_t i = KNX_GA_Search(device + 8); @@ -1207,8 +1209,13 @@ bool Xdrv11(uint8_t function) case FUNC_LOOP: if (!global_state.wifi_down) { knx.loop(); } // Process knx events break; - case FUNC_PRE_INIT: - KNX_INIT(); + case FUNC_EVERY_50_MSECOND: + if (toggle_inhibit) { + toggle_inhibit--; + } + break; + case FUNC_ANY_KEY: + KnxSendButtonPower(); break; #ifdef USE_WEBSERVER #ifdef USE_KNX_WEB_MENU @@ -1220,14 +1227,12 @@ bool Xdrv11(uint8_t function) break; #endif // USE_KNX_WEB_MENU #endif // USE_WEBSERVER - case FUNC_EVERY_50_MSECOND: - if (toggle_inhibit) { - toggle_inhibit--; - } - break; case FUNC_COMMAND: result = DecodeCommand(kKnxCommands, KnxCommand); break; + case FUNC_PRE_INIT: + KNX_INIT(); + break; // case FUNC_SET_POWER: // break; } diff --git a/sonoff/xdrv_12_home_assistant.ino b/sonoff/xdrv_12_home_assistant.ino index d5a52a491..b90a52fee 100644 --- a/sonoff/xdrv_12_home_assistant.ino +++ b/sonoff/xdrv_12_home_assistant.ino @@ -404,13 +404,13 @@ void HAssAnnounceButtons(void) } } -void HAssAnnounceSensor(const char* sensorname, const char* subsensortype) +void HAssAnnounceSensor(const char* sensorname, const char* subsensortype) { char stopic[TOPSZ]; char stemp1[TOPSZ]; char stemp2[TOPSZ]; char unique_id[30]; - bool is_sensor = true; + bool is_sensor = true; // Announce sensor, special handling of temperature and humidity sensors mqtt_data[0] = '\0'; // Clear retained message @@ -466,10 +466,10 @@ void HAssAnnounceSensor(const char* sensorname, const char* subsensortype) } else if (!strcmp_P(subsensortype, PSTR(D_JSON_ILLUMINANCE))){ TryResponseAppend_P(HASS_DISCOVER_SENSOR_ILLUMINANCE, sensorname, subsensortype); } else { - if (is_sensor){ - TryResponseAppend_P(PSTR(",\"unit_of_meas\":\" \"")); // " " As unit of measurement to get a value graph (not available for binary sensors) - } - TryResponseAppend_P(HASS_DISCOVER_SENSOR_ANY, sensorname, subsensortype); + if (is_sensor){ + TryResponseAppend_P(PSTR(",\"unit_of_meas\":\" \"")); // " " As unit of measurement to get a value graph (not available for binary sensors) + } + TryResponseAppend_P(HASS_DISCOVER_SENSOR_ANY, sensorname, subsensortype); } TryResponseAppend_P(PSTR("}")); } @@ -611,6 +611,22 @@ void HAssDiscover(void) hass_init_step = 1; // Delayed discovery } +void HAssAnyKey(void) +{ + if (!Settings.flag.hass_discovery) { return; } + + uint32_t key = (XdrvMailbox.payload >> 16) & 0xFF; + uint32_t device = XdrvMailbox.payload & 0xFF; + uint32_t state = (XdrvMailbox.payload >> 8) & 0xFF; + + char scommand[CMDSZ]; + snprintf_P(scommand, sizeof(scommand), PSTR("%s%d"), (key) ? "SWITCH" : "BUTTON", device); + char stopic[TOPSZ]; + GetTopic_P(stopic, STAT, mqtt_topic, (Settings.flag.mqtt_response) ? scommand : S_RSLT_RESULT); + Response_P(S_JSON_COMMAND_SVALUE, scommand, GetStateText(bitRead(state, device -1))); + MqttPublish(stopic); +} + /*********************************************************************************************\ * Interface \*********************************************************************************************/ @@ -621,10 +637,6 @@ bool Xdrv12(uint8_t function) if (Settings.flag.mqtt_enabled) { switch (function) { - case FUNC_MQTT_INIT: - hass_mode = 0; // Discovery only if Settings.flag.hass_discovery is set - hass_init_step = 2; // Delayed discovery - break; case FUNC_EVERY_SECOND: if (hass_init_step) { hass_init_step--; @@ -641,6 +653,13 @@ bool Xdrv12(uint8_t function) } } break; + case FUNC_ANY_KEY: + HAssAnyKey(); + break; + case FUNC_MQTT_INIT: + hass_mode = 0; // Discovery only if Settings.flag.hass_discovery is set + hass_init_step = 2; // Delayed discovery + break; } } return result;