Connect Shelly BLU Door & Window directly to ESPHome driven ESP32 via Bluetooth

Initial version of BTHome component is now available at esphome/esphome/components/bthome at dev · badrpc/esphome · GitHub. It can be added as an external component:

external_components:
  - source:
      type: git
      url: https://github.com/badrpc/esphome
      ref: v0
    components: [ bthome ]

And then configured in the following way:

esp32_ble_tracker:

bthome:
  - id: blu_door_window
    mac_address: AA:BB:CC:DD:EE:FF # MAC-address of BLU sensor
    encryption_key: "00112233445566778899AABBCCDDEEFF" # 128-bit key

binary_sensor:
  - platform: bthome
    id: blu_door_window
    window:
      id: test_blu_window
      name: "Test blu window"
      # on_press:
      #   then:
      #     - switch.turn_on: relay
      # on_release:
      #   then:
      #     - switch.turn_off: relay

sensor:
  - platform: bthome
    id: blu_door_window

    angle:
      id: test_blu_angle
      name: "Test blu angle"

    battery_level:
      id: test_blu_battery_level
      name: "Test blu battery level"
      entity_category: "diagnostic"

    illuminance:
      id: test_blu_illuminance
      name: "Test blu illuminance"

If encryption_key is not set component will decode unencrypted packets. With encryption key set it will ignore unencrypted packets. It is possible to enable encryption and obtain the key with the Shelly BLE Debug app (I used Android version, I believe there should be an IOS version too). After enabling encryption the app showed “Device found” message with json dump of device parameters, including encryption key.

On the lower level encryption key can be read from eb0fb41b-af4b-4724-a6f9-974f55aba81a UUID but this can only be done from a paired device (Encryption | Shelly Technical Documentation). Technically it is possible to enable encryption and read key directly in ESPHome and I have a proof of concept code to do that but it’s not in this component.

Currently this component only supports Shelly BLU door/window sensor. I would like to make this component a generic BTHome decoder/parser. This will probably mean significant change of the configuration format in the future to make it more generic.

There’s currently the same drawback as esp32_ble_tracker - after rebooting ESP state of the BLU device will be lost until it send next advertisement. This can be further improved by making ESPHome send an explicit query to BLU device after booting but this (as well as enabling encryption and reading a key) is not defined in BTHome standard and rather is specific to BLU devices. Possible direction for future work - a dedicated component based on bthome to fully support BLU devices (pairing, encryption management, refresh state on boot, device specific configuration).