---
substitutions:
  device: gateway
  name: Gateway
  area: DEV
  comment: BLE Proxy, Modbus

esphome:
  name: mcu-${device}
  friendly_name: ${name}
  area: ${area}
  comment: ${comment}

esp32:
  variant: esp32
  toolchain: esp-idf
  framework:
    type: esp-idf
    advanced:
      minimum_chip_revision: 3.0
      sram1_as_iram: true

external_components:
  # Ethernet LLDP 802.1AB transmitter
  - source: github://pr#11760
    components: [ethernet]
    refresh: 1h
  - source: github://pr#2112
    components: ["m5stack_4relay"]
    refresh: 1h

packages:
  # common: !include common/common.yaml
  common_eth: !include common/common_eth.yaml

## UEXT
#    3V3    1 2   GND
#  GPIO4    3 4   GPIO36  U1TXD/U1RXD
# GPIO16    5 6   GPIO13  I2C-SCL/I2C-SDA
# GPIO15    7 8   GPIO2   HS2_CMD/HS2_DATA0
# GPIO14    9 10  GPIO5   HS2_CLK/SPI_CS
#
# SD/MMC; shared with UEXT, no CS!!!
# GPIO15  HS2_CMD
# GPIO14  HS2_CLK
# GPIO2   HS2_DATA0

# GPIO34  BUT1
# GPIO35  Battery Measurment ADC
# GPIO39  Ext Power Sense ADC

i2c:
  sda: 13
  scl: 16
  scan: true

ethernet:
  lldp:
    # Enable LLDP transmission
    #enabled: true
    # Optional: override default port
    port: eth
    # Optional: override default system name
    system_name: mcu-${device}
    # Optional: override default system description
    system_description: ${comment}
    # Optional: override default "fast mode" burst packet count
    tx_fast_count: 4
    # Optional: override default LLDP frame transmission interval
    tx_interval: 30
    # Optional: override default hold count, used in conjuction with tx_interval to calculate overall TTL
    tx_hold: 4

# ethernet:
#   type: LAN8720
#   mdc_pin: 23
#   mdio_pin: 18
#   clk_mode: GPIO17_OUT
#   phy_addr: 0
#   power_pin: 12
# use_address: 10.17.240.245

# # Optional manual IP
# manual_ip:
#   static_ip: 10.17.240.254
#   gateway: 10.17.240.1
#   subnet: 255.255.255.0


# ethernet:
# use_address: 10.17.241.254
# use_address: 10.17.241.98
# use_address: mcu-gateway.local

uart:
  id: uart_modbus
  tx_pin: 4
  rx_pin: 36
  baud_rate: 9600
  # stop_bits: 1
  # data_bits: 8
  # parity: NONE
  # debug:

modbus:
  id: modbus_id

modbus_controller:
  - id: modbus_temp_1
    address: 101
    modbus_id: modbus_id
    # update_interval: 10s
  - id: modbus_temp_2
    address: 102
    modbus_id: modbus_id
    # update_interval: 10s
  - id: modbus_adc_1
    address: 1
    modbus_id: modbus_id
    update_interval: 10s

sensor:
  - platform: modbus_controller
    modbus_controller_id: modbus_temp_1
    id: temp_1
    name: Temperature 1
    address: 0
    unit_of_measurement: "°C"
    register_type: read
    value_type: U_WORD
    accuracy_decimals: 1
    register_count: 1
    filters:
      - multiply: 0.1
  - platform: modbus_controller
    modbus_controller_id: modbus_temp_1
    id: hum_1
    name: Humidity 1
    address: 1
    unit_of_measurement: "%"
    register_type: read
    value_type: U_WORD
    accuracy_decimals: 1
    filters:
      - multiply: 0.1
  - platform: modbus_controller
    modbus_controller_id: modbus_temp_2
    id: temp_2
    name: Temperature 2
    address: 0
    unit_of_measurement: "°C"
    register_type: read
    value_type: U_WORD
    accuracy_decimals: 1
    register_count: 1
    filters:
      - multiply: 0.1
  - platform: modbus_controller
    modbus_controller_id: modbus_temp_2
    id: hum_2
    name: Humidity 2
    address: 1
    unit_of_measurement: "%"
    register_type: read
    value_type: U_WORD
    accuracy_decimals: 1
    filters:
      - multiply: 0.1
  # - platform: bmp085
  #   temperature:
  #     name: BMP180 Temperature
  #   pressure:
  #     name: BMP180 Pressure
  #   address: 0x77
  #   update_interval: 60s
  # - platform: bme280
  #   temperature:
  #     name: BME280 Temperature
  #     oversampling: 16x
  #   pressure:
  #     name: BME280 Pressure
  #   humidity:
  #     name: BME280 Humidity
  #   address: 0x76
  #   update_interval: 60s
  - platform: modbus_controller
    modbus_controller_id: modbus_adc_1
    id: adc_1_1
    name: Solar Pressure
    address: 0
    unit_of_measurement: "MPa"
    register_type: holding
    value_type: U_WORD
    accuracy_decimals: 1
    register_count: 2
    filters:
      - calibrate_linear:
          - 50 -> 0
          - 450 -> 5

switch:
  - platform: modbus_controller
    modbus_controller_id: modbus_adc_1
    name: Readress MODBUS
    register_type: holding
    address: 0x00FD
    entity_category: config
    icon: "mdi:toggle-switch"

esp32_ble_tracker:
  scan_parameters:
    interval: 1100ms
    window: 1100ms
    active: true
  on_ble_advertise:
    - mac_address:
        - D5:63:3C:12:E6:07
      then:
        - lambda: |-
            ESP_LOGV("ble_adv", "Detected HPA250B");

bluetooth_proxy:
  active: true

binary_sensor:
  - platform: gpio
    pin:
      number: 34
      inverted: true
    id: user_button
    on_press:
      then:
        - button.press: safe

button:
  - platform: safe_mode
    id: safe

# display:
#   - platform: lcd_pcf8574
#     dimensions: 20x4
#     address: 0x27
#     lambda: |-
#       it.print(0, 3, "10°C or 10~C");
