Delay HA discovery during PZEM sensor intialization

Delay HA discovery during PZEM sensor intialization (#12076)
This commit is contained in:
Theo Arends 2021-05-11 11:01:46 +02:00
parent d10aad68d1
commit e2e6184877
6 changed files with 25 additions and 18 deletions

View File

@ -188,7 +188,7 @@ struct {
uint8_t module_type; // Current copy of Settings.module or user template type
uint8_t last_source; // Last command source
uint8_t shutters_present; // Number of actual define shutters
// uint8_t prepped_loglevel; // Delayed log level message
uint8_t discovery_counter; // Delayed discovery counter
#ifndef SUPPORT_IF_STATEMENT
uint8_t backlog_index; // Command backlog index

View File

@ -1,5 +1,5 @@
/*
xdrv_12_discovery.ino - Discovery support for Tasmota
xdrv_12_discovery.ino - MQTT Discovery support for Tasmota
Copyright (C) 2021 Erik Montnemery, Federico Leoni and Theo Arends
@ -22,7 +22,7 @@
/*********************************************************************************************\
* Tasmota discovery
*
* A version of xdrv_12_home_assistant supporting the new Tasmota Discovery be used by
* A version of xdrv_12_home_assistant supporting the new Tasmota Discovery used by
* latest versions of Home Assistant or TasmoManager.
*
* SetOption19 0 - [DiscoverOff 0] [Discover 1] Enables discovery (default)
@ -33,8 +33,6 @@
#define XDRV_12 12
uint8_t TasDiscoverData_init_step;
void TasDiscoverMessage(void) {
Response_P(PSTR("{\"ip\":\"%_I\"," // IP Address
"\"dn\":\"%s\"," // Device Name
@ -222,13 +220,13 @@ void TasDiscovery(void) {
}
void TasRediscover(void) {
TasDiscoverData_init_step = 1; // Delayed discovery or clear retained messages
TasmotaGlobal.discovery_counter = 1; // Delayed discovery or clear retained messages
}
void TasDiscoverInit(void) {
if (ResetReason() != REASON_DEEP_SLEEP_AWAKE) {
Settings.flag.hass_discovery = 0; // SetOption19 - Enable Tasmota discovery and Disable legacy Hass discovery
TasDiscoverData_init_step = 10; // Delayed discovery
TasmotaGlobal.discovery_counter = 10; // Delayed discovery
}
}
@ -273,9 +271,9 @@ bool Xdrv12(uint8_t function) {
if (Settings.flag.mqtt_enabled) { // SetOption3 - Enable MQTT
switch (function) {
case FUNC_EVERY_SECOND:
if (TasDiscoverData_init_step) {
TasDiscoverData_init_step--;
if (!TasDiscoverData_init_step) {
if (TasmotaGlobal.discovery_counter) {
TasmotaGlobal.discovery_counter--;
if (!TasmotaGlobal.discovery_counter) {
TasDiscovery(); // Send the topics for discovery
}
}

View File

@ -175,7 +175,6 @@ const char kHAssError2[] PROGMEM =
const char kHAssError3[] PROGMEM =
"HASS: Unable to create one or more entities from Json data, please check your configuration. Failed to parse";
uint8_t hass_init_step = 0;
uint8_t hass_mode = 0;
int hass_tele_period = 0;
@ -1081,7 +1080,7 @@ void HAssDiscovery(void)
void HAssDiscover(void)
{
hass_mode = 1; // Force discovery
hass_init_step = 1; // Delayed discovery
TasmotaGlobal.discovery_counter = 1; // Delayed discovery
}
void HAssAnyKey(void)
@ -1157,10 +1156,10 @@ bool Xdrv12(uint8_t function)
switch (function)
{
case FUNC_EVERY_SECOND:
if (hass_init_step)
if (TasmotaGlobal.discovery_counter)
{
hass_init_step--;
if (!hass_init_step)
TasmotaGlobal.discovery_counter--;
if (!TasmotaGlobal.discovery_counter)
{
HAssDiscovery(); // Scheduled discovery using available resources
NewHAssDiscovery(); // Send the topics for Home Assistant Official Integration
@ -1182,7 +1181,7 @@ bool Xdrv12(uint8_t function)
break;
case FUNC_MQTT_INIT:
hass_mode = 0; // Discovery only if Settings.flag.hass_discovery is set
hass_init_step = 10; // Delayed discovery
TasmotaGlobal.discovery_counter = 10; // Delayed discovery
// if (!Settings.flag.hass_discovery) {
// NewHAssDiscovery();
// }

View File

@ -31,6 +31,7 @@
#define XNRG_03 3
const uint32_t PZEM_STABILIZE = 30; // Number of seconds to stabilize configuration
const uint32_t PZEM_RETRY = 5; // Number of 250 ms retries
#include <TasmotaSerial.h>
@ -208,7 +209,7 @@ void PzemEvery250ms(void)
Pzem.read_state = 1;
}
// AddLog(LOG_LEVEL_DEBUG, PSTR("PZM: Retry %d"), 5 - Pzem.send_retry);
// AddLog(LOG_LEVEL_DEBUG, PSTR("PZM: Retry %d"), PZEM_RETRY - Pzem.send_retry);
}
}
@ -227,13 +228,16 @@ void PzemEvery250ms(void)
Pzem.read_state = 0; // Set address
}
Pzem.send_retry = 5;
Pzem.send_retry = PZEM_RETRY;
PzemSend(pzem_commands[Pzem.read_state]);
}
else {
Pzem.send_retry--;
if ((Energy.phase_count > 1) && (0 == Pzem.send_retry) && (TasmotaGlobal.uptime < PZEM_STABILIZE)) {
Energy.phase_count--; // Decrement phases if no response after retry within 30 seconds after restart
if (TasmotaGlobal.discovery_counter) {
TasmotaGlobal.discovery_counter += (PZEM_RETRY / 4) + 1; // Don't send Discovery yet, delay by 5 * 250ms + 1s
}
}
}
}

View File

@ -111,6 +111,9 @@ void PzemAcEverySecond(void)
PzemAc.send_retry--;
if ((Energy.phase_count > 1) && (0 == PzemAc.send_retry) && (TasmotaGlobal.uptime < PZEM_AC_STABILIZE)) {
Energy.phase_count--; // Decrement phases if no response after retry within 30 seconds after restart
if (TasmotaGlobal.discovery_counter) {
TasmotaGlobal.discovery_counter += ENERGY_WATCHDOG + 1; // Don't send Discovery yet, delay by 4s + 1s
}
}
}
}

View File

@ -107,6 +107,9 @@ void PzemDcEverySecond(void)
PzemDc.send_retry--;
if ((Energy.phase_count > 1) && (0 == PzemDc.send_retry) && (TasmotaGlobal.uptime < PZEM_DC_STABILIZE)) {
Energy.phase_count--; // Decrement channels if no response after retry within 30 seconds after restart
if (TasmotaGlobal.discovery_counter) {
TasmotaGlobal.discovery_counter += ENERGY_WATCHDOG + 1; // Don't send Discovery yet, delay by 4s + 1s
}
}
}
}