From d2c5dbeca2366c7ddbe66e5bbb35a16ffc3b9343 Mon Sep 17 00:00:00 2001 From: sqozz Date: Fri, 20 Sep 2024 00:29:52 +0200 Subject: [PATCH] Make whole components optional --- __init__.py | 1 - switch/tw7100_switch.cpp | 2 +- tw7100.cpp | 111 ++++++++++++++++++++++----------------- tw7100.h | 58 +++++++++++++++++--- 4 files changed, 115 insertions(+), 57 deletions(-) diff --git a/__init__.py b/__init__.py index d1eb51f..efb22ea 100644 --- a/__init__.py +++ b/__init__.py @@ -6,7 +6,6 @@ from esphome.const import CONF_ID CODEOWNERS = ["@sqozz"] DEPENDENCIES = ["uart"] -AUTO_LOAD = ["binary_sensor", "sensor", "text_sensor", "number", "switch"] tw7100_ns = cg.esphome_ns.namespace("tw7100") tw7100 = tw7100_ns.class_("tw7100Component", cg.PollingComponent, uart.UARTDevice) diff --git a/switch/tw7100_switch.cpp b/switch/tw7100_switch.cpp index 81b2b1b..0ebcf68 100644 --- a/switch/tw7100_switch.cpp +++ b/switch/tw7100_switch.cpp @@ -1,5 +1,5 @@ -#include "tw7100_switch.h" #include "../tw7100.h" +#include "tw7100_switch.h" #include "esphome/core/log.h" #include "esphome/core/defines.h" #include "esphome/core/helpers.h" diff --git a/tw7100.cpp b/tw7100.cpp index 7c602cc..7dc418f 100644 --- a/tw7100.cpp +++ b/tw7100.cpp @@ -1,10 +1,13 @@ #include "tw7100.h" -#include "select/tw7100_select.h" #include "esphome/core/log.h" #include "esphome/core/defines.h" #include "esphome/core/helpers.h" #include "esphome/components/uart/uart.h" +#ifdef USE_SELECT +#include "select/tw7100_select.h" +#endif + namespace esphome { namespace tw7100 { @@ -14,31 +17,21 @@ unsigned long waiting_for_answer_since = 0; std::deque cmd_queue; unsigned long last_set_cmd_timestamp; -std::map sourcelist; std::vector pwr_off_query_cmds{ "PWR?", "LAMP?" }; std::vector pwr_on_query_cmds{ -// Projection screen adjustment settings +#ifdef USE_SENSOR + "SIGNAL?", +#endif +#ifdef USE_NUMBER + "VOL?", "VKEYSTONE?", "HKEYSTONE?", - "ASPECT?", - "LUMINANCE?", - "OVSCAN?", -/* - "QC?", - "CORRECTMET?", -*/ - -// Source/Input/Resolution settings - "SOURCE?", - -// Image settings "BRIGHT?", "CONTRAST?", "DENSITY?", "TINT?", "CTEMP?", "FCOLOR?", - "CMODE?", "NRS?", "MPEGNRS?", "OFFSETR?", @@ -47,55 +40,58 @@ std::vector pwr_on_query_cmds{ "GAINR?", "GAING?", "GAINB?", - "GAMMA?", - "CSEL?", - "4KENHANCE?", - "IMGPRESET?", "SHRF?", "SHRS?", "DERANGE?", "DESTRENGTH?", +#endif +#ifdef USE_SWITCH + "BTAUDIO?", + "4KENHANCE?", + "IMGPROC?", + "AUDIOOUT?", + "HREVERSE?", + "VREVERSE?", + "ILLUM?", + "STANDBYCONF?", + "AUTOHOME?", + "WLPWR?", + "LOGTO?", + "MUTE?", +#endif +#ifdef USE_SELECT + "LUMINANCE?", + "SOURCE?", + "ASPECT?", + "OVSCAN?", + "CMODE?", + "GAMMA?", + "IMGPRESET?", "MCFI?", "CLRSPACE?", "DYNRANGE?", - "HDRPQ?", - "HDRHLG?", - "IMGPROC?", - -// Sound settings - "VOL?", - "AUDIOOUT?", - "MUTE?", - -// Environment settings - "HREVERSE?", - "VREVERSE?", "MSEL?", "SPEED?", - "ILLUM?", - "STANDBYCONF?", -// "PRODUCT?", // produces ERR +#endif +// TODO: +// "HDRPQ?", // number 01-16 +// "HDRHLG?", // number 01-16 +// "CSEL?", -// Home Screen settings - "AUTOHOME?", - -// Network settings - "WLPWR?", - -// Bluetooth - "BTAUDIO?", - -// Information - "SIGNAL?", - "LOGTO?", // produces ERR // "SNO?" // Pushed on demand if sensor value is empty (e.g. after boot-up) -// "SOURCELIST?" // Parser present in git history but removed until esphome can update selects after api connections are established -// "SOURCELISTA?", // same as SOURCELIST +/* + "PRODUCT?", // produces ERR + "SOURCELIST?" // Parser present in git history but removed until esphome can update selects after api connections are established + "SOURCELISTA?", // same as SOURCELIST + "QC?", + "CORRECTMET?", +*/ }; void tw7100Component::setup() { static const char *const TAG = "setup()"; ESP_LOGV(TAG, "SETUP"); +#ifdef USE_SELECT if (source_select_ != nullptr) source_select_->setup(); if (luminance_select_ != nullptr) luminance_select_->setup(); if (aspect_select_ != nullptr) aspect_select_->setup(); @@ -108,6 +104,7 @@ void tw7100Component::setup() { if (dynrange_select_ != nullptr) dynrange_select_->setup(); if (msel_select_ != nullptr) msel_select_->setup(); if (speed_select_ != nullptr) speed_select_->setup(); +#endif }; void tw7100Component::update() { @@ -117,9 +114,11 @@ void tw7100Component::update() { cmd_queue.push_back(cmd); } if ((powered_->state)) { +#ifdef USE_TEXT_SENSOR if (this->serial_->state.length() < 1) { cmd_queue.push_back("SNO?"); } +#endif for (auto &cmd : pwr_on_query_cmds) { cmd_queue.push_back(cmd); } @@ -213,8 +212,13 @@ void tw7100Component::update_sensor(std::pair data) { if (cmd == "PWR") { ESP_LOGV(TAG, "updating power sensors"); int value = std::stoi(value_string); +#ifdef USE_BINARY_SENSOR powered_->publish_state(value > 0); +#endif +#ifdef USE_SWITCH power_switch_->publish_state(value > 0); // ack previous cmd or update switch +#endif +#ifdef USE_SENSOR power_status_->publish_state(value); } else if (cmd == "LAMP") { ESP_LOGV(TAG, "updating lamp sensors"); @@ -225,6 +229,8 @@ void tw7100Component::update_sensor(std::pair data) { int value = std::stoi(value_string); has_signal_->publish_state(value > 0); signal_status_->publish_state(value); +#endif +#ifdef USE_SELECT } else if (cmd == "LUMINANCE") { ESP_LOGV(TAG, "updating luminance sensors"); int value = std::stoi(value_string); @@ -264,8 +270,12 @@ void tw7100Component::update_sensor(std::pair data) { ESP_LOGV(TAG, "updating msel select"); } else if (cmd == "SPEED") { ESP_LOGV(TAG, "updating speed select"); +#endif +#ifdef USE_TEXT_SENSOR } else if (cmd == "SNO") { serial_->publish_state(value_string); +#endif +#ifdef USE_SWITCH } else if (cmd == "BTAUDIO") { ESP_LOGV(TAG, "updating btaudio switch"); int value = std::stoi(value_string); @@ -311,6 +321,8 @@ void tw7100Component::update_sensor(std::pair data) { } else if (cmd == "MUTE") { ESP_LOGV(TAG, "updating mute switch with value %s", value_string.c_str()); mute_switch_->publish_state(value_string == "ON"); +#endif +#ifdef USE_NUMBER } else if (cmd == "VOL") { int value = std::stoi(value_string); ESP_LOGV(TAG, "updating volume number with value %s", value_string.c_str()); @@ -395,6 +407,7 @@ void tw7100Component::update_sensor(std::pair data) { int value = std::stoi(value_string); ESP_LOGV(TAG, "updating destrength number with value %s", value_string.c_str()); destrength_number_->publish_state(value); +#endif } else { ESP_LOGW(TAG, "Command %s unknown, skipping updating sensor with value", cmd.c_str()); } diff --git a/tw7100.h b/tw7100.h index b94455a..e8ae9b9 100644 --- a/tw7100.h +++ b/tw7100.h @@ -1,16 +1,36 @@ +#ifndef TW7100_H_ +#define TW7100_H_ #pragma once #include "esphome/core/component.h" #include "esphome/core/log.h" -#include "esphome/components/sensor/sensor.h" -#include "esphome/components/binary_sensor/binary_sensor.h" -#include "esphome/components/text_sensor/text_sensor.h" -#include "esphome/components/switch/switch.h" -#include "esphome/components/select/select.h" -#include "esphome/components/number/number.h" #include "esphome/components/uart/uart.h" + +#ifdef USE_SENSOR +#include "esphome/components/sensor/sensor.h" +#endif + +#ifdef USE_BINARY_SENSOR +#include "esphome/components/binary_sensor/binary_sensor.h" +#endif + +#ifdef USE_TEXT_SENSOR +#include "esphome/components/text_sensor/text_sensor.h" +#endif + +#ifdef USE_SWITCH +#include "esphome/components/switch/switch.h" +#endif + +#ifdef USE_SELECT +#include "esphome/components/select/select.h" #include "select/tw7100_select.h" +#endif + +#ifdef USE_NUMBER +#include "esphome/components/number/number.h" #include "number/tw7100_number.h" +#endif namespace esphome { namespace tw7100 { @@ -35,13 +55,20 @@ class tw7100Component : public PollingComponent, public uart::UARTDevice { void loop() override; void dump_config() override; float get_setup_priority() const override { return setup_priority::BUS; } +#ifdef USE_BINARY_SENSOR void set_powered(binary_sensor::BinarySensor *powered) { this->powered_ = powered; } void set_has_signal(binary_sensor::BinarySensor *has_signal) { this->has_signal_ = has_signal; } +#endif +#ifdef USE_SENSOR void set_lamp_hours(sensor::Sensor *lamp_hours) { this->lamp_hours_ = lamp_hours; } void set_power_status(sensor::Sensor *power_status) { this->power_status_ = power_status; } void set_signal_status(sensor::Sensor *signal_status) { this->signal_status_ = signal_status; } void set_luminance_level(sensor::Sensor *luminance_level) { this->luminance_level_ = luminance_level; } +#endif +#ifdef USE_TEXT_SENSOR void set_serial(text_sensor::TextSensor *serial) { this->serial_ = serial; } +#endif +#ifdef USE_SWITCH void set_set_pwr(switch_::Switch *switch_) { this->power_switch_ = switch_; } void set_set_btaudio(switch_::Switch *switch_) { this->btaudio_switch_ = switch_; } void set_set_mute(switch_::Switch *switch_) { this->mute_switch_ = switch_; } @@ -56,6 +83,8 @@ class tw7100Component : public PollingComponent, public uart::UARTDevice { void set_set_imgproc(switch_::Switch *switch_) { this->img_processing_switch_ = switch_; } void set_set_4kenhance(switch_::Switch *switch_) { this->_4kenhance_switch_ = switch_; } void set_set_logto(switch_::Switch *switch_) { this->logto_switch_ = switch_; } +#endif +#ifdef USE_SELECT void set_set_source(tw7100::tw7100Select *select_) { this->source_select_ = select_; } void set_set_luminance(tw7100::tw7100Select *select_) { this->luminance_select_ = select_; } void set_set_aspect(tw7100::tw7100Select *select_) { this->aspect_select_ = select_; } @@ -68,6 +97,8 @@ class tw7100Component : public PollingComponent, public uart::UARTDevice { void set_set_dynrange(tw7100::tw7100Select *select_) { this->dynrange_select_ = select_; } void set_set_msel(tw7100::tw7100Select *select_) { this->msel_select_ = select_; } void set_set_speed(tw7100::tw7100Select *select_) { this->speed_select_ = select_; } +#endif +#ifdef USE_NUMBER void set_set_vol(tw7100::tw7100Number *number_) { this->volume_number_ = number_; } void set_set_vkeystone(tw7100::tw7100Number *number_) { this->vkeystone_number_ = number_; } void set_set_hkeystone(tw7100::tw7100Number *number_) { this->hkeystone_number_ = number_; } @@ -89,16 +120,24 @@ class tw7100Component : public PollingComponent, public uart::UARTDevice { void set_set_shrs(tw7100::tw7100Number *number_) { this->shrs_number_ = number_; } void set_set_derange(tw7100::tw7100Number *number_) { this->derange_number_ = number_; } void set_set_destrength(tw7100::tw7100Number *number_) { this->destrength_number_ = number_; } +#endif protected: +#ifdef USE_BINARY_SENSOR binary_sensor::BinarySensor *powered_{nullptr}; binary_sensor::BinarySensor *has_signal_{nullptr}; +#endif +#ifdef USE_SENSOR sensor::Sensor *lamp_hours_{nullptr}; sensor::Sensor *power_status_{nullptr}; sensor::Sensor *signal_status_{nullptr}; sensor::Sensor *luminance_level_{nullptr}; +#endif +#ifdef USE_TEXT_SENSOR text_sensor::TextSensor *serial_{nullptr}; +#endif +#ifdef USE_SWITCH switch_::Switch *power_switch_{nullptr}; switch_::Switch *btaudio_switch_{nullptr}; switch_::Switch *mute_switch_{nullptr}; @@ -113,6 +152,8 @@ class tw7100Component : public PollingComponent, public uart::UARTDevice { switch_::Switch *img_processing_switch_{nullptr}; switch_::Switch *_4kenhance_switch_{nullptr}; switch_::Switch *logto_switch_{nullptr}; +#endif +#ifdef USE_SELECT tw7100::tw7100Select *source_select_{nullptr}; tw7100::tw7100Select *luminance_select_{nullptr}; tw7100::tw7100Select *aspect_select_{nullptr}; @@ -125,6 +166,8 @@ class tw7100Component : public PollingComponent, public uart::UARTDevice { tw7100::tw7100Select *dynrange_select_{nullptr}; tw7100::tw7100Select *msel_select_{nullptr}; tw7100::tw7100Select *speed_select_{nullptr}; +#endif +#ifdef USE_NUMBER tw7100::tw7100Number *volume_number_{nullptr}; tw7100::tw7100Number *vkeystone_number_{nullptr}; tw7100::tw7100Number *hkeystone_number_{nullptr}; @@ -146,7 +189,10 @@ class tw7100Component : public PollingComponent, public uart::UARTDevice { tw7100::tw7100Number *shrs_number_{nullptr}; tw7100::tw7100Number *derange_number_{nullptr}; tw7100::tw7100Number *destrength_number_{nullptr}; +#endif }; } // namespace tw7100 } // namespace esphome + +#endif