Merge pull request #2963 from ezcGman/um-sht
SHT Usermod: Fixed MQTT discovery using correct unit; Added getters and isEnabled() check
This commit is contained in:
commit
859d21162c
@ -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
|
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
|
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
|
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 shtCurrentTempC = 0.0f; // Last read temperature in Celsius
|
||||||
float shtCurrentTempF = 0; // Last read temperature in Fahrenheit
|
float shtCurrentHumidity = 0.0f; // Last read humidity in RH%
|
||||||
float shtCurrentHumidity = 0; // Last read humidity in RH%
|
|
||||||
|
|
||||||
|
|
||||||
void initShtTempHumiditySensor();
|
void initShtTempHumiditySensor();
|
||||||
@ -56,9 +55,13 @@ class ShtUsermod : public Usermod
|
|||||||
bool readFromConfig(JsonObject &root);
|
bool readFromConfig(JsonObject &root);
|
||||||
void addToJsonInfo(JsonObject& root);
|
void addToJsonInfo(JsonObject& root);
|
||||||
|
|
||||||
float getTemperatureC();
|
bool isEnabled() { return enabled; }
|
||||||
float getTemperatureF();
|
|
||||||
float getHumidity();
|
float getTemperature();
|
||||||
|
float getTemperatureC() { return shtCurrentTempC; }
|
||||||
|
float getTemperatureF() { return (shtCurrentTempC * 1.8f) + 32.0f; }
|
||||||
|
float getHumidity() { return shtCurrentHumidity; }
|
||||||
|
const char* getUnitString();
|
||||||
|
|
||||||
uint16_t getId() { return USERMOD_ID_SHT; }
|
uint16_t getId() { return USERMOD_ID_SHT; }
|
||||||
};
|
};
|
||||||
@ -90,7 +93,6 @@ void ShtUsermod::initShtTempHumiditySensor()
|
|||||||
shtTempHumidSensor->begin(shtI2cAddress, i2c_sda, i2c_scl);
|
shtTempHumidSensor->begin(shtI2cAddress, i2c_sda, i2c_scl);
|
||||||
if (shtTempHumidSensor->readStatus() == 0xFFFF) {
|
if (shtTempHumidSensor->readStatus() == 0xFFFF) {
|
||||||
DEBUG_PRINTF("[%s] SHT init failed!\n", _name);
|
DEBUG_PRINTF("[%s] SHT init failed!\n", _name);
|
||||||
cleanupShtTempHumiditySensor();
|
|
||||||
cleanup();
|
cleanup();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -107,12 +109,8 @@ void ShtUsermod::initShtTempHumiditySensor()
|
|||||||
*/
|
*/
|
||||||
void ShtUsermod::cleanupShtTempHumiditySensor()
|
void ShtUsermod::cleanupShtTempHumiditySensor()
|
||||||
{
|
{
|
||||||
if (isShtReady()) {
|
if (isShtReady()) shtTempHumidSensor->reset();
|
||||||
shtTempHumidSensor->reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
delete shtTempHumidSensor;
|
delete shtTempHumidSensor;
|
||||||
|
|
||||||
shtInitDone = false;
|
shtInitDone = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,9 +124,7 @@ void ShtUsermod::cleanupShtTempHumiditySensor()
|
|||||||
*/
|
*/
|
||||||
void ShtUsermod::cleanup()
|
void ShtUsermod::cleanup()
|
||||||
{
|
{
|
||||||
if (isShtReady()) {
|
|
||||||
cleanupShtTempHumiditySensor();
|
cleanupShtTempHumiditySensor();
|
||||||
}
|
|
||||||
|
|
||||||
if (pinAllocDone) {
|
if (pinAllocDone) {
|
||||||
PinManagerPinType pins[2] = { { i2c_sda, true }, { i2c_scl, true } };
|
PinManagerPinType pins[2] = { { i2c_sda, true }, { i2c_scl, true } };
|
||||||
@ -162,9 +158,9 @@ void ShtUsermod::publishTemperatureAndHumidityViaMqtt() {
|
|||||||
char buf[128];
|
char buf[128];
|
||||||
|
|
||||||
snprintf_P(buf, 127, PSTR("%s/temperature"), mqttDeviceTopic);
|
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);
|
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());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -191,7 +187,7 @@ void ShtUsermod::publishHomeAssistantAutodiscovery() {
|
|||||||
json[F("stat_cla")] = F("measurement");
|
json[F("stat_cla")] = F("measurement");
|
||||||
snprintf_P(buf, 127, PSTR("%s-temperature"), escapedMac.c_str());
|
snprintf_P(buf, 127, PSTR("%s-temperature"), escapedMac.c_str());
|
||||||
json[F("uniq_id")] = buf;
|
json[F("uniq_id")] = buf;
|
||||||
json[F("unit_of_meas")] = F("°C");
|
json[F("unit_of_meas")] = unitOfTemp ? F("°F") : F("°C");
|
||||||
appendDeviceToMqttDiscoveryMessage(json);
|
appendDeviceToMqttDiscoveryMessage(json);
|
||||||
payload_size = serializeJson(json, json_str);
|
payload_size = serializeJson(json, json_str);
|
||||||
snprintf_P(buf, 127, PSTR("homeassistant/sensor/%s/%s-temperature/config"), escapedMac.c_str(), escapedMac.c_str());
|
snprintf_P(buf, 127, PSTR("homeassistant/sensor/%s/%s-temperature/config"), escapedMac.c_str(), escapedMac.c_str());
|
||||||
@ -222,7 +218,7 @@ void ShtUsermod::publishHomeAssistantAutodiscovery() {
|
|||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
void ShtUsermod::appendDeviceToMqttDiscoveryMessage(JsonDocument& root) {
|
void ShtUsermod::appendDeviceToMqttDiscoveryMessage(JsonDocument& root) {
|
||||||
JsonObject device = root.createNestedObject("dev");
|
JsonObject device = root.createNestedObject(F("dev"));
|
||||||
device[F("ids")] = escapedMac.c_str();
|
device[F("ids")] = escapedMac.c_str();
|
||||||
device[F("name")] = serverDescription;
|
device[F("name")] = serverDescription;
|
||||||
device[F("sw")] = versionString;
|
device[F("sw")] = versionString;
|
||||||
@ -290,13 +286,11 @@ void ShtUsermod::loop()
|
|||||||
if (shtTempHumidSensor->dataReady()) {
|
if (shtTempHumidSensor->dataReady()) {
|
||||||
if (shtTempHumidSensor->readData(false)) {
|
if (shtTempHumidSensor->readData(false)) {
|
||||||
shtCurrentTempC = shtTempHumidSensor->getTemperature();
|
shtCurrentTempC = shtTempHumidSensor->getTemperature();
|
||||||
shtCurrentTempF = shtTempHumidSensor->getFahrenheit();
|
|
||||||
shtCurrentHumidity = shtTempHumidSensor->getHumidity();
|
shtCurrentHumidity = shtTempHumidSensor->getHumidity();
|
||||||
|
|
||||||
publishTemperatureAndHumidityViaMqtt();
|
publishTemperatureAndHumidityViaMqtt();
|
||||||
shtReadDataSuccess = true;
|
shtReadDataSuccess = true;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
shtReadDataSuccess = false;
|
shtReadDataSuccess = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -387,6 +381,7 @@ bool ShtUsermod::readFromConfig(JsonObject &root)
|
|||||||
|
|
||||||
bool oldEnabled = enabled;
|
bool oldEnabled = enabled;
|
||||||
byte oldShtType = shtType;
|
byte oldShtType = shtType;
|
||||||
|
byte oldUnitOfTemp = unitOfTemp;
|
||||||
bool oldHaMqttDiscovery = haMqttDiscovery;
|
bool oldHaMqttDiscovery = haMqttDiscovery;
|
||||||
|
|
||||||
getJsonValue(top[FPSTR(_enabled)], enabled);
|
getJsonValue(top[FPSTR(_enabled)], enabled);
|
||||||
@ -410,6 +405,11 @@ bool ShtUsermod::readFromConfig(JsonObject &root)
|
|||||||
initShtTempHumiditySensor();
|
initShtTempHumiditySensor();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (oldUnitOfTemp != unitOfTemp) {
|
||||||
|
publishTemperatureAndHumidityViaMqtt();
|
||||||
|
publishHomeAssistantAutodiscovery();
|
||||||
|
}
|
||||||
|
|
||||||
if (oldHaMqttDiscovery != haMqttDiscovery && haMqttDiscovery) {
|
if (oldHaMqttDiscovery != haMqttDiscovery && haMqttDiscovery) {
|
||||||
publishHomeAssistantAutodiscovery();
|
publishHomeAssistantAutodiscovery();
|
||||||
}
|
}
|
||||||
@ -448,45 +448,34 @@ void ShtUsermod::addToJsonInfo(JsonObject& root)
|
|||||||
if (shtLastTimeUpdated == 0) {
|
if (shtLastTimeUpdated == 0) {
|
||||||
jsonTemp.add(F(" Not read yet"));
|
jsonTemp.add(F(" Not read yet"));
|
||||||
jsonHumidity.add(F(" Not read yet"));
|
jsonHumidity.add(F(" Not read yet"));
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
jsonTemp.add(F(" Error"));
|
jsonTemp.add(F(" Error"));
|
||||||
jsonHumidity.add(F(" Error"));
|
jsonHumidity.add(F(" Error"));
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonHumidity.add(shtCurrentHumidity);
|
jsonHumidity.add(getHumidity());
|
||||||
jsonHumidity.add(F(" RH"));
|
jsonHumidity.add(F(" RH"));
|
||||||
|
|
||||||
unitOfTemp ? jsonTemp.add(getTemperatureF()) : jsonTemp.add(getTemperatureC());
|
jsonTemp.add(getTemperature());
|
||||||
unitOfTemp ? jsonTemp.add(F(" °F")) : jsonTemp.add(F(" °C"));
|
jsonTemp.add(unitOfTemp ? "°F" : "°C");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Getter for last read temperature in Celsius.
|
* Getter for last read temperature for configured unit.
|
||||||
*
|
*
|
||||||
* @return float
|
* @return float
|
||||||
*/
|
*/
|
||||||
float ShtUsermod::getTemperatureC() {
|
float ShtUsermod::getTemperature() {
|
||||||
return shtCurrentTempC;
|
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() {
|
const char* ShtUsermod::getUnitString() {
|
||||||
return shtCurrentTempF;
|
return unitOfTemp ? "°F" : "°C";
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Getter for last read humidity in RH%.
|
|
||||||
*
|
|
||||||
* @return float
|
|
||||||
*/
|
|
||||||
float ShtUsermod::getHumidity() {
|
|
||||||
return shtCurrentHumidity;
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user