Adds CGPR1 - Qingping Motion & Ambient light sensor support (#1675)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Tom Matheussen 2021-08-10 07:06:04 +02:00 committed by GitHub
parent 93796491af
commit b92311402a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 212 additions and 2 deletions

View file

@ -104,7 +104,7 @@ bool parse_xiaomi_message(const std::vector<uint8_t> &message, XiaomiParseResult
} }
while (payload_length > 3) { while (payload_length > 3) {
if (payload[payload_offset + 1] != 0x10) { if (payload[payload_offset + 1] != 0x10 && payload[payload_offset + 1] != 0x00) {
ESP_LOGVV(TAG, "parse_xiaomi_message(): fixed byte not found, stop parsing residual data."); ESP_LOGVV(TAG, "parse_xiaomi_message(): fixed byte not found, stop parsing residual data.");
break; break;
} }
@ -203,6 +203,11 @@ optional<XiaomiParseResult> parse_xiaomi_header(const esp32_ble_tracker::Service
} else if ((raw[2] == 0x87) && (raw[3] == 0x03)) { // square body, e-ink display } else if ((raw[2] == 0x87) && (raw[3] == 0x03)) { // square body, e-ink display
result.type = XiaomiParseResult::TYPE_MHOC401; result.type = XiaomiParseResult::TYPE_MHOC401;
result.name = "MHOC401"; result.name = "MHOC401";
} else if ((raw[2] == 0x83) && (raw[3] == 0x0A)) { // Qingping-branded, motion & ambient light sensor
result.type = XiaomiParseResult::TYPE_CGPR1;
result.name = "CGPR1";
if (raw.size() == 19)
result.raw_offset -= 6;
} else { } else {
ESP_LOGVV(TAG, "parse_xiaomi_header(): unknown device, no magic bytes."); ESP_LOGVV(TAG, "parse_xiaomi_header(): unknown device, no magic bytes.");
return {}; return {};

View file

@ -23,7 +23,8 @@ struct XiaomiParseResult {
TYPE_MUE4094RT, TYPE_MUE4094RT,
TYPE_WX08ZM, TYPE_WX08ZM,
TYPE_MJYD02YLA, TYPE_MJYD02YLA,
TYPE_MHOC401 TYPE_MHOC401,
TYPE_CGPR1
} type; } type;
std::string name; std::string name;
optional<float> temperature; optional<float> temperature;

View file

@ -0,0 +1,75 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor, binary_sensor, esp32_ble_tracker
from esphome.const import (
CONF_BATTERY_LEVEL,
CONF_BINDKEY,
CONF_DEVICE_CLASS,
CONF_MAC_ADDRESS,
CONF_ID,
DEVICE_CLASS_EMPTY,
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_ILLUMINANCE,
ICON_EMPTY,
UNIT_PERCENT,
CONF_IDLE_TIME,
CONF_ILLUMINANCE,
UNIT_MINUTE,
UNIT_LUX,
ICON_TIMELAPSE,
)
DEPENDENCIES = ["esp32_ble_tracker"]
AUTO_LOAD = ["xiaomi_ble", "sensor"]
xiaomi_cgpr1_ns = cg.esphome_ns.namespace("xiaomi_cgpr1")
XiaomiCGPR1 = xiaomi_cgpr1_ns.class_(
"XiaomiCGPR1",
binary_sensor.BinarySensor,
cg.Component,
esp32_ble_tracker.ESPBTDeviceListener,
)
CONFIG_SCHEMA = cv.All(
binary_sensor.BINARY_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(XiaomiCGPR1),
cv.Required(CONF_BINDKEY): cv.bind_key,
cv.Required(CONF_MAC_ADDRESS): cv.mac_address,
cv.Optional(
CONF_DEVICE_CLASS, default="motion"
): binary_sensor.device_class,
cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema(
UNIT_PERCENT, ICON_EMPTY, 0, DEVICE_CLASS_BATTERY
),
cv.Optional(CONF_IDLE_TIME): sensor.sensor_schema(
UNIT_MINUTE, ICON_TIMELAPSE, 0, DEVICE_CLASS_EMPTY
),
cv.Optional(CONF_ILLUMINANCE): sensor.sensor_schema(
UNIT_LUX, ICON_EMPTY, 0, DEVICE_CLASS_ILLUMINANCE
),
}
)
.extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA)
.extend(cv.COMPONENT_SCHEMA)
)
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config)
yield esp32_ble_tracker.register_ble_device(var, config)
yield binary_sensor.register_binary_sensor(var, config)
cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex))
cg.add(var.set_bindkey(config[CONF_BINDKEY]))
if CONF_IDLE_TIME in config:
sens = yield sensor.new_sensor(config[CONF_IDLE_TIME])
cg.add(var.set_idle_time(sens))
if CONF_BATTERY_LEVEL in config:
sens = yield sensor.new_sensor(config[CONF_BATTERY_LEVEL])
cg.add(var.set_battery_level(sens))
if CONF_ILLUMINANCE in config:
sens = yield sensor.new_sensor(config[CONF_ILLUMINANCE])
cg.add(var.set_illuminance(sens))

