Skip to content
Open
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: 2 additions & 1 deletion localazy/source/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@
"discovery": "Discovery topic prefix",
"hostname": "Hostname for URL",
"tag": "Name tag",
"title": "Home-Assistant"
"title": "Home-Assistant",
"nodeid": "Nest sensors under a node level"
},
"id": "Client ID",
"interval": "Interval",
Expand Down
24 changes: 24 additions & 0 deletions src/AmsConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ bool AmsConfiguration::setHomeAssistantConfig(HomeAssistantConfig& config) {
mqttChanged |= strcmp(config.discoveryPrefix, existing.discoveryPrefix) != 0;
mqttChanged |= strcmp(config.discoveryHostname, existing.discoveryHostname) != 0;
mqttChanged |= strcmp(config.discoveryNameTag, existing.discoveryNameTag) != 0;
mqttChanged |= config.nodeId != existing.nodeId;
} else {
mqttChanged = true;
}
Expand All @@ -489,6 +490,7 @@ void AmsConfiguration::clearHomeAssistantConfig(HomeAssistantConfig& config) {
memset(config.discoveryPrefix, 0, 64);
memset(config.discoveryHostname, 0, 64);
memset(config.discoveryNameTag, 0, 16);
config.nodeId = false;
}

bool AmsConfiguration::pinUsed(uint8_t pin, GpioConfig& config) {
Expand Down Expand Up @@ -1091,6 +1093,14 @@ bool AmsConfiguration::hasConfig() {
configVersion = 0;
return false;
}
case 104:
configVersion = -1; // Prevent loop
if(relocateConfig104()) {
configVersion = 105;
} else {
configVersion = 0;
return false;
}
case EEPROM_CHECK_SUM:
return true;
default:
Expand Down Expand Up @@ -1209,6 +1219,20 @@ bool AmsConfiguration::relocateConfig103() {
return ret;
}

bool AmsConfiguration::relocateConfig104() {
// HomeAssistantConfig gained a trailing nodeId byte in previously-unused
// space; no offsets moved, so just default the new flag and re-stamp.
EEPROM.begin(EEPROM_SIZE);
HomeAssistantConfig ha;
EEPROM.get(CONFIG_HA_START, ha);
ha.nodeId = false; // keep flat topic for existing installs
EEPROM.put(CONFIG_HA_START, ha);
EEPROM.put(EEPROM_CONFIG_ADDRESS, 105);
bool ret = EEPROM.commit();
EEPROM.end();
return ret;
}

bool AmsConfiguration::save() {
EEPROM.begin(EEPROM_SIZE);
uint8_t configVersion = EEPROM.read(EEPROM_CONFIG_ADDRESS);
Expand Down
6 changes: 4 additions & 2 deletions src/AmsConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "Arduino.h"

#define EEPROM_SIZE 1024*3
#define EEPROM_CHECK_SUM 104 // Used to check if config is stored. Change if structure changes
#define EEPROM_CHECK_SUM 105 // Used to check if config is stored. Change if structure changes
#define EEPROM_CLEARED_INDICATOR 0xFC
#define EEPROM_CONFIG_ADDRESS 0

Expand Down Expand Up @@ -212,7 +212,8 @@ struct HomeAssistantConfig {
char discoveryPrefix[64];
char discoveryHostname[64];
char discoveryNameTag[16];
}; // 145
bool nodeId; // Nest sensors under a node level: <prefix>/sensor/<deviceUid>/<uid>/config
}; // 146

struct NtpConfig {
bool enable;
Expand Down Expand Up @@ -394,6 +395,7 @@ class AmsConfiguration {
bool sysChanged = false, networkChanged = false, mqttChanged = false, webChanged = false, meterChanged = true, ntpChanged = true, priceChanged = false, energyAccountingChanged = true, cloudChanged = true, uiLanguageChanged = false, zcChanged = true;

bool relocateConfig103(); // 2.2.12, until, but not including 2.3
bool relocateConfig104(); // adds HomeAssistantConfig.nodeId

void saveToFs();
bool loadFromFs(uint8_t version);
Expand Down
5 changes: 4 additions & 1 deletion src/AmsWebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1315,7 +1315,8 @@ void AmsWebServer::configurationJson() {
snprintf_P(buf, BufferSize, CONF_HA_JSON,
haconf.discoveryPrefix,
haconf.discoveryHostname,
haconf.discoveryNameTag
haconf.discoveryNameTag,
haconf.nodeId ? "true" : "false"
);
server.sendContent(buf);
snprintf_P(buf, BufferSize, CONF_CLOUD_JSON,
Expand Down Expand Up @@ -1698,6 +1699,7 @@ void AmsWebServer::handleSave() {
strcpy(haconf.discoveryPrefix, server.arg(F("ht")).c_str());
strcpy(haconf.discoveryHostname, server.arg(F("hh")).c_str());
strcpy(haconf.discoveryNameTag, server.arg(F("hn")).c_str());
haconf.nodeId = server.arg(F("hi")) == F("true");
config->setHomeAssistantConfig(haconf);
}

Expand Down Expand Up @@ -2548,6 +2550,7 @@ void AmsWebServer::configFileDownload() {
server.sendContent(buf, snprintf_P(buf, BufferSize, PSTR("homeAssistantDiscoveryPrefix %s\n"), haconf.discoveryPrefix));
server.sendContent(buf, snprintf_P(buf, BufferSize, PSTR("homeAssistantDiscoveryHostname %s\n"), haconf.discoveryHostname));
server.sendContent(buf, snprintf_P(buf, BufferSize, PSTR("homeAssistantDiscoveryNameTag %s\n"), haconf.discoveryNameTag));
server.sendContent(buf, snprintf_P(buf, BufferSize, PSTR("homeAssistantDiscoveryNodeId %s\n"), haconf.nodeId ? "true" : "false"));
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/mqtt/HomeAssistantMqttHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ void HomeAssistantMqttHandler::setHomeAssistantConfig(HomeAssistantConfig config
manufacturer = boardManufacturerToString(boardType);

deviceUid = String(hostname); // Maybe configurable in the future?
// When nodeId is set, nest sensors under a node level:
// <prefix>/sensor/<deviceUid>/<uid>/config instead of
// <prefix>/sensor/<deviceUid>_<uid>/config
// uniq_id is unaffected (still <deviceUid>_<uid>), so HA entities are
// not duplicated. Note: toggling leaves the old retained config topics
// orphaned on the broker until manually cleared.
objectIdSep = config.nodeId ? "/" : "_";
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
Expand Down Expand Up @@ -527,7 +534,7 @@ void HomeAssistantMqttHandler::publishSensor(const HomeAssistantSensor sensor) {
strlen_P(sensor.uom) > 0 ? "\"" : ""
);

mqtt.publish(sensorTopic + "/" + deviceUid + "_" + uid + "/config", json, true, 0);
mqtt.publish(sensorTopic + "/" + deviceUid + objectIdSep + uid + "/config", json, true, 0);
loop();
}

Expand Down
1 change: 1 addition & 0 deletions src/mqtt/HomeAssistantMqttHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class HomeAssistantMqttHandler : public AmsMqttHandler {
String sensorTopic;
String updateTopic;
String sensorNamePrefix;
String objectIdSep; // "_" (flat) or "/" (nested under node) for sensor config topics

bool l1Init, l2Init, l2eInit, l3Init, l3eInit, l4Init, l4eInit, rtInit, rteInit, pInit, sInit, rInit, fInit, dInit;
bool tInit[32] = {false};
Expand Down
3 changes: 2 additions & 1 deletion src/webserver/json/conf_ha.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"h": {
"t" : "%s",
"h" : "%s",
"n" : "%s"
"n" : "%s",
"i" : %s
},
6 changes: 3 additions & 3 deletions ui/dist/index.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions ui/src/routes/ConfigurationRoute.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,9 @@
{translations.conf?.mqtt?.ha?.tag ?? "Name tag"}<br/>
<input name="hn" bind:value={configuration.h.n} type="text" class="in-s" pattern={asciiPattern}/>
</div>
<div class="my-1">
<label><input type="checkbox" name="hi" value="true" bind:checked={configuration.h.i} class="rounded mb-1"/> {translations.conf?.mqtt?.ha?.nodeid ?? "Nest sensors under a node level"}</label>
</div>
</div>
{/if}
{#if configuration?.c}
Expand Down
Loading