Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to remove some non-JSON MQTT topics #377

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ In a browser navigate to the IP address assigned to the ESP32.
- Restart on disconnect: Enable to restart the Nuki Hub after 60 seconds without a connection to a network.
- Enable MQTT logging: Enable to fill the maintenance/log MQTT topic with debug log information.
- Check for Firmware Updates every 24h: Enable to allow the Nuki Hub to check the latest release of the Nuki Hub firmware on boot and every 24 hours. Requires the Nuki Hub to be able to connect to github.com. The latest version will be published to MQTT and will be visible on the main page of the Web Configurator.
- Disable some extraneous non-JSON topics: Enable to not publish non-JSON keypad and config MQTT topics.

#### IP Address assignment

Expand Down Expand Up @@ -263,6 +264,8 @@ In a browser navigate to the IP address assigned to the ESP32.
- battery/maxTurnCurrent: The highest current of the turn motor during the last lock action (A) (Lock only).
- battery/lockDistance: The total distance during the last lock action in centidegrees (Lock only).
- battery/keypadCritical: 1 if the battery level of a connected keypad is critical, otherwise 0.
- battery/basicJson: The current battery state (critical, charging, level and keypad critical) of the Nuki Lock/Opener as JSON data.
- battery/advancedJson: : The current battery state (critical, batteryDrain, batteryVoltage, lockAction, startVoltage, lowestVoltage, lockDistance, startTemperature, maxTurnCurrent and batteryResistance) of the Nuki Lock/Opener as JSON data.

### Keypad

Expand Down
8 changes: 4 additions & 4 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ board = esp32dev
build_flags =
${env.build_flags}
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=4
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=0
-DDEBUG_NUKIHUB
-DDEBUG_SENSE_NUKI
-DDEBUG_NUKI_COMMAND
Expand All @@ -80,7 +80,7 @@ board = esp32-s3-devkitc-1
build_flags =
${env.build_flags}
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=4
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=0
-DDEBUG_NUKIHUB
-DDEBUG_SENSE_NUKI
-DDEBUG_NUKI_COMMAND
Expand All @@ -94,7 +94,7 @@ board = esp32-c3-devkitc-02
build_flags =
${env.build_flags}
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=4
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=0
-DDEBUG_NUKIHUB
-DDEBUG_SENSE_NUKI
-DDEBUG_NUKI_COMMAND
Expand All @@ -110,7 +110,7 @@ build_flags =
${env.build_flags}
-DFRAMEWORK_ARDUINO_SOLO1
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=4
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=0
-DDEBUG_NUKIHUB
-DDEBUG_SENSE_NUKI
-DDEBUG_NUKI_COMMAND
Expand Down
2 changes: 2 additions & 0 deletions src/MqttTopics.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
#define mqtt_topic_battery_max_turn_current "/battery/maxTurnCurrent"
#define mqtt_topic_battery_lock_distance "/battery/lockDistance"
#define mqtt_topic_battery_keypad_critical "/battery/keypadCritical"
#define mqtt_topic_battery_basic_json "/battery/basicJson"
#define mqtt_topic_battery_advanced_json "/battery/advancedJson"

#define mqtt_topic_keypad "/keypad"
#define mqtt_topic_keypad_command_action "/keypad/command/action"
Expand Down
40 changes: 28 additions & 12 deletions src/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,13 @@ void Network::initialize()
bool Network::update()
{
unsigned long ts = millis();

if(ts > 120000 && ts < 125000 && _preferences->getInt(preference_bootloop_counter, 0) > 0)
{
_preferences->putInt(preference_bootloop_counter, 0);
Log->println(F("Bootloop counter reset"));
}

_device->update();

if(!_mqttEnabled)
Expand Down Expand Up @@ -806,14 +806,15 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n
"Battery low",
name,
baseTopic,
String("~") + mqtt_topic_battery_critical,
String("~") + mqtt_topic_battery_basic_json,
deviceType,
"battery",
"",
"diagnostic",
"",
{{(char*)"pl_on", (char*)"1"},
{(char*)"pl_off", (char*)"0"}});
{(char*)"pl_off", (char*)"0"},
{(char*)"val_tpl", (char*)"{{value_json.critical}}" }});

