#pragma once #include "esphome/core/component.h" #include "esphome/components/uart/uart.h" // #undef USE_SENSOR // #undef USE_BINARY_SENSOR // #undef USE_TEXT_SENSOR // #undef USE_SELECT // #undef USE_SWITCH #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_SELECT #include "esphome/components/select/select.h" #endif #ifdef USE_SWITCH #include "esphome/components/switch/switch.h" #endif namespace esphome { namespace optoma_rs232 { // clang-format off class DummySensor { public: template static void publish_state(M) {} }; class DummySelect : public DummySensor {}; class DummySwitch : public DummySensor {}; // clang-format on enum Inputs { UNKNOWN = 0, VGA = 2, HDMI_1 = 7, HDMI_2 = 8, }; class OptomaRS232Component : public uart::UARTDevice, public PollingComponent { public: void dump_config() override; void loop() override; float get_setup_priority() const override { return setup_priority::DATA; } void update() override; void beamer_input_select_changed(const std::string &, size_t); void beamer_power_switch_changed(bool); // clang-format off #ifndef USE_SENSOR #define SUB_SENSOR(name) protected: DummySensor *name##_sensor_{nullptr}; public: #endif #ifndef USE_BINARY_SENSOR #define SUB_BINARY_SENSOR(name) protected: DummySensor *name##_binary_sensor_{nullptr}; public: #endif #ifndef USE_TEXT_SENSOR #define SUB_TEXT_SENSOR(name) protected: DummySensor *name##_text_sensor_{nullptr}; public: #endif #ifndef USE_SELECT #define SUB_SELECT(name) protected: DummySelect *name##_select_{nullptr}; public: #endif #ifndef USE_SWITCH #define SUB_SWITCH(name) protected: DummySwitch *name##_switch_{nullptr}; public: #endif // clang-format on SUB_SENSOR(beamer_temp) SUB_SENSOR(beamer_lamp_time) SUB_SENSOR(beamer_fan1) SUB_SENSOR(beamer_color_mode) SUB_BINARY_SENSOR(beamer_power) SUB_TEXT_SENSOR(beamer_input) SUB_SELECT(beamer_input) SUB_SWITCH(beamer_power) protected: Inputs current_input_{UNKNOWN}; void process_line_(const std::string &str); void process_query_response_(const std::string &str); bool waiting_for_command_response_ = false; int last_query_ = -1; private: char buffer_[128]{}; size_t cursor_ = 0; void publish_input_(const std::string &state) const; void publish_power_(bool state) const; }; #ifdef USE_SELECT class InputSelect : public select::Select, public Parented { protected: void control(const std::string &value) override; }; #endif #ifdef USE_SWITCH class PowerSwitch : public switch_::Switch, public Parented { protected: void write_state(bool state) override; }; #endif } // namespace optoma_rs232 } // namespace esphome