myhomeiot / esphome-components

A collection of my ESPHome components
Other
257 stars 25 forks source link

Mi Flora Battery value doesn't get passed to BLE Monitor #23

Open mihsu81 opened 1 year ago

mihsu81 commented 1 year ago

Hi @myhomeiot,

Currently the battery percentage doesn't get passed to BLE Monitor. The last time it did, seems to have been when I was using the arduino framework, a few months back. Unless I'm wrong and something changed in BLE Monitor. The other values are getting passed just fine. Tried with both "ble_monitor.parse_data" and "esphome.on_ble_advertise". Any ideea why it might happen? Thanks in advance for your help.

Here are the logs:

[15:30:35][I][app:102]: ESPHome version 2023.3.0 compiled on Mar 17 2023, 15:28:06
[15:30:58][I][myhomeiot_ble_client:032]: [C4:7C:8D:6D:3D:92] Connecting
[15:31:00][I][myhomeiot_ble_client:087]: [C4:7C:8D:6D:3D:92] Connected successfully, app_id (4)
[15:31:00][I][myhomeiot_ble_client:139]: Battery (72%), firmware (3.3.5)
[15:31:00][I][myhomeiot_ble_client:042]: [C4:7C:8D:6D:3D:92] Disconnecting
[15:31:00][I][esp-idf:000]: W (36256) BT_HCI: hci cmd send: disconnect: hdl 0x0, rsn:0x13

[15:31:00][I][esp-idf:000]: W (36263) BT_APPL: gattc_conn_cb: if=3 st=0 id=3 rsn=0x16

[15:31:00][I][esp-idf:000]: W (36268) BT_APPL: gattc_conn_cb: if=4 st=0 id=4 rsn=0x16

[15:31:00][I][esp-idf:000]: W (36273) BT_APPL: gattc_conn_cb: if=5 st=0 id=5 rsn=0x16

[15:31:00][I][esp-idf:000]: W (36276) BT_APPL: gattc_conn_cb: if=6 st=0 id=6 rsn=0x16

[15:31:00][W][api.connection:083]: Home Assistant 2023.3.5 (192.168.45.201): Connection closed
[15:31:00][I][esp-idf:000]: W (36316) BT_HCI: hcif disc complete: hdl 0x0, rsn 0x16

[15:31:30][I][myhomeiot_ble_client:032]: [C4:7C:8D:6D:47:9D] Connecting
[15:31:31][I][myhomeiot_ble_client:087]: [C4:7C:8D:6D:47:9D] Connected successfully, app_id (4)
[15:31:31][I][myhomeiot_ble_client:163]: Battery (95%), firmware (3.3.5)
[15:31:31][I][myhomeiot_ble_client:042]: [C4:7C:8D:6D:47:9D] Disconnecting
[15:31:31][I][esp-idf:000]: W (67372) BT_HCI: hci cmd send: disconnect: hdl 0x0, rsn:0x13

[15:31:31][I][esp-idf:000]: W (67384) BT_APPL: gattc_conn_cb: if=3 st=0 id=3 rsn=0x16

[15:31:31][I][esp-idf:000]: W (67388) BT_APPL: gattc_conn_cb: if=4 st=0 id=4 rsn=0x16

[15:31:31][I][esp-idf:000]: W (67391) BT_APPL: gattc_conn_cb: if=5 st=0 id=5 rsn=0x16

[15:31:31][I][esp-idf:000]: W (67396) BT_APPL: gattc_conn_cb: if=6 st=0 id=6 rsn=0x16

[15:31:32][W][api.connection:083]: Home Assistant 2023.3.5 (192.168.45.201): Connection closed
[15:31:32][I][esp-idf:000]: W (67427) BT_HCI: hcif disc complete: hdl 0x0, rsn 0x16

image image

Here's my config:

external_components:
  - source: github://myhomeiot/esphome-components

substitutions:
  devicename: ble_gateway04
  friendly_name: BLE-GATEWAY04
  gateway_id: ble_gateway04

esphome:
  name: ble-gateway04
  friendly_name: ${friendly_name}

