🏗 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
+64 -96
View File
@@ -1,146 +1,114 @@
import voluptuous as vol
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation
from esphome.automation import ACTION_REGISTRY, CONDITION_REGISTRY, Condition, maybe_simple_id
from esphome.components import mqtt
from esphome.components.mqtt import setup_mqtt_component
import esphome.config_validation as cv
from esphome.const import CONF_ICON, CONF_ID, CONF_INTERNAL, CONF_INVERTED, CONF_MQTT_ID, \
CONF_ON_TURN_OFF, CONF_ON_TURN_ON, CONF_TRIGGER_ID
from esphome.core import CORE
from esphome.cpp_generator import Pvariable, add, get_variable
from esphome.cpp_types import Action, App, Nameable, Trigger, esphome_ns
from esphome.const import CONF_ICON, CONF_ID, CONF_INTERNAL, CONF_INVERTED, CONF_ON_TURN_OFF, \
CONF_ON_TURN_ON, CONF_TRIGGER_ID, CONF_MQTT_ID
from esphome.core import CORE, coroutine
PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({
IS_PLATFORM_COMPONENT = True
})
switch_ns = esphome_ns.namespace('switch_')
Switch = switch_ns.class_('Switch', Nameable)
switch_ns = cg.esphome_ns.namespace('switch_')
Switch = switch_ns.class_('Switch', cg.Nameable)
SwitchPtr = Switch.operator('ptr')
MQTTSwitchComponent = switch_ns.class_('MQTTSwitchComponent', mqtt.MQTTComponent)
ToggleAction = switch_ns.class_('ToggleAction', Action)
TurnOffAction = switch_ns.class_('TurnOffAction', Action)
TurnOnAction = switch_ns.class_('TurnOnAction', Action)
ToggleAction = switch_ns.class_('ToggleAction', cg.Action)
TurnOffAction = switch_ns.class_('TurnOffAction', cg.Action)
TurnOnAction = switch_ns.class_('TurnOnAction', cg.Action)
SwitchPublishAction = switch_ns.class_('SwitchPublishAction', cg.Action)
SwitchCondition = switch_ns.class_('SwitchCondition', Condition)
SwitchTurnOnTrigger = switch_ns.class_('SwitchTurnOnTrigger', Trigger.template())
SwitchTurnOffTrigger = switch_ns.class_('SwitchTurnOffTrigger', Trigger.template())
SwitchTurnOnTrigger = switch_ns.class_('SwitchTurnOnTrigger', cg.Trigger.template())
SwitchTurnOffTrigger = switch_ns.class_('SwitchTurnOffTrigger', cg.Trigger.template())
icon = cv.icon
SWITCH_SCHEMA = cv.MQTT_COMMAND_COMPONENT_SCHEMA.extend({
cv.GenerateID(CONF_MQTT_ID): cv.declare_variable_id(MQTTSwitchComponent),
vol.Optional(CONF_ICON): cv.icon,
vol.Optional(CONF_INVERTED): cv.boolean,
vol.Optional(CONF_ON_TURN_ON): automation.validate_automation({
cv.OnlyWith(CONF_MQTT_ID, 'mqtt'): cv.declare_variable_id(mqtt.MQTTSwitchComponent),
cv.Optional(CONF_ICON): icon,
cv.Optional(CONF_INVERTED): cv.boolean,
cv.Optional(CONF_ON_TURN_ON): automation.validate_automation({
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_variable_id(SwitchTurnOnTrigger),
}),
vol.Optional(CONF_ON_TURN_OFF): automation.validate_automation({
cv.Optional(CONF_ON_TURN_OFF): automation.validate_automation({
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_variable_id(SwitchTurnOffTrigger),
}),
})
SWITCH_PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(SWITCH_SCHEMA.schema)
def setup_switch_core_(switch_var, config):
def setup_switch_core_(var, config):
if CONF_INTERNAL in config:
add(switch_var.set_internal(config[CONF_INTERNAL]))
cg.add(var.set_internal(config[CONF_INTERNAL]))
if CONF_ICON in config:
add(switch_var.set_icon(config[CONF_ICON]))
cg.add(var.set_icon(config[CONF_ICON]))
if CONF_INVERTED in config:
add(switch_var.set_inverted(config[CONF_INVERTED]))
cg.add(var.set_inverted(config[CONF_INVERTED]))
for conf in config.get(CONF_ON_TURN_ON, []):
rhs = switch_var.make_switch_turn_on_trigger()
trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
automation.build_automations(trigger, [], conf)
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
yield automation.build_automation(trigger, [], conf)
for conf in config.get(CONF_ON_TURN_OFF, []):
rhs = switch_var.make_switch_turn_off_trigger()
trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
automation.build_automations(trigger, [], conf)
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
yield automation.build_automation(trigger, [], conf)
setup_mqtt_component(switch_var.Pget_mqtt(), config)
def setup_switch(switch_obj, config):
if not CORE.has_id(config[CONF_ID]):
switch_obj = Pvariable(config[CONF_ID], switch_obj, has_side_effects=True)
CORE.add_job(setup_switch_core_, switch_obj, config)
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_switch(var, config):
switch_var = Pvariable(config[CONF_ID], var, has_side_effects=True)
add(App.register_switch(switch_var))
CORE.add_job(setup_switch_core_, switch_var, config)
if not CORE.has_id(config[CONF_ID]):
var = cg.Pvariable(config[CONF_ID], var)
cg.add(cg.App.register_switch(var))
yield setup_switch_core_(var, config)
BUILD_FLAGS = '-DUSE_SWITCH'
CONF_SWITCH_TOGGLE = 'switch.toggle'
SWITCH_TOGGLE_ACTION_SCHEMA = maybe_simple_id({
vol.Required(CONF_ID): cv.use_variable_id(Switch),
SWITCH_ACTION_SCHEMA = maybe_simple_id({
cv.Required(CONF_ID): cv.use_variable_id(Switch),
})
@ACTION_REGISTRY.register(CONF_SWITCH_TOGGLE, SWITCH_TOGGLE_ACTION_SCHEMA)
@ACTION_REGISTRY.register('switch.toggle', SWITCH_ACTION_SCHEMA)
def switch_toggle_to_code(config, action_id, template_arg, args):
var = yield get_variable(config[CONF_ID])
rhs = var.make_toggle_action(template_arg)
var = yield cg.get_variable(config[CONF_ID])
type = ToggleAction.template(template_arg)
yield Pvariable(action_id, rhs, type=type)
rhs = type.new(var)
yield cg.Pvariable(action_id, rhs, type=type)
CONF_SWITCH_TURN_OFF = 'switch.turn_off'
SWITCH_TURN_OFF_ACTION_SCHEMA = maybe_simple_id({
vol.Required(CONF_ID): cv.use_variable_id(Switch),
})
@ACTION_REGISTRY.register(CONF_SWITCH_TURN_OFF, SWITCH_TURN_OFF_ACTION_SCHEMA)
@ACTION_REGISTRY.register('switch.turn_off', SWITCH_ACTION_SCHEMA)
def switch_turn_off_to_code(config, action_id, template_arg, args):
var = yield get_variable(config[CONF_ID])
rhs = var.make_turn_off_action(template_arg)
var = yield cg.get_variable(config[CONF_ID])
type = TurnOffAction.template(template_arg)
yield Pvariable(action_id, rhs, type=type)
rhs = type.new(var)
yield cg.Pvariable(action_id, rhs, type=type)
CONF_SWITCH_TURN_ON = 'switch.turn_on'
SWITCH_TURN_ON_ACTION_SCHEMA = maybe_simple_id({
vol.Required(CONF_ID): cv.use_variable_id(Switch),
})
@ACTION_REGISTRY.register(CONF_SWITCH_TURN_ON, SWITCH_TURN_ON_ACTION_SCHEMA)
@ACTION_REGISTRY.register('switch.turn_on', SWITCH_ACTION_SCHEMA)
def switch_turn_on_to_code(config, action_id, template_arg, args):
var = yield get_variable(config[CONF_ID])
rhs = var.make_turn_on_action(template_arg)
var = yield cg.get_variable(config[CONF_ID])
type = TurnOnAction.template(template_arg)
yield Pvariable(action_id, rhs, type=type)
rhs = type.new(var)
yield cg.Pvariable(action_id, rhs, type=type)
CONF_SWITCH_IS_ON = 'switch.is_on'
SWITCH_IS_ON_CONDITION_SCHEMA = maybe_simple_id({
vol.Required(CONF_ID): cv.use_variable_id(Switch),
})
@CONDITION_REGISTRY.register(CONF_SWITCH_IS_ON, SWITCH_IS_ON_CONDITION_SCHEMA)
@CONDITION_REGISTRY.register('switch.is_on', SWITCH_ACTION_SCHEMA)
def switch_is_on_to_code(config, condition_id, template_arg, args):
var = yield get_variable(config[CONF_ID])
rhs = var.make_switch_is_on_condition(template_arg)
var = yield cg.get_variable(config[CONF_ID])
type = SwitchCondition.template(template_arg)
yield Pvariable(condition_id, rhs, type=type)
rhs = type.new(var, True)
yield cg.Pvariable(condition_id, rhs, type=type)
CONF_SWITCH_IS_OFF = 'switch.is_off'
SWITCH_IS_OFF_CONDITION_SCHEMA = maybe_simple_id({
vol.Required(CONF_ID): cv.use_variable_id(Switch),
})
@CONDITION_REGISTRY.register(CONF_SWITCH_IS_OFF, SWITCH_IS_OFF_CONDITION_SCHEMA)
@CONDITION_REGISTRY.register('switch.is_off', SWITCH_ACTION_SCHEMA)
def switch_is_off_to_code(config, condition_id, template_arg, args):
var = yield get_variable(config[CONF_ID])
rhs = var.make_switch_is_off_condition(template_arg)
var = yield cg.get_variable(config[CONF_ID])
type = SwitchCondition.template(template_arg)
yield Pvariable(condition_id, rhs, type=type)
rhs = type.new(var, False)
yield cg.Pvariable(condition_id, rhs, type=type)
def to_code(config):
cg.add_define('USE_SWITCH')
+10
View File
@@ -0,0 +1,10 @@
#include "automation.h"
#include "esphome/core/log.h"
namespace esphome {
namespace switch_ {
static const char *TAG = "switch.automation";
} // namespace switch_
} // namespace esphome
+95
View File
@@ -0,0 +1,95 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/components/switch/switch.h"
namespace esphome {
namespace switch_ {
template<typename... Ts> class TurnOnAction : public Action<Ts...> {
public:
explicit TurnOnAction(Switch *a_switch) : switch_(a_switch) {}
void play(Ts... x) override {
this->switch_->turn_on();
this->play_next(x...);
}
protected:
Switch *switch_;
};
template<typename... Ts> class TurnOffAction : public Action<Ts...> {
public:
explicit TurnOffAction(Switch *a_switch) : switch_(a_switch) {}
void play(Ts... x) override {
this->switch_->turn_off();
this->play_next(x...);
}
protected:
Switch *switch_;
};
template<typename... Ts> class ToggleAction : public Action<Ts...> {
public:
explicit ToggleAction(Switch *a_switch) : switch_(a_switch) {}
void play(Ts... x) override {
this->switch_->toggle();
this->play_next(x...);
}
protected:
Switch *switch_;
};
template<typename... Ts> class SwitchCondition : public Condition<Ts...> {
public:
SwitchCondition(Switch *parent, bool state) : parent_(parent), state_(state) {}
bool check(Ts... x) override { return this->parent_->state == this->state_; }
protected:
Switch *parent_;
bool state_;
};
class SwitchTurnOnTrigger : public Trigger<> {
public:
SwitchTurnOnTrigger(Switch *a_switch) {
a_switch->add_on_state_callback([this](bool state) {
if (state) {
this->trigger();
}
});
}
};
class SwitchTurnOffTrigger : public Trigger<> {
public:
SwitchTurnOffTrigger(Switch *a_switch) {
a_switch->add_on_state_callback([this](bool state) {
if (!state) {
this->trigger();
}
});
}
};
template<typename... Ts> class SwitchPublishAction : public Action<Ts...> {
public:
SwitchPublishAction(Switch *a_switch) : switch_(a_switch) {}
TEMPLATABLE_VALUE(bool, state)
void play(Ts... x) override {
this->switch_->publish_state(this->state_.value(x...));
this->play_next(x...);
}
protected:
Switch *switch_;
};
} // namespace switch_
} // namespace esphome
-33
View File
@@ -1,33 +0,0 @@
import voluptuous as vol
from esphome.components import switch
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_LAMBDA, CONF_NAME, CONF_SWITCHES
from esphome.cpp_generator import add, process_lambda, variable
from esphome.cpp_types import std_vector
CustomSwitchConstructor = switch.switch_ns.class_('CustomSwitchConstructor')
PLATFORM_SCHEMA = switch.PLATFORM_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(CustomSwitchConstructor),
vol.Required(CONF_LAMBDA): cv.lambda_,
vol.Required(CONF_SWITCHES):
cv.ensure_list(switch.SWITCH_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(switch.Switch),
})),
})
def to_code(config):
template_ = yield process_lambda(config[CONF_LAMBDA], [],
return_type=std_vector.template(switch.SwitchPtr))
rhs = CustomSwitchConstructor(template_)
custom = variable(config[CONF_ID], rhs)
for i, conf in enumerate(config[CONF_SWITCHES]):
rhs = custom.Pget_switch(i)
add(rhs.set_name(conf[CONF_NAME]))
switch.register_switch(rhs, conf)
BUILD_FLAGS = '-DUSE_CUSTOM_SWITCH'
-48
View File
@@ -1,48 +0,0 @@
import voluptuous as vol
from esphome import pins
from esphome.components import switch
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_INTERLOCK, CONF_NAME, CONF_PIN, CONF_RESTORE_MODE
from esphome.cpp_generator import Pvariable, add, get_variable
from esphome.cpp_helpers import gpio_output_pin_expression, setup_component
from esphome.cpp_types import App, Component
GPIOSwitch = switch.switch_ns.class_('GPIOSwitch', switch.Switch, Component)
GPIOSwitchRestoreMode = switch.switch_ns.enum('GPIOSwitchRestoreMode')
RESTORE_MODES = {
'RESTORE_DEFAULT_OFF': GPIOSwitchRestoreMode.GPIO_SWITCH_RESTORE_DEFAULT_OFF,
'RESTORE_DEFAULT_ON': GPIOSwitchRestoreMode.GPIO_SWITCH_RESTORE_DEFAULT_ON,
'ALWAYS_OFF': GPIOSwitchRestoreMode.GPIO_SWITCH_ALWAYS_OFF,
'ALWAYS_ON': GPIOSwitchRestoreMode.GPIO_SWITCH_ALWAYS_ON,
}
PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(GPIOSwitch),
vol.Required(CONF_PIN): pins.gpio_output_pin_schema,
vol.Optional(CONF_RESTORE_MODE): cv.one_of(*RESTORE_MODES, upper=True, space='_'),
vol.Optional(CONF_INTERLOCK): cv.ensure_list(cv.use_variable_id(switch.Switch)),
}).extend(cv.COMPONENT_SCHEMA.schema))
def to_code(config):
pin = yield gpio_output_pin_expression(config[CONF_PIN])
rhs = App.make_gpio_switch(config[CONF_NAME], pin)
gpio = Pvariable(config[CONF_ID], rhs)
if CONF_RESTORE_MODE in config:
add(gpio.set_restore_mode(RESTORE_MODES[config[CONF_RESTORE_MODE]]))
if CONF_INTERLOCK in config:
interlock = []
for it in config[CONF_INTERLOCK]:
lock = yield get_variable(it)
interlock.append(lock)
add(gpio.set_interlock(interlock))
switch.setup_switch(gpio, config)
setup_component(gpio, config)
BUILD_FLAGS = '-DUSE_GPIO_SWITCH'
-27
View File
@@ -1,27 +0,0 @@
import voluptuous as vol
from esphome.components import output, switch
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_NAME, CONF_OUTPUT
from esphome.cpp_generator import Pvariable, get_variable
from esphome.cpp_helpers import setup_component
from esphome.cpp_types import App, Component
OutputSwitch = switch.switch_ns.class_('OutputSwitch', switch.Switch, Component)
PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(OutputSwitch),
vol.Required(CONF_OUTPUT): cv.use_variable_id(output.BinaryOutput),
}).extend(cv.COMPONENT_SCHEMA.schema))
def to_code(config):
output_ = yield get_variable(config[CONF_OUTPUT])
rhs = App.make_output_switch(config[CONF_NAME], output_)
switch_ = Pvariable(config[CONF_ID], rhs)
switch.setup_switch(switch_, config)
setup_component(switch, config)
BUILD_FLAGS = '-DUSE_OUTPUT_SWITCH'
@@ -1,165 +0,0 @@
import voluptuous as vol
from esphome.components import switch
from esphome.components.remote_transmitter import RC_SWITCH_RAW_SCHEMA, \
RC_SWITCH_TYPE_A_SCHEMA, RC_SWITCH_TYPE_B_SCHEMA, RC_SWITCH_TYPE_C_SCHEMA, \
RC_SWITCH_TYPE_D_SCHEMA, RemoteTransmitterComponent, binary_code, build_rc_switch_protocol, \
remote_ns
import esphome.config_validation as cv
from esphome.const import CONF_ADDRESS, CONF_CARRIER_FREQUENCY, CONF_CHANNEL, CONF_CODE, \
CONF_COMMAND, CONF_DATA, CONF_DEVICE, CONF_FAMILY, CONF_GROUP, CONF_ID, CONF_INVERTED, \
CONF_JVC, \
CONF_LG, CONF_NAME, CONF_NBITS, CONF_NEC, CONF_PANASONIC, CONF_PROTOCOL, CONF_RAW, \
CONF_RC_SWITCH_RAW, CONF_RC_SWITCH_TYPE_A, CONF_RC_SWITCH_TYPE_B, CONF_RC_SWITCH_TYPE_C, \
CONF_RC_SWITCH_TYPE_D, CONF_REPEAT, CONF_SAMSUNG, CONF_SONY, CONF_STATE, CONF_TIMES, \
CONF_WAIT_TIME, CONF_RC5
from esphome.cpp_generator import Pvariable, add, get_variable, progmem_array
from esphome.cpp_types import int32
DEPENDENCIES = ['remote_transmitter']
REMOTE_KEYS = [CONF_JVC, CONF_NEC, CONF_LG, CONF_SAMSUNG, CONF_SONY, CONF_PANASONIC, CONF_RAW,
CONF_RC_SWITCH_RAW, CONF_RC_SWITCH_TYPE_A, CONF_RC_SWITCH_TYPE_B,
CONF_RC_SWITCH_TYPE_C, CONF_RC_SWITCH_TYPE_D, CONF_RC5]
CONF_REMOTE_TRANSMITTER_ID = 'remote_transmitter_id'
CONF_TRANSMITTER_ID = 'transmitter_id'
RemoteTransmitter = remote_ns.class_('RemoteTransmitter', switch.Switch)
JVCTransmitter = remote_ns.class_('JVCTransmitter', RemoteTransmitter)
LGTransmitter = remote_ns.class_('LGTransmitter', RemoteTransmitter)
NECTransmitter = remote_ns.class_('NECTransmitter', RemoteTransmitter)
PanasonicTransmitter = remote_ns.class_('PanasonicTransmitter', RemoteTransmitter)
RawTransmitter = remote_ns.class_('RawTransmitter', RemoteTransmitter)
RC5Transmitter = remote_ns.class_('RC5Transmitter', RemoteTransmitter)
SamsungTransmitter = remote_ns.class_('SamsungTransmitter', RemoteTransmitter)
SonyTransmitter = remote_ns.class_('SonyTransmitter', RemoteTransmitter)
RCSwitchRawTransmitter = remote_ns.class_('RCSwitchRawTransmitter', RemoteTransmitter)
RCSwitchTypeATransmitter = remote_ns.class_('RCSwitchTypeATransmitter', RCSwitchRawTransmitter)
RCSwitchTypeBTransmitter = remote_ns.class_('RCSwitchTypeBTransmitter', RCSwitchRawTransmitter)
RCSwitchTypeCTransmitter = remote_ns.class_('RCSwitchTypeCTransmitter', RCSwitchRawTransmitter)
RCSwitchTypeDTransmitter = remote_ns.class_('RCSwitchTypeDTransmitter', RCSwitchRawTransmitter)
def validate_raw(value):
if isinstance(value, dict):
return cv.Schema({
cv.GenerateID(): cv.declare_variable_id(int32),
vol.Required(CONF_DATA): [vol.Any(vol.Coerce(int), cv.time_period_microseconds)],
vol.Optional(CONF_CARRIER_FREQUENCY): vol.All(cv.frequency, vol.Coerce(int)),
})(value)
return validate_raw({
CONF_DATA: value
})
PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(RemoteTransmitter),
vol.Optional(CONF_JVC): cv.Schema({
vol.Required(CONF_DATA): cv.hex_uint32_t,
}),
vol.Optional(CONF_LG): cv.Schema({
vol.Required(CONF_DATA): cv.hex_uint32_t,
vol.Optional(CONF_NBITS, default=28): cv.one_of(28, 32, int=True),
}),
vol.Optional(CONF_NEC): cv.Schema({
vol.Required(CONF_ADDRESS): cv.hex_uint16_t,
vol.Required(CONF_COMMAND): cv.hex_uint16_t,
}),
vol.Optional(CONF_SAMSUNG): cv.Schema({
vol.Required(CONF_DATA): cv.hex_uint32_t,
}),
vol.Optional(CONF_SONY): cv.Schema({
vol.Required(CONF_DATA): cv.hex_uint32_t,
vol.Optional(CONF_NBITS, default=12): cv.one_of(12, 15, 20, int=True),
}),
vol.Optional(CONF_PANASONIC): cv.Schema({
vol.Required(CONF_ADDRESS): cv.hex_uint16_t,
vol.Required(CONF_COMMAND): cv.hex_uint32_t,
}),
vol.Optional(CONF_RC5): cv.Schema({
vol.Required(CONF_ADDRESS): vol.All(cv.hex_int, vol.Range(min=0, max=0x1F)),
vol.Required(CONF_COMMAND): vol.All(cv.hex_int, vol.Range(min=0, max=0x3F)),
}),
vol.Optional(CONF_RAW): validate_raw,
vol.Optional(CONF_RC_SWITCH_RAW): RC_SWITCH_RAW_SCHEMA,
vol.Optional(CONF_RC_SWITCH_TYPE_A): RC_SWITCH_TYPE_A_SCHEMA,
vol.Optional(CONF_RC_SWITCH_TYPE_B): RC_SWITCH_TYPE_B_SCHEMA,
vol.Optional(CONF_RC_SWITCH_TYPE_C): RC_SWITCH_TYPE_C_SCHEMA,
vol.Optional(CONF_RC_SWITCH_TYPE_D): RC_SWITCH_TYPE_D_SCHEMA,
vol.Optional(CONF_REPEAT): vol.Any(cv.positive_not_null_int, cv.Schema({
vol.Required(CONF_TIMES): cv.positive_not_null_int,
vol.Required(CONF_WAIT_TIME): cv.positive_time_period_microseconds,
})),
cv.GenerateID(CONF_REMOTE_TRANSMITTER_ID): cv.use_variable_id(RemoteTransmitterComponent),
cv.GenerateID(CONF_TRANSMITTER_ID): cv.declare_variable_id(RemoteTransmitter),
vol.Optional(CONF_INVERTED): cv.invalid("Remote Transmitters do not support inverted mode!"),
}), cv.has_exactly_one_key(*REMOTE_KEYS))
def transmitter_base(full_config):
name = full_config[CONF_NAME]
key, config = next((k, v) for k, v in full_config.items() if k in REMOTE_KEYS)
if key == CONF_JVC:
return JVCTransmitter.new(name, config[CONF_DATA])
if key == CONF_LG:
return LGTransmitter.new(name, config[CONF_DATA], config[CONF_NBITS])
if key == CONF_NEC:
return NECTransmitter.new(name, config[CONF_ADDRESS], config[CONF_COMMAND])
if key == CONF_PANASONIC:
return PanasonicTransmitter.new(name, config[CONF_ADDRESS], config[CONF_COMMAND])
if key == CONF_SAMSUNG:
return SamsungTransmitter.new(name, config[CONF_DATA])
if key == CONF_SONY:
return SonyTransmitter.new(name, config[CONF_DATA], config[CONF_NBITS])
if key == CONF_RC5:
return RC5Transmitter.new(name, config[CONF_ADDRESS], config[CONF_COMMAND])
if key == CONF_RAW:
arr = progmem_array(config[CONF_ID], config[CONF_DATA])
return RawTransmitter.new(name, arr, len(config[CONF_DATA]),
config.get(CONF_CARRIER_FREQUENCY))
if key == CONF_RC_SWITCH_RAW:
return RCSwitchRawTransmitter.new(name, build_rc_switch_protocol(config[CONF_PROTOCOL]),
binary_code(config[CONF_CODE]), len(config[CONF_CODE]))
if key == CONF_RC_SWITCH_TYPE_A:
return RCSwitchTypeATransmitter.new(name, build_rc_switch_protocol(config[CONF_PROTOCOL]),
binary_code(config[CONF_GROUP]),
binary_code(config[CONF_DEVICE]),
config[CONF_STATE])
if key == CONF_RC_SWITCH_TYPE_B:
return RCSwitchTypeBTransmitter.new(name, build_rc_switch_protocol(config[CONF_PROTOCOL]),
config[CONF_ADDRESS], config[CONF_CHANNEL],
config[CONF_STATE])
if key == CONF_RC_SWITCH_TYPE_C:
return RCSwitchTypeCTransmitter.new(name, build_rc_switch_protocol(config[CONF_PROTOCOL]),
ord(config[CONF_FAMILY][0]) - ord('a'),
config[CONF_GROUP], config[CONF_DEVICE],
config[CONF_STATE])
if key == CONF_RC_SWITCH_TYPE_D:
return RCSwitchTypeDTransmitter.new(name, build_rc_switch_protocol(config[CONF_PROTOCOL]),
ord(config[CONF_GROUP][0]) - ord('a'),
config[CONF_DEVICE], config[CONF_STATE])
raise NotImplementedError("Unknown transmitter type {}".format(config))
def to_code(config):
remote = yield get_variable(config[CONF_REMOTE_TRANSMITTER_ID])
rhs = transmitter_base(config)
transmitter = Pvariable(config[CONF_TRANSMITTER_ID], rhs)
if CONF_REPEAT in config:
if isinstance(config[CONF_REPEAT], int):
times = config[CONF_REPEAT]
wait_us = 1000
else:
times = config[CONF_REPEAT][CONF_TIMES]
wait_us = config[CONF_REPEAT][CONF_WAIT_TIME]
add(transmitter.set_repeat(times, wait_us))
switch.register_switch(remote.add_transmitter(transmitter), config)
BUILD_FLAGS = '-DUSE_REMOTE_TRANSMITTER'
-23
View File
@@ -1,23 +0,0 @@
import voluptuous as vol
from esphome.components import switch
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_INVERTED, CONF_NAME
from esphome.cpp_generator import Pvariable
from esphome.cpp_types import App
RestartSwitch = switch.switch_ns.class_('RestartSwitch', switch.Switch)
PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(RestartSwitch),
vol.Optional(CONF_INVERTED): cv.invalid("Restart switches do not support inverted mode!"),
}))
def to_code(config):
rhs = App.make_restart_switch(config[CONF_NAME])
restart = Pvariable(config[CONF_ID], rhs)
switch.setup_switch(restart, config)
BUILD_FLAGS = '-DUSE_RESTART_SWITCH'
-23
View File
@@ -1,23 +0,0 @@
import voluptuous as vol
from esphome.components import switch
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_INVERTED, CONF_NAME
from esphome.cpp_generator import Pvariable
from esphome.cpp_types import App
ShutdownSwitch = switch.switch_ns.class_('ShutdownSwitch', switch.Switch)
PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(ShutdownSwitch),
vol.Optional(CONF_INVERTED): cv.invalid("Shutdown switches do not support inverted mode!"),
}))
def to_code(config):
rhs = App.make_shutdown_switch(config[CONF_NAME])
shutdown = Pvariable(config[CONF_ID], rhs)
switch.setup_switch(shutdown, config)
BUILD_FLAGS = '-DUSE_SHUTDOWN_SWITCH'
+58
View File
@@ -0,0 +1,58 @@
#include "esphome/components/switch/switch.h"
#include "esphome/core/log.h"
namespace esphome {
namespace switch_ {
static const char *TAG = "switch";
std::string Switch::icon() { return ""; }
Switch::Switch(const std::string &name) : Nameable(name), state(false) {}
Switch::Switch() : Switch("") {}
std::string Switch::get_icon() {
if (this->icon_.has_value())
return *this->icon_;
return this->icon();
}
void Switch::set_icon(const std::string &icon) { this->icon_ = icon; }
void Switch::turn_on() {
ESP_LOGD(TAG, "'%s' Turning ON.", this->get_name().c_str());
this->write_state(!this->inverted_);
}
void Switch::turn_off() {
ESP_LOGD(TAG, "'%s' Turning OFF.", this->get_name().c_str());
this->write_state(this->inverted_);
}
void Switch::toggle() {
ESP_LOGD(TAG, "'%s' Toggling %s.", this->get_name().c_str(), this->state ? "OFF" : "ON");
this->write_state(this->inverted_ == this->state);
}
optional<bool> Switch::get_initial_state() {
this->rtc_ = global_preferences.make_preference<bool>(this->get_object_id_hash());
bool initial_state;
if (!this->rtc_.load(&initial_state))
return {};
return initial_state;
}
void Switch::publish_state(bool state) {
if (!this->publish_dedup_.next(state))
return;
this->state = state != this->inverted_;
this->rtc_.save(&this->state);
ESP_LOGD(TAG, "'%s': Sending state %s", this->name_.c_str(), ONOFF(state));
this->state_callback_.call(this->state);
}
bool Switch::assumed_state() { return false; }
void Switch::add_on_state_callback(std::function<void(bool)> &&callback) {
this->state_callback_.add(std::move(callback));
}
void Switch::set_inverted(bool inverted) { this->inverted_ = inverted; }
uint32_t Switch::hash_base() { return 3129890955UL; }
bool Switch::is_inverted() const { return this->inverted_; }
} // namespace switch_
} // namespace esphome
+126
View File
@@ -0,0 +1,126 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/preferences.h"
#include "esphome/core/helpers.h"
namespace esphome {
namespace switch_ {
#define LOG_SWITCH(prefix, type, obj) \
if (obj != nullptr) { \
ESP_LOGCONFIG(TAG, prefix type " '%s'", obj->get_name().c_str()); \
if (!obj->get_icon().empty()) { \
ESP_LOGCONFIG(TAG, prefix " Icon: '%s'", obj->get_icon().c_str()); \
} \
if (obj->assumed_state()) { \
ESP_LOGCONFIG(TAG, prefix " Assumed State: YES"); \
} \
if (obj->is_inverted()) { \
ESP_LOGCONFIG(TAG, prefix " Inverted: YES"); \
} \
}
/** Base class for all switches.
*
* A switch is basically just a combination of a binary sensor (for reporting switch values)
* and a write_state method that writes a state to the hardware.
*/
class Switch : public Nameable {
public:
explicit Switch();
explicit Switch(const std::string &name);
/** Publish a state to the front-end from the back-end.
*
* The input value is inverted if applicable. Then the internal value member is set and
* finally the callbacks are called.
*
* @param state The new state.
*/
void publish_state(bool state);
/// The current reported state of the binary sensor.
bool state;
/** Turn this switch on. This is called by the front-end.
*
* For implementing switches, please override write_state.
*/
void turn_on();
/** Turn this switch off. This is called by the front-end.
*
* For implementing switches, please override write_state.
*/
void turn_off();
/** Toggle this switch. This is called by the front-end.
*
* For implementing switches, please override write_state.
*/
void toggle();
/** Set whether the state should be treated as inverted.
*
* To the developer and user an inverted switch will act just like a non-inverted one.
* In particular, the only thing that's changed by this is the value passed to
* write_state and the state in publish_state. The .state member variable and
* turn_on/turn_off/toggle remain unaffected.
*
* @param inverted Whether to invert this switch.
*/
void set_inverted(bool inverted);
/// Set the icon for this switch. "" for no icon.
void set_icon(const std::string &icon);
/// Get the icon for this switch. Using icon() if not manually set
std::string get_icon();
/** Set callback for state changes.
*
* @param callback The void(bool) callback.
*/
void add_on_state_callback(std::function<void(bool)> &&callback);
optional<bool> get_initial_state();
/** Return whether this switch uses an assumed state - i.e. if both the ON/OFF actions should be displayed in Home
* Assistant because the real state is unknown.
*
* Defaults to false.
*/
virtual bool assumed_state();
bool is_inverted() const;
protected:
/** Write the given state to hardware. You should implement this
* abstract method if you want to create your own switch.
*
* In the implementation of this method, you should also call
* publish_state to acknowledge that the state was written to the hardware.
*
* @param state The state to write. Inversion is already applied if user specified it.
*/
virtual void write_state(bool state) = 0;
/** Override this to set the Home Assistant icon for this switch.
*
* Return "" to disable this feature.
*
* @return The icon of this switch, for example "mdi:fan".
*/
virtual std::string icon(); // NOLINT
uint32_t hash_base() override;
optional<std::string> icon_{}; ///< The icon shown here. Not set means use default from switch. Empty means no icon.
CallbackManager<void(bool)> state_callback_{};
bool inverted_{false};
Deduplicator<bool> publish_dedup_;
ESPPreferenceObject rtc_;
};
} // namespace switch_
} // namespace esphome
-71
View File
@@ -1,71 +0,0 @@
import voluptuous as vol
from esphome import automation
from esphome.automation import ACTION_REGISTRY
from esphome.components import switch
import esphome.config_validation as cv
from esphome.const import CONF_ASSUMED_STATE, CONF_ID, CONF_LAMBDA, CONF_NAME, CONF_OPTIMISTIC, \
CONF_RESTORE_STATE, CONF_STATE, CONF_TURN_OFF_ACTION, CONF_TURN_ON_ACTION
from esphome.cpp_generator import Pvariable, add, get_variable, process_lambda, templatable
from esphome.cpp_helpers import setup_component
from esphome.cpp_types import Action, App, Component, bool_, optional
TemplateSwitch = switch.switch_ns.class_('TemplateSwitch', switch.Switch, Component)
SwitchPublishAction = switch.switch_ns.class_('SwitchPublishAction', Action)
PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(TemplateSwitch),
vol.Optional(CONF_LAMBDA): cv.lambda_,
vol.Optional(CONF_OPTIMISTIC): cv.boolean,
vol.Optional(CONF_ASSUMED_STATE): cv.boolean,
vol.Optional(CONF_TURN_OFF_ACTION): automation.validate_automation(single=True),
vol.Optional(CONF_TURN_ON_ACTION): automation.validate_automation(single=True),
vol.Optional(CONF_RESTORE_STATE): cv.boolean,
}).extend(cv.COMPONENT_SCHEMA.schema))
def to_code(config):
rhs = App.make_template_switch(config[CONF_NAME])
template = Pvariable(config[CONF_ID], rhs)
switch.setup_switch(template, config)
if CONF_LAMBDA in config:
template_ = yield process_lambda(config[CONF_LAMBDA], [],
return_type=optional.template(bool_))
add(template.set_state_lambda(template_))
if CONF_TURN_OFF_ACTION in config:
automation.build_automations(template.get_turn_off_trigger(), [],
config[CONF_TURN_OFF_ACTION])
if CONF_TURN_ON_ACTION in config:
automation.build_automations(template.get_turn_on_trigger(), [],
config[CONF_TURN_ON_ACTION])
if CONF_OPTIMISTIC in config:
add(template.set_optimistic(config[CONF_OPTIMISTIC]))
if CONF_ASSUMED_STATE in config:
add(template.set_assumed_state(config[CONF_ASSUMED_STATE]))
if CONF_RESTORE_STATE in config:
add(template.set_restore_state(config[CONF_RESTORE_STATE]))
setup_component(template, config)
BUILD_FLAGS = '-DUSE_TEMPLATE_SWITCH'
CONF_SWITCH_TEMPLATE_PUBLISH = 'switch.template.publish'
SWITCH_TEMPLATE_PUBLISH_ACTION_SCHEMA = cv.Schema({
vol.Required(CONF_ID): cv.use_variable_id(switch.Switch),
vol.Required(CONF_STATE): cv.templatable(cv.boolean),
})
@ACTION_REGISTRY.register(CONF_SWITCH_TEMPLATE_PUBLISH, SWITCH_TEMPLATE_PUBLISH_ACTION_SCHEMA)
def switch_template_publish_to_code(config, action_id, template_arg, args):
var = yield get_variable(config[CONF_ID])
rhs = var.make_switch_publish_action(template_arg)
type = SwitchPublishAction.template(template_arg)
action = Pvariable(action_id, rhs, type=type)
template_ = yield templatable(config[CONF_STATE], args, bool_)
add(action.set_state(template_))
yield action
-45
View File
@@ -1,45 +0,0 @@
import voluptuous as vol
from esphome.components import switch, uart
from esphome.components.uart import UARTComponent
import esphome.config_validation as cv
from esphome.const import CONF_DATA, CONF_ID, CONF_INVERTED, CONF_NAME, CONF_UART_ID
from esphome.core import HexInt
from esphome.cpp_generator import Pvariable, get_variable
from esphome.cpp_types import App
from esphome.py_compat import text_type
DEPENDENCIES = ['uart']
UARTSwitch = switch.switch_ns.class_('UARTSwitch', switch.Switch, uart.UARTDevice)
def validate_data(value):
if isinstance(value, text_type):
return value.encode('utf-8')
if isinstance(value, str):
return value
if isinstance(value, list):
return cv.Schema([cv.hex_uint8_t])(value)
raise vol.Invalid("data must either be a string wrapped in quotes or a list of bytes")
PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(UARTSwitch),
cv.GenerateID(CONF_UART_ID): cv.use_variable_id(UARTComponent),
vol.Required(CONF_DATA): validate_data,
vol.Optional(CONF_INVERTED): cv.invalid("UART switches do not support inverted mode!"),
}))
def to_code(config):
uart_ = yield get_variable(config[CONF_UART_ID])
data = config[CONF_DATA]
if isinstance(data, str):
data = [HexInt(ord(x)) for x in data]
rhs = App.make_uart_switch(uart_, config[CONF_NAME], data)
var = Pvariable(config[CONF_ID], rhs)
switch.setup_switch(var, config)
BUILD_FLAGS = '-DUSE_UART_SWITCH'