-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmiot_motion.cpp
171 lines (137 loc) · 4.47 KB
/
miot_motion.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
163
164
165
166
167
168
169
170
171
#include "esphome/core/log.h"
#include "miot_motion.h"
namespace esphome {
namespace miot_motion {
static const char *const TAG = "miot_motion";
constexpr uint16_t PRODUCT_ID_MJYD02YLA = 0x07F6;
constexpr uint16_t PRODUCT_ID_RTCGQ02LM = 0x0A8D;
constexpr uint16_t PRODUCT_ID_CGPR1 = 0x0A83;
void MiotMotion::dump_config() {
this->dump_config_(TAG, "Motion");
LOG_BINARY_SENSOR(" ", "Motion", this);
LOG_BINARY_SENSOR(" ", "Light", this->light_binary_sensor_);
LOG_SENSOR(" ", "Idle Time", this->idle_time_sensor_);
LOG_SENSOR(" ", "Illuminance", this->illuminance_sensor_);
if (this->reset_timeout_) {
ESP_LOGCONFIG(TAG, " Timeout: %.3f s", this->reset_timeout_ * 0.001f);
}
}
void MiotMotion::setup() {
if (this->reset_timeout_) {
this->add_on_state_callback(
[this](bool state) { this->set_timeout(TAG, this->reset_timeout_, [this]() { this->publish_state(false); }); });
}
}
void MiotMotion::process_idle_time_(uint32_t idle_time, bool has_time) {
// 0 = motion detected
this->publish_state(idle_time == 0);
if (this->idle_time_sensor_ && has_time) {
this->idle_time_sensor_->publish_state(idle_time);
}
}
void MiotMotion::process_idle_time_(const miot::BLEObject &obj) {
const auto idle_time = obj.get_idle_time();
if (idle_time.has_value()) {
this->process_idle_time_(*idle_time, true);
}
}
void MiotMotion::process_timeout_(const miot::BLEObject &obj) {
const auto timeout = obj.get_timeout();
if (timeout.has_value()) {
this->process_idle_time_(*timeout, false);
}
}
void MiotMotion::process_no_motion_time_(const miot::BLEObject &obj) {
const auto idle_time = obj.get_no_motion_time();
if (idle_time.has_value()) {
this->process_idle_time_(*idle_time, true);
}
}
void MiotMotion::process_motion_with_illuminance_(float illuminance) {
// MJYD02YLA: 1 - moving no light, 100 - moving with light
// RTCGQ02LM: 0 - moving no light, 256 - moving with light
this->publish_state(true);
if (this->light_binary_sensor_) {
this->light_binary_sensor_->publish_state(illuminance >= 100);
}
if (this->illuminance_sensor_) {
this->illuminance_sensor_->publish_state(illuminance);
}
}
void MiotMotion::process_motion_with_light_event_(const miot::BLEObject &obj) {
const auto illuminance = obj.get_motion_with_light_event();
if (illuminance.has_value()) {
this->process_motion_with_illuminance_(*illuminance);
}
}
void MiotMotion::process_motion_with_illuminance_(const miot::BLEObject &obj) {
const auto illuminance = obj.get_motion_with_illuminance();
if (illuminance.has_value()) {
this->process_motion_with_illuminance_(*illuminance);
}
}
void MiotMotion::process_light_intensity_(const miot::BLEObject &obj) {
if (this->light_binary_sensor_) {
const auto light = obj.get_light_intensity();
if (light.has_value()) {
this->light_binary_sensor_->publish_state(*light);
}
}
}
void MiotMotion::process_illuminance_(const miot::BLEObject &obj) {
const auto illuminance = obj.get_illuminance();
if (!illuminance.has_value()) {
return;
}
if (this->light_binary_sensor_) {
this->light_binary_sensor_->publish_state(*illuminance >= 100);
}
if (this->illuminance_sensor_) {
this->illuminance_sensor_->publish_state(*illuminance);
}
}
void MiotMotion::process_motion_event_(const miot::BLEObject &obj) {
const auto motion = obj.get_motion();
if (!motion.has_value()) {
return;
}
this->publish_state(*motion);
}
bool MiotMotion::process_object_(const miot::BLEObject &obj) {
switch (obj.id) {
// RTCGQ02LM, MJYD02YLA, CGPR1
case miot::MIID_IDLE_TIME:
this->process_idle_time_(obj);
break;
// RTCGQ02LM
case miot::MIID_TIMEOUT:
this->process_timeout_(obj);
break;
case miot::MIID_NO_MOTION_TIME:
this->process_no_motion_time_(obj);
break;
case miot::MIID_MOTION_EVENT:
this->process_motion_event_(obj);
break;
// RTCGQ02LM, MJYD02YLA
case miot::MIID_MOTION_WITH_LIGHT_EVENT:
this->process_motion_with_light_event_(obj);
break;
case miot::MIID_MOTION_WITH_ILLUMINANCE:
this->process_motion_with_illuminance_(obj);
break;
// RTCGQ02LM
case miot::MIID_LIGHT_INTENSITY:
this->process_light_intensity_(obj);
break;
// MJYD02YLA, CGPR1
case miot::MIID_ILLUMINANCE:
this->process_illuminance_(obj);
break;
default:
return this->process_default_(obj);
}
return true;
}
} // namespace miot_motion
} // namespace esphome