Add support for additional Xiaomi BLE sensors (#1027) (#1027)

This commit is contained in:
Alexander Pohl
2020-05-27 00:33:28 +02:00
committed by GitHub
parent e64246f642
commit 3fba3a5e2e
48 changed files with 1875 additions and 276 deletions
@@ -0,0 +1,36 @@
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_MAC_ADDRESS, CONF_TABLET, \
UNIT_PERCENT, ICON_BUG, ICON_BATTERY, CONF_ID
DEPENDENCIES = ['esp32_ble_tracker']
AUTO_LOAD = ['xiaomi_ble']
xiaomi_wx08zm_ns = cg.esphome_ns.namespace('xiaomi_wx08zm')
XiaomiWX08ZM = xiaomi_wx08zm_ns.class_('XiaomiWX08ZM', binary_sensor.BinarySensor,
esp32_ble_tracker.ESPBTDeviceListener, cg.Component)
CONFIG_SCHEMA = cv.All(binary_sensor.BINARY_SENSOR_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(XiaomiWX08ZM),
cv.Required(CONF_MAC_ADDRESS): cv.mac_address,
cv.Optional(CONF_TABLET): sensor.sensor_schema(UNIT_PERCENT, ICON_BUG, 0),
cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema(UNIT_PERCENT, ICON_BATTERY, 0),
}).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))
if CONF_TABLET in config:
sens = yield sensor.new_sensor(config[CONF_TABLET])
cg.add(var.set_tablet(sens))
if CONF_BATTERY_LEVEL in config:
sens = yield sensor.new_sensor(config[CONF_BATTERY_LEVEL])
cg.add(var.set_battery_level(sens))
@@ -0,0 +1,64 @@
#include "xiaomi_wx08zm.h"
#include "esphome/core/log.h"
#ifdef ARDUINO_ARCH_ESP32
namespace esphome {
namespace xiaomi_wx08zm {
static const char *TAG = "xiaomi_wx08zm";
void XiaomiWX08ZM::dump_config() {
ESP_LOGCONFIG(TAG, "Xiaomi WX08ZM");
LOG_BINARY_SENSOR(" ", "Mosquito Repellent", this);
LOG_SENSOR(" ", "Tablet Resource", this->tablet_);
LOG_SENSOR(" ", "Battery Level", this->battery_level_);
}
bool XiaomiWX08ZM::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) {
ESP_LOGVV(TAG, "parse_device(): payload decryption is currently not supported on this device.");
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->is_active.has_value()) {
this->publish_state(*res->is_active);
}
if (res->tablet.has_value() && this->tablet_ != nullptr)
this->tablet_->publish_state(*res->tablet);
if (res->battery_level.has_value() && this->battery_level_ != nullptr)
this->battery_level_->publish_state(*res->battery_level);
success = true;
}
if (!success) {
return false;
}
return true;
}
} // namespace xiaomi_wx08zm
} // namespace esphome
#endif
@@ -0,0 +1,36 @@
#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_wx08zm {
class XiaomiWX08ZM : public Component,
public binary_sensor::BinarySensorInitiallyOff,
public esp32_ble_tracker::ESPBTDeviceListener {
public:
void set_address(uint64_t address) { address_ = address; }
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_tablet(sensor::Sensor *tablet) { tablet_ = tablet; }
void set_battery_level(sensor::Sensor *battery_level) { battery_level_ = battery_level; }
protected:
uint64_t address_;
sensor::Sensor *tablet_{nullptr};
sensor::Sensor *battery_level_{nullptr};
};
} // namespace xiaomi_wx08zm
} // namespace esphome
#endif