|
本帖最后由 wz199021 于 2024-6-4 18:24 编辑
天逸功放3100通过esp8266接入homeassistant所需材料:D1_mini TTL转232公头
代码:
esphome:
name: hd3100
friendly_name: HD3100
esp8266:
board: d1_mini
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "eSWRH8ZJd7gYqDIqE6/O6OBkwkyaaztRbjX9maRIpPdKM="
ota:
password: "11045507f3d76e675714zaq7282a78d0f74"
wifi:
ssid: "xxxxxxxxx"
password: "xxxxxxxxx"
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Hd3100 Fallback Hotspot"
password: "rVIShU1IeE06"
captive_portal:
web_server:
port: 80
uart:
tx_pin: GPIO1 # 对应D10
rx_pin: GPIO3 # 对应D9
baud_rate: 9600
id: uart_bus
globals:
- id: amplifier_status
type: bool
restore_value: no
initial_value: 'false'
- id: signal_source
type: std::string
restore_value: no
initial_value: '"HDMI1"'
- id: volume_level
type: float
restore_value: no
initial_value: '50.0'
switch:
- platform: template
name: "Amplifier Power"
lambda: |-
return id(amplifier_status);
turn_on_action:
- uart.write: "##POWER ON*"
- lambda: |-
id(amplifier_status) = true;
turn_off_action:
- uart.write: "##POWER OFF*"
- lambda: |-
id(amplifier_status) = false;
- platform: template
name: "Set Signal Source to HDMI1"
turn_on_action:
- uart.write: "##SI 00*"
- lambda: |-
id(signal_source) = "HDMI1";
- platform: template
name: "Set Signal Source to HDMI2"
turn_on_action:
- uart.write: "##SI 01*"
- lambda: |-
id(signal_source) = "HDMI2";
- platform: template
name: "Set Signal Source to HDMI3"
turn_on_action:
- uart.write: "##SI 02*"
- lambda: |-
id(signal_source) = "HDMI3";
- platform: template
name: "Set Signal Source to HDMI4"
turn_on_action:
- uart.write: "##SI 03*"
- lambda: |-
id(signal_source) = "HDMI4";
- platform: template
name: "Set Signal Source to HDMI5"
turn_on_action:
- uart.write: "##SI 04*"
- lambda: |-
id(signal_source) = "HDMI5";
- platform: template
name: "Set Signal Source to HDMI6"
turn_on_action:
- uart.write: "##SI 05*"
- lambda: |-
id(signal_source) = "HDMI6";
binary_sensor:
- platform: status
name: "Amplifier Connectivity"
text_sensor:
- platform: template
name: "Current Signal Source"
lambda: |-
return {id(signal_source)};
id: current_signal_source
sensor:
- platform: template
name: "Current Volume"
lambda: |-
return id(volume_level);
id: current_volume
interval:
- interval: 10s
then:
- logger.log:
format: "Sending status requests to amplifier"
level: DEBUG
tag: main
- uart.write: "##POWER ?*"
- delay: 500ms
- uart.write: "##SI ?*"
- delay: 500ms
- uart.write: "##VOL ?*"
- interval: 1s
then:
- lambda: |-
static std::string received = "";
while (id(uart_bus).available()) {
uint8_t byte;
id(uart_bus).read_byte(&byte);
char c = static_cast<char>(byte);
if (c == '*') {
// Process complete command
ESP_LOGD("uart", "Received: %s", received.c_str());
if (received.find("#POWER ON") != std::string::npos) {
id(amplifier_status) = true;
ESP_LOGD("uart", "Amplifier turned ON");
} else if (received.find("#POWER OFF") != std::string::npos) {
id(amplifier_status) = false;
ESP_LOGD("uart", "Amplifier turned OFF");
} else if (received.find("#SI ") != std::string::npos) {
// Process signal source status
size_t pos_index = received.find("#SI ") + 4;
size_t pos_end = received.find(" ", pos_index);
std::string source_number = received.substr(pos_index, pos_end - pos_index);
std::string source_name;
if (source_number == "00") {
source_name = "HDMI1";
} else if (source_number == "01") {
source_name = "HDMI2";
} else if (source_number == "02") {
source_name = "HDMI3";
} else if (source_number == "03") {
source_name = "HDMI4";
} else if (source_number == "04") {
source_name = "HDMI5";
} else if (source_number == "05") {
source_name = "HDMI6";
} else {
source_name = "UNKNOWN";
}
id(signal_source) = source_name;
ESP_LOGD("uart", "Signal source name: %s", source_name.c_str());
// Update Home Assistant state
id(current_signal_source).publish_state(source_name);
} else if (received.find("#VOL ") != std::string::npos) {
// Process volume level
size_t pos_index = received.find("#VOL ") + 5;
size_t pos_end = received.find("*", pos_index);
std::string volume_str = received.substr(pos_index, pos_end - pos_index);
float volume = std::stof(volume_str);
id(volume_level) = volume;
ESP_LOGD("uart", "Volume level: %f", volume);
// Update Home Assistant state
id(current_volume).publish_state(volume);
}
// Clear the received string for the next command
received.clear();
} else {
received += c;
}
}
|
|