mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-05 20:38:27 +02:00
@@ -0,0 +1,47 @@
|
||||
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_MAC_ADDRESS, CONF_ID, CONF_BINDKEY, \
|
||||
CONF_DEVICE_CLASS, CONF_LIGHT, CONF_BATTERY_LEVEL, UNIT_PERCENT, ICON_BATTERY, \
|
||||
CONF_IDLE_TIME, UNIT_MINUTE, ICON_TIMELAPSE
|
||||
|
||||
DEPENDENCIES = ['esp32_ble_tracker']
|
||||
AUTO_LOAD = ['xiaomi_ble']
|
||||
|
||||
xiaomi_mjyd02yla_ns = cg.esphome_ns.namespace('xiaomi_mjyd02yla')
|
||||
XiaomiMJYD02YLA = xiaomi_mjyd02yla_ns.class_('XiaomiMJYD02YLA', binary_sensor.BinarySensor,
|
||||
cg.Component, esp32_ble_tracker.ESPBTDeviceListener)
|
||||
|
||||
CONFIG_SCHEMA = cv.All(binary_sensor.BINARY_SENSOR_SCHEMA.extend({
|
||||
cv.GenerateID(): cv.declare_id(XiaomiMJYD02YLA),
|
||||
cv.Required(CONF_MAC_ADDRESS): cv.mac_address,
|
||||
cv.Required(CONF_BINDKEY): cv.bind_key,
|
||||
cv.Optional(CONF_DEVICE_CLASS, default='motion'): binary_sensor.device_class,
|
||||
cv.Optional(CONF_IDLE_TIME): sensor.sensor_schema(UNIT_MINUTE, ICON_TIMELAPSE, 0),
|
||||
cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema(UNIT_PERCENT, ICON_BATTERY, 0),
|
||||
cv.Optional(CONF_LIGHT): binary_sensor.BINARY_SENSOR_SCHEMA.extend({
|
||||
cv.Optional(CONF_DEVICE_CLASS, default='light'): binary_sensor.device_class,
|
||||
}),
|
||||
}).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_LIGHT in config:
|
||||
sens = yield binary_sensor.new_binary_sensor(config[CONF_LIGHT])
|
||||
cg.add(var.set_light(sens))
|
||||
|
||||
cg.add_library("mbedtls", "cdf462088d")
|
||||
@@ -0,0 +1,79 @@
|
||||
#include "xiaomi_mjyd02yla.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
|
||||
namespace esphome {
|
||||
namespace xiaomi_mjyd02yla {
|
||||
|
||||
static const char *TAG = "xiaomi_mjyd02yla";
|
||||
|
||||
void XiaomiMJYD02YLA::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "Xiaomi MJYD02YL-A");
|
||||
LOG_BINARY_SENSOR(" ", "Motion", this);
|
||||
LOG_BINARY_SENSOR(" ", "Light", this->is_light_);
|
||||
LOG_SENSOR(" ", "Idle Time", this->idle_time_);
|
||||
LOG_SENSOR(" ", "Battery Level", this->battery_level_);
|
||||
}
|
||||
|
||||
bool XiaomiMJYD02YLA::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->is_light.has_value() && this->is_light_ != nullptr)
|
||||
this->is_light_->publish_state(*res->is_light);
|
||||
if (res->has_motion.has_value())
|
||||
this->publish_state(*res->has_motion);
|
||||
success = true;
|
||||
}
|
||||
|
||||
if (!success) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void XiaomiMJYD02YLA::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_mjyd02yla
|
||||
} // namespace esphome
|
||||
|
||||
#endif
|
||||
@@ -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_mjyd02yla {
|
||||
|
||||
class XiaomiMJYD02YLA : 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_idle_time(sensor::Sensor *idle_time) { idle_time_ = idle_time; }
|
||||
void set_battery_level(sensor::Sensor *battery_level) { battery_level_ = battery_level; }
|
||||
void set_light(binary_sensor::BinarySensor *light) { is_light_ = light; }
|
||||
|
||||
protected:
|
||||
uint64_t address_;
|
||||
uint8_t bindkey_[16];
|
||||
sensor::Sensor *idle_time_{nullptr};
|
||||
sensor::Sensor *battery_level_{nullptr};
|
||||
binary_sensor::BinarySensor *is_light_{nullptr};
|
||||
};
|
||||
|
||||
} // namespace xiaomi_mjyd02yla
|
||||
} // namespace esphome
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user