mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-06 21:09:53 +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,28 +1,21 @@
|
||||
import voluptuous as vol
|
||||
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.automation import ACTION_REGISTRY, maybe_simple_id, Condition
|
||||
from esphome.components import mqtt
|
||||
from esphome.components.mqtt import setup_mqtt_component
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_ID, CONF_INTERNAL, CONF_MQTT_ID, CONF_DEVICE_CLASS, CONF_STATE, \
|
||||
CONF_POSITION, CONF_TILT, CONF_STOP
|
||||
from esphome.core import CORE
|
||||
from esphome.cpp_generator import Pvariable, add, get_variable, templatable
|
||||
from esphome.cpp_types import Action, Nameable, esphome_ns, App
|
||||
from esphome.const import CONF_ID, CONF_INTERNAL, CONF_DEVICE_CLASS, CONF_STATE, \
|
||||
CONF_POSITION, CONF_TILT, CONF_STOP, CONF_MQTT_ID
|
||||
from esphome.core import CORE, coroutine
|
||||
|
||||
PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({
|
||||
|
||||
})
|
||||
IS_PLATFORM_COMPONENT = True
|
||||
|
||||
DEVICE_CLASSES = [
|
||||
'', 'awning', 'blind', 'curtain', 'damper', 'door', 'garage',
|
||||
'shade', 'shutter', 'window'
|
||||
]
|
||||
|
||||
cover_ns = esphome_ns.namespace('cover')
|
||||
cover_ns = cg.esphome_ns.namespace('cover')
|
||||
|
||||
Cover = cover_ns.class_('Cover', Nameable)
|
||||
MQTTCoverComponent = cover_ns.class_('MQTTCoverComponent', mqtt.MQTTComponent)
|
||||
Cover = cover_ns.class_('Cover', cg.Nameable)
|
||||
|
||||
COVER_OPEN = cover_ns.COVER_OPEN
|
||||
COVER_CLOSED = cover_ns.COVER_CLOSED
|
||||
@@ -42,112 +35,102 @@ COVER_OPERATIONS = {
|
||||
validate_cover_operation = cv.one_of(*COVER_OPERATIONS, upper=True)
|
||||
|
||||
# Actions
|
||||
OpenAction = cover_ns.class_('OpenAction', Action)
|
||||
CloseAction = cover_ns.class_('CloseAction', Action)
|
||||
StopAction = cover_ns.class_('StopAction', Action)
|
||||
ControlAction = cover_ns.class_('ControlAction', Action)
|
||||
OpenAction = cover_ns.class_('OpenAction', cg.Action)
|
||||
CloseAction = cover_ns.class_('CloseAction', cg.Action)
|
||||
StopAction = cover_ns.class_('StopAction', cg.Action)
|
||||
ControlAction = cover_ns.class_('ControlAction', cg.Action)
|
||||
CoverPublishAction = cover_ns.class_('CoverPublishAction', cg.Action)
|
||||
CoverIsOpenCondition = cover_ns.class_('CoverIsOpenCondition', Condition)
|
||||
CoverIsClosedCondition = cover_ns.class_('CoverIsClosedCondition', Condition)
|
||||
|
||||
COVER_SCHEMA = cv.MQTT_COMMAND_COMPONENT_SCHEMA.extend({
|
||||
cv.GenerateID(): cv.declare_variable_id(Cover),
|
||||
cv.GenerateID(CONF_MQTT_ID): cv.declare_variable_id(MQTTCoverComponent),
|
||||
vol.Optional(CONF_DEVICE_CLASS): cv.one_of(*DEVICE_CLASSES, lower=True),
|
||||
cv.OnlyWith(CONF_MQTT_ID, 'mqtt'): cv.declare_variable_id(mqtt.MQTTCoverComponent),
|
||||
cv.Optional(CONF_DEVICE_CLASS): cv.one_of(*DEVICE_CLASSES, lower=True),
|
||||
# TODO: MQTT topic options
|
||||
})
|
||||
|
||||
COVER_PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(COVER_SCHEMA.schema)
|
||||
|
||||
|
||||
def setup_cover_core_(cover_var, config):
|
||||
@coroutine
|
||||
def setup_cover_core_(var, config):
|
||||
if CONF_INTERNAL in config:
|
||||
add(cover_var.set_internal(config[CONF_INTERNAL]))
|
||||
cg.add(var.set_internal(config[CONF_INTERNAL]))
|
||||
if CONF_DEVICE_CLASS in config:
|
||||
add(cover_var.set_device_class(config[CONF_DEVICE_CLASS]))
|
||||
setup_mqtt_component(cover_var.Pget_mqtt(), config)
|
||||
|
||||
|
||||
def setup_cover(cover_obj, config):
|
||||
CORE.add_job(setup_cover_core_, cover_obj, config)
|
||||
cg.add(var.set_device_class(config[CONF_DEVICE_CLASS]))
|
||||
|
||||
if CONF_MQTT_ID in config:
|
||||
mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], var)
|
||||
yield mqtt.register_mqtt_component(mqtt_, config)
|
||||
|
||||
|
||||
@coroutine
|
||||
def register_cover(var, config):
|
||||
if not CORE.has_id(config[CONF_ID]):
|
||||
var = Pvariable(config[CONF_ID], var, has_side_effects=True)
|
||||
add(App.register_cover(var))
|
||||
CORE.add_job(setup_cover_core_, var, config)
|
||||
var = cg.Pvariable(config[CONF_ID], var)
|
||||
cg.add(cg.App.register_cover(var))
|
||||
yield setup_cover_core_(var, config)
|
||||
|
||||
|
||||
BUILD_FLAGS = '-DUSE_COVER'
|
||||
|
||||
CONF_COVER_OPEN = 'cover.open'
|
||||
COVER_OPEN_ACTION_SCHEMA = maybe_simple_id({
|
||||
vol.Required(CONF_ID): cv.use_variable_id(Cover),
|
||||
COVER_ACTION_SCHEMA = maybe_simple_id({
|
||||
cv.Required(CONF_ID): cv.use_variable_id(Cover),
|
||||
})
|
||||
|
||||
|
||||
@ACTION_REGISTRY.register(CONF_COVER_OPEN, COVER_OPEN_ACTION_SCHEMA)
|
||||
@ACTION_REGISTRY.register('cover.open', COVER_ACTION_SCHEMA)
|
||||
def cover_open_to_code(config, action_id, template_arg, args):
|
||||
var = yield get_variable(config[CONF_ID])
|
||||
var = yield cg.get_variable(config[CONF_ID])
|
||||
type = OpenAction.template(template_arg)
|
||||
rhs = type.new(var)
|
||||
yield Pvariable(action_id, rhs, type=type)
|
||||
yield cg.Pvariable(action_id, rhs, type=type)
|
||||
|
||||
|
||||
CONF_COVER_CLOSE = 'cover.close'
|
||||
COVER_CLOSE_ACTION_SCHEMA = maybe_simple_id({
|
||||
vol.Required(CONF_ID): cv.use_variable_id(Cover),
|
||||
})
|
||||
|
||||
|
||||
@ACTION_REGISTRY.register(CONF_COVER_CLOSE, COVER_CLOSE_ACTION_SCHEMA)
|
||||
@ACTION_REGISTRY.register('cover.close', COVER_ACTION_SCHEMA)
|
||||
def cover_close_to_code(config, action_id, template_arg, args):
|
||||
var = yield get_variable(config[CONF_ID])
|
||||
var = yield cg.get_variable(config[CONF_ID])
|
||||
type = CloseAction.template(template_arg)
|
||||
rhs = type.new(var)
|
||||
yield Pvariable(action_id, rhs, type=type)
|
||||
yield cg.Pvariable(action_id, rhs, type=type)
|
||||
|
||||
|
||||
CONF_COVER_STOP = 'cover.stop'
|
||||
COVER_STOP_ACTION_SCHEMA = maybe_simple_id({
|
||||
vol.Required(CONF_ID): cv.use_variable_id(Cover),
|
||||
})
|
||||
|
||||
|
||||
@ACTION_REGISTRY.register(CONF_COVER_STOP, COVER_STOP_ACTION_SCHEMA)
|
||||
@ACTION_REGISTRY.register('cover.stop', COVER_ACTION_SCHEMA)
|
||||
def cover_stop_to_code(config, action_id, template_arg, args):
|
||||
var = yield get_variable(config[CONF_ID])
|
||||
var = yield cg.get_variable(config[CONF_ID])
|
||||
type = StopAction.template(template_arg)
|
||||
rhs = type.new(var)
|
||||
yield Pvariable(action_id, rhs, type=type)
|
||||
yield cg.Pvariable(action_id, rhs, type=type)
|
||||
|
||||
|
||||
CONF_COVER_CONTROL = 'cover.control'
|
||||
COVER_CONTROL_ACTION_SCHEMA = cv.Schema({
|
||||
vol.Required(CONF_ID): cv.use_variable_id(Cover),
|
||||
vol.Optional(CONF_STOP): cv.templatable(cv.boolean),
|
||||
vol.Exclusive(CONF_STATE, 'pos'): cv.templatable(cv.one_of(*COVER_STATES)),
|
||||
vol.Exclusive(CONF_POSITION, 'pos'): cv.templatable(cv.percentage),
|
||||
vol.Optional(CONF_TILT): cv.templatable(cv.percentage),
|
||||
cv.Required(CONF_ID): cv.use_variable_id(Cover),
|
||||
cv.Optional(CONF_STOP): cv.templatable(cv.boolean),
|
||||
cv.Exclusive(CONF_STATE, 'pos'): cv.templatable(cv.one_of(*COVER_STATES)),
|
||||
cv.Exclusive(CONF_POSITION, 'pos'): cv.templatable(cv.percentage),
|
||||
cv.Optional(CONF_TILT): cv.templatable(cv.percentage),
|
||||
})
|
||||
|
||||
|
||||
@ACTION_REGISTRY.register(CONF_COVER_CONTROL, COVER_CONTROL_ACTION_SCHEMA)
|
||||
@ACTION_REGISTRY.register('cover.control', COVER_CONTROL_ACTION_SCHEMA)
|
||||
def cover_control_to_code(config, action_id, template_arg, args):
|
||||
var = yield get_variable(config[CONF_ID])
|
||||
var = yield cg.get_variable(config[CONF_ID])
|
||||
type = StopAction.template(template_arg)
|
||||
rhs = type.new(var)
|
||||
action = Pvariable(action_id, rhs, type=type)
|
||||
action = cg.Pvariable(action_id, rhs, type=type)
|
||||
if CONF_STOP in config:
|
||||
template_ = yield templatable(config[CONF_STOP], args, bool)
|
||||
add(action.set_stop(template_))
|
||||
template_ = yield cg.templatable(config[CONF_STOP], args, bool)
|
||||
cg.add(action.set_stop(template_))
|
||||
if CONF_STATE in config:
|
||||
template_ = yield templatable(config[CONF_STATE], args, float,
|
||||
to_exp=COVER_STATES)
|
||||
add(action.set_position(template_))
|
||||
template_ = yield cg.templatable(config[CONF_STATE], args, float,
|
||||
to_exp=COVER_STATES)
|
||||
cg.add(action.set_position(template_))
|
||||
if CONF_POSITION in config:
|
||||
template_ = yield templatable(config[CONF_POSITION], args, float)
|
||||
add(action.set_position(template_))
|
||||
template_ = yield cg.templatable(config[CONF_POSITION], args, float)
|
||||
cg.add(action.set_position(template_))
|
||||
if CONF_TILT in config:
|
||||
template_ = yield templatable(config[CONF_TILT], args, float)
|
||||
add(action.set_tilt(template_))
|
||||
template_ = yield cg.templatable(config[CONF_TILT], args, float)
|
||||
cg.add(action.set_tilt(template_))
|
||||
yield action
|
||||
|
||||
|
||||
def to_code(config):
|
||||
cg.add_define('USE_COVER')
|
||||
cg.add_global(cover_ns.using)
|
||||
|
||||
Reference in New Issue
Block a user