mirror of https://github.com/arendst/Tasmota.git
Merge branch 'development' into release
This commit is contained in:
commit
1be984e5ee
|
@ -6,6 +6,6 @@
|
|||
- [ ] The pull request is done against the latest dev branch
|
||||
- [ ] Only relevant files were touched
|
||||
- [ ] Only one feature/fix was added per PR.
|
||||
- [ ] The code change is tested and works on core 2.6.1
|
||||
- [ ] The code change is tested and works on core Tasmota_core_stage
|
||||
- [ ] The code change pass travis tests. **Your PR cannot be merged unless tests pass**
|
||||
- [ ] I accept the [CLA](https://github.com/arendst/Tasmota/blob/development/CONTRIBUTING.md#contributor-license-agreement-cla).
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -6,6 +6,10 @@
|
|||
|
||||
- Release
|
||||
|
||||
### 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
|
||||
|
|
|
@ -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("<ack"));
|
||||
#endif // DEVICE_GROUPS_DEBUG
|
||||
|
@ -468,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"));
|
||||
}
|
||||
|
||||
|
@ -485,11 +497,13 @@ void ProcessDeviceGroupMessage(char * packet, int packet_length)
|
|||
}
|
||||
|
||||
// If we already processed this or a later message from this group member, ignore this message.
|
||||
if (message_sequence < device_group_member->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("<old message"));
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("<old message"));
|
||||
#endif // DEVICE_GROUPS_DEBUG
|
||||
return;
|
||||
return;
|
||||
}
|
||||
}
|
||||
device_group_member->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,45 @@ 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.
|
||||
#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
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -712,10 +712,13 @@ void MqttCheck(void)
|
|||
}
|
||||
}
|
||||
|
||||
bool ButtonTopicActive(void)
|
||||
bool KeyTopicActive(uint32_t key)
|
||||
{
|
||||
// key = 0 - Button topic
|
||||
// key = 1 - Switch topic
|
||||
key &= 1;
|
||||
char key_topic[TOPSZ];
|
||||
Format(key_topic, SettingsText(SET_MQTT_BUTTON_TOPIC), sizeof(key_topic));
|
||||
Format(key_topic, SettingsText(SET_MQTT_BUTTON_TOPIC + key), sizeof(key_topic));
|
||||
return ((strlen(key_topic) != 0) && strcmp(key_topic, "0"));
|
||||
}
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
@ -26,96 +26,104 @@ const char kHAssJsonSensorTypes[] PROGMEM =
|
|||
D_JSON_TEMPERATURE "|" D_JSON_PRESSURE "|" D_JSON_PRESSUREATSEALEVEL "|"
|
||||
D_JSON_APPARENT_POWERUSAGE "|Battery|" D_JSON_CURRENT "|" D_JSON_DISTANCE "|" D_JSON_FREQUENCY "|" D_JSON_HUMIDITY "|" D_JSON_ILLUMINANCE "|"
|
||||
D_JSON_MOISTURE "|PB0.3|PB0.5|PB1|PB2.5|PB5|PB10|PM1|PM2.5|PM10|" D_JSON_POWERFACTOR "|" D_JSON_POWERUSAGE "|"
|
||||
D_JSON_REACTIVE_POWERUSAGE "|" D_JSON_TODAY "|" D_JSON_TOTAL "|" D_JSON_VOLTAGE "|" D_JSON_WEIGHT "|" D_JSON_YESTERDAY;
|
||||
D_JSON_REACTIVE_POWERUSAGE "|" D_JSON_TODAY "|" D_JSON_TOTAL "|" D_JSON_VOLTAGE "|" D_JSON_WEIGHT "|" D_JSON_YESTERDAY
|
||||
D_JSON_CO2 "|" D_JSON_ECO2 "|" D_JSON_TVOC;
|
||||
const char kHAssJsonSensorUnits[] PROGMEM =
|
||||
"|||"
|
||||
"W|%|A|Cm|Hz|%|LX|"
|
||||
"%|ppd|ppd|ppd|ppd|ppd|ppd|µg/m³|µg/m³|µg/m³||W|"
|
||||
"W|KWh|KWh|V|Kg|KWh";
|
||||
"VA|%|A|Cm|Hz|%|LX|"
|
||||
"%|ppd|ppd|ppd|ppd|ppd|ppd|µg/m³|µg/m³|µg/m³|Cos φ|W|"
|
||||
"VAr|kWh|kWh|V|Kg|kWh|"
|
||||
"ppm|ppm|ppb|";
|
||||
const char kHAssJsonSensorDevCla[] PROGMEM =
|
||||
"dev_cla\":\"temperature|dev_cla\":\"pressure|dev_cla\":\"pressure|"
|
||||
"dev_cla\":\"power|dev_cla\":\"battery|ic\":\"mdi:alpha-a-circle-outline|ic\":\"mdi:leak|ic\":\"mdi:current-ac|dev_cla\":\"humidity|dev_cla\":\"illuminance|"
|
||||
"ic\":\"mdi:cup-water|ic\":\"mdi:flask|ic\":\"mdi:flask|ic\":\"mdi:flask|ic\":\"mdi:flask|ic\":\"mdi:flask|ic\":\"mdi:flask|"
|
||||
"ic\":\"mdi:air-filter|ic\":\"mdi:air-filter|ic\":\"mdi:air-filter|ic\":\"mdi:alpha-f-circle-outline|dev_cla\":\"power|"
|
||||
"dev_cla\":\"power|dev_cla\":\"power|dev_cla\":\"power|ic\":\"mdi:alpha-v-circle-outline|ic\":\"mdi:scale|dev_cla\":\"power";
|
||||
"dev_cla\":\"power|dev_cla\":\"power|dev_cla\":\"power|ic\":\"mdi:alpha-v-circle-outline|ic\":\"mdi:scale|dev_cla\":\"power"
|
||||
"ic\":\"mdi:periodic-table-co2|ic\":\"mdi:air-filter|ic\":\"mdi:periodic-table-co2";
|
||||
// List of sensors ready for discovery
|
||||
|
||||
const char HASS_DISCOVER_SENSOR[] PROGMEM =
|
||||
",\"unit_of_meas\":\"%s\",\"%s\"," // unit of measure and class (or icon)
|
||||
"\"frc_upd\":true," // force update for better graph representation
|
||||
"\"val_tpl\":\"{{value_json['%s']['%s']"; // "COUNTER":{"C1":0} -> {{ value_json['COUNTER'].['C1']
|
||||
",\"unit_of_meas\":\"%s\",\"%s\"," // unit of measure and class (or icon)
|
||||
"\"frc_upd\":true," // force update for better graph representation
|
||||
"\"val_tpl\":\"{{value_json['%s']['%s']"; // "COUNTER":{"C1":0} -> {{ value_json['COUNTER'].['C1']
|
||||
|
||||
const char HASS_DISCOVER_BASE[] PROGMEM =
|
||||
"{\"name\":\"%s\"," // dualr2 1
|
||||
"\"stat_t\":\"%s\"," // stat/dualr2/RESULT (implies "\"optimistic\":\"false\",")
|
||||
"\"avty_t\":\"%s\"," // tele/dualr2/LWT
|
||||
"\"pl_avail\":\"" D_ONLINE "\"," // Online
|
||||
"\"pl_not_avail\":\"" D_OFFLINE "\""; // Offline
|
||||
"{\"name\":\"%s\"," // dualr2 1
|
||||
"\"stat_t\":\"%s\"," // stat/dualr2/RESULT (implies "\"optimistic\":\"false\",")
|
||||
"\"avty_t\":\"%s\"," // tele/dualr2/LWT
|
||||
"\"pl_avail\":\"" D_ONLINE "\"," // Online
|
||||
"\"pl_not_avail\":\"" D_OFFLINE "\""; // Offline
|
||||
|
||||
const char HASS_DISCOVER_RELAY[] PROGMEM =
|
||||
",\"cmd_t\":\"%s\"," // cmnd/dualr2/POWER2
|
||||
"\"val_tpl\":\"{{value_json.%s}}\"," // POWER2
|
||||
"\"pl_off\":\"%s\"," // OFF
|
||||
"\"pl_on\":\"%s\""; // ON
|
||||
",\"cmd_t\":\"%s\"," // cmnd/dualr2/POWER2
|
||||
"\"val_tpl\":\"{{value_json.%s}}\"," // POWER2
|
||||
"\"pl_off\":\"%s\"," // OFF
|
||||
"\"pl_on\":\"%s\""; // ON
|
||||
|
||||
const char HASS_DISCOVER_BUTTON_TOGGLE[] PROGMEM =
|
||||
",\"value_template\":\"{%%if is_state(entity_id,\\\"off\\\")-%%}ON{%%-endif%%}\"," // STATE
|
||||
"\"off_delay\":1"; // HAss has no support for TOGGLE, fake it by resetting to OFF after 1s
|
||||
|
||||
const char HASS_DISCOVER_BUTTON_SWITCH_ONOFF[] PROGMEM =
|
||||
",\"value_template\":\"{{value_json.%s}}\"," // STATE
|
||||
"\"frc_upd\":true," // In ON/OFF case, enable force_update to make automations work
|
||||
"\"pl_on\":\"%s\"," // ON
|
||||
"\"pl_off\":\"%s\""; // OFF
|
||||
const char HASS_DISCOVER_BIN_SWITCH[] PROGMEM =
|
||||
",\"val_tpl\":\"{{value_json.%s}}\"," // STATE
|
||||
"\"frc_upd\":true," // In ON/OFF case, enable force_update to make automations work
|
||||
"\"pl_on\":\"%s\"," // ON
|
||||
"\"pl_off\":\"%s\""; // OFF
|
||||
|
||||
const char HASS_DISCOVER_LIGHT_DIMMER[] PROGMEM =
|
||||
",\"bri_cmd_t\":\"%s\"," // cmnd/led2/Dimmer
|
||||
"\"bri_stat_t\":\"%s\"," // stat/led2/RESULT
|
||||
"\"bri_scl\":100," // 100%
|
||||
"\"on_cmd_type\":\"%s\"," // power on (first), power on (last), no power on (brightness)
|
||||
"\"bri_val_tpl\":\"{{value_json." D_CMND_DIMMER "}}\"";
|
||||
",\"bri_cmd_t\":\"%s\"," // cmnd/led2/Dimmer
|
||||
"\"bri_stat_t\":\"%s\"," // stat/led2/RESULT
|
||||
"\"bri_scl\":100," // 100%
|
||||
"\"on_cmd_type\":\"%s\"," // power on (first), power on (last), no power on (brightness)
|
||||
"\"bri_val_tpl\":\"{{value_json." D_CMND_DIMMER "}}\"";
|
||||
|
||||
const char HASS_DISCOVER_LIGHT_COLOR[] PROGMEM =
|
||||
",\"rgb_cmd_t\":\"%s2\"," // cmnd/led2/Color2
|
||||
"\"rgb_stat_t\":\"%s\"," // stat/led2/RESULT
|
||||
"\"rgb_val_tpl\":\"{{value_json." D_CMND_COLOR ".split(',')[0:3]|join(',')}}\"";
|
||||
",\"rgb_cmd_t\":\"%s2\"," // cmnd/led2/Color2
|
||||
"\"rgb_stat_t\":\"%s\"," // stat/led2/RESULT
|
||||
"\"rgb_val_tpl\":\"{{value_json." D_CMND_COLOR ".split(',')[0:3]|join(',')}}\"";
|
||||
|
||||
const char HASS_DISCOVER_LIGHT_WHITE[] PROGMEM =
|
||||
",\"whit_val_cmd_t\":\"%s\"," // cmnd/led2/White
|
||||
"\"whit_val_stat_t\":\"%s\"," // stat/led2/RESULT
|
||||
"\"white_value_scale\":100," // (No abbreviation defined)
|
||||
"\"whit_val_tpl\":\"{{value_json.Channel[3]}}\"";
|
||||
",\"whit_val_cmd_t\":\"%s\"," // cmnd/led2/White
|
||||
"\"whit_val_stat_t\":\"%s\"," // stat/led2/RESULT
|
||||
"\"whit_val_scl\":100,"
|
||||
"\"whit_val_tpl\":\"{{value_json.Channel[3]}}\"";
|
||||
|
||||
const char HASS_DISCOVER_LIGHT_CT[] PROGMEM =
|
||||
",\"clr_temp_cmd_t\":\"%s\"," // cmnd/led2/CT
|
||||
"\"clr_temp_stat_t\":\"%s\"," // stat/led2/RESULT
|
||||
"\"clr_temp_val_tpl\":\"{{value_json." D_CMND_COLORTEMPERATURE "}}\"";
|
||||
",\"clr_temp_cmd_t\":\"%s\"," // cmnd/led2/CT
|
||||
"\"clr_temp_stat_t\":\"%s\"," // stat/led2/RESULT
|
||||
"\"clr_temp_val_tpl\":\"{{value_json." D_CMND_COLORTEMPERATURE "}}\"";
|
||||
|
||||
const char HASS_DISCOVER_LIGHT_SCHEME[] PROGMEM =
|
||||
",\"fx_cmd_t\":\"%s\"," // cmnd/led2/Scheme
|
||||
"\"fx_stat_t\":\"%s\"," // stat/led2/RESULT
|
||||
"\"fx_val_tpl\":\"{{value_json." D_CMND_SCHEME "}}\","
|
||||
"\"fx_list\":[\"0\",\"1\",\"2\",\"3\",\"4\"]"; // string list with reference to scheme parameter.
|
||||
",\"fx_cmd_t\":\"%s\"," // cmnd/led2/Scheme
|
||||
"\"fx_stat_t\":\"%s\"," // stat/led2/RESULT
|
||||
"\"fx_val_tpl\":\"{{value_json." D_CMND_SCHEME "}}\","
|
||||
"\"fx_list\":[\"0\",\"1\",\"2\",\"3\",\"4\"]"; // string list with reference to scheme parameter.
|
||||
|
||||
const char HASS_DISCOVER_SENSOR_HASS_STATUS[] PROGMEM =
|
||||
",\"json_attributes_topic\":\"%s\","
|
||||
"\"unit_of_meas\":\" \"," // " " As unit of measurement to get a value graph in HAss
|
||||
"\"val_tpl\":\"{{value_json['" D_JSON_RSSI "']}}\","
|
||||
"\"ic\":\"mdi:information-outline\"";
|
||||
",\"json_attr_t\":\"%s\","
|
||||
"\"unit_of_meas\":\"%%\","
|
||||
"\"val_tpl\":\"{{value_json['" D_JSON_RSSI "']}}\","
|
||||
"\"ic\":\"mdi:information-outline\"";
|
||||
|
||||
const char HASS_DISCOVER_DEVICE_INFO[] PROGMEM =
|
||||
",\"uniq_id\":\"%s\","
|
||||
"\"device\":{\"identifiers\":[\"%06X\"],"
|
||||
"\"connections\":[[\"mac\",\"%s\"]],"
|
||||
"\"name\":\"%s\","
|
||||
"\"model\":\"%s\","
|
||||
"\"sw_version\":\"%s%s\","
|
||||
"\"manufacturer\":\"Tasmota\"}";
|
||||
",\"uniq_id\":\"%s\","
|
||||
"\"dev\":{\"ids\":[\"%06X\"],"
|
||||
"\"name\":\"%s\","
|
||||
"\"mdl\":\"%s\","
|
||||
"\"sw\":\"%s%s\","
|
||||
"\"mf\":\"Tasmota\"}";
|
||||
|
||||
const char HASS_DISCOVER_DEVICE_INFO_SHORT[] PROGMEM =
|
||||
",\"uniq_id\":\"%s\","
|
||||
"\"device\":{\"identifiers\":[\"%06X\"],"
|
||||
"\"connections\":[[\"mac\",\"%s\"]]}";
|
||||
",\"uniq_id\":\"%s\","
|
||||
"\"dev\":{\"ids\":[\"%06X\"]}";
|
||||
|
||||
const char HASS_TRIGGER_TYPE[] PROGMEM =
|
||||
"{\"atype\":\"trigger\","
|
||||
"\"t\":\"%sT\","
|
||||
"\"pl\":\"{\\\"TRIG\\\":\\\"%s\\\"}\","
|
||||
"\"type\":\"%s\","
|
||||
"\"stype\":\"%s\","
|
||||
"\"dev\":{\"ids\":[\"%06X\"]}}";
|
||||
|
||||
const char kHAssTriggerType[] PROGMEM =
|
||||
"none|button_short_press|button_long_press|button_double_press";
|
||||
|
||||
uint8_t hass_init_step = 0;
|
||||
uint8_t hass_mode = 0;
|
||||
|
@ -183,12 +191,9 @@ void HAssAnnounceRelayLight(void)
|
|||
char *state_topic = stemp2;
|
||||
char *availability_topic = stemp3;
|
||||
|
||||
if (i > MAX_FRIENDLYNAMES)
|
||||
{
|
||||
if (i > MAX_FRIENDLYNAMES) {
|
||||
snprintf_P(name, sizeof(name), PSTR("%s %d"), SettingsText(SET_FRIENDLYNAME1), i);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
snprintf_P(name, sizeof(name), SettingsText(SET_FRIENDLYNAME1 + i - 1));
|
||||
}
|
||||
GetPowerDevice(value_template, i, sizeof(value_template), Settings.flag.device_index_enable); // SetOption26 - Switch between POWER or POWER1
|
||||
|
@ -198,7 +203,7 @@ void HAssAnnounceRelayLight(void)
|
|||
|
||||
Response_P(HASS_DISCOVER_BASE, name, state_topic, availability_topic);
|
||||
TryResponseAppend_P(HASS_DISCOVER_RELAY, command_topic, value_template, SettingsText(SET_STATE_TXT1), SettingsText(SET_STATE_TXT2));
|
||||
TryResponseAppend_P(HASS_DISCOVER_DEVICE_INFO_SHORT, unique_id, ESP.getChipId(), WiFi.macAddress().c_str());
|
||||
TryResponseAppend_P(HASS_DISCOVER_DEVICE_INFO_SHORT, unique_id, ESP.getChipId());
|
||||
|
||||
#if defined(USE_LIGHT) || defined(USE_PWM_DIMMER)
|
||||
if (is_light || PWM_DIMMER == my_module_type)
|
||||
|
@ -244,7 +249,7 @@ void HAssAnnounceRelayLight(void)
|
|||
}
|
||||
}
|
||||
|
||||
void HAssAnnounceButtonSwitch(uint8_t device, char *topic, uint8_t present, uint8_t key, uint8_t toggle)
|
||||
void HAssAnnouncerTriggers(uint8_t device, uint8_t present, uint8_t key, uint8_t toggle, uint8_t hold)
|
||||
{
|
||||
// key 0 = button
|
||||
// key 1 = switch
|
||||
|
@ -255,108 +260,199 @@ void HAssAnnounceButtonSwitch(uint8_t device, char *topic, uint8_t present, uint
|
|||
|
||||
mqtt_data[0] = '\0'; // Clear retained message
|
||||
|
||||
// Clear or Set topic
|
||||
snprintf_P(unique_id, sizeof(unique_id), PSTR("%06X_%s_%d"), ESP.getChipId(), key ? "SW" : "BTN", device + 1);
|
||||
for (uint8_t i = 2; i <= 3; i++) {
|
||||
snprintf_P(unique_id, sizeof(unique_id), PSTR("%06X_%s_%d_%s"), ESP.getChipId(), key ? "SW" : "BTN", device + 1, GetStateText(i));
|
||||
snprintf_P(stopic, sizeof(stopic), PSTR(HOME_ASSISTANT_DISCOVERY_PREFIX "/device_automation/%s/config"), unique_id);
|
||||
|
||||
if (Settings.flag.hass_discovery && present) { // SetOption19 - Control Home Assistantautomatic discovery (See SetOption59)
|
||||
char name[33 + 6]; // friendlyname(33) + " " + "BTN" + " " + index
|
||||
char value_template[33];
|
||||
char prefix[TOPSZ];
|
||||
char *state_topic = stemp1;
|
||||
char *availability_topic = stemp2;
|
||||
char jsoname[8];
|
||||
|
||||
GetPowerDevice(value_template, device + 1, sizeof(value_template), key + Settings.flag.device_index_enable); // Force index for Switch 1, Index on Button1 is controlled by SetOption26 - Switch between POWER or POWER1
|
||||
snprintf_P(jsoname, sizeof(jsoname), PSTR("%s%d"), key ? "SWITCH" : "BUTTON", device + 1);
|
||||
GetTopic_P(state_topic, STAT, mqtt_topic, jsoname);
|
||||
GetTopic_P(availability_topic, TELE, mqtt_topic, S_LWT);
|
||||
|
||||
char param[21];
|
||||
char subtype[9];
|
||||
uint8_t pload = toggle;
|
||||
|
||||
if ((i == 2 && toggle != 0) || (i == 3 && hold != 0)) {
|
||||
if (i == 3) { pload = hold; }
|
||||
GetTextIndexed(param, sizeof(param), pload, kHAssTriggerType);
|
||||
snprintf_P(subtype, sizeof(subtype), PSTR("%s_%d"), key ? "switch" : "button", device + 1);
|
||||
Response_P(HASS_TRIGGER_TYPE, state_topic, GetStateText(i), param, subtype, ESP.getChipId());
|
||||
} else { mqtt_data[0] = '\0'; } // Need to be cleaned again to avoid duplicate.
|
||||
}
|
||||
MqttPublish(stopic, true);
|
||||
}
|
||||
}
|
||||
|
||||
void HAssAnnouncerBinSensors(uint8_t device, uint8_t present, uint8_t dual, uint8_t toggle)
|
||||
{
|
||||
char stopic[TOPSZ];
|
||||
char stemp1[TOPSZ];
|
||||
char stemp2[TOPSZ];
|
||||
char unique_id[30];
|
||||
|
||||
mqtt_data[0] = '\0'; // Clear retained message
|
||||
|
||||
snprintf_P(unique_id, sizeof(unique_id), PSTR("%06X_SW_%d"), ESP.getChipId(), device + 1);
|
||||
snprintf_P(stopic, sizeof(stopic), PSTR(HOME_ASSISTANT_DISCOVERY_PREFIX "/binary_sensor/%s/config"), unique_id);
|
||||
|
||||
if (Settings.flag.hass_discovery && present)
|
||||
{ // SetOption19 - Control Home Assistantautomatic discovery (See SetOption59)
|
||||
char name[33 + 6]; // friendlyname(33) + " " + "BTN" + " " + index
|
||||
char value_template[33];
|
||||
char prefix[TOPSZ];
|
||||
char *state_topic = stemp1;
|
||||
char *availability_topic = stemp2;
|
||||
char jsoname[8];
|
||||
|
||||
snprintf_P(name, sizeof(name), PSTR("%s %s%d"), SettingsText(SET_FRIENDLYNAME1), key ? "Switch" : "Button", device + 1);
|
||||
snprintf_P(jsoname, sizeof(jsoname), PSTR("%s%d"), key ? "SWITCH" : "BUTTON", device + 1);
|
||||
GetPowerDevice(value_template, device + 1, sizeof(value_template),
|
||||
key + Settings.flag.device_index_enable); // Force index for Switch 1, Index on Button1 is controlled by SetOption26 - Switch between POWER or POWER1
|
||||
GetTopic_P(state_topic, STAT, mqtt_topic, (PSTR("/'%s'"), jsoname));
|
||||
GetTopic_P(availability_topic, TELE, mqtt_topic, S_LWT);
|
||||
if (Settings.flag.hass_discovery && present ) { // SetOption19 - Control Home Assistantautomatic discovery (See SetOption59)
|
||||
if (!toggle || dual) {
|
||||
char name[33 + 6]; // friendlyname(33) + " " + "BTN" + " " + index
|
||||
char value_template[33];
|
||||
char prefix[TOPSZ];
|
||||
char *state_topic = stemp1;
|
||||
char *availability_topic = stemp2;
|
||||
char jsoname[8];
|
||||
|
||||
Response_P(HASS_DISCOVER_BASE, name, state_topic, availability_topic);
|
||||
TryResponseAppend_P(HASS_DISCOVER_DEVICE_INFO_SHORT, unique_id, ESP.getChipId(), WiFi.macAddress().c_str());
|
||||
GetPowerDevice(value_template, device + 1, sizeof(value_template), 1 + Settings.flag.device_index_enable); // Force index for Switch 1, Index on Button1 is controlled by SetOption26 - Switch between POWER or POWER1
|
||||
snprintf_P(jsoname, sizeof(jsoname), PSTR("SWITCH%d"), device + 1);
|
||||
GetTopic_P(state_topic, STAT, mqtt_topic, jsoname);
|
||||
GetTopic_P(availability_topic, TELE, mqtt_topic, S_LWT);
|
||||
|
||||
if (toggle) {
|
||||
if (!key) {
|
||||
TryResponseAppend_P(HASS_DISCOVER_BUTTON_TOGGLE);
|
||||
} else { // A switch must maintain his state until the next update
|
||||
TryResponseAppend_P(",\"value_template\":\"{%%if is_state(entity_id,\\\"on\\\")-%%}OFF{%%-else-%%}ON{%%-endif%%}\"");
|
||||
}
|
||||
} else {
|
||||
TryResponseAppend_P(HASS_DISCOVER_BUTTON_SWITCH_ONOFF, PSTR(D_RSLT_STATE), SettingsText(SET_STATE_TXT2), SettingsText(SET_STATE_TXT1));
|
||||
snprintf_P(name, sizeof(name), PSTR("%s Switch%d"), SettingsText(SET_FRIENDLYNAME1), device + 1);
|
||||
Response_P(HASS_DISCOVER_BASE, name, state_topic, availability_topic);
|
||||
TryResponseAppend_P(HASS_DISCOVER_BIN_SWITCH, PSTR(D_RSLT_STATE), SettingsText(SET_STATE_TXT2), SettingsText(SET_STATE_TXT1));
|
||||
TryResponseAppend_P(HASS_DISCOVER_DEVICE_INFO_SHORT, unique_id, ESP.getChipId());
|
||||
TryResponseAppend_P(PSTR("}"));
|
||||
}
|
||||
TryResponseAppend_P(PSTR("}"));
|
||||
}
|
||||
MqttPublish(stopic, true);
|
||||
}
|
||||
|
||||
void HAssAnnounceSwitches(void)
|
||||
{
|
||||
char sw_topic[TOPSZ];
|
||||
|
||||
// Send info about buttons
|
||||
char *tmp = SettingsText(SET_MQTT_SWITCH_TOPIC);
|
||||
Format(sw_topic, tmp, sizeof(sw_topic));
|
||||
if (!strcmp_P(sw_topic, "0") || strlen(sw_topic) == 0)
|
||||
for (uint32_t switch_index = 0; switch_index < MAX_SWITCHES; switch_index++)
|
||||
{
|
||||
for (uint32_t switch_index = 0; switch_index < MAX_SWITCHES; switch_index++)
|
||||
uint8_t switch_present = 0;
|
||||
uint8_t dual = 0;
|
||||
uint8_t toggle = 1;
|
||||
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
|
||||
{
|
||||
uint8_t switch_present = 0;
|
||||
uint8_t toggle = 1;
|
||||
|
||||
if (pin[GPIO_SWT1 + switch_index] < 99)
|
||||
{
|
||||
switch_present = 1;
|
||||
// switch matrix for triggers and binary sensor generation when switchtopic is set as custom (default index is 0,0 - TOGGLE, TOGGLE):
|
||||
// SWITCHMODE INTERNAL BINARY PRESS DOUBLE PRESS HOLD T,H
|
||||
// 0 TOGGLE NO TOGGLE (button_short_press) NONE NONE 1,0
|
||||
// 1 FOLLOW YES NONE NONE NONE 0,0
|
||||
// 2 FOLLOW_INV YES NONE NONE NONE 0,0
|
||||
// 3 PUSHBUTTON YES TOGGLE (button_short_press) NONE NONE 1,0
|
||||
// 4 PUSHBUTTON_INV YES TOGGLE (button_short_press) NONE NONE 1,0
|
||||
// 5 PUSHBUTTONHOLD YES TOGGLE (button_short_press) NONE HOLD (button_long_press) 1,2
|
||||
// 6 PUSHBUTTONHOLD_INV YES TOGGLE (button_short_press) NONE HOLD (button_long_press) 1,2
|
||||
// 7 PUSHBUTTON_TOGGLE NO TOGGLE (button_short_press) NONE NONE 1,0
|
||||
// 8 TOGGLEMULTI NO TOGGLE (button_short_press) HOLD (button_double_press) NONE 1,3
|
||||
// 9 FOLLOWMULTI YES NONE HOLD (button_double_press) NONE 0,3
|
||||
// 10 FOLLOWMULTI_INV YES NONE HOLD (button_double_press) NONE 0,3
|
||||
// 11 PUSHHOLDMULTI NO TOGGLE (button_short_press) NONE INC_DEC (button_long_press) 1,0
|
||||
// INV (not available) CLEAR (not available)
|
||||
// 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];
|
||||
|
||||
switch (swmode) {
|
||||
case 1:
|
||||
case 2:
|
||||
toggle = 0; // Binary sensor and no triggers
|
||||
break;
|
||||
case 3:
|
||||
case 4:
|
||||
dual = 1; // Binary sensor and TOGGLE (button_short_press) trigger
|
||||
break;
|
||||
case 5:
|
||||
case 6:
|
||||
dual = 1; // Binary sensor, TOGGLE (button_short_press) and HOLD (button_long_press) triggers
|
||||
hold = 2;
|
||||
break;
|
||||
case 8:
|
||||
hold = 3; // TOGGLE (button_short_press) and HOLD (button_double_press) triggers
|
||||
break;
|
||||
case 9:
|
||||
case 10:
|
||||
dual = 1; // Binary sensor and HOLD (button_long_press) trigger
|
||||
toggle = 0;
|
||||
hold = 3;
|
||||
break;
|
||||
}
|
||||
|
||||
// Check if MQTT message will be ON/OFF or TOGGLE
|
||||
if (Settings.switchmode[switch_index] == FOLLOW || Settings.switchmode[switch_index] == FOLLOW_INV ||
|
||||
Settings.flag3.button_switch_force_local || // SetOption61 - Force local operation when button/switch topic is set
|
||||
!strcmp(mqtt_topic, sw_topic) || !strcmp(SettingsText(SET_MQTT_GRP_TOPIC), sw_topic))
|
||||
{
|
||||
toggle = 0; // MQTT message will be ON/OFF
|
||||
}
|
||||
HAssAnnounceButtonSwitch(switch_index, sw_topic, switch_present, 1, toggle);
|
||||
}
|
||||
} else { switch_present = 0;}
|
||||
|
||||
HAssAnnouncerTriggers(switch_index, switch_present, 1, toggle, hold);
|
||||
HAssAnnouncerBinSensors(switch_index, switch_present, dual, toggle);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void HAssAnnounceButtons(void)
|
||||
{
|
||||
char key_topic[TOPSZ];
|
||||
|
||||
// Send info about buttons
|
||||
char *tmp = SettingsText(SET_MQTT_BUTTON_TOPIC);
|
||||
Format(key_topic, tmp, sizeof(key_topic));
|
||||
if (!strcmp_P(key_topic, "0") || strlen(key_topic) == 0)
|
||||
for (uint32_t button_index = 0; button_index < MAX_KEYS; button_index++)
|
||||
{
|
||||
for (uint32_t button_index = 0; button_index < MAX_KEYS; button_index++)
|
||||
{
|
||||
uint8_t button_present = 0;
|
||||
uint8_t toggle = 1;
|
||||
uint8_t button_present = 0;
|
||||
uint8_t toggle = 1;
|
||||
uint8_t hold = 0;
|
||||
|
||||
if (!button_index && ((SONOFF_DUAL == my_module_type) || (CH4 == my_module_type)))
|
||||
{
|
||||
if (!button_index && ((SONOFF_DUAL == my_module_type) || (CH4 == my_module_type)))
|
||||
{
|
||||
button_present = 1;
|
||||
} else {
|
||||
if (pin[GPIO_KEY1 + button_index] < 99) {
|
||||
button_present = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (pin[GPIO_KEY1 + button_index] < 99)
|
||||
{
|
||||
button_present = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
// Trigger types: "0 = none | 1 = button_short_press | 2 = button_long_press | 3 = button_double_press";
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
if (Settings.flag.button_swap) { // [SetOption11] Swap button single and double press functionality
|
||||
if (!Settings.flag.button_single) {
|
||||
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)
|
||||
} 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.
|
||||
}
|
||||
|
||||
// Check if MQTT message will be ON/OFF or TOGGLE
|
||||
if (Settings.flag3.button_switch_force_local || // SetOption61 - Force local operation when button/switch topic is set
|
||||
!strcmp(mqtt_topic, key_topic) || !strcmp(SettingsText(SET_MQTT_GRP_TOPIC), key_topic))
|
||||
{
|
||||
toggle = 0; // MQTT message will be ON/OFF
|
||||
}
|
||||
HAssAnnounceButtonSwitch(button_index, key_topic, button_present, 0, toggle);
|
||||
}
|
||||
} else { button_present = 0; }
|
||||
|
||||
HAssAnnouncerTriggers(button_index, button_present, 0, toggle, hold);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -369,9 +465,7 @@ void HAssAnnounceSensor(const char *sensorname, const char *subsensortype, const
|
|||
|
||||
mqtt_data[0] = '\0'; // Clear retained message
|
||||
// Clear or Set topic
|
||||
char subname[20];
|
||||
NoAlNumToUnderscore(subname, MultiSubName); //Replace all non alphaumeric characters to '_' to avoid topic name issues
|
||||
snprintf_P(unique_id, sizeof(unique_id), PSTR("%06X_%s_%s"), ESP.getChipId(), sensorname, subname);
|
||||
snprintf_P(unique_id, sizeof(unique_id), PSTR("%06X_%s_%s"), ESP.getChipId(), sensorname, MultiSubName);
|
||||
snprintf_P(stopic, sizeof(stopic), PSTR(HOME_ASSISTANT_DISCOVERY_PREFIX "/sensor/%s/config"), unique_id);;
|
||||
|
||||
if (Settings.flag.hass_discovery)
|
||||
|
@ -381,13 +475,13 @@ void HAssAnnounceSensor(const char *sensorname, const char *subsensortype, const
|
|||
char *state_topic = stemp1;
|
||||
char *availability_topic = stemp2;
|
||||
|
||||
snprintf_P(stopic, sizeof(stopic), PSTR(HOME_ASSISTANT_DISCOVERY_PREFIX "/sensor/%s/config"), unique_id);
|
||||
GetTopic_P(state_topic, TELE, mqtt_topic, PSTR(D_RSLT_SENSOR));
|
||||
snprintf_P(name, sizeof(name), PSTR("%s %s %s"), SettingsText(SET_FRIENDLYNAME1), sensorname, MultiSubName);
|
||||
GetTopic_P(availability_topic, TELE, mqtt_topic, S_LWT);
|
||||
|
||||
Response_P(HASS_DISCOVER_BASE, name, state_topic, availability_topic);
|
||||
TryResponseAppend_P(HASS_DISCOVER_DEVICE_INFO_SHORT, unique_id, ESP.getChipId(), WiFi.macAddress().c_str());
|
||||
TryResponseAppend_P(HASS_DISCOVER_DEVICE_INFO_SHORT, unique_id, ESP.getChipId());
|
||||
|
||||
|
||||
char jname[32];
|
||||
int sensor_index = GetCommandCode(jname, sizeof(jname), subsensortype, kHAssJsonSensorTypes);
|
||||
|
@ -398,7 +492,7 @@ void HAssAnnounceSensor(const char *sensorname, const char *subsensortype, const
|
|||
case 0: // Temperature
|
||||
snprintf_P(param1, sizeof(param1), PSTR("°%c"),TempUnit()); // C or F
|
||||
break;
|
||||
case 1:
|
||||
case 1: // Pressure
|
||||
case 2:
|
||||
snprintf_P(param1, sizeof(param1), PSTR("%s"), PressureUnit().c_str());
|
||||
break;
|
||||
|
@ -439,7 +533,6 @@ void HAssAnnounceSensors(void)
|
|||
snprintf_P(sensordata, sizeof(sensordata), PSTR("%s}"), sensordata); // {"INA219":{"Voltage":4.494,"Current":0.020,"Power":0.089}}
|
||||
// USE THE FOLLOWING LINE TO TEST JSON
|
||||
//snprintf_P(sensordata, sizeof(sensordata), PSTR("{\"HX711\":{\"Weight\":[22,34,1023.4], \"Battery\":25}}"));
|
||||
|
||||
StaticJsonBuffer<500> jsonBuffer;
|
||||
JsonObject &root = jsonBuffer.parseObject(sensordata);
|
||||
if (!root.success())
|
||||
|
@ -464,8 +557,9 @@ void HAssAnnounceSensors(void)
|
|||
subqty = subsensors.size();
|
||||
char MultiSubName[20];
|
||||
for (int i = 1; i <= subqty; i++) {
|
||||
snprintf_P(MultiSubName, sizeof(MultiSubName), PSTR("%s %d"), subsensor.key, i);
|
||||
snprintf_P(MultiSubName, sizeof(MultiSubName), PSTR("%s_%d"), subsensor.key, i);
|
||||
HAssAnnounceSensor(sensorname, subsensor.key, MultiSubName, i, 1);
|
||||
|
||||
}
|
||||
} else { HAssAnnounceSensor(sensorname, subsensor.key, subsensor.key, 0, 0);}
|
||||
}
|
||||
|
@ -502,9 +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);
|
||||
|
@ -529,11 +622,13 @@ void HAssDiscovery(void)
|
|||
{
|
||||
// Configure Tasmota for default Home Assistant parameters to keep discovery message as short as possible
|
||||
if (Settings.flag.hass_discovery)
|
||||
{ // SetOption19 - Control Home Assistant automatic discovery (See SetOption59)
|
||||
Settings.flag.mqtt_response = 0; // SetOption4 - Switch between MQTT RESULT or COMMAND - Response always as RESULT and not as uppercase command
|
||||
Settings.flag.decimal_text = 1; // SetOption17 - Switch between decimal or hexadecimal output - Respond with decimal color values
|
||||
Settings.flag3.hass_tele_on_power = 1; // SetOption59 - Send tele/%topic%/STATE in addition to stat/%topic%/RESULT - send tele/STATE message as stat/RESULT
|
||||
Settings.light_scheme = 0; // To just control color it needs to be Scheme 0
|
||||
{ // SetOption19 - Control Home Assistant automatic discovery (See SetOption59)
|
||||
Settings.flag.mqtt_response = 0; // SetOption4 - Switch between MQTT RESULT or COMMAND - Response always as RESULT and not as uppercase command
|
||||
Settings.flag.decimal_text = 1; // SetOption17 - Switch between decimal or hexadecimal output - Respond with decimal color values
|
||||
Settings.flag3.hass_tele_on_power = 1; // SetOption59 - Send tele/%topic%/STATE in addition to stat/%topic%/RESULT - send tele/STATE message as stat/RESULT
|
||||
// the purpose of that is so that if HA is restarted, state in HA will be correct within one teleperiod otherwise state
|
||||
// will not be correct until the device state is changed this is why in the patterns for switch and light, we tell HA to trigger on STATE, not RESULT.
|
||||
Settings.light_scheme = 0; // To just control color it needs to be Scheme 0
|
||||
}
|
||||
|
||||
if (Settings.flag.hass_discovery || (1 == hass_mode))
|
||||
|
@ -570,18 +665,28 @@ void HAssAnyKey(void)
|
|||
|
||||
uint32_t key = (XdrvMailbox.payload >> 16) & 0xFF; // 0 = Button, 1 = Switch
|
||||
uint32_t device = XdrvMailbox.payload & 0xFF; // Device number or 1 if more Buttons than Devices
|
||||
uint32_t state = (XdrvMailbox.payload >> 8) & 0xFF; // 0 = Off, 1 = On, 2 = Toggle
|
||||
uint32_t state = (XdrvMailbox.payload >> 8) & 0xFF; // 0 = Off, 1 = On, 2 = Toggle, 3 = Hold
|
||||
|
||||
if (!key && ButtonTopicActive()) { // Button and ButtonTopic is active
|
||||
if (!key && KeyTopicActive(0)) { // Button and ButtonTopic is active
|
||||
device = (XdrvMailbox.payload >> 24) & 0xFF; // Button number
|
||||
}
|
||||
|
||||
char scommand[CMDSZ];
|
||||
snprintf_P(scommand, sizeof(scommand), PSTR("%s%d"), (key) ? "SWITCH" : "BUTTON", device);
|
||||
char sw_topic[TOPSZ];
|
||||
char key_topic[TOPSZ];
|
||||
char *tmpbtn = SettingsText(SET_MQTT_BUTTON_TOPIC);
|
||||
char *tmpsw = SettingsText(SET_MQTT_SWITCH_TOPIC);
|
||||
uint8_t evkey = 0; // Flag to select the correct topic for a trigger or a binary_sensor
|
||||
Format(sw_topic, tmpsw, sizeof(sw_topic));
|
||||
Format(key_topic, tmpbtn, sizeof(key_topic));
|
||||
|
||||
if (state == 2 || state == 3 ) { evkey = 1;}
|
||||
snprintf_P(scommand, sizeof(scommand), PSTR("%s%d%s"), (key) ? "SWITCH" : "BUTTON", device, (evkey) ? "T" : "");
|
||||
|
||||
char stopic[TOPSZ];
|
||||
|
||||
GetTopic_P(stopic, STAT, mqtt_topic, scommand);
|
||||
Response_P(S_JSON_COMMAND_SVALUE, PSTR(D_RSLT_STATE), GetStateText(state));
|
||||
Response_P(S_JSON_COMMAND_SVALUE, (evkey) ? "TRIG" : PSTR(D_RSLT_STATE), GetStateText(state));
|
||||
MqttPublish(stopic);
|
||||
}
|
||||
|
||||
|
@ -630,4 +735,4 @@ bool Xdrv12(uint8_t function)
|
|||
return result;
|
||||
}
|
||||
|
||||
#endif // USE_HOME_ASSISTANT
|
||||
#endif // USE_HOME_ASSISTANT
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue