Add select

This commit is contained in:
T 2025-03-14 17:57:43 +01:00
parent 419143d70a
commit 6454dc6d58
6 changed files with 61 additions and 11 deletions

View File

@ -14,9 +14,10 @@ OptomaRS232Component = optoma_ns.class_("OptomaRS232Component", cg.PollingCompon
CONFIG_SCHEMA = (
cv.Schema({
cv.GenerateID(): cv.declare_id(OptomaRS232Component),
})
.extend(cv.polling_component_schema("10s"))
.extend(uart.UART_DEVICE_SCHEMA)
}).extend(
cv.polling_component_schema("10s"),
uart.UART_DEVICE_SCHEMA
)
)

View File

@ -56,6 +56,29 @@ void OptomaRS232Component::update() {
write_array(reinterpret_cast<const uint8_t *>(QUERIES[last_query_]), strlen(QUERIES[last_query_]));
}
void OptomaRS232Component::beamer_input_select_changed(const std::string &state, size_t) {
const char *data;
Inputs inp{UNKNOWN};
if (state == "Unknown")
return;
if (state == "HDMI 1") {
inp = HDMI_1;
data = "~0012 1\r";
} else if (state == "HDMI 2") {
inp = HDMI_2;
data = "~0012 15\r";
} else if (state == "VGA") {
inp = VGA;
data = "~0012 5\r"; // VGA 1
} else {
return;
}
if (inp == current_input_)
return;
current_input_ = inp;
write_array(reinterpret_cast<const uint8_t *>(data), strlen(data));
}
void OptomaRS232Component::process_line_(const std::string &str) {
// if we are waiting for the projector to respond to a command.
// it will respond P (pass) or F (fail) before giving us the actual response to the command.
@ -110,19 +133,23 @@ void OptomaRS232Component::process_query_response_(const std::string &str) {
// publish(beamer_firmware_,strtol(buf + 10, 0, 10));
buf[10] = 0;
int input = -1; // atol(buf + 8); BUGGY
int input = strtol(buf + 8, 0, 10); // BUGGY
switch (input) {
case Inputs::HDMI_1:
current_input_ = Inputs::HDMI_1;
publish(beamer_input_text_sensor_, "HDMI 1");
break;
case Inputs::HDMI_2:
current_input_ = Inputs::HDMI_2;
publish(beamer_input_text_sensor_, "HDMI 2");
break;
case Inputs::VGA:
current_input_ = Inputs::VGA;
publish(beamer_input_text_sensor_, "VGA");
break;
default:
case Inputs::UNKNOWN:
current_input_ = Inputs::UNKNOWN;
publish(beamer_input_text_sensor_, "Unknown");
break;
}
@ -139,5 +166,14 @@ void OptomaRS232Component::process_query_response_(const std::string &str) {
}
}
#ifdef USE_SELECT
void InputSelect::control(const std::string &value) {
this->publish_state(value);
auto index = this->index_of(value);
if (index.has_value())
this->parent_->beamer_input_select_changed(value, *index);
}
#endif
} // namespace optoma_rs232
} // namespace esphome

View File

@ -36,6 +36,7 @@ class OptomaRS232Component : public uart::UARTDevice, public PollingComponent {
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);
// clang-format off
#ifndef USE_SENSOR
@ -61,6 +62,8 @@ class OptomaRS232Component : public uart::UARTDevice, public PollingComponent {
SUB_SELECT(beamer_input)
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;
@ -71,5 +74,12 @@ class OptomaRS232Component : public uart::UARTDevice, public PollingComponent {
size_t cursor_ = 0;
};
#ifdef USE_SELECT
class InputSelect : public select::Select, public Parented<OptomaRS232Component> {
protected:
void control(const std::string &value) override;
};
#endif
} // namespace optoma_rs232
} // namespace esphome

View File

@ -6,6 +6,8 @@ from . import CONF_OPTOMA_RS232_ID, OptomaRS232Component, optoma_ns
DEPENDENCIES = ["optoma_rs232"]
InputSelect = optoma_ns.class_("InputSelect", select.Select)
Inputs = optoma_ns.enum("Inputs")
INPUT_OPTIONS = {
"Unknown": 0,
@ -14,8 +16,7 @@ INPUT_OPTIONS = {
"HDMI 2": 8,
}
CONFIG_SCHEMA = select.select_schema(
).extend({
CONFIG_SCHEMA = select.select_schema(InputSelect).extend({
cv.GenerateID(CONF_OPTOMA_RS232_ID): cv.use_id(OptomaRS232Component),
})

View File

@ -19,6 +19,7 @@ from . import CONF_OPTOMA_RS232_ID, optoma_ns, OptomaRS232Component
DEPENDENCIES = ["optoma_rs232"]
CONF_FAN_SPEED = "fan_speed"
CONF_LAMP_TIME = "lamp_hours"
CONFIG_SCHEMA = (
cv.Schema({
@ -29,7 +30,7 @@ CONFIG_SCHEMA = (
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_DURATION): sensor.sensor_schema(
cv.Optional(CONF_LAMP_TIME): sensor.sensor_schema(
unit_of_measurement=UNIT_HOUR,
accuracy_decimals=0,
device_class=DEVICE_CLASS_DURATION,
@ -42,8 +43,10 @@ CONFIG_SCHEMA = (
state_class=STATE_CLASS_MEASUREMENT,
),
})
.extend(cv.polling_component_schema("10s"))
.extend(uart.UART_DEVICE_SCHEMA)
.extend(
cv.polling_component_schema("10s"),
uart.UART_DEVICE_SCHEMA
)
)

View File

@ -6,8 +6,7 @@ from . import CONF_OPTOMA_RS232_ID, OptomaRS232Component
DEPENDENCIES = ["optoma_rs232"]
CONFIG_SCHEMA = text_sensor.text_sensor_schema(
).extend({
CONFIG_SCHEMA = text_sensor.text_sensor_schema().extend({
cv.GenerateID(CONF_OPTOMA_RS232_ID): cv.use_id(OptomaRS232Component),
})