mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-03 19:38:30 +02: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:
@@ -1,23 +1,18 @@
|
||||
import voluptuous as vol
|
||||
|
||||
from esphome.automation import ACTION_REGISTRY
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.automation import ACTION_REGISTRY
|
||||
from esphome.const import CONF_ACCELERATION, CONF_DECELERATION, CONF_ID, CONF_MAX_SPEED, \
|
||||
CONF_POSITION, CONF_TARGET
|
||||
from esphome.core import CORE
|
||||
from esphome.cpp_generator import Pvariable, add, get_variable, templatable
|
||||
from esphome.cpp_types import Action, esphome_ns, int32
|
||||
from esphome.core import CORE, coroutine
|
||||
|
||||
PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({
|
||||
|
||||
})
|
||||
IS_PLATFORM_COMPONENT = True
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
stepper_ns = esphome_ns.namespace('stepper')
|
||||
stepper_ns = cg.esphome_ns.namespace('stepper')
|
||||
Stepper = stepper_ns.class_('Stepper')
|
||||
|
||||
SetTargetAction = stepper_ns.class_('SetTargetAction', Action)
|
||||
ReportPositionAction = stepper_ns.class_('ReportPositionAction', Action)
|
||||
SetTargetAction = stepper_ns.class_('SetTargetAction', cg.Action)
|
||||
ReportPositionAction = stepper_ns.class_('ReportPositionAction', cg.Action)
|
||||
|
||||
|
||||
def validate_acceleration(value):
|
||||
@@ -32,10 +27,10 @@ def validate_acceleration(value):
|
||||
try:
|
||||
value = float(value)
|
||||
except ValueError:
|
||||
raise vol.Invalid("Expected acceleration as floating point number, got {}".format(value))
|
||||
raise cv.Invalid("Expected acceleration as floating point number, got {}".format(value))
|
||||
|
||||
if value <= 0:
|
||||
raise vol.Invalid("Acceleration must be larger than 0 steps/s^2!")
|
||||
raise cv.Invalid("Acceleration must be larger than 0 steps/s^2!")
|
||||
|
||||
return value
|
||||
|
||||
@@ -52,69 +47,65 @@ def validate_speed(value):
|
||||
try:
|
||||
value = float(value)
|
||||
except ValueError:
|
||||
raise vol.Invalid("Expected speed as floating point number, got {}".format(value))
|
||||
raise cv.Invalid("Expected speed as floating point number, got {}".format(value))
|
||||
|
||||
if value <= 0:
|
||||
raise vol.Invalid("Speed must be larger than 0 steps/s!")
|
||||
raise cv.Invalid("Speed must be larger than 0 steps/s!")
|
||||
|
||||
return value
|
||||
|
||||
|
||||
STEPPER_SCHEMA = cv.Schema({
|
||||
vol.Required(CONF_MAX_SPEED): validate_speed,
|
||||
vol.Optional(CONF_ACCELERATION): validate_acceleration,
|
||||
vol.Optional(CONF_DECELERATION): validate_acceleration,
|
||||
cv.Required(CONF_MAX_SPEED): validate_speed,
|
||||
cv.Optional(CONF_ACCELERATION, default='inf'): validate_acceleration,
|
||||
cv.Optional(CONF_DECELERATION, default='inf'): validate_acceleration,
|
||||
})
|
||||
|
||||
STEPPER_PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(STEPPER_SCHEMA.schema)
|
||||
|
||||
|
||||
@coroutine
|
||||
def setup_stepper_core_(stepper_var, config):
|
||||
if CONF_ACCELERATION in config:
|
||||
add(stepper_var.set_acceleration(config[CONF_ACCELERATION]))
|
||||
cg.add(stepper_var.set_acceleration(config[CONF_ACCELERATION]))
|
||||
if CONF_DECELERATION in config:
|
||||
add(stepper_var.set_deceleration(config[CONF_DECELERATION]))
|
||||
cg.add(stepper_var.set_deceleration(config[CONF_DECELERATION]))
|
||||
if CONF_MAX_SPEED in config:
|
||||
add(stepper_var.set_max_speed(config[CONF_MAX_SPEED]))
|
||||
cg.add(stepper_var.set_max_speed(config[CONF_MAX_SPEED]))
|
||||
|
||||
|
||||
def setup_stepper(stepper_var, config):
|
||||
CORE.add_job(setup_stepper_core_, stepper_var, config)
|
||||
@coroutine
|
||||
def register_stepper(var, config):
|
||||
if not CORE.has_id(config[CONF_ID]):
|
||||
var = cg.Pvariable(config[CONF_ID], var)
|
||||
yield setup_stepper_core_(var, config)
|
||||
|
||||
|
||||
BUILD_FLAGS = '-DUSE_STEPPER'
|
||||
|
||||
CONF_STEPPER_SET_TARGET = 'stepper.set_target'
|
||||
STEPPER_SET_TARGET_ACTION_SCHEMA = cv.Schema({
|
||||
vol.Required(CONF_ID): cv.use_variable_id(Stepper),
|
||||
vol.Required(CONF_TARGET): cv.templatable(cv.int_),
|
||||
})
|
||||
|
||||
|
||||
@ACTION_REGISTRY.register(CONF_STEPPER_SET_TARGET, STEPPER_SET_TARGET_ACTION_SCHEMA)
|
||||
@ACTION_REGISTRY.register('stepper.set_target', cv.Schema({
|
||||
cv.Required(CONF_ID): cv.use_variable_id(Stepper),
|
||||
cv.Required(CONF_TARGET): cv.templatable(cv.int_),
|
||||
}))
|
||||
def stepper_set_target_to_code(config, action_id, template_arg, args):
|
||||
var = yield get_variable(config[CONF_ID])
|
||||
rhs = var.make_set_target_action(template_arg)
|
||||
var = yield cg.get_variable(config[CONF_ID])
|
||||
type = SetTargetAction.template(template_arg)
|
||||
action = Pvariable(action_id, rhs, type=type)
|
||||
template_ = yield templatable(config[CONF_TARGET], args, int32)
|
||||
add(action.set_target(template_))
|
||||
rhs = type.new(var)
|
||||
action = cg.Pvariable(action_id, rhs, type=type)
|
||||
template_ = yield cg.templatable(config[CONF_TARGET], args, cg.int32)
|
||||
cg.add(action.set_target(template_))
|
||||
yield action
|
||||
|
||||
|
||||
CONF_STEPPER_REPORT_POSITION = 'stepper.report_position'
|
||||
STEPPER_REPORT_POSITION_ACTION_SCHEMA = cv.Schema({
|
||||
vol.Required(CONF_ID): cv.use_variable_id(Stepper),
|
||||
vol.Required(CONF_POSITION): cv.templatable(cv.int_),
|
||||
})
|
||||
|
||||
|
||||
@ACTION_REGISTRY.register(CONF_STEPPER_REPORT_POSITION, STEPPER_REPORT_POSITION_ACTION_SCHEMA)
|
||||
@ACTION_REGISTRY.register('stepper.report_position', cv.Schema({
|
||||
cv.Required(CONF_ID): cv.use_variable_id(Stepper),
|
||||
cv.Required(CONF_POSITION): cv.templatable(cv.int_),
|
||||
}))
|
||||
def stepper_report_position_to_code(config, action_id, template_arg, args):
|
||||
var = yield get_variable(config[CONF_ID])
|
||||
rhs = var.make_report_position_action(template_arg)
|
||||
var = yield cg.get_variable(config[CONF_ID])
|
||||
type = ReportPositionAction.template(template_arg)
|
||||
action = Pvariable(action_id, rhs, type=type)
|
||||
template_ = yield templatable(config[CONF_POSITION], args, int32)
|
||||
add(action.set_position(template_))
|
||||
rhs = type.new(var)
|
||||
action = cg.Pvariable(action_id, rhs, type=type)
|
||||
template_ = yield cg.templatable(config[CONF_POSITION], args, cg.int32)
|
||||
cg.add(action.set_position(template_))
|
||||
yield action
|
||||
|
||||
|
||||
def to_code(config):
|
||||
cg.add_global(stepper_ns.using)
|
||||
|
||||
Reference in New Issue
Block a user