// Battery voltage
publishHassTopic("sensor",
Expand All @@ -823,13 +824,14 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n
"Battery voltage",
name,
baseTopic,
String("~") + mqtt_topic_battery_voltage,
String("~") + mqtt_topic_battery_advanced_json,
deviceType,
"voltage",
"measurement",
"diagnostic",
"",
{ {(char*)"unit_of_meas", (char*)"V"} });
{ {(char*)"unit_of_meas", (char*)"V"},
{(char*)"val_tpl", (char*)"{{value_json.level}}" }});

// Trigger
publishHassTopic("sensor",
Expand Down Expand Up @@ -935,7 +937,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n
"",
{ { (char*)"en", (char*)"true" },
{(char*)"ic", (char*)"mdi:counter"}});

// Nuki Hub build
publishHassTopic("sensor",
"nuki_hub_build",
Expand Down Expand Up @@ -1333,13 +1335,14 @@ void Network::publishHASSConfigAdditionalLockEntities(char *deviceType, const ch
"Battery level",
name,
baseTopic,
String("~") + mqtt_topic_battery_level,
String("~") + mqtt_topic_battery_basic_json,
deviceType,
"battery",
"measurement",
"diagnostic",
"",
{ {(char*)"unit_of_meas", (char*)"%"} });
{ {(char*)"unit_of_meas", (char*)"%"},
{(char*)"val_tpl", (char*)"{{value_json.level}}" }});

if((int)basicLockConfigAclPrefs[7] == 1)
{
Expand Down Expand Up @@ -2298,7 +2301,7 @@ void Network::publishHASSConfigAdditionalOpenerEntities(char *deviceType, const
"",
{{(char*)"pl_on", (char*)"ring"},
{(char*)"pl_off", (char*)"standby"}});

JsonDocument json;
json = createHassJson(uidString, "_ring_event", "Ring", name, baseTopic, String("~") + mqtt_topic_lock_ring, deviceType, "doorbell", "", "", "", {{(char*)"val_tpl", (char*)"{ \"event_type\": \"{{ value }}\" }"}});
json["event_types"][0] = "ring";
Expand Down Expand Up @@ -3032,14 +3035,15 @@ void Network::publishHASSConfigKeypad(char *deviceType, const char *baseTopic, c
"Keypad battery low",
name,
baseTopic,
String("~") + mqtt_topic_battery_keypad_critical,
String("~") + mqtt_topic_battery_basic_json,
deviceType,
"battery",
"",
"diagnostic",
"",
{{(char*)"pl_on", (char*)"1"},
{(char*)"pl_off", (char*)"0"}});
{(char*)"pl_off", (char*)"0"},
{(char*)"val_tpl", (char*)"{{value_json.keypadCritical}}" }});

// Query Keypad
publishHassTopic("button",
Expand Down Expand Up @@ -3152,6 +3156,18 @@ void Network::removeHassTopic(const String& mqttDeviceType, const String& mqttDe
}
}

void Network::removeTopic(const String& mqttPath, const String& mqttTopic)
{
String path = mqttPath;
path.concat(mqttTopic);
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, "");

#ifdef DEBUG_NUKIHUB
Log->print(F("Removing MQTT topic: "));
Log->println(path.c_str());
#endif
}


void Network::removeHASSConfig(char* uidString)
{
Expand Down
1 change: 1 addition & 0 deletions src/Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Network
void publishHASSWifiRssiConfig(char* deviceType, const char* baseTopic, char* name, char* uidString);
void removeHASSConfig(char* uidString);
void removeHASSConfigTopic(char* deviceType, char* name, char* uidString);
void removeTopic(const String& mqttPath, const String& mqttTopic);
void batteryTypeToString(const Nuki::BatteryType battype, char* str);
void advertisingModeToString(const Nuki::AdvertisingMode advmode, char* str);
void timeZoneIdToString(const Nuki::TimeZoneId timeZoneId, char* str);
Expand Down
Loading