1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-01 01:22:20 +01:00

🏗 Merge C++ into python codebase (#504)

## Description:

Move esphome-core codebase into esphome (and a bunch of other refactors). See https://github.com/esphome/feature-requests/issues/97

Yes this is a shit ton of work and no there's no way to automate it :( But it will be worth it 👍

Progress:
- Core support (file copy etc): 80%
- Base Abstractions (light, switch): ~50%
- Integrations: ~10%
- Working? Yes, (but only with ported components).

Other refactors:
- Moves all codegen related stuff into a single class: `esphome.codegen` (imported as `cg`)
- Rework coroutine syntax
- Move from `component/platform.py` to `domain/component.py` structure as with HA
- Move all defaults out of C++ and into config validation.
- Remove `make_...` helpers from Application class. Reason: Merge conflicts with every single new integration.
- Pointer Variables are stored globally instead of locally in setup(). Reason: stack size limit.

Future work:
- Rework const.py - Move all `CONF_...` into a conf class (usage `conf.UPDATE_INTERVAL` vs `CONF_UPDATE_INTERVAL`). Reason: Less convoluted import block
- Enable loading from `custom_components` folder.

**Related issue (if applicable):** https://github.com/esphome/feature-requests/issues/97

**Pull request in [esphome-docs](https://github.com/esphome/esphome-docs) with documentation (if applicable):** esphome/esphome-docs#<esphome-docs PR number goes here>

## Checklist:
  - [ ] The code change is tested and works locally.
  - [ ] Tests have been added to verify that the new code works (under `tests/` folder).

If user exposed functionality or configuration variables are added/changed:
  - [ ] Documentation added/updated in [esphomedocs](https://github.com/OttoWinter/esphomedocs).
This commit is contained in:
Otto Winter
2019-04-17 12:06:00 +02:00
committed by GitHub
parent 049807e3ab
commit 6682c43dfa
817 changed files with 54156 additions and 10830 deletions

View File

@@ -0,0 +1,20 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components.esp32_ble_tracker import CONF_ESP32_BLE_ID, ESPBTDeviceListener, \
ESP_BLE_DEVICE_SCHEMA
from esphome.const import CONF_ID
DEPENDENCIES = ['esp32_ble_tracker']
xiaomi_ble_ns = cg.esphome_ns.namespace('xiaomi_ble')
XiaomiListener = xiaomi_ble_ns.class_('XiaomiListener', cg.Component, ESPBTDeviceListener)
CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_variable_id(XiaomiListener),
}).extend(ESP_BLE_DEVICE_SCHEMA).extend(cv.COMPONENT_SCHEMA)
def to_code(config):
hub = yield cg.get_variable(config[CONF_ESP32_BLE_ID])
var = cg.new_Pvariable(config[CONF_ID], hub)
yield cg.register_component(var, config)

View File

@@ -0,0 +1,147 @@
#include "xiaomi_ble.h"
#include "esphome/core/log.h"
#ifdef ARDUINO_ARCH_ESP32
namespace esphome {
namespace xiaomi_ble {
static const char *TAG = "xiaomi_ble";
bool parse_xiaomi_data_byte(uint8_t data_type, const uint8_t *data, uint8_t data_length, XiaomiParseResult &result) {
switch (data_type) {
case 0x0D: { // temperature+humidity, 4 bytes, 16-bit signed integer (LE) each, 0.1 °C, 0.1 %
if (data_length != 4)
return false;
const int16_t temperature = uint16_t(data[0]) | (uint16_t(data[1]) << 8);
const int16_t humidity = uint16_t(data[2]) | (uint16_t(data[3]) << 8);
result.temperature = temperature / 10.0f;
result.humidity = humidity / 10.0f;
return true;
}
case 0x0A: { // battery, 1 byte, 8-bit unsigned integer, 1 %
if (data_length != 1)
return false;
result.battery_level = data[0];
return true;
}
case 0x06: { // humidity, 2 bytes, 16-bit signed integer (LE), 0.1 %
if (data_length != 2)
return false;
const int16_t humidity = uint16_t(data[0]) | (uint16_t(data[1]) << 8);
result.humidity = humidity / 10.0f;
return true;
}
case 0x04: { // temperature, 2 bytes, 16-bit signed integer (LE), 0.1 °C
if (data_length != 2)
return false;
const int16_t temperature = uint16_t(data[0]) | (uint16_t(data[1]) << 8);
result.temperature = temperature / 10.0f;
return true;
}
case 0x09: { // conductivity, 2 bytes, 16-bit unsigned integer (LE), 1 µS/cm
if (data_length != 2)
return false;
const uint16_t conductivity = uint16_t(data[0]) | (uint16_t(data[1]) << 8);
result.conductivity = conductivity;
return true;
}
case 0x07: { // illuminance, 3 bytes, 24-bit unsigned integer (LE), 1 lx
if (data_length != 3)
return false;
const uint32_t illuminance = uint32_t(data[0]) | (uint32_t(data[1]) << 8) | (uint32_t(data[2]) << 16);
result.illuminance = illuminance;
return true;
}
case 0x08: { // soil moisture, 1 byte, 8-bit unsigned integer, 1 %
if (data_length != 1)
return false;
result.moisture = data[0];
return true;
}
default:
return false;
}
}
optional<XiaomiParseResult> parse_xiaomi(const esp32_ble_tracker::ESPBTDevice &device) {
if (!device.get_service_data_uuid().has_value()) {
// ESP_LOGVV(TAG, "Xiaomi no service data");
return {};
}
if (!device.get_service_data_uuid()->contains(0x95, 0xFE)) {
// ESP_LOGVV(TAG, "Xiaomi no service data UUID magic bytes");
return {};
}
const auto *raw = reinterpret_cast<const uint8_t *>(device.get_service_data().data());
if (device.get_service_data().size() < 14) {
// ESP_LOGVV(TAG, "Xiaomi service data too short!");
return {};
}
bool is_mijia = (raw[1] & 0x20) == 0x20 && raw[2] == 0xAA && raw[3] == 0x01;
bool is_miflora = (raw[1] & 0x20) == 0x20 && raw[2] == 0x98 && raw[3] == 0x00;
if (!is_mijia && !is_miflora) {
// ESP_LOGVV(TAG, "Xiaomi no magic bytes");
return {};
}
uint8_t raw_offset = is_mijia ? 11 : 12;
const uint8_t raw_type = raw[raw_offset];
const uint8_t data_length = raw[raw_offset + 2];
const uint8_t *data = &raw[raw_offset + 3];
const uint8_t expected_length = data_length + raw_offset + 3;
const uint8_t actual_length = device.get_service_data().size();
if (expected_length != actual_length) {
// ESP_LOGV(TAG, "Xiaomi %s data length mismatch (%u != %d)", type, expected_length, actual_length);
return {};
}
XiaomiParseResult result;
result.type = is_miflora ? XiaomiParseResult::TYPE_MIFLORA : XiaomiParseResult::TYPE_MIJIA;
bool success = parse_xiaomi_data_byte(raw_type, data, data_length, result);
if (!success)
return {};
return result;
}
bool XiaomiListener::parse_device(const esp32_ble_tracker::ESPBTDevice &device) {
auto res = parse_xiaomi(device);
if (!res.has_value())
return false;
const char *name = res->type == XiaomiParseResult::TYPE_MIFLORA ? "Mi Flora" : "Mi Jia";
ESP_LOGD(TAG, "Got Xiaomi %s:", name);
if (res->temperature.has_value()) {
ESP_LOGD(TAG, " Temperature: %.1f°C", *res->temperature);
}
if (res->humidity.has_value()) {
ESP_LOGD(TAG, " Humidity: %.1f%%", *res->humidity);
}
if (res->battery_level.has_value()) {
ESP_LOGD(TAG, " Battery Level: %.0f%%", *res->battery_level);
}
if (res->conductivity.has_value()) {
ESP_LOGD(TAG, " Conductivity: %.0fµS/cm", *res->conductivity);
}
if (res->illuminance.has_value()) {
ESP_LOGD(TAG, " Illuminance: %.0flx", *res->illuminance);
}
if (res->moisture.has_value()) {
ESP_LOGD(TAG, " Moisture: %.0f%%", *res->moisture);
}
return true;
}
void XiaomiListener::setup() { this->setup_ble(); }
XiaomiListener::XiaomiListener(esp32_ble_tracker::ESP32BLETracker *parent) : ESPBTDeviceListener(parent) {}
} // namespace xiaomi_ble
} // namespace esphome
#endif

View File

@@ -0,0 +1,35 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
#ifdef ARDUINO_ARCH_ESP32
namespace esphome {
namespace xiaomi_ble {
struct XiaomiParseResult {
enum { TYPE_MIJIA, TYPE_MIFLORA } type;
optional<float> temperature;
optional<float> humidity;
optional<float> battery_level;
optional<float> conductivity;
optional<float> illuminance;
optional<float> moisture;
};
bool parse_xiaomi_data_byte(uint8_t data_type, const uint8_t *data, uint8_t data_length, XiaomiParseResult &result);
optional<XiaomiParseResult> parse_xiaomi(const esp32_ble_tracker::ESPBTDevice &device);
class XiaomiListener : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
public:
XiaomiListener(esp32_ble_tracker::ESP32BLETracker *parent);
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override;
void setup() override;
};
} // namespace xiaomi_ble
} // namespace esphome
#endif