ble_monitor icon indicating copy to clipboard operation
ble_monitor copied to clipboard

Allow proxy HCI packets to Home Assistant Bluetooth stack

Open myhomeiot opened this issue 2 months ago • 2 comments

This change will allow to proxy all or some packets (like Apple BTLE continuity and beacon's) into Home Assistant bluetooth stack using ESPHome BLE Gateway and use it for tracking devices over Private BLE Device and Bermuda BLE Trilateration integrations.

If you will proxy all packets it's will looks like you are using ESPHome Bluetooth Proxy component except possibility to make connection from Home Assistant to devices.

Home Assistant config:

  - alias: ESPHome BLE Advertise
    mode: queued
    trigger:
      - platform: event
        event_type: esphome.on_ble_advertise
    action:
      - service: ble_monitor.parse_data
        data: "{{ trigger.event.data }}"

or

      - service: ble_monitor.parse_data
        data:
          packet: "{{ trigger.event.data.packet }}"
          device_id: "{{ trigger.event.data.device_id if trigger.event.data.device_id is defined else None }}"
          gateway_id: "{{ trigger.event.data.gateway_id if trigger.event.data.gateway_id is defined else 'unknown' }}"
          proxy: "{{ trigger.event.data.proxy if trigger.event.data.proxy is defined else false }}"

ESPHome configuration for proxy Apple BTLE continuity and beacon's packets:

esp32_ble_tracker:
  on_ble_advertise:
    then:
      - if:
          condition:
            lambda: |-
              static const esp32_ble_tracker::ESPBTUUID apple_manufacturer_id = esp32_ble_tracker::ESPBTUUID::from_uint16(0x004C);
              for (auto data : x.get_manufacturer_datas())
                if (data.uuid == apple_manufacturer_id)
                  return true;
              return false;
          then:
            - homeassistant.event:
                event: esphome.on_ble_advertise
                data:
                  packet: !lambda return ble_gateway::BLEGateway::scan_result_to_hci_packet_hex(x.get_scan_result());
                  gateway_id: ${device_id}
                  proxy: !lambda return true;
#            - lambda: |-
#                ESP_LOGD("Proxy", "[%s] Packet %s", x.address_str().c_str(),
#                  ble_gateway::BLEGateway::scan_result_to_hci_packet_hex(x.get_scan_result()).c_str());

ESPHome configuration for proxy all BLE Advertisement packets:

esp32_ble_tracker:
  on_ble_advertise:
    then:
      - homeassistant.event:
          event: esphome.on_ble_advertise
          data:
            packet: !lambda return ble_gateway::BLEGateway::scan_result_to_hci_packet_hex(x.get_scan_result());
            gateway_id: ${device_id}
            proxy: !lambda return true;

myhomeiot avatar May 04 '24 10:05 myhomeiot