substitutions:
  devicename: daikin-airsensor-b1-study
  friendly_name: DAIKIN Air Sensor B1 Study
  device_description: DAIKIN Air Sensor BRY88AB151K

esphome:
  name: $devicename
  comment: ${device_description}
  project:
    name: "DAIKIN.BRY88AB151K"
    version: "BRY88AB151K"
  # name_add_mac_suffix: true
  includes:
    - "cm1106.h"
    - pm2005.h          # For PM2005 module
#    - pm2105.h          # For PM2105 module
    
esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "**********************"

ota:
  password: "**********************"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "$devicename"
    password: "*********************"

captive_portal:

web_server:
  port: 80
  version: 2

uart:
- id: cm1106_uart
  rx_pin: GPIO16
  tx_pin: GPIO17
  baud_rate: 9600

sensor:
- platform: wifi_signal
  name: "${friendly_name} Wifi Signal"
  update_interval: 30s
- platform: uptime
  name: "${friendly_name} Uptime"
- platform: custom
  lambda: |-
    auto cm1106Sensor = new CM1106Sensor(id(cm1106_uart), 10000);
    App.register_component(cm1106Sensor);
    return {cm1106Sensor};
  sensors:
  - name: "${friendly_name} CO2"
    id: daikinco2
    accuracy_decimals: 0
    unit_of_measurement: "ppm"
    device_class: carbon_dioxide
    on_value_range:                       #CO2 LED always on when CO2 is below 1000
      - below: 1000       
        then:
          - output.turn_on: ledco2
      - above: 1000                       #CO2 LED blink slowly when CO2 is above 1000 but below 2000
        below: 2000 
        then:
        - while:
            condition:
              sensor.in_range:
                id: daikinco2
                above: 1000
                below: 2000 
            then:
            - output.turn_on: ledco2
            - delay: 500ms
            - output.turn_off: ledco2
            - delay: 500ms
      - above: 2000                       #CO2 LED blink quickly when CO2 is above 2000
        then:
        - while:
            condition:
              sensor.in_range:
                id: daikinco2
                above: 2000
            then:
            - output.turn_on: ledco2
            - delay: 200ms
            - output.turn_off: ledco2
            - delay: 200ms

- platform: custom
  lambda: |-
    auto pmsensor = new pm2005();
    App.register_component(pmsensor);
    return {pmsensor->vpm1, pmsensor->vpm25, pmsensor->vpm10};
  sensors:
  - name: "${friendly_name} PM1"
    id: daikinpm1
    unit_of_measurement: μg/m³
    accuracy_decimals: 0
    device_class: PM1
  - name: "${friendly_name} PM2.5"
    id: daikinpm25
    unit_of_measurement: μg/m³
    accuracy_decimals: 0
    device_class: PM25
    on_value_range:                       #PM2.5 LED always on when PM2.5 is below 35
      - below: 35       
        then:
          - output.turn_on: ledpm25
      - above: 35                         #PM2.5 LED blink slowly when PM2.5 is above 35 but below 75
        below: 75 
        then:
        - while:
            condition:
              sensor.in_range:
                id: daikinpm25
                above: 35
                below: 75 
            then:
            - output.turn_on: ledpm25
            - delay: 500ms
            - output.turn_off: ledpm25
            - delay: 500ms
      - above: 75                         #PM2.5 LED blink quickly when PM2.5 is above 75
        then:
        - while:
            condition:
              sensor.in_range:
                id: daikinpm25
                above: 75
            then:
            - output.turn_on: ledpm25
            - delay: 200ms
            - output.turn_off: ledpm25
            - delay: 200ms
  - name: "${friendly_name} PM10"
    id: daikinpm10
    unit_of_measurement: μg/m³
    accuracy_decimals: 0
    device_class: PM10

- platform: hdc1080
  i2c_id: sgp30_bus
  temperature:
    name: "${friendly_name} Temperature"
    filters:
      - offset: -4.0
    id: temperature
  humidity:
    name: "${friendly_name} Humidity"
    filters:
      - offset: 6.0
    id: humidity
  update_interval: 30s
- platform: sgp30
  i2c_id: sgp30_bus
  eco2:
    name: "${friendly_name} eCO2"
    accuracy_decimals: 1
#    internal: true
  tvoc:
    name: "${friendly_name} TVOC"
    id: daikintvocppb
    filters:
#      - lambda: return x * 0.0023;        #ppd -> mg/m³
      - lambda: return x * 2.3;        #ppd -> μg/m³
      - exponential_moving_average:
          alpha: 0.1
          send_every: 10
          send_first_at: 1
#    unit_of_measurement: "mg/m³"
    unit_of_measurement: "μg/m³"
#    accuracy_decimals: 2
    accuracy_decimals: 0
    on_value_range:                       #TVOC LED always on when TVOC is below 35
      - below: 500       
        then:
          - output.turn_on: ledtvoc
      - above: 500                         #TVOC LED blink slowly when TVOC is above 35 but below 75
        below: 700 
        then:
        - while:
            condition:
              sensor.in_range:
                id: daikinpm25
                above: 500
                below: 700 
            then:
            - output.turn_on: ledtvoc
            - delay: 500ms
            - output.turn_off: ledtvoc
            - delay: 500ms
      - above: 700                         #TVOC LED blink quickly when TVOC is above 75
        then:
        - while:
            condition:
              sensor.in_range:
                id: daikinpm25
                above: 700
            then:
            - output.turn_on: ledtvoc
            - delay: 200ms
            - output.turn_off: ledtvoc
            - delay: 200ms
  compensation:
    temperature_source: temperature
    humidity_source: humidity
  store_baseline: yes
  address: 0x58
  update_interval: 1s

switch:
- platform: custom
  lambda: |-
    auto cm1106Calib = new CM1106CalibrateSwitch(id(cm1106_uart));
    App.register_component(cm1106Calib);
    return {cm1106Calib};
  switches:
  - id: calibration
    internal: true

status_led:
  pin: 
    number: 25
    inverted: True

button:
  - platform: restart
    name: "${friendly_name} Restart"
  - platform: template
    name: "${friendly_name} CO2 Calibration"
    on_press:
      then:
        - switch.turn_on: calibration

binary_sensor:
  - platform: gpio
    pin: GPIO34
    id: RESET
    internal: true
    on_press:  #按下背面RESET按钮后CO2指示灯亮，十分钟后将环境CO2浓度设置为基准400ppm并关闭CO2指示灯
      then:
        - output.turn_on: ledco2
        - delay: 10min
        - switch.turn_on: calibration
        - delay: 10s
        - output.turn_on: ledco2

i2c:
- id: PM25_bus
  sda: 23
  scl: 22
  scan: true
- id: sgp30_bus
  sda: 19
  scl: 21
  scan: true  
  
output:
  - platform: gpio  #DAIKIN Air Sensor CO2 LED
    pin: GPIO4
    id: ledco2
  - platform: gpio  #DAIKIN Air Sensor TVOC LED
    pin: GPIO32
    id: ledtvoc
  - platform: gpio  #DAIKIN Air Sensor PM2.5 LED
    pin: GPIO33
    id: ledpm25    
    
text_sensor:
  - platform: wifi_info
    ip_address:
      name: "${friendly_name} IP Address"      
      icon: "mdi:ip-outline"
    ssid:
      name: "${friendly_name} Connected SSID"
      icon: "mdi:router-wireless"
    mac_address:
      name: "${friendly_name} MAC Address"
      icon: "mdi:lan"
  - platform: version
    name: "${friendly_name} Version"    