esphome:
  name: esp360remote

esp32:
  board: esp32doit-devkit-v1
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
# Services available for Home Assistant automation and SmartIR
api:
  services:
    - service: send_ir_raw
      variables:
        command: int[]
      then:
        - remote_transmitter.transmit_raw:
            transmitter_id: IR_TX
            code: !lambda 'return command;'
    - service: send_rf_raw
      variables:
        protocol_number: int
        raw_data: string
      then:
        - remote_transmitter.transmit_rc_switch_raw:
            transmitter_id: RF_TX
            protocol: !lambda 'return  esphome::remote_base::RC_SWITCH_PROTOCOLS[protocol_number];'
            code: !lambda 'return raw_data;'

ota:
  password: ""

wifi:
  ssid: "nossid"
  password: "nopassword"

  #HotSpot when it can't connect to WiFi
  ap:
    ssid: "Esp360Remote"
    password: "Esp360Remote"

captive_portal:
    
#i2c bus definition
i2c:
  sda: 21
  scl: 22
  scan: true
  id: bus_a

#Offsets have been added to the measurements taken. 
#These are necessary due to the heat generated by the PCB, the second layer of the board, and the case. 
#This way, sufficiently accurate values are obtained for automations
sensor:
  - platform: shtcx
    temperature:
      name: "Living Room Temperature"
      filters:
        - offset: -4
    humidity:
      name: "Living Room Humidity"
      filters:
        - offset: +19.1   
    address: 0x70
    update_interval: 120s
  - platform: bh1750
    name: "BH1750 Illuminance"
    filters:
      - multiply: 5
    address: 0x23
    update_interval: 1s

#Setting up IR and RF receivers
remote_receiver:
  - id: RF_RX
    pin: 
      number: GPIO5
      inverted: True
    dump: 
      - rc_switch
    tolerance: 35%
    filter: 200us
    idle: 4ms
    buffer_size: 10kb
    on_rc_switch:
      - homeassistant.event:
          event: esphome.rf_code_received
          data:
            protocol: !lambda 'return x.protocol;'
            code: !lambda 'return x.code;' 
  - id: IR_RX
    pin: 
      number: GPIO19
      inverted: True
    dump: all
    on_raw:
       - homeassistant.event:
          event: esphome.ir_raw_code_received
          data:
            code: !lambda '
            reverse(x.begin(), x.end());
            std::string str = "";
            for (auto it = x.begin(); it != x.end(); ++it)
            {
              str += to_string(*it);
              str += ",";
              if (it != x.end() - 1) str += " ";
            }
            if (str.size() > 0)  str.erase( str.end() - 2);
            return esphome::to_string("[" + str + "]");'   
    on_pronto:
       - homeassistant.event:
          event: esphome.ir_pronto_code_received
          data:
            code: !lambda 'return x.data;'
    on_lg:
       - homeassistant.event:
          event: esphome.ir_lg_code_received
          data:
            data: !lambda 'return x.data;'
            nbits: !lambda 'return x.nbits;'
    on_panasonic:
       - homeassistant.event:
           event: esphome.ir_panasonic_code_received
           data:
            address: !lambda 'return x.address;'
            command: !lambda 'return x.command;'

#Setting up IR and RF transmitters
remote_transmitter:
  - id: RF_TX
    pin: GPIO18
    carrier_duty_percent: 100%
  - id: IR_TX
    pin: GPIO13
    carrier_duty_percent: 50%


web_server:
  port: 80

#Status LED 
switch:
  - platform: gpio
    pin: 12
    name: "Status Led"
    restore_mode: ALWAYS_ON
