esphome_optoma_rs232/optoma_rs232.h

111 lines
2.8 KiB
C
Raw Permalink Normal View History

2025-03-14 15:23:22 +01:00
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/uart/uart.h"
2025-03-20 16:49:53 +01:00
2025-03-20 22:20:45 +01:00
// #undef USE_SENSOR
// #undef USE_BINARY_SENSOR
// #undef USE_TEXT_SENSOR
// #undef USE_SELECT
// #undef USE_SWITCH
2025-03-20 16:49:53 +01:00
#ifdef USE_SENSOR
2025-03-14 15:23:22 +01:00
#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
2025-03-20 16:49:53 +01:00
#ifdef USE_SWITCH
#include "esphome/components/switch/switch.h"
#endif
2025-03-14 15:23:22 +01:00
namespace esphome {
namespace optoma_rs232 {
// clang-format off
class DummySensor { public: template<typename M> static void publish_state(M) {} };
2025-03-20 16:49:53 +01:00
class DummySelect : public DummySensor {};
class DummySwitch : public DummySensor {};
2025-03-14 15:23:22 +01:00
// 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;
2025-03-14 17:57:43 +01:00
void beamer_input_select_changed(const std::string &, size_t);
2025-03-20 16:49:53 +01:00
void beamer_power_switch_changed(bool);
2025-03-14 15:23:22 +01:00
// clang-format off
#ifndef USE_SENSOR
2025-03-20 22:20:45 +01:00
#define SUB_SENSOR(name) protected: DummySensor *name##_sensor_{nullptr}; public:
2025-03-14 15:23:22 +01:00
#endif
#ifndef USE_BINARY_SENSOR
2025-03-20 22:20:45 +01:00
#define SUB_BINARY_SENSOR(name) protected: DummySensor *name##_binary_sensor_{nullptr}; public:
2025-03-14 15:23:22 +01:00
#endif
#ifndef USE_TEXT_SENSOR
2025-03-20 22:20:45 +01:00
#define SUB_TEXT_SENSOR(name) protected: DummySensor *name##_text_sensor_{nullptr}; public:
2025-03-14 15:23:22 +01:00
#endif
#ifndef USE_SELECT
2025-03-20 22:20:45 +01:00
#define SUB_SELECT(name) protected: DummySelect *name##_select_{nullptr}; public:
2025-03-20 16:49:53 +01:00
#endif
#ifndef USE_SWITCH
2025-03-20 22:20:45 +01:00
#define SUB_SWITCH(name) protected: DummySwitch *name##_switch_{nullptr}; public:
2025-03-14 15:23:22 +01:00
#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)
2025-03-20 16:49:53 +01:00
SUB_SWITCH(beamer_power)
2025-03-14 15:23:22 +01:00
protected:
2025-03-14 17:57:43 +01:00
Inputs current_input_{UNKNOWN};
2025-03-14 15:23:22 +01:00
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;
2025-03-14 18:05:03 +01:00
void publish_input_(const std::string &state) const;
2025-03-20 16:49:53 +01:00
void publish_power_(bool state) const;
2025-03-14 15:23:22 +01:00
};
2025-03-14 17:57:43 +01:00
#ifdef USE_SELECT
class InputSelect : public select::Select, public Parented<OptomaRS232Component> {
protected:
void control(const std::string &value) override;
};
#endif
2025-03-20 16:49:53 +01:00
#ifdef USE_SWITCH
class PowerSwitch : public switch_::Switch, public Parented<OptomaRS232Component> {
protected:
void write_state(bool state) override;
};
#endif
2025-03-14 17:57:43 +01:00
2025-03-14 15:23:22 +01:00
} // namespace optoma_rs232
} // namespace esphome