From 0743e192e916940c87da9de3d08868c8591280bd Mon Sep 17 00:00:00 2001 From: j0ta29 Date: Mon, 13 Nov 2023 22:00:53 +0000 Subject: [PATCH] added manual configurable state_info text_sensor --- esphome/components/optolink/__init__.py | 15 -------- esphome/components/optolink/optolink.cpp | 15 -------- esphome/components/optolink/optolink.h | 35 ------------------- .../optolink/text_sensor/__init__.py | 22 +++++++++--- .../text_sensor/optolink_text_sensor.cpp | 5 ++- .../text_sensor/optolink_text_sensor.h | 10 ++++-- 6 files changed, 30 insertions(+), 72 deletions(-) diff --git a/esphome/components/optolink/__init__.py b/esphome/components/optolink/__init__.py index 60362a41de..04f0b77e5a 100644 --- a/esphome/components/optolink/__init__.py +++ b/esphome/components/optolink/__init__.py @@ -19,7 +19,6 @@ CODEOWNERS = ["@j0ta29"] DEPENDENCIES = ["text_sensor"] AUTO_LOAD = [] MULTI_CONF = False -CONF_DEVICE_INFO = "device_info" optolink_ns = cg.esphome_ns.namespace("optolink") CONF_OPTOLINK_ID = "optolink_id" @@ -107,20 +106,6 @@ async def to_code(config): ) await cg.register_component(debugSensor, config) - if CONF_DEVICE_INFO in config: - debugSensor = cg.new_Pvariable( - config[DEVICE_INFO_SENSOR_ID], config[CONF_DEVICE_INFO], var - ) - await ts.register_text_sensor( - debugSensor, - { - "id": config[DEVICE_INFO_SENSOR_ID], - "name": config[CONF_DEVICE_INFO], - "disabled_by_default": "false", - }, - ) - await cg.register_component(debugSensor, config) - if CORE.is_esp32: cg.add(var.set_rx_pin(config[CONF_RX_PIN]["number"])) cg.add(var.set_tx_pin(config[CONF_TX_PIN]["number"])) diff --git a/esphome/components/optolink/optolink.cpp b/esphome/components/optolink/optolink.cpp index 52f7bf9d5f..d67e8145a6 100644 --- a/esphome/components/optolink/optolink.cpp +++ b/esphome/components/optolink/optolink.cpp @@ -79,21 +79,6 @@ size_t Optolink::write(uint8_t ch) { return 1; } -void OptolinkDeviceInfoSensor::setup() { - datapoint_ = new Datapoint(get_name().c_str(), "optolink", 0x00f8, false); - datapoint_->setCallback([this](const IDatapoint &dp, DPValue dp_value) { - uint32_t value = dp_value.getU32(); - ESP_LOGI(TAG, "recieved data for datapoint %s: %d", dp.getName(), value); - uint8_t *bytes = (uint8_t *) &value; - uint16_t tmp = esphome::byteswap(*((uint16_t *) bytes)); - std::string geraetekennung = esphome::format_hex_pretty(&tmp, 1); - std::string hardware_revision = esphome::format_hex_pretty((uint8_t *) bytes + 2, 1); - std::string software_index = esphome::format_hex_pretty((uint8_t *) bytes + 3, 1); - publish_state("Device ID: " + geraetekennung + "|Hardware Revision: " + hardware_revision + - "|Software Index: " + software_index); - }); -} - } // namespace optolink } // namespace esphome diff --git a/esphome/components/optolink/optolink.h b/esphome/components/optolink/optolink.h index 3eba197d3c..620cdac0e8 100644 --- a/esphome/components/optolink/optolink.h +++ b/esphome/components/optolink/optolink.h @@ -37,41 +37,6 @@ class Optolink : public esphome::Component, public Print { std::string get_error() { return error_; } }; -class OptolinkStateSensor : public esphome::text_sensor::TextSensor, public esphome::PollingComponent { - public: - OptolinkStateSensor(std::string name, Optolink *optolink) { - optolink_ = optolink; - set_name(name.c_str()); - set_update_interval(1000); - set_entity_category(esphome::ENTITY_CATEGORY_DIAGNOSTIC); - } - - protected: - void setup() override{}; - void update() override { publish_state(optolink_->get_error()); } - - private: - Optolink *optolink_; -}; - -class OptolinkDeviceInfoSensor : public esphome::text_sensor::TextSensor, public esphome::PollingComponent { - public: - OptolinkDeviceInfoSensor(const std::string &name, Optolink *optolink) { - optolink_ = optolink; - set_name(name.c_str()); - set_update_interval(1800000); - set_entity_category(esphome::ENTITY_CATEGORY_DIAGNOSTIC); - } - - protected: - void setup() override; - void update() override { optolink_->read_value(datapoint_); } - - private: - Optolink *optolink_; - IDatapoint *datapoint_; -}; - } // namespace optolink } // namespace esphome diff --git a/esphome/components/optolink/text_sensor/__init__.py b/esphome/components/optolink/text_sensor/__init__.py index c6300438a5..20fee81c64 100644 --- a/esphome/components/optolink/text_sensor/__init__.py +++ b/esphome/components/optolink/text_sensor/__init__.py @@ -21,6 +21,7 @@ MODE = { "DAY_SCHEDULE": TextSensorMode.DAY_SCHEDULE, "DAY_SCHEDULE_SYNCHRONIZED": TextSensorMode.DAY_SCHEDULE_SYNCHRONIZED, "DEVICE_INFO": TextSensorMode.DEVICE_INFO, + "STATE_INFO": TextSensorMode.STATE_INFO, } DAY_OF_WEEK = { "MONDAY": 0, @@ -38,6 +39,19 @@ OptolinkTextSensor = optolink_ns.class_( ) +def check_address(): + def validator_(config): + address_needed = config[CONF_MODE] in ["MAP", "RAW"] + address_defined = CONF_ADDRESS in config + if address_needed and not address_defined: + raise cv.Invalid(f"{CONF_ADDRESS} is required in mode MAP or RAW") + if not address_needed and address_defined: + raise cv.Invalid(f"{CONF_ADDRESS} is only allowed in mode MAP or RAW") + return config + + return validator_ + + def check_bytes(): def validator_(config): bytes_needed = config[CONF_MODE] in ["MAP", "RAW"] @@ -45,9 +59,7 @@ def check_bytes(): if bytes_needed and not bytes_defined: raise cv.Invalid(f"{CONF_BYTES} is required in mode MAP or RAW") if not bytes_needed and bytes_defined: - raise cv.Invalid( - f"{CONF_BYTES} is not allowed in mode DAY_SCHEDULE and DAY_SCHEDULE_SYNCHRONIZED" - ) + raise cv.Invalid(f"{CONF_BYTES} is only allowed in mode MAP or RAW") return config return validator_ @@ -97,7 +109,8 @@ CONFIG_SCHEMA = cv.All( text_sensor.TEXT_SENSOR_SCHEMA.extend( { cv.GenerateID(): cv.declare_id(OptolinkTextSensor), - cv.Optional(CONF_MODE, default="MAP"): cv.enum(MODE, upper=True), + cv.Required(CONF_MODE): cv.enum(MODE, upper=True), + cv.Optional(CONF_ADDRESS): cv.hex_uint32_t, cv.Optional(CONF_BYTES): cv.int_range(min=1, max=9), cv.Optional(CONF_DAY_OF_WEEK): cv.enum(DAY_OF_WEEK, upper=True), cv.Optional(CONF_ENTITY_ID): cv.entity_id, @@ -105,6 +118,7 @@ CONFIG_SCHEMA = cv.All( ) .extend(cv.COMPONENT_SCHEMA) .extend(SENSOR_BASE_SCHEMA), + check_address(), check_bytes(), check_dow(), check_entity_id(), diff --git a/esphome/components/optolink/text_sensor/optolink_text_sensor.cpp b/esphome/components/optolink/text_sensor/optolink_text_sensor.cpp index 58a432eb93..212f726039 100644 --- a/esphome/components/optolink/text_sensor/optolink_text_sensor.cpp +++ b/esphome/components/optolink/text_sensor/optolink_text_sensor.cpp @@ -108,6 +108,9 @@ void OptolinkTextSensor::setup() { bytes_ = 4; address_ = 0x00f8; break; + case STATE_INFO: + set_entity_category(esphome::ENTITY_CATEGORY_DIAGNOSTIC); + return; // no datapoint setup! } setup_datapoint(); }; @@ -136,6 +139,7 @@ void OptolinkTextSensor::value_changed(uint8_t *value, size_t length) { } break; case DEVICE_INFO: + case STATE_INFO: case MAP: unfitting_value_type(); break; @@ -145,7 +149,6 @@ void OptolinkTextSensor::value_changed(uint8_t *value, size_t length) { void OptolinkTextSensor::value_changed(uint32_t value) { switch (mode_) { case DEVICE_INFO: { - ESP_LOGI(TAG, "recieved data for datapoint %s: %d", datapoint_->getName(), value); uint8_t *bytes = (uint8_t *) &value; uint16_t tmp = esphome::byteswap(*((uint16_t *) bytes)); std::string geraetekennung = esphome::format_hex_pretty(&tmp, 1); diff --git a/esphome/components/optolink/text_sensor/optolink_text_sensor.h b/esphome/components/optolink/text_sensor/optolink_text_sensor.h index 0e8617547b..f9c9fb24e6 100644 --- a/esphome/components/optolink/text_sensor/optolink_text_sensor.h +++ b/esphome/components/optolink/text_sensor/optolink_text_sensor.h @@ -10,7 +10,7 @@ namespace esphome { namespace optolink { -enum TextSensorMode { MAP, RAW, DAY_SCHEDULE, DAY_SCHEDULE_SYNCHRONIZED, DEVICE_INFO }; +enum TextSensorMode { MAP, RAW, DAY_SCHEDULE, DAY_SCHEDULE_SYNCHRONIZED, DEVICE_INFO, STATE_INFO }; class OptolinkTextSensor : public OptolinkSensorBase, public esphome::text_sensor::TextSensor, @@ -24,7 +24,13 @@ class OptolinkTextSensor : public OptolinkSensorBase, protected: void setup() override; - void update() override { optolink_->read_value(datapoint_); } + void update() override { + if (mode_ == STATE_INFO) { + publish_state(optolink_->get_error()); + } else { + optolink_->read_value(datapoint_); + } + } const StringRef &get_component_name() override { return get_name(); } void value_changed(float state) override { publish_state(std::to_string(state)); };