esp32:
  board: mhetesp32minikit
  framework:
    type: esp-idf
    sdkconfig_options:
      CONFIG_BT_BLE_42_FEATURES_SUPPORTED: y
      CONFIG_ESP_TASK_WDT: y
      CONFIG_ESP_TASK_WDT_TIMEOUT_S: "10"

preferences:
  flash_write_interval: 1min

logger:
  level: INFO

api:
  reboot_timeout: 1h
  encryption:
    key: !secret api_encryption_key

ota:
  safe_mode: true
  password: !secret ota_password

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  ap:
    ssid: "${friendly_name} Fallback"
    #password: !secret esphome_fallback_password

time:
  - platform: homeassistant
    id: homeassistant_time

mdns:
  disabled: false

esp32_ble:

esp32_ble_tracker:

bluetooth_proxy:
  active: true
  cache_services: true

ble_gateway:
  id: $gateway_id
  discovery: true
  devices:
    - mac_address: A4:C1:38:75:D6:49
    - mac_address: A4:C1:38:2E:C1:14
    - mac_address: A4:C1:38:97:A6:7D
    - mac_address: 8C:F6:81:A0:1F:ED
    - mac_address: 8C:F6:81:91:BF:C8
    - mac_address: 8C:F6:81:A0:37:2A
    - mac_address: 8C:F6:81:91:D0:20
    - mac_address: 2C:11:65:23:9A:5B
    - mac_address: C4:7C:8D:6D:30:EE
    - mac_address: C4:7C:8D:6E:1A:78
    - mac_address: C4:7C:8D:6D:47:9F
    - mac_address: C4:7C:8D:6E:1A:67
    - mac_address: C4:7C:8D:6D:44:D3
    - mac_address: C4:7C:8D:6D:3D:92
    - mac_address: C4:7C:8D:6D:30:89
    - mac_address: C4:7C:8D:6D:30:A8
    - mac_address: C4:7C:8D:6D:30:FD
    - mac_address: C4:7C:8D:6E:1D:EF
    - mac_address: C4:7C:8D:6D:30:87
    - mac_address: C4:7C:8D:6D:3D:8D
    - mac_address: C4:7C:8D:6D:30:86
    - mac_address: C4:7C:8D:6D:30:6E
    - mac_address: C4:7C:8D:6D:30:70
    - mac_address: C4:7C:8D:6E:19:8C
    - mac_address: C4:7C:8D:6D:47:9D
    - mac_address: C4:7C:8D:6D:30:D2
    - mac_address: C4:7C:8D:6E:1A:66
    - mac_address: C4:7C:8D:6D:3D:7F
    - mac_address: C4:7C:8D:6E:1E:B0
    - mac_address: 68:AB:BC:4F:E5:C7
    - mac_address: 68:AB:BC:4F:0A:56
    - mac_address: B4:60:ED:F3:3A:0B
    - mac_address: EC:4D:3E:BE:A7:6D
    - mac_address: ED:DE:34:3F:48:0C
    - mac_address: EF:A8:A8:06:8C:E6
    - mac_address: 0C:95:41:D2:D8:B0
    - mac_address: F6:92:88:5A:32:DF
    - mac_address: EC:6B:41:D1:6B:F8
    - mac_address: D8:71:4D:6A:7D:FE
    - mac_address: D0:F0:18:44:1A:DE
    - mac_address: D0:F0:18:44:1A:E2
    - mac_address: D0:F0:18:78:0D:08
  on_ble_advertise:
    then:
      homeassistant.service:
        service: ble_monitor.parse_data
        data:
          packet: !lambda return packet;
          gateway_id: $gateway_id
#    then
#      homeassistant.event:
#        event: esphome.on_ble_advertise
#        data:
#          packet: !lambda return packet;
#          gateway_id: $gateway_id

myhomeiot_ble_host:

myhomeiot_ble_client:
  - mac_address: C4:7C:8D:6D:3D:92 #Ficus Microcarpa Bonsai
    service_uuid: '1204'
    characteristic_uuid: '1A02'
    update_interval: 24h
    on_value:
      then:
        homeassistant.event:
          event: esphome.on_ble_advertise
#        homeassistant.service:
#          service: ble_monitor.parse_data
          data:
            packet: !lambda |-
              if (x.size() < 2)
              {
                ESP_LOGE("myhomeiot_ble_client", "payload has wrong size (%d)", x.size());
                return "";
              };
              ESP_LOGI("myhomeiot_ble_client", "Battery (%d%%), firmware (%s)", x[0], std::string(x.begin(), x.end()).substr(2).c_str());
              char buffer[70 + 1];
              const uint8_t *remote_bda = xthis.remote_bda();
              snprintf(buffer, sizeof(buffer), "043E2002010000%02X%02X%02X%02X%02X%02X14020106030295FE0C1695FE41209800000A1001%02X00",
                remote_bda[5], remote_bda[4], remote_bda[3], remote_bda[2], remote_bda[1], remote_bda[0], x[0]);
              return std::string(buffer).c_str();
            gateway_id: $gateway_id
  - mac_address: C4:7C:8D:6D:47:9D #Tradescantia Pallida
    service_uuid: '1204'
    characteristic_uuid: '1A02'
    update_interval: 24h
    on_value:
      then:
        homeassistant.event:
          event: esphome.on_ble_advertise
#        homeassistant.service:
#          service: ble_monitor.parse_data
          data:
            packet: !lambda |-
              if (x.size() < 2)
              {
                ESP_LOGE("myhomeiot_ble_client", "payload has wrong size (%d)", x.size());
                return "";
              };
              ESP_LOGI("myhomeiot_ble_client", "Battery (%d%%), firmware (%s)", x[0], std::string(x.begin(), x.end()).substr(2).c_str());
              char buffer[70 + 1];
              const uint8_t *remote_bda = xthis.remote_bda();
              snprintf(buffer, sizeof(buffer), "043E2002010000%02X%02X%02X%02X%02X%02X14020106030295FE0C1695FE41209800000A1001%02X00",
                remote_bda[5], remote_bda[4], remote_bda[3], remote_bda[2], remote_bda[1], remote_bda[0], x[0]);
              return std::string(buffer).c_str();
            gateway_id: $gateway_id

binary_sensor:
  - platform: status
    name: "${friendly_name} Status"

  - platform: homeassistant
    id: ${devicename}_discovery
    name: ${friendly_name} Discovery
    entity_id: binary_sensor.${devicename}
    attribute: discovery
    on_state:
      then:
        lambda: id($gateway_id).set_discovery(x);

text_sensor:
  - platform: homeassistant
    id: ${devicename}_devices
    name: ${friendly_name} Devices
    entity_id: binary_sensor.${devicename}
    attribute: devices
    on_value:
      then:
        lambda: id($gateway_id).set_devices(x);

  - platform: wifi_info
    ip_address:
      name: ${friendly_name} IP Address
    ssid:
      name: ${friendly_name} Connected SSID
    bssid:
      name: ${friendly_name} Connected BSSID
    mac_address:
      name: ${friendly_name} MAC Wifi Address

  - platform: version
    name: ${friendly_name} ESPHome Version

switch:
  - platform: template
    id: switch_${devicename}_discovery
    name: ${friendly_name} Discovery
    icon: mdi:bluetooth-connect
    lambda: return id($gateway_id).get_discovery();
    turn_on_action: [lambda: id($gateway_id).set_discovery(true);]
    turn_off_action: [lambda: id($gateway_id).set_discovery(false);]
    disabled_by_default: true
    entity_category: config

button:
  - platform: restart
    name: ${friendly_name} Restart
    id: ${devicename}_reset
    entity_category: diagnostic

  - platform: factory_reset
    name: ${friendly_name} Factory Reset
    id: ${devicename}_fatory_reset
    internal: true
    entity_category: diagnostic

  - platform: safe_mode
    name: ${friendly_name} Safe Mode Boot
    id: ${devicename}_safe_mode_boot
    entity_category: diagnostic

sensor:
  - platform: uptime
    name: ${friendly_name} Device Uptime
    id: ${devicename}_device_uptime

  - platform: wifi_signal
    name: ${friendly_name} Wifi Signal
    id: ${devicename}_wifi_signal
    update_interval: 60s

  - platform: internal_temperature
    name: ${friendly_name} Internal Temperature
    id: ${devicename}_internal_temperature
    update_interval: 60s