Skip to content

Commit

Permalink
remove/recreate HA config for dallas/analog on change #888
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDvP committed Jan 11, 2023
1 parent 1f0d2b1 commit 3e3a600
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/analogsensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ void AnalogSensor::reload() {
#if defined(EMSESP_STANDALONE)
analog_enabled_ = true; // for local offline testing
#endif
for (auto sensor : sensors_) {
remove_ha_topic(sensor.gpio());
sensor.ha_registered = false;
}
if (!analog_enabled_) {
sensors_.clear();
return;
Expand Down Expand Up @@ -607,11 +611,15 @@ std::string AnalogSensor::Sensor::name() const {
return name_;
}

// set the counter value, id is gpio-no
// set the dig_out/counter/DAC/PWM value, id is gpio-no
bool AnalogSensor::command_setvalue(const char * value, const int8_t gpio) {
float val;
if (!Helpers::value2float(value, val)) {
return false;
bool b;
if (!Helpers::value2bool(value, b)) {
return false;
}
val = b ? 1 : 0;
}
for (auto & sensor : sensors_) {
if (sensor.gpio() == gpio) {
Expand Down
6 changes: 5 additions & 1 deletion src/dallassensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ void DallasSensor::reload() {
});

for (auto & sensor : sensors_) {
remove_ha_topic(sensor.id());
sensor.ha_registered = false; // force HA configs to be re-created
}
}
Expand Down Expand Up @@ -498,7 +499,10 @@ void DallasSensor::publish_values(const bool force) {
// create the HA MQTT config
// to e.g. homeassistant/sensor/ems-esp/dallassensor_28-233D-9497-0C03/config
if (Mqtt::ha_enabled()) {
if (!sensor.ha_registered || force) {
if (!has_value && sensor.ha_registered) {
remove_ha_topic(sensor.id());
sensor.ha_registered = false;
} else if (!sensor.ha_registered || force) {
LOG_DEBUG("Recreating HA config for sensor ID %s", sensor.id().c_str());

StaticJsonDocument<EMSESP_JSON_SIZE_MEDIUM> config;
Expand Down

0 comments on commit 3e3a600

Please sign in to comment.