View file

@ -0,0 +1,79 @@
#include "xiaomi_cgpr1.h"
#include "esphome/core/log.h"
#ifdef ARDUINO_ARCH_ESP32
namespace esphome {
namespace xiaomi_cgpr1 {
static const char *TAG = "xiaomi_cgpr1";
void XiaomiCGPR1::dump_config() {
ESP_LOGCONFIG(TAG, "Xiaomi CGPR1");
LOG_BINARY_SENSOR(" ", "Motion", this);
LOG_SENSOR(" ", "Idle Time", this->idle_time_);
LOG_SENSOR(" ", "Battery Level", this->battery_level_);
LOG_SENSOR(" ", "Illuminance", this->illuminance_);
}
bool XiaomiCGPR1::parse_device(const esp32_ble_tracker::ESPBTDevice &device) {
if (device.address_uint64() != this->address_) {
ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
return false;
}
ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", device.address_str().c_str());
bool success = false;
for (auto &service_data : device.get_service_datas()) {
auto res = xiaomi_ble::parse_xiaomi_header(service_data);
if (!res.has_value()) {
continue;
}
if (res->is_duplicate) {
continue;
}
if (res->has_encryption &&
(!(xiaomi_ble::decrypt_xiaomi_payload(const_cast<std::vector<uint8_t> &>(service_data.data), this->bindkey_,
this->address_)))) {
continue;
}
if (!(xiaomi_ble::parse_xiaomi_message(service_data.data, *res))) {
continue;
}
if (!(xiaomi_ble::report_xiaomi_results(res, device.address_str()))) {
continue;
}
if (res->idle_time.has_value() && this->idle_time_ != nullptr)
this->idle_time_->publish_state(*res->idle_time);
if (res->battery_level.has_value() && this->battery_level_ != nullptr)
this->battery_level_->publish_state(*res->battery_level);
if (res->illuminance.has_value() && this->illuminance_ != nullptr)
this->illuminance_->publish_state(*res->illuminance);
if (res->has_motion.has_value())
this->publish_state(*res->has_motion);
success = true;
}
if (!success) {
return false;
}
return true;
}
void XiaomiCGPR1::set_bindkey(const std::string &bindkey) {
memset(bindkey_, 0, 16);
if (bindkey.size() != 32) {
return;
}
char temp[3] = {0};
for (int i = 0; i < 16; i++) {
strncpy(temp, &(bindkey.c_str()[i * 2]), 2);
bindkey_[i] = std::strtoul(temp, NULL, 16);
}
}
} // namespace xiaomi_cgpr1
} // namespace esphome
#endif

View file

@ -0,0 +1,40 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/binary_sensor/binary_sensor.h"
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
#include "esphome/components/xiaomi_ble/xiaomi_ble.h"
#ifdef ARDUINO_ARCH_ESP32
namespace esphome {
namespace xiaomi_cgpr1 {
class XiaomiCGPR1 : public Component,
public binary_sensor::BinarySensorInitiallyOff,
public esp32_ble_tracker::ESPBTDeviceListener {
public:
void set_address(uint64_t address) { address_ = address; }
void set_bindkey(const std::string &bindkey);
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override;
void dump_config() override;
float get_setup_priority() const override { return setup_priority::DATA; }
void set_battery_level(sensor::Sensor *battery_level) { battery_level_ = battery_level; }
void set_illuminance(sensor::Sensor *illuminance) { illuminance_ = illuminance; }
void set_idle_time(sensor::Sensor *idle_time) { idle_time_ = idle_time; }
protected:
uint64_t address_;
uint8_t bindkey_[16];
sensor::Sensor *idle_time_{nullptr};
sensor::Sensor *battery_level_{nullptr};
sensor::Sensor *illuminance_{nullptr};
};
} // namespace xiaomi_cgpr1
} // namespace esphome
#endif

View file

@ -303,6 +303,16 @@ binary_sensor:
name: 'WX08ZM Tablet Resource' name: 'WX08ZM Tablet Resource'
battery_level: battery_level:
name: 'WX08ZM Battery Level' name: 'WX08ZM Battery Level'
- platform: xiaomi_cgpr1
name: 'CGPR1 Motion'
mac_address: '12:34:56:12:34:56'
bindkey: '48403ebe2d385db8d0c187f81e62cb64'
battery_level:
name: 'CGPR1 battery Level'
idle_time:
name: 'CGPR1 Idle Time'
illuminance:
name: 'CGPR1 Illuminance'
esp32_ble_tracker: esp32_ble_tracker:
on_ble_advertise: on_ble_advertise: