fix to support negative values for 1-byte datapoints

This commit is contained in:
j0ta29 2023-12-19 21:47:30 +00:00
parent 50821fd22b
commit 0e30e119a0
13 changed files with 84 additions and 21 deletions

View file

@ -5,7 +5,6 @@
#include "esphome/components/binary_sensor/binary_sensor.h" #include "esphome/components/binary_sensor/binary_sensor.h"
#include "../optolink.h" #include "../optolink.h"
#include "../datapoint_component.h" #include "../datapoint_component.h"
#include "VitoWiFi.h"
namespace esphome { namespace esphome {
namespace optolink { namespace optolink {

View file

@ -2,7 +2,6 @@
#include "optolink_number.h" #include "optolink_number.h"
#include "../optolink.h" #include "../optolink.h"
#include "VitoWiFi.h"
namespace esphome { namespace esphome {
namespace optolink { namespace optolink {
@ -20,6 +19,30 @@ void OptolinkNumber::control(float value) {
} }
}; };
void OptolinkNumber::datapoint_value_changed(uint8_t state) {
if (traits.get_min_value() >= 0) {
publish_state(state);
} else {
publish_state((sint8_t) state);
}
};
void OptolinkNumber::datapoint_value_changed(uint16_t state) {
if (traits.get_min_value() >= 0) {
publish_state(state);
} else {
publish_state((sint16_t) state);
}
};
void OptolinkNumber::datapoint_value_changed(uint32_t state) {
if (traits.get_min_value() >= 0) {
publish_state(state);
} else {
publish_state((sint32_t) state);
}
};
} // namespace optolink } // namespace optolink
} // namespace esphome } // namespace esphome

View file

@ -3,9 +3,8 @@
#ifdef USE_ARDUINO #ifdef USE_ARDUINO
#include "esphome/components/number/number.h" #include "esphome/components/number/number.h"
#include "../datapoint_component.h"
#include "../optolink.h" #include "../optolink.h"
#include "VitoWiFi.h" #include "../datapoint_component.h"
namespace esphome { namespace esphome {
namespace optolink { namespace optolink {
@ -21,9 +20,9 @@ class OptolinkNumber : public DatapointComponent, public esphome::number::Number
const StringRef &get_component_name() override { return get_name(); } const StringRef &get_component_name() override { return get_name(); }
void datapoint_value_changed(float state) override { publish_state(state); }; void datapoint_value_changed(float state) override { publish_state(state); };
void datapoint_value_changed(uint8_t state) override { publish_state(state); }; void datapoint_value_changed(uint8_t state) override;
void datapoint_value_changed(uint16_t state) override { publish_state(state); }; void datapoint_value_changed(uint16_t state) override;
void datapoint_value_changed(uint32_t state) override { publish_state(state); }; void datapoint_value_changed(uint32_t state) override;
}; };
} // namespace optolink } // namespace optolink

View file

@ -3,7 +3,6 @@
#include "esphome/core/defines.h" #include "esphome/core/defines.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include "optolink.h" #include "optolink.h"
#include "VitoWiFi.h"
#if defined(VITOWIFI_PROTOCOL) #if defined(VITOWIFI_PROTOCOL)
// NOLINTNEXTLINE // NOLINTNEXTLINE
@ -40,7 +39,7 @@ void Optolink::setup() {
} }
void Optolink::loop() { void Optolink::loop() {
ESP_LOGD(TAG, "queue size: %d", VitoWiFi.queueSize()); // ESP_LOGD(TAG, "queue size: %d", VitoWiFi.queueSize());
VitoWiFi.loop(); VitoWiFi.loop();
} }

View file

@ -2,7 +2,6 @@
#include "optolink_select.h" #include "optolink_select.h"
#include "../optolink.h" #include "../optolink.h"
#include "VitoWiFi.h"
namespace esphome { namespace esphome {
namespace optolink { namespace optolink {

View file

@ -2,11 +2,10 @@
#ifdef USE_ARDUINO #ifdef USE_ARDUINO
#include <map>
#include "esphome/components/select/select.h" #include "esphome/components/select/select.h"
#include "../optolink.h" #include "../optolink.h"
#include "../datapoint_component.h" #include "../datapoint_component.h"
#include "VitoWiFi.h" #include <map>
namespace esphome { namespace esphome {
namespace optolink { namespace optolink {

View file

@ -6,6 +6,7 @@ from esphome.const import (
CONF_BYTES, CONF_BYTES,
CONF_DIV_RATIO, CONF_DIV_RATIO,
CONF_ID, CONF_ID,
CONF_MIN_VALUE,
) )
from .. import CONF_OPTOLINK_ID, SENSOR_BASE_SCHEMA, optolink_ns from .. import CONF_OPTOLINK_ID, SENSOR_BASE_SCHEMA, optolink_ns
@ -22,6 +23,7 @@ CONFIG_SCHEMA = (
cv.GenerateID(): cv.declare_id(OptolinkSensor), cv.GenerateID(): cv.declare_id(OptolinkSensor),
cv.Required(CONF_ADDRESS): cv.hex_uint32_t, cv.Required(CONF_ADDRESS): cv.hex_uint32_t,
cv.Required(CONF_BYTES): cv.one_of(1, 2, 4, int=True), cv.Required(CONF_BYTES): cv.one_of(1, 2, 4, int=True),
cv.Optional(CONF_MIN_VALUE): cv.float_,
} }
) )
.extend(cv.COMPONENT_SCHEMA) .extend(cv.COMPONENT_SCHEMA)
@ -39,3 +41,5 @@ async def to_code(config):
cg.add(var.set_address(config[CONF_ADDRESS])) cg.add(var.set_address(config[CONF_ADDRESS]))
cg.add(var.set_bytes(config[CONF_BYTES])) cg.add(var.set_bytes(config[CONF_BYTES]))
cg.add(var.set_div_ratio(config[CONF_DIV_RATIO])) cg.add(var.set_div_ratio(config[CONF_DIV_RATIO]))
if CONF_MIN_VALUE in config:
cg.add(var.set_min_value(config[CONF_MIN_VALUE]))

View file

@ -0,0 +1,40 @@
#ifdef USE_ARDUINO
#include "optolink_sensor.h"
#include "../optolink.h"
namespace esphome {
namespace optolink {
static const char *const TAG = "optolink.sensor";
void OptolinkSensor::set_min_value(float min_value) { min_value_ = -29.3; }
void OptolinkSensor::datapoint_value_changed(uint8_t state) {
if (min_value_ >= 0.0) {
publish_state(state);
} else {
publish_state((int8_t) state);
}
};
void OptolinkSensor::datapoint_value_changed(uint16_t state) {
if (min_value_ >= 0.0) {
publish_state(state);
} else {
publish_state((int16_t) state);
}
}
void OptolinkSensor::datapoint_value_changed(uint32_t state) {
if (min_value_ >= 0.0) {
publish_state(state);
} else {
publish_state((int32_t) state);
}
};
} // namespace optolink
} // namespace esphome
#endif

View file

@ -5,7 +5,7 @@
#include "esphome/components/sensor/sensor.h" #include "esphome/components/sensor/sensor.h"
#include "../optolink.h" #include "../optolink.h"
#include "../datapoint_component.h" #include "../datapoint_component.h"
#include "VitoWiFi.h" #include <cfloat>
namespace esphome { namespace esphome {
namespace optolink { namespace optolink {
@ -16,15 +16,20 @@ class OptolinkSensor : public DatapointComponent, public esphome::sensor::Sensor
set_state_class(esphome::sensor::STATE_CLASS_MEASUREMENT); set_state_class(esphome::sensor::STATE_CLASS_MEASUREMENT);
} }
void set_min_value(float min_value);
protected: protected:
void setup() { setup_datapoint(); } void setup() { setup_datapoint(); }
void update() override { datapoint_read_request(); } void update() override { datapoint_read_request(); }
const StringRef &get_component_name() override { return get_name(); } const StringRef &get_component_name() override { return get_name(); }
void datapoint_value_changed(float state) override { publish_state(state); }; void datapoint_value_changed(float state) override { publish_state(state); };
void datapoint_value_changed(uint8_t state) override { publish_state(state); }; void datapoint_value_changed(uint8_t state) override;
void datapoint_value_changed(uint16_t state) override { publish_state(state); }; void datapoint_value_changed(uint16_t state) override;
void datapoint_value_changed(uint32_t state) override { publish_state(state); }; void datapoint_value_changed(uint32_t state) override;
private:
float min_value_ = -FLT_MAX;
}; };
} // namespace optolink } // namespace optolink
} // namespace esphome } // namespace esphome

View file

@ -2,7 +2,6 @@
#include "optolink_switch.h" #include "optolink_switch.h"
#include "../optolink.h" #include "../optolink.h"
#include "VitoWiFi.h"
namespace esphome { namespace esphome {
namespace optolink { namespace optolink {

View file

@ -3,9 +3,8 @@
#ifdef USE_ARDUINO #ifdef USE_ARDUINO
#include "esphome/components/switch/switch.h" #include "esphome/components/switch/switch.h"
#include "../datapoint_component.h"
#include "../optolink.h" #include "../optolink.h"
#include "VitoWiFi.h" #include "../datapoint_component.h"
namespace esphome { namespace esphome {
namespace optolink { namespace optolink {

View file

@ -4,7 +4,6 @@
#include "optolink_text_sensor.h" #include "optolink_text_sensor.h"
#include "../optolink.h" #include "../optolink.h"
#include "../datapoint_component.h" #include "../datapoint_component.h"
#include "VitoWiFi.h"
namespace esphome { namespace esphome {
namespace optolink { namespace optolink {

View file

@ -5,7 +5,6 @@
#include "esphome/components/text_sensor/text_sensor.h" #include "esphome/components/text_sensor/text_sensor.h"
#include "../optolink.h" #include "../optolink.h"
#include "../datapoint_component.h" #include "../datapoint_component.h"
#include "VitoWiFi.h"
namespace esphome { namespace esphome {
namespace optolink { namespace optolink {