# Remember to enable HA API (api.yaml) for the proxy to be available.

esp32:
  # Required for BLE.
  framework:
    type: esp-idf

packages:
  # Allow HomeAssistant to use the proxy.
  api: !include api.yaml

globals:
  - id: ble_scans
    type: int
    restore_value: no
    initial_value: '0'

esp32_ble_tracker:
  scan_parameters:
    # Recommended scan intervals for esphome.
    interval: 1100ms
    window: 1100ms
    active: true
  on_scan_end:
    - then:
      - sensor.template.publish:
          id: ble_scans_finished
          state: !lambda 'return ++id(ble_scans);'
      - script.execute: restart_after_delay

bluetooth_proxy:
  id: btproxy
  active: true
  connection_slots: 4 # recommended limit for ESP32 Ethernet

esp32_ble:
  max_connections: 4

script:
  - id: restart_after_delay
    mode: restart
    then:
      - delay: 15min
      - logger.log:
          level: ERROR
          format: "No recent successful Bluetooth scan, restarting ESP chip"
      - button.press: restart_button

sensor:
  - platform: template
    name: "BLE scans finished"
    id: ble_scans_finished
    state_class: "total_increasing"
    accuracy_decimals: 0
    entity_category: diagnostic
    icon: "mdi:bluetooth-settings"
