Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Custom ESPHome firmware and Tuya integration
#35
(Today, 02:10 AM)admin Wrote: post your yaml file at here.

Code:
# Basic Config
esphome:
  name: kc868-a6-test
  friendly_name: KC868_A6_Test
#  platform: ESP32
#  board: esp32dev

external_components:
  - source:
      type: git
      url: https://github.com/hzkincony/esphome-tuya-iot
      ref: v1.2.1

esp32:
  board: esp32dev
  framework:
#    type: esp-idf
#    type: arduino
    type: arduino  # Важно изменить на arduino
    version: latest

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "mdhQnkxHuyHBBOp3c+vVZw1mijHvK6GlGVDXbwhzSMM="

mqtt:
  broker: !secret mqtt_host
  username: !secret mqtt_username
  password: !secret mqtt_password
  id: mqtt_client
 
ota:
  - platform: esphome
#    password: "5992f85d33c321fdc84443dbf6d1eab2"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Kc868-A6-Test Fallback Hotspot"
    password: "iuUkzi7OQNef"

captive_portal:
   

time:
  - platform: homeassistant
    id: homeassistant_time

# Example configuration entry for ESP32
i2c:
  sda: 4
  scl: 15
  scan: true
  id: bus_a

# Example configuration entry
pcf8574:
  - id: 'pcf8574_hub_out_1'  # for output channel 1-8
    address: 0x24

  - id: 'pcf8574_hub_in_1'  # for input channel 1-8
    address: 0x22

# Individual outputs
switch:
  - platform: gpio
    name: "a6-light1"
    id: light1
    on_turn_on:
      - lambda: !lambda |-
          id(tuya_iot_component).property_report("output1", true);
    on_turn_off:
      - lambda: !lambda |-
          id(tuya_iot_component).property_report("output1", false);
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 0
      mode: OUTPUT
      inverted: true

  - platform: gpio
    name: "a6-light2"
    id: light2
    on_turn_on:
      - lambda: !lambda |-
          id(tuya_iot_component).property_report("output2", true);
    on_turn_off:
      - lambda: !lambda |-
          id(tuya_iot_component).property_report("output2", false);
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 1
      mode: OUTPUT
      inverted: true
     
  - platform: gpio
    name: "a6-light3"
    id: light3
    on_turn_on:
      - lambda: !lambda |-
          id(tuya_iot_component).property_report("output3", true);
    on_turn_off:
      - lambda: !lambda |-
          id(tuya_iot_component).property_report("output3", false);
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 2
      mode: OUTPUT
      inverted: true
     
  - platform: gpio
    name: "a6-light4"
    id: light4
    on_turn_on:
      - lambda: !lambda |-
          id(tuya_iot_component).property_report("output4", true);
    on_turn_off:
      - lambda: !lambda |-
          id(tuya_iot_component).property_report("output4", false);
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 3
      mode: OUTPUT
      inverted: true
     
  - platform: gpio
    name: "a6-light5"
    id: light5
    on_turn_on:
      - lambda: !lambda |-
          id(tuya_iot_component).property_report("output5", true);
    on_turn_off:
      - lambda: !lambda |-
          id(tuya_iot_component).property_report("output5", false);
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 4
      mode: OUTPUT
      inverted: true
     
  - platform: gpio
    name: "a6-light6"
    id: light6
    on_turn_on:
      - lambda: !lambda |-
          id(tuya_iot_component).property_report("output6", true);
    on_turn_off:
      - lambda: !lambda |-
          id(tuya_iot_component).property_report("output6", false);
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 5
      mode: OUTPUT
      inverted: true
     

binary_sensor:
  - platform: gpio
    name: "a6-input1"
    pin:
      pcf8574: pcf8574_hub_in_1
      number: 0
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "a6-input2"
    pin:
      pcf8574: pcf8574_hub_in_1
      number: 1
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "a6-input3"
    pin:
      pcf8574: pcf8574_hub_in_1
      number: 2
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "a6-input4"
    pin:
      pcf8574: pcf8574_hub_in_1
      number: 3
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "a6-input5"
    pin:
      pcf8574: pcf8574_hub_in_1
      number: 4
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "a6-input6"
    pin:
      pcf8574: pcf8574_hub_in_1
      number: 5
      mode: INPUT
      inverted: true
#switch:
#  - platform: gpio
#    pin: 25
#    name: "switch test 1"
#    id: switch_test_1
#    on_turn_on:
#      - lambda: !lambda |-
#          id(tuya_iot_component).property_report("output1", true);
#    on_turn_off:
#      - lambda: !lambda |-
#          id(tuya_iot_component).property_report("output1", false);
#  - platform: gpio
#    pin: 26
#    name: "switch test 2"
#    id: switch_test_2
#    on_turn_on:
#      - lambda: !lambda |-
#          id(tuya_iot_component).property_report("output2", true);
#    on_turn_off:
#      - lambda: !lambda |-
#          id(tuya_iot_component).property_report("output2", false);


tuya_iot:
  id: tuya_iot_component
  product_id: xp***lm
  device_id: 26***3j
  device_secret: H***w
  region: eu # eu, us, eus, weu, in, cn
  # eu: Central Europe Data Center
  # us: US West Data Center
  # eus: US East Data Center
  # weu: Western Europe Data Center
  # in: India Data Center
  # cn: Chinese Data Center



  on_event:
    - event_name: property/set
      then:
        - lambda: !lambda |-
              // Append the required switch in switch_map to respond to commands issued by Tuya.
              static std::map<float, switch_::Switch*> switch_map {
                { 1, id(light1) },
                { 2, id(light2) },
                { 3, id(light3) },
                { 4, id(light4) },
                { 5, id(light5) },
                { 6, id(light6) },
              };

              bool is_target_output = false;
              bool output_state = false;
              int output_number = 0;
              // Modify this number 2 to match the number of switches in the above switch_map.
              for (int i = 1; i <= 6; i++) {
                String key = "output" + String(i);
                if (x.containsKey("data") && x["data"].containsKey(key.c_str())) {
                  is_target_output = true;
                  output_state = x["data"][key.c_str()];
                  output_number = i;
                  break;
                }
              }

              if (is_target_output) {
                auto iterator = switch_map.find(output_number);
                if (iterator != switch_map.end()) {
                  auto sw = iterator->second;
                  if (output_state) {
                    sw->turn_on();
                  } else {
                    sw->turn_off();
                  }
                }
              }

              bool is_target_all_on = false;
              bool all_on_state = false;

              if (x.containsKey("data") && x["data"].containsKey("all_on")) {
                is_target_all_on = true;
                all_on_state = x["data"]["all_on"];
              }

              if (is_target_all_on) {
                if (all_on_state) {
                  for(const auto& pair : switch_map) {
                    auto sw = pair.second;
                    sw->turn_on();
                  }
                }
              }

              bool is_target_all_off = false;
              bool all_off_state = false;

              if (x.containsKey("data") && x["data"].containsKey("all_off")) {
                is_target_all_off = true;
                all_off_state = x["data"]["all_off"];
              }

              if (is_target_all_off) {
                if (all_off_state) {
                  for(const auto& pair : switch_map) {
                    auto sw = pair.second;
                    sw->turn_off();
                  }
                }
              }
Reply


Messages In This Thread
RE: Custom ESPHome firmware and Tuya integration - by alexps70 - 10 hours ago

Forum Jump:


Users browsing this thread:
1 Guest(s)