-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmiot_explorer.cpp
162 lines (145 loc) · 5.57 KB
/
miot_explorer.cpp
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "esphome/core/log.h"
#include "esphome/core/application.h"
#include "miot_explorer.h"
namespace esphome {
namespace miot_explorer {
static const char *const TAG = "miot_explorer";
void MiotExplorer::dump_config() {
this->dump_config_(TAG, "Explorer");
ESP_LOGCONFIG(TAG, "Product ID: %04X", this->product_id_);
LOG_TEXT_SENSOR("", "Explorer", this);
LOG_SENSOR(" ", "Consumable", this->consumable_);
}
bool MiotExplorer::process_mibeacon(const miot::MiBeacon &mib) {
if (this->product_id_ == 0) {
this->product_id_ = mib.product_id;
this->publish_state(str_sprintf("%04X", this->product_id_));
}
return MiotListener::process_mibeacon(mib);
}
bool MiotExplorer::process_object_(const miot::BLEObject &obj) {
switch (obj.id) {
case miot::MIID_BATTERY:
case miot::MIID_MIAOMIAOCE_BATTERY_1003:
this->process_default_(obj);
break;
case miot::MIID_CONSUMABLE: {
auto consumable = obj.get_consumable();
if (consumable.has_value()) {
if (this->consumable_) {
this->consumable_->publish_state(*obj.get_consumable());
} else {
this->process_string_(obj.id, "Consumable", to_string(*obj.get_consumable()));
}
}
break;
}
case miot::MIID_PAIRING_EVENT:
this->process_pairing_event_(obj.id, "Pairing object", obj.get_pairing_object());
break;
case miot::MIID_DOOR_SENSOR:
this->process_uint8_(obj.id, "Opening", obj.get_door_sensor());
break;
case miot::MIID_IDLE_TIME:
this->process_uint32_(obj.id, "Idle time", obj.get_idle_time());
break;
case miot::MIID_TIMEOUT:
this->process_uint32_(obj.id, "Timeout", obj.get_timeout());
break;
case miot::MIID_MOTION_WITH_LIGHT_EVENT:
this->process_uint32_(obj.id, "Motion with light", obj.get_motion_with_light_event());
break;
case miot::MIID_FLOODING:
this->process_bool_(obj.id, "Flooding", obj.get_flooding());
break;
case miot::MIID_LIGHT_INTENSITY:
this->process_bool_(obj.id, "Light intensity", obj.get_light_intensity());
break;
case miot::MIID_TEMPERATURE:
this->process_float_(obj.id, "Temperature", obj.get_temperature());
break;
case miot::MIID_HUMIDITY:
this->process_float_(obj.id, "Humidity", obj.get_humidity());
break;
case miot::MIID_TEMPERATURE_HUMIDITY:
this->process_temperature_humidity_(obj.id, "Temperture/Humidity", obj.get_temperature_humidity());
break;
case miot::MIID_BUTTON_EVENT:
this->process_button_event_(obj.id, "Button event", obj.get_button_event());
break;
case miot::MIID_ILLUMINANCE:
this->process_float_(obj.id, "Illuminance", obj.get_illuminance());
break;
case miot::MIID_WATER_BOIL:
this->process_water_boil_(obj.id, "Water Boil", obj.get_water_boil());
break;
default:
this->process_string_(obj.id, "", format_hex_pretty(obj.data));
break;
}
return true;
}
void MiotExplorer::process_string_(miot::MIID miid, const std::string &name, const std::string &data) {
auto it = this->sensors_.find(miid);
TextSensor *sens;
if (it != this->sensors_.end()) {
sens = static_cast<TextSensor *>(it->second);
} else {
sens = new TextSensor();
auto full = str_sprintf("%s [%04X] %s", this->get_name().c_str(), miid, name.c_str());
sens->set_name(strdup(full.c_str()));
App.register_text_sensor(sens);
this->sensors_[miid] = sens;
}
sens->publish_state(data);
}
void MiotExplorer::process_uint8_(miot::MIID miid, const std::string &name, const optional<uint8_t> &value) {
if (value.has_value()) {
this->process_string_(miid, name, to_string(*value));
}
}
void MiotExplorer::process_uint16_(miot::MIID miid, const std::string &name, const optional<uint16_t> &value) {
if (value.has_value()) {
this->process_string_(miid, name, to_string(*value));
}
}
void MiotExplorer::process_pairing_event_(miot::MIID miid, const std::string &name, const optional<miot::MIID> &value) {
if (value.has_value()) {
this->process_string_(miid, name, to_string(*value));
}
}
void MiotExplorer::process_uint32_(miot::MIID miid, const std::string &name, const optional<uint32_t> &value) {
if (value.has_value()) {
this->process_string_(miid, name, to_string(*value));
}
}
void MiotExplorer::process_bool_(miot::MIID miid, const std::string &name, const optional<bool> &value) {
if (value.has_value()) {
this->process_string_(miid, name, std::string(ONOFF(*value)));
}
}
void MiotExplorer::process_float_(miot::MIID miid, const std::string &name, const optional<float> &value) {
if (value.has_value()) {
this->process_string_(miid, name, str_sprintf("%.1f", *value));
}
}
void MiotExplorer::process_temperature_humidity_(miot::MIID miid, const std::string &name,
const miot::TemperatureHumidity *th) {
if (th != nullptr) {
this->process_string_(miid, name, str_sprintf("%.1f / %.1f", th->get_temperature(), th->get_humidity()));
}
}
void MiotExplorer::process_button_event_(miot::MIID miid, const std::string &name,
const miot::ButtonEvent *button_event) {
if (button_event != nullptr) {
this->process_string_(miid, name, button_event->str());
}
}
void MiotExplorer::process_water_boil_(miot::MIID miid, const std::string &name, const miot::WaterBoil *water_boil) {
if (water_boil != nullptr) {
auto str = str_sprintf("%s / %.1f", ONOFF(water_boil->get_power()), water_boil->get_temperature());
this->process_string_(miid, name, str);
}
}
} // namespace miot_explorer
} // namespace esphome