mirror of
https://github.com/esphome/esphome.git
synced 2025-10-29 22:24:26 +00:00
Cleanup dashboard JS (#491)
* Cleanup dashboard JS * Add vscode * Save start_mark/end_mark * Updates * Updates * Remove need for cv.nameable It's a bit hacky but removes so much bloat from integrations * Add enum helper * Document APIs, and Improvements * Fixes * Fixes * Update PULL_REQUEST_TEMPLATE.md * Updates * Updates * Updates
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
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.automation import Condition, maybe_simple_id
|
||||
from esphome.components import mqtt
|
||||
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
|
||||
CONF_ON_TURN_ON, CONF_TRIGGER_ID, CONF_MQTT_ID, CONF_NAME
|
||||
from esphome.core import CORE, coroutine
|
||||
|
||||
IS_PLATFORM_COMPONENT = True
|
||||
@@ -13,32 +13,34 @@ switch_ns = cg.esphome_ns.namespace('switch_')
|
||||
Switch = switch_ns.class_('Switch', cg.Nameable)
|
||||
SwitchPtr = Switch.operator('ptr')
|
||||
|
||||
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)
|
||||
ToggleAction = switch_ns.class_('ToggleAction', automation.Action)
|
||||
TurnOffAction = switch_ns.class_('TurnOffAction', automation.Action)
|
||||
TurnOnAction = switch_ns.class_('TurnOnAction', automation.Action)
|
||||
SwitchPublishAction = switch_ns.class_('SwitchPublishAction', automation.Action)
|
||||
|
||||
SwitchCondition = switch_ns.class_('SwitchCondition', Condition)
|
||||
SwitchTurnOnTrigger = switch_ns.class_('SwitchTurnOnTrigger', cg.Trigger.template())
|
||||
SwitchTurnOffTrigger = switch_ns.class_('SwitchTurnOffTrigger', cg.Trigger.template())
|
||||
SwitchTurnOnTrigger = switch_ns.class_('SwitchTurnOnTrigger', automation.Trigger.template())
|
||||
SwitchTurnOffTrigger = switch_ns.class_('SwitchTurnOffTrigger', automation.Trigger.template())
|
||||
|
||||
icon = cv.icon
|
||||
|
||||
SWITCH_SCHEMA = cv.MQTT_COMMAND_COMPONENT_SCHEMA.extend({
|
||||
cv.OnlyWith(CONF_MQTT_ID, 'mqtt'): cv.declare_variable_id(mqtt.MQTTSwitchComponent),
|
||||
cv.OnlyWith(CONF_MQTT_ID, 'mqtt'): cv.declare_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),
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(SwitchTurnOnTrigger),
|
||||
}),
|
||||
cv.Optional(CONF_ON_TURN_OFF): automation.validate_automation({
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_variable_id(SwitchTurnOffTrigger),
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(SwitchTurnOffTrigger),
|
||||
}),
|
||||
})
|
||||
|
||||
|
||||
@coroutine
|
||||
def setup_switch_core_(var, config):
|
||||
cg.add(var.set_name(config[CONF_NAME]))
|
||||
if CONF_INTERNAL in config:
|
||||
cg.add(var.set_internal(config[CONF_INTERNAL]))
|
||||
if CONF_ICON in config:
|
||||
@@ -66,49 +68,30 @@ def register_switch(var, config):
|
||||
|
||||
|
||||
SWITCH_ACTION_SCHEMA = maybe_simple_id({
|
||||
cv.Required(CONF_ID): cv.use_variable_id(Switch),
|
||||
cv.Required(CONF_ID): cv.use_id(Switch),
|
||||
})
|
||||
|
||||
|
||||
@ACTION_REGISTRY.register('switch.toggle', SWITCH_ACTION_SCHEMA)
|
||||
@automation.register_action('switch.toggle', ToggleAction, SWITCH_ACTION_SCHEMA)
|
||||
@automation.register_action('switch.turn_off', TurnOffAction, SWITCH_ACTION_SCHEMA)
|
||||
@automation.register_action('switch.turn_on', TurnOnAction, SWITCH_ACTION_SCHEMA)
|
||||
def switch_toggle_to_code(config, action_id, template_arg, args):
|
||||
var = yield cg.get_variable(config[CONF_ID])
|
||||
type = ToggleAction.template(template_arg)
|
||||
rhs = type.new(var)
|
||||
yield cg.Pvariable(action_id, rhs, type=type)
|
||||
paren = yield cg.get_variable(config[CONF_ID])
|
||||
yield cg.new_Pvariable(action_id, template_arg, paren)
|
||||
|
||||
|
||||
@ACTION_REGISTRY.register('switch.turn_off', SWITCH_ACTION_SCHEMA)
|
||||
def switch_turn_off_to_code(config, action_id, template_arg, args):
|
||||
var = yield cg.get_variable(config[CONF_ID])
|
||||
type = TurnOffAction.template(template_arg)
|
||||
rhs = type.new(var)
|
||||
yield cg.Pvariable(action_id, rhs, type=type)
|
||||
|
||||
|
||||
@ACTION_REGISTRY.register('switch.turn_on', SWITCH_ACTION_SCHEMA)
|
||||
def switch_turn_on_to_code(config, action_id, template_arg, args):
|
||||
var = yield cg.get_variable(config[CONF_ID])
|
||||
type = TurnOnAction.template(template_arg)
|
||||
rhs = type.new(var)
|
||||
yield cg.Pvariable(action_id, rhs, type=type)
|
||||
|
||||
|
||||
@CONDITION_REGISTRY.register('switch.is_on', SWITCH_ACTION_SCHEMA)
|
||||
@automation.register_condition('switch.is_on', SwitchCondition, SWITCH_ACTION_SCHEMA)
|
||||
def switch_is_on_to_code(config, condition_id, template_arg, args):
|
||||
var = yield cg.get_variable(config[CONF_ID])
|
||||
type = SwitchCondition.template(template_arg)
|
||||
rhs = type.new(var, True)
|
||||
yield cg.Pvariable(condition_id, rhs, type=type)
|
||||
paren = yield cg.get_variable(config[CONF_ID])
|
||||
yield cg.new_Pvariable(condition_id, template_arg, paren)
|
||||
|
||||
|
||||
@CONDITION_REGISTRY.register('switch.is_off', SWITCH_ACTION_SCHEMA)
|
||||
@automation.register_condition('switch.is_off', SwitchCondition, SWITCH_ACTION_SCHEMA)
|
||||
def switch_is_off_to_code(config, condition_id, template_arg, args):
|
||||
var = yield cg.get_variable(config[CONF_ID])
|
||||
type = SwitchCondition.template(template_arg)
|
||||
rhs = type.new(var, False)
|
||||
yield cg.Pvariable(condition_id, rhs, type=type)
|
||||
paren = yield cg.get_variable(config[CONF_ID])
|
||||
yield cg.new_Pvariable(condition_id, template_arg, paren)
|
||||
|
||||
|
||||
def to_code(config):
|
||||
cg.add_global(switch_ns.using)
|
||||
cg.add_define('USE_SWITCH')
|
||||
|
||||
@@ -11,10 +11,7 @@ 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...);
|
||||
}
|
||||
void play(Ts... x) override { this->switch_->turn_on(); }
|
||||
|
||||
protected:
|
||||
Switch *switch_;
|
||||
@@ -24,10 +21,7 @@ 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...);
|
||||
}
|
||||
void play(Ts... x) override { this->switch_->turn_off(); }
|
||||
|
||||
protected:
|
||||
Switch *switch_;
|
||||
@@ -37,10 +31,7 @@ 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...);
|
||||
}
|
||||
void play(Ts... x) override { this->switch_->toggle(); }
|
||||
|
||||
protected:
|
||||
Switch *switch_;
|
||||
@@ -82,10 +73,7 @@ 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...);
|
||||
}
|
||||
void play(Ts... x) override { this->switch_->publish_state(this->state_.value(x...)); }
|
||||
|
||||
protected:
|
||||
Switch *switch_;
|
||||
|
||||
Reference in New Issue
Block a user