From ea6d339b9ce494963062c3a69ce3034335e29427 Mon Sep 17 00:00:00 2001 From: ezcGman Date: Mon, 19 Dec 2022 22:15:39 +0100 Subject: [PATCH 1/7] UM SHT: Fixed MQTT discovery using correct unit --- usermods/sht/usermod_sht.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usermods/sht/usermod_sht.h b/usermods/sht/usermod_sht.h index 42b22a79..88daeb43 100644 --- a/usermods/sht/usermod_sht.h +++ b/usermods/sht/usermod_sht.h @@ -191,7 +191,7 @@ void ShtUsermod::publishHomeAssistantAutodiscovery() { json[F("stat_cla")] = F("measurement"); snprintf_P(buf, 127, PSTR("%s-temperature"), escapedMac.c_str()); json[F("uniq_id")] = buf; - json[F("unit_of_meas")] = F("°C"); + json[F("unit_of_meas")] = F((unitOfTemp ? "°F" : "°C")); appendDeviceToMqttDiscoveryMessage(json); payload_size = serializeJson(json, json_str); snprintf_P(buf, 127, PSTR("homeassistant/sensor/%s/%s-temperature/config"), escapedMac.c_str(), escapedMac.c_str()); From f3d52f493258a66dafeabc603ddba194fe0a353a Mon Sep 17 00:00:00 2001 From: ezcGman Date: Mon, 19 Dec 2022 22:30:11 +0100 Subject: [PATCH 2/7] UM SHT: MQTT re-publish values on unit change --- usermods/sht/usermod_sht.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/usermods/sht/usermod_sht.h b/usermods/sht/usermod_sht.h index 88daeb43..652bdfde 100644 --- a/usermods/sht/usermod_sht.h +++ b/usermods/sht/usermod_sht.h @@ -387,6 +387,7 @@ bool ShtUsermod::readFromConfig(JsonObject &root) bool oldEnabled = enabled; byte oldShtType = shtType; + byte oldUnitOfTemp = unitOfTemp; bool oldHaMqttDiscovery = haMqttDiscovery; getJsonValue(top[FPSTR(_enabled)], enabled); @@ -410,6 +411,11 @@ bool ShtUsermod::readFromConfig(JsonObject &root) initShtTempHumiditySensor(); } + if (oldUnitOfTemp != unitOfTemp) { + publishTemperatureAndHumidityViaMqtt(); + publishHomeAssistantAutodiscovery(); + } + if (oldHaMqttDiscovery != haMqttDiscovery && haMqttDiscovery) { publishHomeAssistantAutodiscovery(); } From 5cfea54b06828b936f4fce9964a6d4f4e23ffebf Mon Sep 17 00:00:00 2001 From: ezcGman Date: Tue, 20 Dec 2022 23:58:11 +0100 Subject: [PATCH 3/7] UM SHT: Apply PR feedback --- usermods/sht/usermod_sht.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usermods/sht/usermod_sht.h b/usermods/sht/usermod_sht.h index 652bdfde..9a3a2f4d 100644 --- a/usermods/sht/usermod_sht.h +++ b/usermods/sht/usermod_sht.h @@ -191,7 +191,7 @@ void ShtUsermod::publishHomeAssistantAutodiscovery() { json[F("stat_cla")] = F("measurement"); snprintf_P(buf, 127, PSTR("%s-temperature"), escapedMac.c_str()); json[F("uniq_id")] = buf; - json[F("unit_of_meas")] = F((unitOfTemp ? "°F" : "°C")); + json[F("unit_of_meas")] = unitOfTemp ? F("°F") : F("°C"); appendDeviceToMqttDiscoveryMessage(json); payload_size = serializeJson(json, json_str); snprintf_P(buf, 127, PSTR("homeassistant/sensor/%s/%s-temperature/config"), escapedMac.c_str(), escapedMac.c_str()); From f12025b86edcb09771593a15a96fd48a5057a3a8 Mon Sep 17 00:00:00 2001 From: ezcGman Date: Wed, 21 Dec 2022 00:05:26 +0100 Subject: [PATCH 4/7] UM SHT: Added getters and isEnabled check --- usermods/sht/usermod_sht.h | 41 +++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/usermods/sht/usermod_sht.h b/usermods/sht/usermod_sht.h index 9a3a2f4d..a5b0e97a 100644 --- a/usermods/sht/usermod_sht.h +++ b/usermods/sht/usermod_sht.h @@ -56,9 +56,13 @@ class ShtUsermod : public Usermod bool readFromConfig(JsonObject &root); void addToJsonInfo(JsonObject& root); - float getTemperatureC(); - float getTemperatureF(); - float getHumidity(); + bool isEnabled() { return enabled; } + + float getTemperature(); + float getTemperatureC() { return shtCurrentTempC; } + float getTemperatureF() { return shtCurrentTempF; } + float getHumidity() { return shtCurrentHumidity; } + const char* getUnitString(); uint16_t getId() { return USERMOD_ID_SHT; } }; @@ -162,9 +166,9 @@ void ShtUsermod::publishTemperatureAndHumidityViaMqtt() { char buf[128]; snprintf_P(buf, 127, PSTR("%s/temperature"), mqttDeviceTopic); - mqtt->publish(buf, 0, false, String((unitOfTemp ? getTemperatureF() : getTemperatureC())).c_str()); + mqtt->publish(buf, 0, false, String(getTemperature()).c_str()); snprintf_P(buf, 127, PSTR("%s/humidity"), mqttDeviceTopic); - mqtt->publish(buf, 0, false, String(shtCurrentHumidity).c_str()); + mqtt->publish(buf, 0, false, String(getHumidity()).c_str()); } /** @@ -463,36 +467,27 @@ void ShtUsermod::addToJsonInfo(JsonObject& root) return; } - jsonHumidity.add(shtCurrentHumidity); + jsonHumidity.add(getHumidity()); jsonHumidity.add(F(" RH")); - unitOfTemp ? jsonTemp.add(getTemperatureF()) : jsonTemp.add(getTemperatureC()); + jsonTemp.add(getTemperature()); unitOfTemp ? jsonTemp.add(F(" °F")) : jsonTemp.add(F(" °C")); } /** - * Getter for last read temperature in Celsius. + * Getter for last read temperature for configured unit. * * @return float */ -float ShtUsermod::getTemperatureC() { - return shtCurrentTempC; +float ShtUsermod::getTemperature() { + return unitOfTemp ? getTemperatureF() : getTemperatureC(); } /** - * Getter for last read temperature in Fahrenheit. + * Returns the current configured unit as human readable string. * - * @return float + * @return const char* */ -float ShtUsermod::getTemperatureF() { - return shtCurrentTempF; -} - -/** - * Getter for last read humidity in RH%. - * - * @return float - */ -float ShtUsermod::getHumidity() { - return shtCurrentHumidity; +const char* ShtUsermod::getUnitString() { + return unitOfTemp ? "°F" : "°C"; } \ No newline at end of file From 4ecad6592696a44be7ad7641f645a98a250aabd7 Mon Sep 17 00:00:00 2001 From: ezcGman Date: Wed, 21 Dec 2022 00:34:22 +0100 Subject: [PATCH 5/7] UM SHT: Codestyle --- usermods/sht/usermod_sht.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usermods/sht/usermod_sht.h b/usermods/sht/usermod_sht.h index a5b0e97a..2244e3ad 100644 --- a/usermods/sht/usermod_sht.h +++ b/usermods/sht/usermod_sht.h @@ -471,7 +471,7 @@ void ShtUsermod::addToJsonInfo(JsonObject& root) jsonHumidity.add(F(" RH")); jsonTemp.add(getTemperature()); - unitOfTemp ? jsonTemp.add(F(" °F")) : jsonTemp.add(F(" °C")); + jsonTemp.add(unitOfTemp ? F(" °F") : F(" °C")); } /** From 7e48875fd458f1c447f8fd016df5f32d32bb254b Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Sun, 25 Dec 2022 11:02:50 +0100 Subject: [PATCH 6/7] Minor optimisation. --- usermods/sht/usermod_sht.h | 38 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/usermods/sht/usermod_sht.h b/usermods/sht/usermod_sht.h index 2244e3ad..c9dec6ba 100644 --- a/usermods/sht/usermod_sht.h +++ b/usermods/sht/usermod_sht.h @@ -26,9 +26,8 @@ class ShtUsermod : public Usermod const byte shtI2cAddress = 0x44; // i2c address of the sensor. 0x44 is the default for all SHT sensors. Change this, if needed unsigned long shtLastTimeUpdated = 0; // Remembers when we read data the last time bool shtDataRequested = false; // Reading data is done async. This remembers if we asked the sensor to read data - float shtCurrentTempC = 0; // Last read temperature in Celsius - float shtCurrentTempF = 0; // Last read temperature in Fahrenheit - float shtCurrentHumidity = 0; // Last read humidity in RH% + float shtCurrentTempC = 0.0f; // Last read temperature in Celsius + float shtCurrentHumidity = 0.0f; // Last read humidity in RH% void initShtTempHumiditySensor(); @@ -60,7 +59,7 @@ class ShtUsermod : public Usermod float getTemperature(); float getTemperatureC() { return shtCurrentTempC; } - float getTemperatureF() { return shtCurrentTempF; } + float getTemperatureF() { return (shtCurrentTempC * 1.8f) + 32.0f; } float getHumidity() { return shtCurrentHumidity; } const char* getUnitString(); @@ -68,10 +67,10 @@ class ShtUsermod : public Usermod }; // Strings to reduce flash memory usage (used more than twice) -const char ShtUsermod::_name[] PROGMEM = "SHT-Sensor"; -const char ShtUsermod::_enabled[] PROGMEM = "Enabled"; -const char ShtUsermod::_shtType[] PROGMEM = "SHT-Type"; -const char ShtUsermod::_unitOfTemp[] PROGMEM = "Unit"; +const char ShtUsermod::_name[] PROGMEM = "SHT-Sensor"; +const char ShtUsermod::_enabled[] PROGMEM = "Enabled"; +const char ShtUsermod::_shtType[] PROGMEM = "SHT-Type"; +const char ShtUsermod::_unitOfTemp[] PROGMEM = "Unit"; const char ShtUsermod::_haMqttDiscovery[] PROGMEM = "Add-To-HA-MQTT-Discovery"; /** @@ -94,7 +93,6 @@ void ShtUsermod::initShtTempHumiditySensor() shtTempHumidSensor->begin(shtI2cAddress, i2c_sda, i2c_scl); if (shtTempHumidSensor->readStatus() == 0xFFFF) { DEBUG_PRINTF("[%s] SHT init failed!\n", _name); - cleanupShtTempHumiditySensor(); cleanup(); return; } @@ -111,12 +109,8 @@ void ShtUsermod::initShtTempHumiditySensor() */ void ShtUsermod::cleanupShtTempHumiditySensor() { - if (isShtReady()) { - shtTempHumidSensor->reset(); - } - + if (isShtReady()) shtTempHumidSensor->reset(); delete shtTempHumidSensor; - shtInitDone = false; } @@ -130,9 +124,7 @@ void ShtUsermod::cleanupShtTempHumiditySensor() */ void ShtUsermod::cleanup() { - if (isShtReady()) { - cleanupShtTempHumiditySensor(); - } + cleanupShtTempHumiditySensor(); if (pinAllocDone) { PinManagerPinType pins[2] = { { i2c_sda, true }, { i2c_scl, true } }; @@ -226,7 +218,7 @@ void ShtUsermod::publishHomeAssistantAutodiscovery() { * @return void */ void ShtUsermod::appendDeviceToMqttDiscoveryMessage(JsonDocument& root) { - JsonObject device = root.createNestedObject("dev"); + JsonObject device = root.createNestedObject(F("dev")); device[F("ids")] = escapedMac.c_str(); device[F("name")] = serverDescription; device[F("sw")] = versionString; @@ -294,13 +286,11 @@ void ShtUsermod::loop() if (shtTempHumidSensor->dataReady()) { if (shtTempHumidSensor->readData(false)) { shtCurrentTempC = shtTempHumidSensor->getTemperature(); - shtCurrentTempF = shtTempHumidSensor->getFahrenheit(); shtCurrentHumidity = shtTempHumidSensor->getHumidity(); publishTemperatureAndHumidityViaMqtt(); shtReadDataSuccess = true; - } - else { + } else { shtReadDataSuccess = false; } @@ -458,12 +448,10 @@ void ShtUsermod::addToJsonInfo(JsonObject& root) if (shtLastTimeUpdated == 0) { jsonTemp.add(F(" Not read yet")); jsonHumidity.add(F(" Not read yet")); - } - else { + } else { jsonTemp.add(F(" Error")); jsonHumidity.add(F(" Error")); } - return; } @@ -471,7 +459,7 @@ void ShtUsermod::addToJsonInfo(JsonObject& root) jsonHumidity.add(F(" RH")); jsonTemp.add(getTemperature()); - jsonTemp.add(unitOfTemp ? F(" °F") : F(" °C")); + jsonTemp.add(unitOfTemp ? "°F") : "°C"); } /** From c739a7ea2f810e66397961c614f2d4ab346e8c2b Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Sun, 25 Dec 2022 11:05:25 +0100 Subject: [PATCH 7/7] Erroneous ) --- usermods/sht/usermod_sht.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usermods/sht/usermod_sht.h b/usermods/sht/usermod_sht.h index c9dec6ba..721cb7f0 100644 --- a/usermods/sht/usermod_sht.h +++ b/usermods/sht/usermod_sht.h @@ -459,7 +459,7 @@ void ShtUsermod::addToJsonInfo(JsonObject& root) jsonHumidity.add(F(" RH")); jsonTemp.add(getTemperature()); - jsonTemp.add(unitOfTemp ? "°F") : "°C"); + jsonTemp.add(unitOfTemp ? "°F" : "°C"); } /**