mirror of
https://github.com/esphome/esphome.git
synced 2025-10-29 22:24:26 +00: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:
30
esphome/components/rdm6300/__init__.py
Normal file
30
esphome/components/rdm6300/__init__.py
Normal file
@@ -0,0 +1,30 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome import automation
|
||||
from esphome.components import uart
|
||||
from esphome.const import CONF_ID, CONF_ON_TAG, CONF_TRIGGER_ID
|
||||
|
||||
DEPENDENCIES = ['uart']
|
||||
AUTO_LOAD = ['binary_sensor']
|
||||
|
||||
rdm6300_ns = cg.esphome_ns.namespace('rdm6300')
|
||||
RDM6300Component = rdm6300_ns.class_('RDM6300Component', cg.Component, uart.UARTDevice)
|
||||
RDM6300Trigger = rdm6300_ns.class_('RDM6300Trigger', cg.Trigger.template(cg.uint32))
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema({
|
||||
cv.GenerateID(): cv.declare_variable_id(RDM6300Component),
|
||||
cv.Optional(CONF_ON_TAG): automation.validate_automation({
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_variable_id(RDM6300Trigger),
|
||||
}),
|
||||
}).extend(cv.COMPONENT_SCHEMA).extend(uart.UART_DEVICE_SCHEMA)
|
||||
|
||||
|
||||
def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
yield cg.register_component(var, config)
|
||||
yield uart.register_uart_device(var, config)
|
||||
|
||||
for conf in config.get(CONF_ON_TAG, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID])
|
||||
cg.add(var.register_trigger(trigger))
|
||||
yield automation.build_automation(trigger, [(cg.uint32, 'x')], conf)
|
||||
23
esphome/components/rdm6300/binary_sensor.py
Normal file
23
esphome/components/rdm6300/binary_sensor.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import binary_sensor, rdm6300
|
||||
from esphome.const import CONF_UID, CONF_ID, CONF_NAME
|
||||
from . import rdm6300_ns
|
||||
|
||||
DEPENDENCIES = ['rdm6300']
|
||||
|
||||
CONF_RDM6300_ID = 'rdm6300_id'
|
||||
RDM6300BinarySensor = rdm6300_ns.class_('RDM6300BinarySensor', binary_sensor.BinarySensor)
|
||||
|
||||
CONFIG_SCHEMA = cv.nameable(binary_sensor.BINARY_SENSOR_SCHEMA.extend({
|
||||
cv.GenerateID(): cv.declare_variable_id(RDM6300BinarySensor),
|
||||
cv.GenerateID(CONF_RDM6300_ID): cv.use_variable_id(rdm6300.RDM6300Component),
|
||||
cv.Required(CONF_UID): cv.uint32_t,
|
||||
}))
|
||||
|
||||
|
||||
def to_code(config):
|
||||
hub = yield cg.get_variable(config[CONF_RDM6300_ID])
|
||||
var = cg.new_Pvariable(config[CONF_ID], config[CONF_NAME], config[CONF_UID])
|
||||
yield binary_sensor.register_binary_sensor(var, config)
|
||||
cg.add(hub.register_card(var))
|
||||
69
esphome/components/rdm6300/rdm6300.cpp
Normal file
69
esphome/components/rdm6300/rdm6300.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#include "rdm6300.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace rdm6300 {
|
||||
|
||||
static const char *TAG = "rdm6300";
|
||||
|
||||
static const uint8_t RDM6300_START_BYTE = 0x02;
|
||||
static const uint8_t RDM6300_END_BYTE = 0x03;
|
||||
static const int8_t RDM6300_STATE_WAITING_FOR_START = -1;
|
||||
|
||||
void rdm6300::RDM6300Component::loop() {
|
||||
while (this->available() > 0) {
|
||||
uint8_t data;
|
||||
if (!this->read_byte(&data)) {
|
||||
ESP_LOGW(TAG, "Reading data from RDM6300 failed!");
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->read_state_ == RDM6300_STATE_WAITING_FOR_START) {
|
||||
if (data == RDM6300_START_BYTE) {
|
||||
this->read_state_ = 0;
|
||||
} else {
|
||||
// Not start byte, probably not synced up correctly.
|
||||
}
|
||||
} else if (this->read_state_ < 12) {
|
||||
uint8_t value = (data > '9') ? data - '7' : data - '0';
|
||||
if (this->read_state_ % 2 == 0) {
|
||||
this->buffer_[this->read_state_ / 2] = value << 4;
|
||||
} else {
|
||||
this->buffer_[this->read_state_ / 2] += value;
|
||||
}
|
||||
this->read_state_++;
|
||||
} else if (data != RDM6300_END_BYTE) {
|
||||
ESP_LOGW(TAG, "Invalid end byte from RDM6300!");
|
||||
this->read_state_ = RDM6300_STATE_WAITING_FOR_START;
|
||||
} else {
|
||||
uint8_t checksum = 0;
|
||||
for (uint8_t i = 0; i < 5; i++)
|
||||
checksum ^= this->buffer_[i];
|
||||
this->read_state_ = RDM6300_STATE_WAITING_FOR_START;
|
||||
if (checksum != this->buffer_[5]) {
|
||||
ESP_LOGW(TAG, "Checksum from RDM6300 doesn't match! (0x%02X!=0x%02X)", checksum, this->buffer_[5]);
|
||||
} else {
|
||||
// Valid data
|
||||
this->status_clear_warning();
|
||||
const uint32_t result = (uint32_t(this->buffer_[1]) << 24) | (uint32_t(this->buffer_[2]) << 16) |
|
||||
(uint32_t(this->buffer_[3]) << 8) | this->buffer_[4];
|
||||
bool report = result != last_id_;
|
||||
for (auto *card : this->cards_) {
|
||||
if (card->process(result)) {
|
||||
report = false;
|
||||
}
|
||||
}
|
||||
for (auto *trig : this->triggers_)
|
||||
trig->process(result);
|
||||
|
||||
if (report) {
|
||||
ESP_LOGD(TAG, "Found new tag with ID %u", result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace rdm6300
|
||||
} // namespace esphome
|
||||
55
esphome/components/rdm6300/rdm6300.h
Normal file
55
esphome/components/rdm6300/rdm6300.h
Normal file
@@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/components/binary_sensor/binary_sensor.h"
|
||||
#include "esphome/components/uart/uart.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace rdm6300 {
|
||||
|
||||
class RDM6300BinarySensor;
|
||||
class RDM6300Trigger;
|
||||
|
||||
class RDM6300Component : public Component, public uart::UARTDevice {
|
||||
public:
|
||||
void loop() override;
|
||||
|
||||
void register_card(RDM6300BinarySensor *obj) { this->cards_.push_back(obj); }
|
||||
void register_trigger(RDM6300Trigger *trig) { this->triggers_.push_back(trig); }
|
||||
|
||||
float get_setup_priority() const override { return setup_priority::DATA; }
|
||||
|
||||
protected:
|
||||
int8_t read_state_{-1};
|
||||
uint8_t buffer_[6]{};
|
||||
std::vector<RDM6300BinarySensor *> cards_;
|
||||
std::vector<RDM6300Trigger *> triggers_;
|
||||
uint32_t last_id_{0};
|
||||
};
|
||||
|
||||
class RDM6300BinarySensor : public binary_sensor::BinarySensor {
|
||||
public:
|
||||
RDM6300BinarySensor(const std::string &name, uint32_t id) : BinarySensor(name), id_(id) {}
|
||||
|
||||
bool process(uint32_t id) {
|
||||
if (this->id_ == id) {
|
||||
this->publish_state(true);
|
||||
yield();
|
||||
this->publish_state(false);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected:
|
||||
uint32_t id_;
|
||||
};
|
||||
|
||||
class RDM6300Trigger : public Trigger<uint32_t> {
|
||||
public:
|
||||
void process(uint32_t uid) { this->trigger(uid); }
|
||||
};
|
||||
|
||||
} // namespace rdm6300
|
||||
} // namespace esphome
|
||||
Reference in New Issue
Block a user