# Basic Config
esphome:
  name: kc868-a8-web-dht-tuya-office
  platform: ESP32
  board: esp32dev

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

# Enable logging
logger:
  baud_rate: 0

api:
  encryption:
    key: "XQ+SVTDw/Sm5xSxxxxxRmUvDheVIdLex2ezXmQUQ="

ota:
  - platform: esphome
    password: "7cde83b5fxxxxxfc6938d9077"

ethernet:
  type: LAN8720
  mdc_pin: GPIO23
  mdio_pin: GPIO18
  clk_mode: GPIO17_OUT
  phy_addr: 0

i2c:
  sda: 4
  scl: 5
  scan: true
  id: bus_a



  # Add web server
web_server:
  port: 80
  auth:
    username: admin
    password: "admin"

text_sensor:
  - platform: homeassistant
    name: "Casa Inteligente"
    id: home_assistant_text
    entity_id: input_text.test


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


time:
  - platform: homeassistant
    id: homeassistant_time


# Individual outputs
switch:
  - platform: gpio
    name: "light1"
    id: light1
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 0
      mode: OUTPUT
      inverted: true
    restore_mode: RESTORE_DEFAULT_ON
    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
    name: "light2"
    id: light2
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 1
      mode: OUTPUT
      inverted: true
    restore_mode: RESTORE_DEFAULT_ON
    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);

  - platform: gpio
    name: "light3"
    id: light3
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 2
      mode: OUTPUT
      inverted: true
    restore_mode: RESTORE_DEFAULT_ON
    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);

  - platform: gpio
    name: "light4"
    id: light4
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 3
      mode: OUTPUT
      inverted: true
    restore_mode: RESTORE_DEFAULT_ON
    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);

  - platform: gpio
    name: "light5"
    id: light5
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 4
      mode: OUTPUT
      inverted: true
    restore_mode: RESTORE_DEFAULT_ON
    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);

  - platform: gpio
    name: "light6"
    id: light6
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 5
      mode: OUTPUT
      inverted: true
    restore_mode: RESTORE_DEFAULT_ON
    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);

  - platform: gpio
    name: "light7"
    id: light7
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 6
      mode: OUTPUT
      inverted: true
    restore_mode: RESTORE_DEFAULT_ON
    on_turn_on:
      - lambda: !lambda |-
          id(tuya_iot_component).property_report("output7", true);
    on_turn_off:
      - lambda: !lambda |-
          id(tuya_iot_component).property_report("output7", false);

  - platform: gpio
    name: "light8"
    id: light8
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 7
      mode: OUTPUT
      inverted: true
    restore_mode: RESTORE_DEFAULT_ON
    on_turn_on:
      - lambda: !lambda |-
          id(tuya_iot_component).property_report("output8", true);
    on_turn_off:
      - lambda: !lambda |-
          id(tuya_iot_component).property_report("output8", false);

binary_sensor:
  - platform: gpio
    name: "input1"
    on_press:
      then:
        - switch.toggle: light1
    pin:
      pcf8574: pcf8574_hub_in_1
      number: 0
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "input2"
    on_press:
      then:
        - switch.toggle: light2
    pin:
      pcf8574: pcf8574_hub_in_1
      number: 1
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "input3"
    on_press:
      then:
        - switch.toggle: light3
    pin:
      pcf8574: pcf8574_hub_in_1
      number: 2
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "input4"
    on_press:
      then:
        - switch.toggle: light4
    pin:
      pcf8574: pcf8574_hub_in_1
      number: 3
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "input5"
    on_press:
      then:
        - switch.toggle: light5
    pin:
      pcf8574: pcf8574_hub_in_1
      number: 4
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "input6"
    on_press:
      then:
        - switch.toggle: light6
    pin:
      pcf8574: pcf8574_hub_in_1
      number: 5
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "input7"
    on_press:
      then:
        - switch.toggle: light7
    pin:
      pcf8574: pcf8574_hub_in_1
      number: 6
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "input8"
    on_press:
      then:
        - switch.toggle: light8
    pin:
      pcf8574: pcf8574_hub_in_1
      number: 7
      mode: INPUT
      inverted: true


  - platform: gpio
    name: "a8-s3"
    pin:
      number: 32
      inverted: true

  - platform: gpio
    name: "a8-s4"
    pin:
      number: 33
      inverted: true

one_wire:
  - platform: gpio
    pin: GPIO14

sensor:
  - platform: dallas_temp
    name: "Temperatura QCC"
    id: temperatura_1
    address: 0x061xxxxxxf64ff28  # Endereço específico do sensor
    accuracy_decimals: 1  # Precisão do sensor em casas decimais
    update_interval: 30s  # Intervalo de atualização dos dados

  
  - platform: dht
    pin: GPIO13  # Ajuste para o pino que você está usando
    model: DHT22  # Substitua pelo modelo do seu sensor (DHT11, DHT22, AM2301, etc.)
    temperature:
      name: "Temperatura externa"
    humidity:
      name: "Umidade externa"
    update_interval: 30s  # Ajuste o intervalo de atualização conforme necessário

tuya_iot:
  id: tuya_iot_component
  product_id: 3zcaxxxxx2gpwyu
  device_id: 26cxxxxxx957aruk
  device_secret: xlsk4xxxxxqX32K
  region: us # 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) },
                { 7, id(light7) },
                { 8, id(light8) },
              };

              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 <= 8; 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();
                  }
                }
              }