mirror of
https://github.com/esphome/esphome.git
synced 2024-12-24 22:44:54 +01:00
Add files via upload
This commit is contained in:
parent
b16f0a536f
commit
4cbf38173c
5 changed files with 265 additions and 0 deletions
37
esphome/components/xiaomi_jtyjgd03mi/__init__.py
Normal file
37
esphome/components/xiaomi_jtyjgd03mi/__init__.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import esp32_ble_tracker
|
||||
from esphome.const import CONF_MAC_ADDRESS, CONF_ID, CONF_BINDKEY
|
||||
|
||||
|
||||
AUTO_LOAD = ["xiaomi_ble"]
|
||||
CODEOWNERS = ["@jesserockz"]
|
||||
DEPENDENCIES = ["esp32_ble_tracker"]
|
||||
MULTI_CONF = True
|
||||
|
||||
xiaomi_jtyjgd03mi_ns = cg.esphome_ns.namespace("xiaomi_jtyjgd03mi")
|
||||
XiaomiJTYJQD03MI = xiaomi_jtyjgd03mi_ns.class_(
|
||||
"XiaomiJTYJQD03MI", esp32_ble_tracker.ESPBTDeviceListener, cg.Component
|
||||
)
|
||||
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(XiaomiJTYJQD03MI),
|
||||
cv.Required(CONF_BINDKEY): cv.bind_key,
|
||||
cv.Required(CONF_MAC_ADDRESS): cv.mac_address,
|
||||
}
|
||||
)
|
||||
.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]))
|
|
@ -1 +1,46 @@
|
|||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import binary_sensor
|
||||
from esphome.const import (
|
||||
CONF_SMOKE,
|
||||
CONF_TIMEOUT,
|
||||
DEVICE_CLASS_SMOKE,
|
||||
CONF_ID,
|
||||
CONF_BUTTON,
|
||||
)
|
||||
from esphome.core import TimePeriod
|
||||
|
||||
from . import XiaomiJTYJQD03MI
|
||||
|
||||
DEPENDENCIES = ["xiaomi_jtyjgd03mi"]
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.use_id(XiaomiJTYJQD03MI),
|
||||
cv.Optional(CONF_SMOKE): binary_sensor.binary_sensor_schema(
|
||||
device_class=DEVICE_CLASS_SMOKE
|
||||
),
|
||||
cv.Optional(CONF_BUTTON): binary_sensor.binary_sensor_schema().extend(
|
||||
{
|
||||
cv.Optional(CONF_TIMEOUT, default="200ms"): cv.All(
|
||||
cv.positive_time_period_milliseconds,
|
||||
cv.Range(max=TimePeriod(milliseconds=65535)),
|
||||
),
|
||||
}
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
parent = await cg.get_variable(config[CONF_ID])
|
||||
|
||||
if CONF_SMOKE in config:
|
||||
sens = await binary_sensor.new_binary_sensor(config[CONF_SMOKE])
|
||||
cg.add(parent.set_smoke(sens))
|
||||
|
||||
if CONF_BUTTON in config:
|
||||
sens = await binary_sensor.new_binary_sensor(config[CONF_BUTTON])
|
||||
cg.add(parent.set_button(sens))
|
||||
cg.add(parent.set_button_timeout(config[CONF_BUTTON][CONF_TIMEOUT]))
|
||||
|
|
37
esphome/components/xiaomi_jtyjgd03mi/sensor.py
Normal file
37
esphome/components/xiaomi_jtyjgd03mi/sensor.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import sensor
|
||||
from esphome.const import (
|
||||
CONF_BATTERY_LEVEL,
|
||||
ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
UNIT_PERCENT,
|
||||
CONF_ID,
|
||||
DEVICE_CLASS_BATTERY,
|
||||
)
|
||||
|
||||
from . import XiaomiJTYJQD03MI
|
||||
|
||||
DEPENDENCIES = ["xiaomi_jtyjgd03mi"]
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.use_id(XiaomiJTYJQD03MI),
|
||||
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,
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
parent = await cg.get_variable(config[CONF_ID])
|
||||
|
||||
if CONF_BATTERY_LEVEL in config:
|
||||
sens = await sensor.new_sensor(config[CONF_BATTERY_LEVEL])
|
||||
cg.add(parent.set_battery_level(sens))
|
88
esphome/components/xiaomi_jtyjgd03mi/xiaomi_jtyjgd03mi.cpp
Normal file
88
esphome/components/xiaomi_jtyjgd03mi/xiaomi_jtyjgd03mi.cpp
Normal file
|
@ -0,0 +1,88 @@
|
|||
#include "xiaomi_jtyjgd03mi.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
|
||||
#ifdef USE_ESP32
|
||||
|
||||
namespace esphome {
|
||||
namespace xiaomi_jtyjgd03mi {
|
||||
|
||||
static const char *const TAG = "xiaomi_jtyjgd03mi";
|
||||
|
||||
void XiaomiJTYJQD03MI::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "Xiaomi JTYJQD03MI");
|
||||
ESP_LOGCONFIG(TAG, " Bindkey: %s", format_hex_pretty(this->bindkey_, 16).c_str());
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
LOG_BINARY_SENSOR(" ", "Smoke", this->smoke_);
|
||||
LOG_BINARY_SENSOR(" ", "Button", this->button_);
|
||||
#endif
|
||||
#ifdef USE_SENSOR
|
||||
LOG_SENSOR(" ", "Battery Level", this->battery_level_);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool XiaomiJTYJQD03MI::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;
|
||||
}
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
if (res->has_smoke.has_value() && this->smoke_ != nullptr) {
|
||||
this->smoke_->publish_state(*res->has_smoke);
|
||||
}
|
||||
|
||||
if (res->button_press.has_value() && this->button_ != nullptr) {
|
||||
this->button_->publish_state(*res->button_press);
|
||||
this->set_timeout("button_timeout", this->button_timeout_,
|
||||
[this, res]() { this->button_->publish_state(false); });
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_SENSOR
|
||||
if (res->battery_level.has_value() && this->battery_level_ != nullptr)
|
||||
this->battery_level_->publish_state(*res->battery_level);
|
||||
#endif
|
||||
success = true;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
void XiaomiJTYJQD03MI::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, nullptr, 16);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace xiaomi_jtyjgd03mi
|
||||
} // namespace esphome
|
||||
|
||||
#endif
|
58
esphome/components/xiaomi_jtyjgd03mi/xiaomi_jtyjgd03mi.h
Normal file
58
esphome/components/xiaomi_jtyjgd03mi/xiaomi_jtyjgd03mi.h
Normal file
|
@ -0,0 +1,58 @@
|
|||
#pragma once
|
||||
|
||||
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
|
||||
#include "esphome/core/defines.h"
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
#include "esphome/components/binary_sensor/binary_sensor.h"
|
||||
#endif
|
||||
#ifdef USE_SENSOR
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#endif
|
||||
#include "esphome/components/xiaomi_ble/xiaomi_ble.h"
|
||||
#include "esphome/core/component.h"
|
||||
|
||||
#ifdef USE_ESP32
|
||||
|
||||
namespace esphome {
|
||||
namespace xiaomi_jtyjgd03mi {
|
||||
|
||||
class XiaomiJTYJQD03MI : public Component, 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; }
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
void set_smoke(binary_sensor::BinarySensor *smoke) { this->smoke_ = smoke; }
|
||||
|
||||
void set_button(binary_sensor::BinarySensor *button) { this->button_ = button; }
|
||||
void set_button_timeout(uint16_t timeout) { this->button_timeout_ = timeout; }
|
||||
#endif
|
||||
|
||||
#ifdef USE_SENSOR
|
||||
void set_battery_level(sensor::Sensor *battery_level) { battery_level_ = battery_level; }
|
||||
#endif
|
||||
|
||||
protected:
|
||||
uint64_t address_;
|
||||
uint8_t bindkey_[16];
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
uint16_t motion_timeout_;
|
||||
uint16_t button_timeout_;
|
||||
|
||||
binary_sensor::BinarySensor *smoke_{nullptr};
|
||||
binary_sensor::BinarySensor *button_{nullptr};
|
||||
#endif
|
||||
#ifdef USE_SENSOR
|
||||
sensor::Sensor *battery_level_{nullptr};
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace xiaomi_jtyjgd03mi
|
||||
} // namespace esphome
|
||||
|
||||
#endif
|
Loading…
Reference in a new issue