From 0e30e119a02f7298c1290b2eec7ce4a1ff35c8ac Mon Sep 17 00:00:00 2001 From: j0ta29 Date: Tue, 19 Dec 2023 21:47:30 +0000 Subject: [PATCH] fix to support negative values for 1-byte datapoints --- .../binary_sensor/optolink_binary_sensor.h | 1 - .../optolink/number/optolink_number.cpp | 25 +++++++++++- .../optolink/number/optolink_number.h | 9 ++--- esphome/components/optolink/optolink.cpp | 3 +- .../optolink/select/optolink_select.cpp | 1 - .../optolink/select/optolink_select.h | 3 +- .../components/optolink/sensor/__init__.py | 4 ++ .../optolink/sensor/optolink_sensor.cpp | 40 +++++++++++++++++++ .../optolink/sensor/optolink_sensor.h | 13 ++++-- .../optolink/switch/optolink_switch.cpp | 1 - .../optolink/switch/optolink_switch.h | 3 +- .../text_sensor/optolink_text_sensor.cpp | 1 - .../text_sensor/optolink_text_sensor.h | 1 - 13 files changed, 84 insertions(+), 21 deletions(-) create mode 100644 esphome/components/optolink/sensor/optolink_sensor.cpp diff --git a/esphome/components/optolink/binary_sensor/optolink_binary_sensor.h b/esphome/components/optolink/binary_sensor/optolink_binary_sensor.h index 4cdb5bd1f8..67f8e99307 100644 --- a/esphome/components/optolink/binary_sensor/optolink_binary_sensor.h +++ b/esphome/components/optolink/binary_sensor/optolink_binary_sensor.h @@ -5,7 +5,6 @@ #include "esphome/components/binary_sensor/binary_sensor.h" #include "../optolink.h" #include "../datapoint_component.h" -#include "VitoWiFi.h" namespace esphome { namespace optolink { diff --git a/esphome/components/optolink/number/optolink_number.cpp b/esphome/components/optolink/number/optolink_number.cpp index 3dd850448b..3fcd847d14 100644 --- a/esphome/components/optolink/number/optolink_number.cpp +++ b/esphome/components/optolink/number/optolink_number.cpp @@ -2,7 +2,6 @@ #include "optolink_number.h" #include "../optolink.h" -#include "VitoWiFi.h" namespace esphome { 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 esphome diff --git a/esphome/components/optolink/number/optolink_number.h b/esphome/components/optolink/number/optolink_number.h index 49bd75700b..d7d3062a31 100644 --- a/esphome/components/optolink/number/optolink_number.h +++ b/esphome/components/optolink/number/optolink_number.h @@ -3,9 +3,8 @@ #ifdef USE_ARDUINO #include "esphome/components/number/number.h" -#include "../datapoint_component.h" #include "../optolink.h" -#include "VitoWiFi.h" +#include "../datapoint_component.h" namespace esphome { namespace optolink { @@ -21,9 +20,9 @@ class OptolinkNumber : public DatapointComponent, public esphome::number::Number const StringRef &get_component_name() override { return get_name(); } 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(uint16_t state) override { publish_state(state); }; - void datapoint_value_changed(uint32_t state) override { publish_state(state); }; + void datapoint_value_changed(uint8_t state) override; + void datapoint_value_changed(uint16_t state) override; + void datapoint_value_changed(uint32_t state) override; }; } // namespace optolink diff --git a/esphome/components/optolink/optolink.cpp b/esphome/components/optolink/optolink.cpp index 4921297c5f..e46ebe9505 100644 --- a/esphome/components/optolink/optolink.cpp +++ b/esphome/components/optolink/optolink.cpp @@ -3,7 +3,6 @@ #include "esphome/core/defines.h" #include "esphome/core/log.h" #include "optolink.h" -#include "VitoWiFi.h" #if defined(VITOWIFI_PROTOCOL) // NOLINTNEXTLINE @@ -40,7 +39,7 @@ void Optolink::setup() { } void Optolink::loop() { - ESP_LOGD(TAG, "queue size: %d", VitoWiFi.queueSize()); + // ESP_LOGD(TAG, "queue size: %d", VitoWiFi.queueSize()); VitoWiFi.loop(); } diff --git a/esphome/components/optolink/select/optolink_select.cpp b/esphome/components/optolink/select/optolink_select.cpp index 7f6e068472..0f21c6d0c6 100644 --- a/esphome/components/optolink/select/optolink_select.cpp +++ b/esphome/components/optolink/select/optolink_select.cpp @@ -2,7 +2,6 @@ #include "optolink_select.h" #include "../optolink.h" -#include "VitoWiFi.h" namespace esphome { namespace optolink { diff --git a/esphome/components/optolink/select/optolink_select.h b/esphome/components/optolink/select/optolink_select.h index f3512cf545..e578a858b1 100644 --- a/esphome/components/optolink/select/optolink_select.h +++ b/esphome/components/optolink/select/optolink_select.h @@ -2,11 +2,10 @@ #ifdef USE_ARDUINO -#include #include "esphome/components/select/select.h" #include "../optolink.h" #include "../datapoint_component.h" -#include "VitoWiFi.h" +#include namespace esphome { namespace optolink { diff --git a/esphome/components/optolink/sensor/__init__.py b/esphome/components/optolink/sensor/__init__.py index a7ada97826..4c91a4666f 100644 --- a/esphome/components/optolink/sensor/__init__.py +++ b/esphome/components/optolink/sensor/__init__.py @@ -6,6 +6,7 @@ from esphome.const import ( CONF_BYTES, CONF_DIV_RATIO, CONF_ID, + CONF_MIN_VALUE, ) from .. import CONF_OPTOLINK_ID, SENSOR_BASE_SCHEMA, optolink_ns @@ -22,6 +23,7 @@ CONFIG_SCHEMA = ( cv.GenerateID(): cv.declare_id(OptolinkSensor), cv.Required(CONF_ADDRESS): cv.hex_uint32_t, cv.Required(CONF_BYTES): cv.one_of(1, 2, 4, int=True), + cv.Optional(CONF_MIN_VALUE): cv.float_, } ) .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_bytes(config[CONF_BYTES])) 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])) diff --git a/esphome/components/optolink/sensor/optolink_sensor.cpp b/esphome/components/optolink/sensor/optolink_sensor.cpp new file mode 100644 index 0000000000..a1c2df7815 --- /dev/null +++ b/esphome/components/optolink/sensor/optolink_sensor.cpp @@ -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 diff --git a/esphome/components/optolink/sensor/optolink_sensor.h b/esphome/components/optolink/sensor/optolink_sensor.h index 7538fd6390..59e9d4bdb7 100644 --- a/esphome/components/optolink/sensor/optolink_sensor.h +++ b/esphome/components/optolink/sensor/optolink_sensor.h @@ -5,7 +5,7 @@ #include "esphome/components/sensor/sensor.h" #include "../optolink.h" #include "../datapoint_component.h" -#include "VitoWiFi.h" +#include namespace esphome { namespace optolink { @@ -16,15 +16,20 @@ class OptolinkSensor : public DatapointComponent, public esphome::sensor::Sensor set_state_class(esphome::sensor::STATE_CLASS_MEASUREMENT); } + void set_min_value(float min_value); + protected: void setup() { setup_datapoint(); } void update() override { datapoint_read_request(); } const StringRef &get_component_name() override { return get_name(); } 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(uint16_t state) override { publish_state(state); }; - void datapoint_value_changed(uint32_t state) override { publish_state(state); }; + void datapoint_value_changed(uint8_t state) override; + void datapoint_value_changed(uint16_t state) override; + void datapoint_value_changed(uint32_t state) override; + + private: + float min_value_ = -FLT_MAX; }; } // namespace optolink } // namespace esphome diff --git a/esphome/components/optolink/switch/optolink_switch.cpp b/esphome/components/optolink/switch/optolink_switch.cpp index c5bec71a8c..cfd14e287f 100644 --- a/esphome/components/optolink/switch/optolink_switch.cpp +++ b/esphome/components/optolink/switch/optolink_switch.cpp @@ -2,7 +2,6 @@ #include "optolink_switch.h" #include "../optolink.h" -#include "VitoWiFi.h" namespace esphome { namespace optolink { diff --git a/esphome/components/optolink/switch/optolink_switch.h b/esphome/components/optolink/switch/optolink_switch.h index ce97179ea6..75cbe85c36 100644 --- a/esphome/components/optolink/switch/optolink_switch.h +++ b/esphome/components/optolink/switch/optolink_switch.h @@ -3,9 +3,8 @@ #ifdef USE_ARDUINO #include "esphome/components/switch/switch.h" -#include "../datapoint_component.h" #include "../optolink.h" -#include "VitoWiFi.h" +#include "../datapoint_component.h" namespace esphome { namespace optolink { diff --git a/esphome/components/optolink/text_sensor/optolink_text_sensor.cpp b/esphome/components/optolink/text_sensor/optolink_text_sensor.cpp index 1225834a77..9d1d8d1e32 100644 --- a/esphome/components/optolink/text_sensor/optolink_text_sensor.cpp +++ b/esphome/components/optolink/text_sensor/optolink_text_sensor.cpp @@ -4,7 +4,6 @@ #include "optolink_text_sensor.h" #include "../optolink.h" #include "../datapoint_component.h" -#include "VitoWiFi.h" namespace esphome { namespace optolink { diff --git a/esphome/components/optolink/text_sensor/optolink_text_sensor.h b/esphome/components/optolink/text_sensor/optolink_text_sensor.h index 6dd1210dbb..1583da1d99 100644 --- a/esphome/components/optolink/text_sensor/optolink_text_sensor.h +++ b/esphome/components/optolink/text_sensor/optolink_text_sensor.h @@ -5,7 +5,6 @@ #include "esphome/components/text_sensor/text_sensor.h" #include "../optolink.h" #include "../datapoint_component.h" -#include "VitoWiFi.h" namespace esphome { namespace optolink {