Skip to content

Commit

Permalink
add more home assistant battery sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
technyon committed Aug 9, 2022
1 parent bdb377a commit c56a019
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
64 changes: 64 additions & 0 deletions Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n

_device->mqttClient()->publish(path.c_str(), configJSON.c_str(), true);

// Battery critical
configJSON = "{\"dev\":{\"ids\":[\"nuki_";
configJSON.concat(uidString);
configJSON.concat("\"],\"mf\":\"Nuki\",\"mdl\":\"");
Expand All @@ -405,9 +406,72 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n
path.concat("/battery_low/config");

_device->mqttClient()->publish(path.c_str(), configJSON.c_str(), true);

// Battery voltage
configJSON = "{\"dev\":{\"ids\":[\"nuki_";
configJSON.concat(uidString);
configJSON.concat("\"],\"mf\":\"Nuki\",\"mdl\":\"");
configJSON.concat(deviceType);
configJSON.concat("\",\"name\":\"");
configJSON.concat(name);
configJSON.concat("\"},\"~\":\"");
configJSON.concat(baseTopic);
configJSON.concat("\",\"name\":\"");
configJSON.concat(name);
configJSON.concat(" battery voltage\",\"unique_id\":\"");
configJSON.concat(uidString);
configJSON.concat(
"_battery_voltage\",\"dev_cla\":\"voltage\",\"ent_cat\":\"diagnostic\",\"stat_t\":\"~");
configJSON.concat(mqtt_topic_battery_voltage);
configJSON.concat("\",\"state_cla\":\"measurement\",\"unit_of_meas\":\"V\"");
configJSON.concat("}");

path = discoveryTopic;
path.concat("/sensor/");
path.concat(uidString);
path.concat("/battery_voltage/config");

_device->mqttClient()->publish(path.c_str(), configJSON.c_str(), true);
}
}

void Network::publishHASSConfigBatLevel(char *deviceType, const char *baseTopic, char *name, char *uidString,
char *lockAction, char *unlockAction, char *openAction, char *lockedState,
char *unlockedState)
{
String discoveryTopic = _preferences->getString(preference_mqtt_hass_discovery);

if (discoveryTopic != "")
{
// Battery level
String configJSON = "{\"dev\":{\"ids\":[\"nuki_";
configJSON.concat(uidString);
configJSON.concat("\"],\"mf\":\"Nuki\",\"mdl\":\"");
configJSON.concat(deviceType);
configJSON.concat("\",\"name\":\"");
configJSON.concat(name);
configJSON.concat("\"},\"~\":\"");
configJSON.concat(baseTopic);
configJSON.concat("\",\"name\":\"");
configJSON.concat(name);
configJSON.concat(" battery level\",\"unique_id\":\"");
configJSON.concat(uidString);
configJSON.concat(
"_battery_level\",\"dev_cla\":\"battery\",\"ent_cat\":\"diagnostic\",\"stat_t\":\"~");
configJSON.concat(mqtt_topic_battery_level);
configJSON.concat("\",\"state_cla\":\"measurement\",\"unit_of_meas\":\"%\"");
configJSON.concat("}");

String path = discoveryTopic;
path.concat("/sensor/");
path.concat(uidString);
path.concat("/battery_level/config");

_device->mqttClient()->publish(path.c_str(), configJSON.c_str(), true);
}
}


void Network::removeHASSConfig(char* uidString)
{
String discoveryTopic = _preferences->getString(preference_mqtt_hass_discovery);
Expand Down
1 change: 1 addition & 0 deletions Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Network
bool publishString(const char* prefix, const char* topic, const char* value);

void publishHASSConfig(char* deviceType, const char* baseTopic, char* name, char* uidString, char* lockAction, char* unlockAction, char* openAction, char* lockedState, char* unlockedState);
void publishHASSConfigBatLevel(char* deviceType, const char* baseTopic, char* name, char* uidString, char* lockAction, char* unlockAction, char* openAction, char* lockedState, char* unlockedState);
void removeHASSConfig(char* uidString);

void publishPresenceDetection(char* csv);
Expand Down
1 change: 1 addition & 0 deletions NetworkLock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ NetworkLock::publishHASSConfig(char *deviceType, const char *baseTopic, char *na
char *unlockAction, char *openAction, char *lockedState, char *unlockedState)
{
_network->publishHASSConfig(deviceType, baseTopic, name, uidString, lockAction, unlockAction, openAction, lockedState, unlockedState);
_network->publishHASSConfigBatLevel(deviceType, baseTopic, name, uidString, lockAction, unlockAction, openAction, lockedState, unlockedState);
}

void NetworkLock::removeHASSConfig(char *uidString)
Expand Down
2 changes: 1 addition & 1 deletion Version.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#pragma once

#define nuki_hub_version "5.6"
#define nuki_hub_version "5.7"
Binary file modified webflash/nuki_hub.bin
Binary file not shown.

0 comments on commit c56a019

Please sign in to comment.