From 357eb19945aaca2db35e89e7328e1b62d0990a19 Mon Sep 17 00:00:00 2001 From: j0ta29 Date: Sat, 26 Aug 2023 18:48:37 +0000 Subject: [PATCH] added support for div_ratio 1000 --- esphome/components/optolink/__init__.py | 4 ++- .../optolink/optolink_sensor_base.cpp | 26 +++++++++++++++++++ .../optolink/optolink_sensor_base.h | 8 ++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/esphome/components/optolink/__init__.py b/esphome/components/optolink/__init__.py index 2b086db404..4377f4c5e6 100644 --- a/esphome/components/optolink/__init__.py +++ b/esphome/components/optolink/__init__.py @@ -44,7 +44,9 @@ SENSOR_BASE_SCHEMA = cv.Schema( ), cv.Required(CONF_ADDRESS): cv.hex_uint32_t, # cv.Required(CONF_BYTES): cv.one_of(1, 2, 4, int=True), - cv.Optional(CONF_DIV_RATIO, default=1): cv.one_of(1, 10, 100, 3600, int=True), + cv.Optional(CONF_DIV_RATIO, default=1): cv.one_of( + 1, 10, 100, 1000, 3600, int=True + ), } ) diff --git a/esphome/components/optolink/optolink_sensor_base.cpp b/esphome/components/optolink/optolink_sensor_base.cpp index 856a59d56e..2af6a22471 100644 --- a/esphome/components/optolink/optolink_sensor_base.cpp +++ b/esphome/components/optolink/optolink_sensor_base.cpp @@ -84,6 +84,17 @@ void OptolinkSensorBase::setup_datapoint() { unfitting_value_type(); } break; + case 1000: + switch (bytes_) { + case 4: + datapoint_ = new Datapoint(get_component_name().c_str(), "optolink", address_, writeable_); + datapoint_->setCallback([this](const IDatapoint &dp, DPValue dp_value) { + ESP_LOGI(TAG, "recieved data for datapoint %s: %f", dp.getName(), dp_value.getFloat()); + value_changed(dp_value.getFloat()); + }); + break; + } + break; case 3600: switch (bytes_) { case 4: @@ -94,6 +105,7 @@ void OptolinkSensorBase::setup_datapoint() { }); break; } + break; default: unfitting_value_type(); } @@ -209,6 +221,20 @@ DPValue conv2_100_F::decode(const uint8_t *in) { return out; } +void conv4_1000_F::encode(uint8_t *out, DPValue in) { + int32_t tmp = floor((in.getFloat() * 1000) + 0.5); + out[3] = tmp >> 24; + out[2] = tmp >> 16; + out[1] = tmp >> 8; + out[0] = tmp & 0xFF; +} + +DPValue conv4_1000_F::decode(const uint8_t *in) { + int32_t tmp = in[3] << 24 | in[2] << 16 | in[1] << 8 | in[0]; + DPValue out(tmp / 1000.0f); + return out; +} + } // namespace optolink } // namespace esphome diff --git a/esphome/components/optolink/optolink_sensor_base.h b/esphome/components/optolink/optolink_sensor_base.h index abb6b56967..dd5d89c925 100644 --- a/esphome/components/optolink/optolink_sensor_base.h +++ b/esphome/components/optolink/optolink_sensor_base.h @@ -58,6 +58,14 @@ class conv2_100_F : public DPType { size_t get_length() const { return 2; } }; +// NOLINTNEXTLINE +class conv4_1000_F : public DPType { + public: + void encode(uint8_t *out, DPValue in); + DPValue decode(const uint8_t *in); + const size_t getLength() const { return 4; } +}; + } // namespace optolink } // namespace esphome