mirror of
https://github.com/esphome/esphome.git
synced 2024-11-21 14:38:10 +01:00
Add support for LYWSD02MMC Xiaomi device (#7080)
This commit is contained in:
parent
8d5be27746
commit
fc146dabed
12 changed files with 225 additions and 3 deletions
|
@ -453,6 +453,7 @@ esphome/components/wl_134/* @hobbypunk90
|
|||
esphome/components/x9c/* @EtienneMD
|
||||
esphome/components/xgzp68xx/* @gcormier
|
||||
esphome/components/xiaomi_hhccjcy10/* @fariouche
|
||||
esphome/components/xiaomi_lywsd02mmc/* @juanluss31
|
||||
esphome/components/xiaomi_lywsd03mmc/* @ahpohl
|
||||
esphome/components/xiaomi_mhoc303/* @drug123
|
||||
esphome/components/xiaomi_mhoc401/* @vevsvevs
|
||||
|
|
|
@ -49,8 +49,8 @@ bool parse_xiaomi_value(uint16_t value_type, const uint8_t *data, uint8_t value_
|
|||
const uint16_t conductivity = encode_uint16(data[1], data[0]);
|
||||
result.conductivity = conductivity;
|
||||
}
|
||||
// battery, 1 byte, 8-bit unsigned integer, 1 %
|
||||
else if ((value_type == 0x100A) && (value_length == 1)) {
|
||||
// battery / MiaoMiaoce battery, 1 byte, 8-bit unsigned integer, 1 %
|
||||
else if ((value_type == 0x100A || value_type == 0x4803) && (value_length == 1)) {
|
||||
result.battery_level = data[0];
|
||||
}
|
||||
// temperature + humidity, 4 bytes, 16-bit signed integer (LE) each, 0.1 °C, 0.1 %
|
||||
|
@ -80,6 +80,17 @@ bool parse_xiaomi_value(uint16_t value_type, const uint8_t *data, uint8_t value_
|
|||
result.has_motion = !idle_time;
|
||||
} else if ((value_type == 0x1018) && (value_length == 1)) {
|
||||
result.is_light = data[0];
|
||||
}
|
||||
// MiaoMiaoce temperature, 4 bytes, float, 0.1 °C
|
||||
else if ((value_type == 0x4C01) && (value_length == 4)) {
|
||||
const uint32_t int_number = encode_uint32(data[3], data[2], data[1], data[0]);
|
||||
float temperature;
|
||||
std::memcpy(&temperature, &int_number, sizeof(temperature));
|
||||
result.temperature = temperature;
|
||||
}
|
||||
// MiaoMiaoce humidity, 1 byte, 8-bit unsigned integer, 1 %
|
||||
else if ((value_type == 0x4C02) && (value_length == 1)) {
|
||||
result.humidity = data[0];
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
@ -111,7 +122,8 @@ bool parse_xiaomi_message(const std::vector<uint8_t> &message, XiaomiParseResult
|
|||
}
|
||||
|
||||
while (payload_length > 3) {
|
||||
if (payload[payload_offset + 1] != 0x10 && payload[payload_offset + 1] != 0x00) {
|
||||
if (payload[payload_offset + 1] != 0x10 && payload[payload_offset + 1] != 0x00 &&
|
||||
payload[payload_offset + 1] != 0x4C && payload[payload_offset + 1] != 0x48) {
|
||||
ESP_LOGVV(TAG, "parse_xiaomi_message(): fixed byte not found, stop parsing residual data.");
|
||||
break;
|
||||
}
|
||||
|
@ -190,6 +202,11 @@ optional<XiaomiParseResult> parse_xiaomi_header(const esp32_ble_tracker::Service
|
|||
} else if (device_uuid == 0x045b) { // rectangular body, e-ink display
|
||||
result.type = XiaomiParseResult::TYPE_LYWSD02;
|
||||
result.name = "LYWSD02";
|
||||
} else if (device_uuid == 0x2542) { // rectangular body, e-ink display — with bindkeys
|
||||
result.type = XiaomiParseResult::TYPE_LYWSD02MMC;
|
||||
result.name = "LYWSD02MMC";
|
||||
if (raw.size() == 19)
|
||||
result.raw_offset -= 6;
|
||||
} else if (device_uuid == 0x040a) { // Mosquito Repellent Smart Version
|
||||
result.type = XiaomiParseResult::TYPE_WX08ZM;
|
||||
result.name = "WX08ZM";
|
||||
|
|
|
@ -17,6 +17,7 @@ struct XiaomiParseResult {
|
|||
TYPE_HHCCPOT002,
|
||||
TYPE_LYWSDCGQ,
|
||||
TYPE_LYWSD02,
|
||||
TYPE_LYWSD02MMC,
|
||||
TYPE_CGG1,
|
||||
TYPE_LYWSD03MMC,
|
||||
TYPE_CGD1,
|
||||
|
|
0
esphome/components/xiaomi_lywsd02mmc/__init__.py
Normal file
0
esphome/components/xiaomi_lywsd02mmc/__init__.py
Normal file
77
esphome/components/xiaomi_lywsd02mmc/sensor.py
Normal file
77
esphome/components/xiaomi_lywsd02mmc/sensor.py
Normal file
|
@ -0,0 +1,77 @@
|
|||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import sensor, esp32_ble_tracker
|
||||
from esphome.const import (
|
||||
CONF_BATTERY_LEVEL,
|
||||
CONF_HUMIDITY,
|
||||
CONF_MAC_ADDRESS,
|
||||
CONF_TEMPERATURE,
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
UNIT_CELSIUS,
|
||||
UNIT_PERCENT,
|
||||
DEVICE_CLASS_HUMIDITY,
|
||||
DEVICE_CLASS_BATTERY,
|
||||
CONF_ID,
|
||||
CONF_BINDKEY,
|
||||
)
|
||||
|
||||
AUTO_LOAD = ["xiaomi_ble"]
|
||||
CODEOWNERS = ["@juanluss31"]
|
||||
DEPENDENCIES = ["esp32_ble_tracker"]
|
||||
|
||||
xiaomi_lywsd02mmc_ns = cg.esphome_ns.namespace("xiaomi_lywsd02mmc")
|
||||
XiaomiLYWSD02MMC = xiaomi_lywsd02mmc_ns.class_(
|
||||
"XiaomiLYWSD02MMC", esp32_ble_tracker.ESPBTDeviceListener, cg.Component
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(XiaomiLYWSD02MMC),
|
||||
cv.Required(CONF_MAC_ADDRESS): cv.mac_address,
|
||||
cv.Required(CONF_BINDKEY): cv.bind_key,
|
||||
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_CELSIUS,
|
||||
accuracy_decimals=1,
|
||||
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_PERCENT,
|
||||
accuracy_decimals=0,
|
||||
device_class=DEVICE_CLASS_HUMIDITY,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_PERCENT,
|
||||
accuracy_decimals=0,
|
||||
device_class=DEVICE_CLASS_BATTERY,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
),
|
||||
}
|
||||
)
|
||||
.extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA)
|
||||
.extend(cv.COMPONENT_SCHEMA)
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
await esp32_ble_tracker.register_ble_device(var, config)
|
||||
|
||||
cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex))
|
||||
cg.add(var.set_bindkey(config[CONF_BINDKEY]))
|
||||
|
||||
if temperature_config := config.get(CONF_TEMPERATURE):
|
||||
sens = await sensor.new_sensor(temperature_config)
|
||||
cg.add(var.set_temperature(sens))
|
||||
if humidity_config := config.get(CONF_HUMIDITY):
|
||||
sens = await sensor.new_sensor(humidity_config)
|
||||
cg.add(var.set_humidity(sens))
|
||||
if battery_level_config := config.get(CONF_BATTERY_LEVEL):
|
||||
sens = await sensor.new_sensor(battery_level_config)
|
||||
cg.add(var.set_battery_level(sens))
|
73
esphome/components/xiaomi_lywsd02mmc/xiaomi_lywsd02mmc.cpp
Normal file
73
esphome/components/xiaomi_lywsd02mmc/xiaomi_lywsd02mmc.cpp
Normal file
|
@ -0,0 +1,73 @@
|
|||
#include "xiaomi_lywsd02mmc.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#ifdef USE_ESP32
|
||||
|
||||
namespace esphome {
|
||||
namespace xiaomi_lywsd02mmc {
|
||||
|
||||
static const char *const TAG = "xiaomi_lywsd02mmc";
|
||||
|
||||
void XiaomiLYWSD02MMC::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "Xiaomi LYWSD02MMC");
|
||||
ESP_LOGCONFIG(TAG, " Bindkey: %s", format_hex_pretty(this->bindkey_, 16).c_str());
|
||||
LOG_SENSOR(" ", "Temperature", this->temperature_);
|
||||
LOG_SENSOR(" ", "Humidity", this->humidity_);
|
||||
LOG_SENSOR(" ", "Battery Level", this->battery_level_);
|
||||
}
|
||||
|
||||
bool XiaomiLYWSD02MMC::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->temperature.has_value() && this->temperature_ != nullptr)
|
||||
this->temperature_->publish_state(*res->temperature);
|
||||
if (res->humidity.has_value() && this->humidity_ != nullptr)
|
||||
this->humidity_->publish_state(*res->humidity);
|
||||
if (res->battery_level.has_value() && this->battery_level_ != nullptr)
|
||||
this->battery_level_->publish_state(*res->battery_level);
|
||||
success = true;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
void XiaomiLYWSD02MMC::set_bindkey(const std::string &bindkey) {
|
||||
memset(this->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);
|
||||
this->bindkey_[i] = std::strtoul(temp, nullptr, 16);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace xiaomi_lywsd02mmc
|
||||
} // namespace esphome
|
||||
|
||||
#endif
|
37
esphome/components/xiaomi_lywsd02mmc/xiaomi_lywsd02mmc.h
Normal file
37
esphome/components/xiaomi_lywsd02mmc/xiaomi_lywsd02mmc.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
|
||||
#include "esphome/components/xiaomi_ble/xiaomi_ble.h"
|
||||
|
||||
#ifdef USE_ESP32
|
||||
|
||||
namespace esphome {
|
||||
namespace xiaomi_lywsd02mmc {
|
||||
|
||||
class XiaomiLYWSD02MMC : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
|
||||
public:
|
||||
void set_address(uint64_t address) { this->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_temperature(sensor::Sensor *temperature) { this->temperature_ = temperature; }
|
||||
void set_humidity(sensor::Sensor *humidity) { this->humidity_ = humidity; }
|
||||
void set_battery_level(sensor::Sensor *battery_level) { this->battery_level_ = battery_level; }
|
||||
|
||||
protected:
|
||||
uint64_t address_;
|
||||
uint8_t bindkey_[16];
|
||||
sensor::Sensor *temperature_{nullptr};
|
||||
sensor::Sensor *humidity_{nullptr};
|
||||
sensor::Sensor *battery_level_{nullptr};
|
||||
};
|
||||
|
||||
} // namespace xiaomi_lywsd02mmc
|
||||
} // namespace esphome
|
||||
|
||||
#endif
|
12
tests/components/xiaomi_lywsd02mmc/common.yaml
Normal file
12
tests/components/xiaomi_lywsd02mmc/common.yaml
Normal file
|
@ -0,0 +1,12 @@
|
|||
esp32_ble_tracker:
|
||||
|
||||
sensor:
|
||||
- platform: xiaomi_lywsd02mmc
|
||||
mac_address: A4:C1:38:54:5E:18
|
||||
bindkey: 2529d8e0d23150a588675cc54ad48400
|
||||
temperature:
|
||||
name: Xiaomi LYWSD02MMC Temperature
|
||||
humidity:
|
||||
name: Xiaomi LYWSD02MMC Humidity
|
||||
battery_level:
|
||||
name: Xiaomi LYWSD02MMC Battery Level
|
1
tests/components/xiaomi_lywsd02mmc/test.esp32-ard.yaml
Normal file
1
tests/components/xiaomi_lywsd02mmc/test.esp32-ard.yaml
Normal file
|
@ -0,0 +1 @@
|
|||
<<: !include common.yaml
|
|
@ -0,0 +1 @@
|
|||
<<: !include common.yaml
|
|
@ -0,0 +1 @@
|
|||
<<: !include common.yaml
|
1
tests/components/xiaomi_lywsd02mmc/test.esp32-idf.yaml
Normal file
1
tests/components/xiaomi_lywsd02mmc/test.esp32-idf.yaml
Normal file
|
@ -0,0 +1 @@
|
|||
<<: !include common.yaml
|
Loading…
Reference in a new issue