-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmi_band.yaml
145 lines (130 loc) · 5.04 KB
/
mi_band.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
substitutions:
mi_band_mac: !secret mi_band_mac
mi_band_name_prefix: Mi Band
mi_band_id_prefix: mi_band
esp32_ble_tracker:
on_ble_service_data_advertise:
- mac_address: ${mi_band_mac}
service_uuid: 'FEE0'
then:
- lambda: |-
if (x.size() != 4)
ESP_LOGE("esp32_ble_tracker", "Payload has wrong size (%d)", x.size());
else
id(${mi_band_id_prefix}_steps).publish_state((uint32_t)(x[3] << 24) | (uint32_t)(x[2] << 16) | (uint32_t)(x[1] << 8) | (uint32_t)(x[0]));
on_ble_manufacturer_data_advertise:
- mac_address: ${mi_band_mac}
manufacturer_id: '0157'
then:
- lambda: |-
if (x.size() != 24)
ESP_LOGE("esp32_ble_tracker", "Payload has wrong size (%d)", x.size());
else
if (x[3] != 0xFF)
id(${mi_band_id_prefix}_heart_rate).publish_state(x[3]);
myhomeiot_ble_host:
myhomeiot_ble_client:
- mac_address: ${mi_band_mac}
service_uuid: 'FEE0'
characteristic_uuid: '00000006-0000-3512-2118-0009AF100700' # Mi Band 2/3/4/6
# characteristic_uuid: '0000FF0C-0000-1000-8000-00805F9B34FB' # Mi Band 1
update_interval: 6h
on_value:
then:
lambda: |-
// https://github.com/Freeyourgadget/Gadgetbridge/blob/ef46035900f5fadb7403b2419c7b58e4ab94219e/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiBatteryInfo.java#L26
// Broken-down Time: https://www.gnu.org/software/libc/manual/html_node/Broken_002ddown-Time.html
const time_t J2000 = 946684800UL; // Jan 01 2000 12:00:00 UTC
switch (x.size()) {
case 10: { // Mi Band 1
id(${mi_band_id_prefix}_battery).publish_state(x[0]);
id(${mi_band_id_prefix}_battery_state).publish_state(x[9] == 2);
struct tm c_tm = {x[6], x[5], x[4], x[3], x[2], (uint16_t) (2000 + x[1]) - 1900, 0, 0, -1};
time_t time = mktime(&c_tm);
if (time > J2000)
id(${mi_band_id_prefix}_last_charging_timestamp).publish_state(time);
break;
}
case 20: { // Mi Band 2/3/4/6
if (x[0] != 0x0F) {
ESP_LOGE("myhomeiot_ble_client", "Payload has wrong signature (0x%02X)", x[0]);
return;
};
id(${mi_band_id_prefix}_battery).publish_state(x[1]);
id(${mi_band_id_prefix}_battery_state).publish_state(x[2] == 1);
struct tm c_tm = {x[9], x[8], x[7], x[6], x[5] - 1, (uint16_t) (x[4] << 8 | x[3]) - 1900, 0, 0, -1};
time_t time = mktime(&c_tm);
if (time > J2000)
id(${mi_band_id_prefix}_full_charging_timestamp).publish_state(time);
c_tm = {x[17], x[16], x[15], x[14], x[13] - 1, (uint16_t) (x[12] << 8 | x[11]) - 1900, 0, 0, -1};
time = mktime(&c_tm);
if (time > J2000) {
id(${mi_band_id_prefix}_last_charging_timestamp).publish_state(time);
id(${mi_band_id_prefix}_battery_last_charging).publish_state(x[19]);
}
break;
};
default:
ESP_LOGE("myhomeiot_ble_client", "Payload has wrong size (%d)", x.size());
}
binary_sensor:
- platform: ble_presence
mac_address: ${mi_band_mac}
name: ${mi_band_name_prefix}
device_class: presence
- platform: template
id: ${mi_band_id_prefix}_battery_state
name: ${mi_band_name_prefix} Battery State
device_class: battery_charging
sensor:
- platform: ble_rssi
mac_address: ${mi_band_mac}
name: ${mi_band_name_prefix} RSSI
filters:
- median:
window_size: 7
send_every: 4
send_first_at: 3
- platform: template
id: ${mi_band_id_prefix}_battery
name: ${mi_band_name_prefix} Battery
device_class: battery
unit_of_measurement: '%'
state_class: measurement
accuracy_decimals: 0
- platform: template
id: ${mi_band_id_prefix}_full_charging_timestamp
name: ${mi_band_name_prefix} Full Charging Timestamp
device_class: timestamp
icon: mdi:battery-clock
accuracy_decimals: 0
disabled_by_default: true
- platform: template
id: ${mi_band_id_prefix}_last_charging_timestamp
name: ${mi_band_name_prefix} Last Charging Timestamp
device_class: timestamp
icon: mdi:battery-clock-outline
accuracy_decimals: 0
disabled_by_default: true
- platform: template
id: ${mi_band_id_prefix}_battery_last_charging
name: ${mi_band_name_prefix} Battery Last Charging
icon: mdi:power-plug-battery-outline
unit_of_measurement: '%'
state_class: measurement
accuracy_decimals: 0
disabled_by_default: true
- platform: template
id: ${mi_band_id_prefix}_steps
name: ${mi_band_name_prefix} Steps
icon: mdi:walk
unit_of_measurement: steps
state_class: measurement
accuracy_decimals: 0
- platform: template
id: ${mi_band_id_prefix}_heart_rate
name: ${mi_band_name_prefix} Heart Rate
icon: mdi:heart-pulse
unit_of_measurement: bpm
state_class: measurement
accuracy_decimals: 0