From 3138d880d0ed2cb3edff46cc1749bd1a0a596582 Mon Sep 17 00:00:00 2001 From: Paul C Diem Date: Thu, 12 Mar 2020 12:51:54 -0500 Subject: [PATCH 1/5] Add periodic announcement, Raise member timeout to 45s, Fix already acked check --- tasmota/support_device_groups.ino | 160 ++++++++++++++++++++---------- tasmota/tasmota.h | 5 +- tasmota/xdrv_04_light.ino | 6 +- tasmota/xdrv_35_pwm_dimmer.ino | 6 +- 4 files changed, 117 insertions(+), 60 deletions(-) diff --git a/tasmota/support_device_groups.ino b/tasmota/support_device_groups.ino index b6df25606..8bb1e29dd 100644 --- a/tasmota/support_device_groups.ino +++ b/tasmota/support_device_groups.ino @@ -23,21 +23,25 @@ #ifdef USE_DEVICE_GROUPS //#define DEVICE_GROUPS_DEBUG +#define DGR_MEMBER_TIMEOUT 45000 +#define DGR_ANNOUNCEMENT_INTERVAL 60000 extern bool udp_connected; struct device_group_member { struct device_group_member * flink; IPAddress ip_address; - uint32_t timeout_time; uint16_t received_sequence; uint16_t acked_sequence; }; struct device_group { + uint32_t next_announcement_time; uint32_t next_ack_check_time; + uint32_t member_timeout_time; uint16_t last_full_status_sequence; uint16_t message_length; + uint16_t ack_check_interval; uint8_t message_header_length; uint8_t initial_status_requests_remaining; bool local; @@ -48,12 +52,12 @@ struct device_group { }; struct device_group * device_groups; +uint32_t next_check_time = 0; uint16_t outgoing_sequence = 0; bool device_groups_initialized = false; bool device_groups_initialization_failed = false; bool building_status_message = false; bool processing_remote_device_message = false; -bool waiting_for_acks; bool udp_was_connected = false; void DeviceGroupsInit(void) @@ -90,6 +94,17 @@ char * IPAddressToString(const IPAddress& ip_address) return buffer; } +char * BeginDeviceGroupMessage(struct device_group * device_group, uint16_t flags, bool hold_sequence = false) +{ + char * message_ptr = &device_group->message[device_group->message_header_length]; + if (!hold_sequence && !++outgoing_sequence) outgoing_sequence = 1; + *message_ptr++ = outgoing_sequence & 0xff; + *message_ptr++ = outgoing_sequence >> 8; + *message_ptr++ = flags & 0xff; + *message_ptr++ = flags >> 8; + return message_ptr; +} + // Return true if we're configured to share the specified item. bool DeviceGroupItemShared(bool incoming, uint8_t item) { @@ -121,6 +136,7 @@ bool DeviceGroupItemShared(bool incoming, uint8_t item) void SendDeviceGroupPacket(IPAddress ip, char * packet, int len, const char * label) { + if (!ip) ip = IPAddress(239,255,255,250); for (int attempt = 1; attempt <= 5; attempt++) { if (PortUdp.beginPacket(ip, 1900)) { PortUdp.write(packet, len); @@ -166,7 +182,7 @@ void _SendDeviceGroupMessage(uint8_t device_group_index, DeviceGroupMessageType device_group->message_length = 0; SendDeviceGroupMessage(device_group_index, DGR_MSGTYP_PARTIAL_UPDATE, DGR_ITEM_POWER, power); XdrvMailbox.command_code = DGR_ITEM_STATUS; - XdrvCall(FUNC_DEVICE_GROUP_REQUEST); + XdrvCall(FUNC_DEVICE_GROUP_ITEM); building_status_message = false; // If we have nothing to share with the other members, restore the message sequence and return. @@ -190,10 +206,6 @@ void _SendDeviceGroupMessage(uint8_t device_group_index, DeviceGroupMessageType #ifdef DEVICE_GROUPS_DEBUG AddLog_P2(LOG_LEVEL_DEBUG, PSTR("Building device group %s packet"), device_group->group_name); #endif // DEVICE_GROUPS_DEBUG - uint16_t original_sequence = outgoing_sequence; - if (!building_status_message && message_type != DGR_MSGTYP_PARTIAL_UPDATE && !++outgoing_sequence) outgoing_sequence = 1; - *message_ptr++ = outgoing_sequence & 0xff; - *message_ptr++ = outgoing_sequence >> 8; value = 0; if (message_type == DGR_MSGTYP_UPDATE_MORE_TO_COME) @@ -203,9 +215,7 @@ void _SendDeviceGroupMessage(uint8_t device_group_index, DeviceGroupMessageType #ifdef DEVICE_GROUPS_DEBUG AddLog_P2(LOG_LEVEL_DEBUG, PSTR(">sequence=%u, flags=%u"), outgoing_sequence, value); #endif // DEVICE_GROUPS_DEBUG - *message_ptr++ = value & 0xff; - *message_ptr++ = value >> 8; - char * first_item_ptr = message_ptr; + message_ptr = BeginDeviceGroupMessage(device_group, value, building_status_message || message_type == DGR_MSGTYP_PARTIAL_UPDATE); // If we're still building this update or all group members haven't acknowledged the previous // update yet, update the message to include these new updates. First we need to rebuild the @@ -356,13 +366,6 @@ void _SendDeviceGroupMessage(uint8_t device_group_index, DeviceGroupMessageType } va_end(ap); - // If there weren't any items added to the message, restore the outgoing message sequence and - // return. - if (message_ptr == first_item_ptr) { - outgoing_sequence = original_sequence; - return; - } - // Add the EOL item code and calculate the message length. *message_ptr++ = 0; device_group->message_length = message_ptr - device_group->message; @@ -375,17 +378,24 @@ void _SendDeviceGroupMessage(uint8_t device_group_index, DeviceGroupMessageType #ifdef DEVICE_GROUPS_DEBUG AddLog_P2(LOG_LEVEL_DEBUG, PSTR("DGR: sending %u-byte device group %s packet via multicast, sequence=%u"), device_group->message_length, device_group->group_name, device_group->message[device_group->message_header_length] | device_group->message[device_group->message_header_length + 1] << 8); #endif // DEVICE_GROUPS_DEBUG - SendDeviceGroupPacket(IPAddress(239,255,255,250), device_group->message, device_group->message_length, PSTR("Multicast")); - device_group->next_ack_check_time = millis() + 100; + SendDeviceGroupPacket(0, device_group->message, device_group->message_length, PSTR("Multicast")); + uint32_t now = millis(); if (message_type == DGR_MSGTYP_UPDATE_MORE_TO_COME) { - for (struct device_group_member * device_group_member = device_group->device_group_members; device_group_member != nullptr; device_group_member = device_group_member->flink) { - device_group_member->acked_sequence = outgoing_sequence; - } + device_group->next_ack_check_time = 0; +// for (struct device_group_member * device_group_member = device_group->device_group_members; device_group_member != nullptr; device_group_member = device_group_member->flink) { +// device_group_member->acked_sequence = outgoing_sequence; +// } } else { - waiting_for_acks = true; + device_group->ack_check_interval = 100; + device_group->next_ack_check_time = now + device_group->ack_check_interval; + if (device_group->next_ack_check_time < next_check_time) next_check_time = device_group->next_ack_check_time; + device_group->member_timeout_time = now + DGR_MEMBER_TIMEOUT; } + + device_group->next_announcement_time = now + DGR_ANNOUNCEMENT_INTERVAL; + if (device_group->next_announcement_time < next_check_time) next_check_time = device_group->next_announcement_time; } void ProcessDeviceGroupMessage(char * packet, int packet_length) @@ -452,13 +462,15 @@ void ProcessDeviceGroupMessage(char * packet, int packet_length) AddLog_P2(LOG_LEVEL_DEBUG, PSTR("Received device group %s packet from %s: sequence=%u, flags=%u"), device_group->group_name, IPAddressToString(remote_ip), message_sequence, flags); #endif // DEVICE_GROUPS_DEBUG + // If this is an announcement, simply return. + if (flags == DGR_FLAG_ANNOUNCEMENT) return; + // If this is an ack message, save the message sequence if it's newwer than the last ack we // received from this member. if (flags == DGR_FLAG_ACK) { if (message_sequence > device_group_member->acked_sequence || device_group_member->acked_sequence - message_sequence < 64536) { device_group_member->acked_sequence = message_sequence; } - device_group_member->timeout_time = 0; #ifdef DEVICE_GROUPS_DEBUG AddLog_P2(LOG_LEVEL_DEBUG, PSTR("received_sequence && device_group_member->received_sequence - message_sequence > 64536) { + if (message_sequence <= device_group_member->received_sequence) { + if (message_sequence == device_group_member->received_sequence || device_group_member->received_sequence - message_sequence > 64536) { #ifdef DEVICE_GROUPS_DEBUG - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("received_sequence = message_sequence; @@ -583,13 +597,13 @@ void ProcessDeviceGroupMessage(char * packet, int packet_length) } } else { - XdrvCall(FUNC_DEVICE_GROUP_REQUEST); + XdrvCall(FUNC_DEVICE_GROUP_ITEM); } } } XdrvMailbox.command_code = DGR_ITEM_EOL; - XdrvCall(FUNC_DEVICE_GROUP_REQUEST); + XdrvCall(FUNC_DEVICE_GROUP_ITEM); skip_light_fade = false; processing_remote_device_message = false; @@ -610,72 +624,93 @@ void DeviceGroupsLoop(void) { if (!Settings.flag4.device_groups_enabled) return; if (udp_connected) { + uint32_t now = millis(); + + // If UDP was not connected before, (re)initialize. if (!udp_was_connected) { udp_was_connected = true; if (!device_groups_initialized) DeviceGroupsInit(); if (device_groups_initialization_failed) return; + // Load the status request message for all device groups. This message will be multicast 5 + // times. + next_check_time = now + 1000; for (uint32_t device_group_index = 0; device_group_index < device_group_count; device_group_index++) { device_group * device_group = &device_groups[device_group_index]; - char * message_ptr = &device_group->message[device_group->message_header_length]; - if (!++outgoing_sequence) outgoing_sequence = 1; - *message_ptr++ = outgoing_sequence & 0xff; - *message_ptr++ = outgoing_sequence >> 8; - *message_ptr++ = DGR_FLAG_RESET | DGR_FLAG_STATUS_REQUEST; - *message_ptr++ = 0; - device_group->message_length = message_ptr - device_group->message; + device_group->message_length = BeginDeviceGroupMessage(device_group, DGR_FLAG_RESET | DGR_FLAG_STATUS_REQUEST) - device_group->message; device_group->initial_status_requests_remaining = 5; - device_group->next_ack_check_time = millis() + 1000; + device_group->next_ack_check_time = next_check_time; } - waiting_for_acks = true; } if (device_groups_initialization_failed) return; - if (waiting_for_acks) { - uint32_t now = millis(); - waiting_for_acks = false; + // If it's time to check on things, iterate through the device groups. + if (next_check_time <= now) { +#ifdef DEVICE_GROUPS_DEBUG +AddLog_P2(LOG_LEVEL_DEBUG, PSTR("DGR: Ckecking next_check_time=%u, now=%u"), next_check_time, now); +#endif // DEVICE_GROUPS_DEBUG + next_check_time = now + DGR_ANNOUNCEMENT_INTERVAL * 2; + for (uint32_t device_group_index = 0; device_group_index < device_group_count; device_group_index++) { device_group * device_group = &device_groups[device_group_index]; + + // If we're still waiting for acks to the last update from this device group, ... if (device_group->next_ack_check_time) { + + // If it's time to check for acks, ... if (device_group->next_ack_check_time <= now) { + + // If we're still sending the initial status request message, send it. if (device_group->initial_status_requests_remaining) { #ifdef DEVICE_GROUPS_DEBUG AddLog_P2(LOG_LEVEL_DEBUG, PSTR("DGR: sending initial status request for group %s"), device_group->group_name); #endif // DEVICE_GROUPS_DEBUG if (--device_group->initial_status_requests_remaining) { - SendDeviceGroupPacket(IPAddress(239,255,255,250), device_group->message, device_group->message_length, PSTR("Initial")); + SendDeviceGroupPacket(0, device_group->message, device_group->message_length, PSTR("Initial")); device_group->message[device_group->message_header_length + 2] = DGR_FLAG_STATUS_REQUEST; // The reset flag is on only for the first packet - turn it off now device_group->next_ack_check_time = now + 200; - waiting_for_acks = true; } + + // If we've sent the initial status request message 5 times, send our status to all + // the members. else { device_group->next_ack_check_time = 0; _SendDeviceGroupMessage(device_group_index, DGR_MSGTYP_FULL_STATUS); } } + + // If we're done initializing, iterate through the group memebers, ... else { +#ifdef DEVICE_GROUPS_DEBUG + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("DGR: checking for ack's")); +#endif // DEVICE_GROUPS_DEBUG bool acked = true; struct device_group_member ** flink = &device_group->device_group_members; struct device_group_member * device_group_member; while ((device_group_member = *flink)) { + + // If we have not received an ack to our last message from this member, ... if (device_group_member->acked_sequence != outgoing_sequence) { - if (device_group_member->timeout_time && device_group_member->timeout_time < now) { + // If we haven't receive an ack from this member in DGR_MEMBER_TIMEOUT ms, assume + // they're offline and remove them from the group. + if (device_group->member_timeout_time < now) { #ifdef DEVICE_GROUPS_DEBUG AddLog_P2(LOG_LEVEL_DEBUG, PSTR("DGR: removing member %s (%p)"), IPAddressToString(device_group_member->ip_address), device_group_member); #endif // DEVICE_GROUPS_DEBUG *flink = device_group_member->flink; free(device_group_member); } + + // Otherwise, unicast the last message directly to this member. else { #ifdef DEVICE_GROUPS_DEBUG AddLog_P2(LOG_LEVEL_DEBUG, PSTR("DGR: sending %u-byte device group %s packet via unicast to %s, sequence %u, last message acked=%u"), device_group->message_length, device_group->group_name, IPAddressToString(device_group_member->ip_address), outgoing_sequence, device_group_member->acked_sequence); #endif // DEVICE_GROUPS_DEBUG SendDeviceGroupPacket(device_group_member->ip_address, device_group->message, device_group->message_length, PSTR("Unicast")); - if (!device_group_member->timeout_time) device_group_member->timeout_time = now + 15000; acked = false; flink = &device_group_member->flink; } @@ -684,20 +719,43 @@ void DeviceGroupsLoop(void) flink = &device_group_member->flink; } } + + // If we've received an ack to the last message from all members, clear the ack check + // time and zero-out the message length. if (acked) { device_group->next_ack_check_time = 0; - device_group->message_length = 0; + device_group->message_length = 0; // Let _SendDeviceGroupMessage know we're done with this update } + + // If there are still members we haven't received an ack from, set the next ack check + // time. We start at 200ms and double the interval each pass with a maximum interval of + // 5 seconds. else { - device_group->next_ack_check_time = now + 500; - waiting_for_acks = true; + device_group->ack_check_interval *= 2; + if (device_group->ack_check_interval > 5000) device_group->ack_check_interval = 5000; + device_group->next_ack_check_time = now + device_group->ack_check_interval; } } } - else { - waiting_for_acks = true; - } + + if (device_group->next_ack_check_time < next_check_time) next_check_time = device_group->next_ack_check_time; } + + // If it's time to send multicast announcement for this group, send it. This is to + // announcement ourself to any members that have somehow not heard about us. We send it at + // the announcement interval plus a random number of milliseconds so that even if all the + // devices booted at the same time, they don't all multicast their announcements at the same + // time. +AddLog_P2(LOG_LEVEL_DEBUG, PSTR("DGR: next_announcement_time=%u, now=%u"), device_group->next_announcement_time, now); + if (device_group->next_announcement_time <= now) { + device_group->message_length = BeginDeviceGroupMessage(device_group, DGR_FLAG_ANNOUNCEMENT) - device_group->message; +#ifdef DEVICE_GROUPS_DEBUG + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("DGR: sending %u-byte device group %s announcement"), device_group->message_length, device_group->group_name); +#endif // DEVICE_GROUPS_DEBUG + SendDeviceGroupPacket(0, device_group->message, device_group->message_length, PSTR("Announcement")); + device_group->next_announcement_time = now + DGR_ANNOUNCEMENT_INTERVAL + random(10000); + } + if (device_group->next_announcement_time < next_check_time) next_check_time = device_group->next_announcement_time; } } } diff --git a/tasmota/tasmota.h b/tasmota/tasmota.h index a9ff6f57f..664446502 100644 --- a/tasmota/tasmota.h +++ b/tasmota/tasmota.h @@ -278,7 +278,7 @@ enum XsnsFunctions {FUNC_SETTINGS_OVERRIDE, FUNC_PIN_STATE, FUNC_MODULE_INIT, FU 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, FUNC_HOTPLUG_SCAN, - FUNC_DEVICE_GROUP_REQUEST }; + FUNC_DEVICE_GROUP_ITEM }; enum AddressConfigSteps { ADDR_IDLE, ADDR_RECEIVE, ADDR_SEND }; @@ -305,14 +305,13 @@ enum SettingsTextIndex { SET_OTAURL, enum DeviceGroupMessageType { DGR_MSGTYP_FULL_STATUS, DGR_MSGTYP_PARTIAL_UPDATE, DGR_MSGTYP_UPDATE, DGR_MSGTYP_UPDATE_MORE_TO_COME, DGR_MSGTYP_UPDATE_DIRECT, DGR_MSGTYP_REUPDATE }; -enum DeviceGroupMessageFlag { DGR_FLAG_RESET = 1, DGR_FLAG_STATUS_REQUEST = 2, DGR_FLAG_FULL_STATUS = 4, DGR_FLAG_ACK = 8, DGR_FLAG_MORE_TO_COME = 16, DGR_FLAG_DIRECT = 32 }; +enum DeviceGroupMessageFlag { DGR_FLAG_RESET = 1, DGR_FLAG_STATUS_REQUEST = 2, DGR_FLAG_FULL_STATUS = 4, DGR_FLAG_ACK = 8, DGR_FLAG_MORE_TO_COME = 16, DGR_FLAG_DIRECT = 32, DGR_FLAG_ANNOUNCEMENT = 64 }; enum DeviceGroupItem { DGR_ITEM_EOL, DGR_ITEM_STATUS, DGR_ITEM_LIGHT_FADE, DGR_ITEM_LIGHT_SPEED, DGR_ITEM_LIGHT_BRI, DGR_ITEM_LIGHT_SCHEME, DGR_ITEM_LIGHT_FIXED_COLOR, DGR_ITEM_BRI_PRESET_LOW, DGR_ITEM_BRI_PRESET_HIGH, DGR_ITEM_BRI_POWER_ON, // Add new 8-bit items before this line DGR_ITEM_LAST_8BIT, DGR_ITEM_MAX_8BIT = 63, - DGR_ITEM_ACK, DGR_ITEM_ANALOG1, DGR_ITEM_ANALOG2, DGR_ITEM_ANALOG3, DGR_ITEM_ANALOG4, DGR_ITEM_ANALOG5, // Add new 16-bit items before this line DGR_ITEM_LAST_16BIT, DGR_ITEM_MAX_16BIT = 127, diff --git a/tasmota/xdrv_04_light.ino b/tasmota/xdrv_04_light.ino index 4c331b273..9a3bf7abc 100644 --- a/tasmota/xdrv_04_light.ino +++ b/tasmota/xdrv_04_light.ino @@ -2090,7 +2090,7 @@ void LightSendDeviceGroupStatus() DGR_ITEM_LIGHT_BRI, (power ? light_state.getBri() : 0)); } -void LightHandleDeviceGroupRequest() +void LightHandleDeviceGroupItem() { static bool send_state = false; uint32_t value = XdrvMailbox.payload; @@ -2716,8 +2716,8 @@ bool Xdrv04(uint8_t function) LightAnimate(); break; #ifdef USE_DEVICE_GROUPS - case FUNC_DEVICE_GROUP_REQUEST: - LightHandleDeviceGroupRequest(); + case FUNC_DEVICE_GROUP_ITEM: + LightHandleDeviceGroupItem(); break; #endif // USE_DEVICE_GROUPS case FUNC_SET_POWER: diff --git a/tasmota/xdrv_35_pwm_dimmer.ino b/tasmota/xdrv_35_pwm_dimmer.ino index cf7dee97a..1b093e640 100644 --- a/tasmota/xdrv_35_pwm_dimmer.ino +++ b/tasmota/xdrv_35_pwm_dimmer.ino @@ -228,7 +228,7 @@ void PWMDimmerSetBri(uint8_t bri) } #ifdef USE_DEVICE_GROUPS -void PWMDimmerHandleDeviceGroupRequest() +void PWMDimmerHandleDeviceGroupItem() { static bool send_state = false; uint32_t value = XdrvMailbox.payload; @@ -1041,8 +1041,8 @@ bool Xdrv35(uint8_t function) break; #ifdef USE_DEVICE_GROUPS - case FUNC_DEVICE_GROUP_REQUEST: - PWMDimmerHandleDeviceGroupRequest(); + case FUNC_DEVICE_GROUP_ITEM: + PWMDimmerHandleDeviceGroupItem(); break; #endif // USE_DEVICE_GROUPS From 2f5846d81b275963ac1687028cd9286d504aa660 Mon Sep 17 00:00:00 2001 From: Erik Date: Fri, 13 Mar 2020 11:14:03 +0100 Subject: [PATCH 2/5] Tweaks for HA discovery --- tasmota/xdrv_12_home_assistant.ino | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tasmota/xdrv_12_home_assistant.ino b/tasmota/xdrv_12_home_assistant.ino index 900df6541..30fa0a04c 100644 --- a/tasmota/xdrv_12_home_assistant.ino +++ b/tasmota/xdrv_12_home_assistant.ino @@ -417,15 +417,15 @@ void HAssAnnounceButtons(void) } // button matrix for triggers generation when buttontopic is set as custom (default TOGGLE = 1 HOLD = 0): - // N SetOption1 SetOption11 SetOption13 PRESS DOUBLE PRESS HOLD T,H - // 1 0 0 0 TOGGLE (button_short_press) NONE (toggle real relay) NONE (reset device) 1,0 - // 2 1 0 0 TOGGLE (button_short_press) NONE (toggle real relay) HOLD (button_long_press) 1,2 - // 3 0 1 0 NONE (toggle real relay) TOGGLE (button_double_press) NONE (reset device) 3,0 - // 4 1 1 0 NONE (toggle real relay) TOGGLE (button_double_press) HOLD (button_long_press) 3,2 - // 5 0 0 1 TOGGLE (button_short_press) NONE (toggle real relay) NONE (reset device) 1,0 - // 6 1 0 1 TOGGLE (button_short_press) NONE (toggle real relay) NONE (MQTT HOLD) 1,0 - // 7 0 1 1 NONE (toggle real relay) NONE (toggle real relay) NONE (reset device) 0,0 - // 8 1 1 1 NONE (toggle real relay) NONE (toggle real relay) NONE (MQTT HOLD) 0.0 + // N SetOption1 SetOption11 SetOption13 PRESS DOUBLE PRESS HOLD T,H + // 1 0 0 0 TOGGLE (button_short_press) NONE (toggle real relay) NONE (reset device) 1,0 + // 2 1 0 0 TOGGLE (button_short_press) NONE (toggle real relay) HOLD (button_long_press) 1,2 + // 3 0 1 0 NONE (toggle real relay) TOGGLE (button_double_press) NONE (reset device) 3,0 + // 4 1 1 0 NONE (toggle real relay) TOGGLE (button_double_press) HOLD (button_long_press) 3,2 + // 5 0 0 1 TOGGLE (button_short_press) NONE (toggle real relay) NONE (reset device) 1,0 + // 6 1 0 1 TOGGLE (button_short_press) NONE (toggle real relay) NONE (MQTT HOLD) 1,0 + // 7 0 1 1 NONE (toggle real relay) NONE (toggle real relay) NONE (reset device) 0,0 + // 8 1 1 1 NONE (toggle real relay) NONE (toggle real relay) NONE (MQTT HOLD) 0.0 // Trigger types: "0 = none | 1 = button_short_press | 2 = button_long_press | 3 = button_double_press"; @@ -596,8 +596,8 @@ void HAssAnnounceStatusSensor(void) Response_P(HASS_DISCOVER_BASE, name, state_topic, availability_topic); TryResponseAppend_P(HASS_DISCOVER_SENSOR_HASS_STATUS, state_topic); - TryResponseAppend_P(HASS_DISCOVER_DEVICE_INFO, unique_id, ESP.getChipId(), WiFi.macAddress().c_str(), - SettingsText(SET_FRIENDLYNAME1), ModuleName().c_str(), my_version, my_image); + TryResponseAppend_P(HASS_DISCOVER_DEVICE_INFO, unique_id, ESP.getChipId(), SettingsText(SET_FRIENDLYNAME1), + ModuleName().c_str(), my_version, my_image); TryResponseAppend_P(PSTR("}")); } MqttPublish(stopic, true); From c62a0318cd1018580642cc6b7bdd3c4fefe339b8 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Fri, 13 Mar 2020 12:00:52 +0100 Subject: [PATCH 3/5] Bump version to 8.1.0.11 - Bump version to 8.1.0.11 - Add HAss Discovery support for Button and Switch triggers by Federico Leoni (#7901) --- RELEASENOTES.md | 3 ++- tasmota/CHANGELOG.md | 4 ++++ tasmota/tasmota_version.h | 2 +- tasmota/xdrv_02_mqtt.ino | 7 ------- tasmota/xdrv_12_home_assistant.ino | 24 ++++++++++++------------ 5 files changed, 19 insertions(+), 21 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 56c3a65b1..0a6e1fbcb 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -52,7 +52,7 @@ The following binary downloads have been compiled with ESP8266/Arduino library c ## Changelog -### Version 8.1.0.10 +### Version 8.1.0.11 - Change default my_user_config.h driver and sensor support removing most sensors and adding most drivers - Change DHT driver (#7468, #7717) @@ -116,3 +116,4 @@ The following binary downloads have been compiled with ESP8266/Arduino library c - Add support for Jarolift rollers by Keeloq algorithm - Add support for MaxBotix HRXL-MaxSonar ultrasonic range finders by Jon Little (#7814) - Add support for Romanian language translations by Augustin Marti +- Add HAss Discovery support for Button and Switch triggers by Federico Leoni (#7901) diff --git a/tasmota/CHANGELOG.md b/tasmota/CHANGELOG.md index 1ab3a02dd..11d730543 100644 --- a/tasmota/CHANGELOG.md +++ b/tasmota/CHANGELOG.md @@ -1,5 +1,9 @@ ## Unreleased (development) +### 8.1.0.11 20200313 + +- Add HAss Discovery support for Button and Switch triggers by Federico Leoni (#7901) + ### 8.1.0.10 20200227 - Change default my_user_config.h driver and sensor support removing most sensors and adding most drivers diff --git a/tasmota/tasmota_version.h b/tasmota/tasmota_version.h index b9b08b46e..636018ddc 100644 --- a/tasmota/tasmota_version.h +++ b/tasmota/tasmota_version.h @@ -20,7 +20,7 @@ #ifndef _TASMOTA_VERSION_H_ #define _TASMOTA_VERSION_H_ -const uint32_t VERSION = 0x0801000A; +const uint32_t VERSION = 0x0801000B; // Lowest compatible version const uint32_t VERSION_COMPATIBLE = 0x07010006; diff --git a/tasmota/xdrv_02_mqtt.ino b/tasmota/xdrv_02_mqtt.ino index 2a15c33df..adda72868 100644 --- a/tasmota/xdrv_02_mqtt.ino +++ b/tasmota/xdrv_02_mqtt.ino @@ -712,13 +712,6 @@ void MqttCheck(void) } } -bool ButtonTopicActive(void) -{ - char key_topic[TOPSZ]; - Format(key_topic, SettingsText(SET_MQTT_BUTTON_TOPIC), sizeof(key_topic)); - return ((strlen(key_topic) != 0) && strcmp(key_topic, "0")); -} - bool KeyTopicActive(uint32_t key) { // key = 0 - Button topic diff --git a/tasmota/xdrv_12_home_assistant.ino b/tasmota/xdrv_12_home_assistant.ino index 30fa0a04c..4e002a220 100644 --- a/tasmota/xdrv_12_home_assistant.ino +++ b/tasmota/xdrv_12_home_assistant.ino @@ -1,7 +1,7 @@ /* xdrv_12_home_assistant.ino - home assistant support for Tasmota - Copyright (C) 2020 Theo Arends + Copyright (C) 2020 Erik Montnemery, Federico Leoni and Theo Arends 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 @@ -339,7 +339,7 @@ void HAssAnnounceSwitches(void) uint8_t hold = 0; if (pin[GPIO_SWT1 + switch_index] < 99) { switch_present = 1; } - + if (KeyTopicActive(1) && strcmp(SettingsText(SET_MQTT_SWITCH_TOPIC), mqtt_topic)) // Enable Discovery for Switches only if Switchtopic is set to a custom name { @@ -361,7 +361,7 @@ void HAssAnnounceSwitches(void) // 12 PUSHHOLDMULTI_INV NO TOGGLE (button_short_press) NONE CLEAR (button_long_press) 1,0 // INV (not available) INC_DEC (not available) // Please note: SwitchMode11 and 12 will register just TOGGLE (button_short_press) - + // Trigger types: "0 = none | 1 = button_short_press | 2 = button_long_press | 3 = button_double_press"; uint8_t swmode = Settings.switchmode[switch_index]; @@ -390,7 +390,7 @@ void HAssAnnounceSwitches(void) hold = 3; break; } - + } else { switch_present = 0;} HAssAnnouncerTriggers(switch_index, switch_present, 1, toggle, hold); @@ -431,7 +431,7 @@ void HAssAnnounceButtons(void) if (Settings.flag.button_restrict) { // [SetOption1] Enable/Disable button multipress if (!Settings.flag.button_single) { - hold = 2; // Default TOGGLE (button_short_press) + HOLD (button_long_press) trigger if [SetOption13] is OFF + hold = 2; // Default TOGGLE (button_short_press) + HOLD (button_long_press) trigger if [SetOption13] is OFF } } @@ -440,19 +440,19 @@ void HAssAnnounceButtons(void) if (!Settings.flag.button_restrict) { hold = 0; // TOGGLE (button_double_press) and remove HOLD (button_long_press) trigger if [SetOption1] is OFF } - toggle = 3; // TOGGLE (button_double_press) + toggle = 3; // TOGGLE (button_double_press) } else {toggle = 0; hold = 0;} // [SetOption13] Immediate action on button press, no TOGGLE or HOLD triggers } - + if (KeyTopicActive(0)) { // Enable Discovery for Buttons only if Buttontopic is set to 1 or a custom name if (!strcmp(SettingsText(SET_MQTT_BUTTON_TOPIC), mqtt_topic)) { - toggle = 0; // When ButtonTopic is set to 1, TOGGLE is not allowed but an HOLD trigger can be generated. - } - + toggle = 0; // When ButtonTopic is set to 1, TOGGLE is not allowed but an HOLD trigger can be generated. + } + } else { button_present = 0; } - - HAssAnnouncerTriggers(button_index, button_present, 0, toggle, hold); + + HAssAnnouncerTriggers(button_index, button_present, 0, toggle, hold); } } From ce8e68d118336107370eb9e7eadfd1545ddf2cb1 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Fri, 13 Mar 2020 12:17:19 +0100 Subject: [PATCH 4/5] Disable recurring debug message Disable recurring debug message --- tasmota/support_device_groups.ino | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tasmota/support_device_groups.ino b/tasmota/support_device_groups.ino index 8bb1e29dd..d0844de7a 100644 --- a/tasmota/support_device_groups.ino +++ b/tasmota/support_device_groups.ino @@ -480,7 +480,7 @@ void ProcessDeviceGroupMessage(char * packet, int packet_length) // Send an ack message to the sender. if (!(flags & DGR_FLAG_MORE_TO_COME)) { *(message_ptr - 2) = DGR_FLAG_ACK; - *(message_ptr - 1) = 0; + *(message_ptr - 1) = 0; SendDeviceGroupPacket(remote_ip, packet, message_ptr - packet, PSTR("Ack")); } @@ -746,7 +746,9 @@ AddLog_P2(LOG_LEVEL_DEBUG, PSTR("DGR: Ckecking next_check_time=%u, now=%u"), nex // the announcement interval plus a random number of milliseconds so that even if all the // devices booted at the same time, they don't all multicast their announcements at the same // time. -AddLog_P2(LOG_LEVEL_DEBUG, PSTR("DGR: next_announcement_time=%u, now=%u"), device_group->next_announcement_time, now); +#ifdef DEVICE_GROUPS_DEBUG + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("DGR: next_announcement_time=%u, now=%u"), device_group->next_announcement_time, now); +#endif // DEVICE_GROUPS_DEBUG if (device_group->next_announcement_time <= now) { device_group->message_length = BeginDeviceGroupMessage(device_group, DGR_FLAG_ANNOUNCEMENT) - device_group->message; #ifdef DEVICE_GROUPS_DEBUG From d26ce074d819ae4bbd245d91e1db28393ae50031 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Fri, 13 Mar 2020 12:50:27 +0100 Subject: [PATCH 5/5] Fix command FriendlyName when no index is given Fix command FriendlyName newname when no index is given --- tasmota/support_command.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasmota/support_command.ino b/tasmota/support_command.ino index 43db86430..2becd43e1 100644 --- a/tasmota/support_command.ino +++ b/tasmota/support_command.ino @@ -1342,7 +1342,7 @@ void CmndWifiConfig(void) void CmndFriendlyname(void) { if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_FRIENDLYNAMES)) { - if (!XdrvMailbox.usridx) { + if (!XdrvMailbox.usridx && !XdrvMailbox.data_len) { ResponseCmndAll(SET_FRIENDLYNAME1, MAX_FRIENDLYNAMES); } else { if (XdrvMailbox.data_len > 0) {