1
0
mirror of https://github.com/esphome/esphome.git synced 2025-03-15 15:18:16 +00:00

added automations

This commit is contained in:
Michiel van Turnhout 2019-07-29 11:15:16 +02:00
parent f00945b7a8
commit 3ae2a89a9f
4 changed files with 49 additions and 10 deletions

View File

@ -8,10 +8,11 @@ from esphome.automation import maybe_simple_id
from esphome.core import CORE, EsphomeError, Lambda, coroutine, coroutine_with_priority from esphome.core import CORE, EsphomeError, Lambda, coroutine, coroutine_with_priority
from esphome.components import sensor from esphome.components import sensor
from esphome.py_compat import text_type from esphome.py_compat import text_type
from esphome.const import CONF_ID from esphome.const import CONF_ID, CONF_TRIGGER_ID
IS_PLATFORM_COMPONENT = True IS_PLATFORM_COMPONENT = True
CONF_ON_RECEIVE = 'on_receive'
CONF_CANBUS_ID = 'canbus_id' CONF_CANBUS_ID = 'canbus_id'
CONF_CAN_ID = 'can_id' CONF_CAN_ID = 'can_id'
CONF_CAN_DATA = 'can_data' CONF_CAN_DATA = 'can_data'
@ -21,10 +22,17 @@ CONF_CANBUS_SEND_ACTION = 'canbus.send'
canbus_ns = cg.esphome_ns.namespace('canbus') canbus_ns = cg.esphome_ns.namespace('canbus')
CanbusComponent = canbus_ns.class_('CanbusComponent', cg.Component) CanbusComponent = canbus_ns.class_('CanbusComponent', cg.Component)
CanbusTrigger = canbus_ns.class_('CanbusTrigger',
automation.Trigger.template(cg.std_string),
cg.Component)
CONFIG_SCHEMA = cv.Schema({ CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_id(CanbusComponent), cv.GenerateID(): cv.declare_id(CanbusComponent),
cv.Required(CONF_SENDER_ID): cv.int_range(min=0, max=255), cv.Required(CONF_SENDER_ID): cv.int_range(min=0, max=255),
cv.Optional(CONF_ON_RECEIVE): automation.validate_automation({
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(CanbusTrigger),
cv.GenerateID(CONF_CAN_ID): cv.int_range(min=1, max=4096),
}),
}).extend(cv.COMPONENT_SCHEMA) }).extend(cv.COMPONENT_SCHEMA)
# Actions # Actions
@ -36,7 +44,6 @@ CANBUS_ACTION_SCHEMA = maybe_simple_id({
cv.Required(CONF_CAN_DATA): cv.templatable(cv.int_), cv.Required(CONF_CAN_DATA): cv.templatable(cv.int_),
}) })
@coroutine @coroutine
def setup_canbus_core_(var, config): def setup_canbus_core_(var, config):
yield cg.register_component(var, config) yield cg.register_component(var, config)
@ -58,13 +65,15 @@ def register_canbus(var, config):
@automation.register_action(CONF_CANBUS_SEND_ACTION, SendAction, CANBUS_ACTION_SCHEMA) @automation.register_action(CONF_CANBUS_SEND_ACTION, SendAction, CANBUS_ACTION_SCHEMA)
def canbus_action_to_code(config, action_id, template_arg, args): def canbus_action_to_code(config, action_id, template_arg, args):
canbus = yield cg.get_variable(config[CONF_CANBUS_ID]) canbus = yield cg.get_variable(config[CONF_CANBUS_ID])
#args_ = yield cg.get_variable(config[CONF_CAN_DATA]) var = yield cg.new_Pvariable(action_id, template_arg, canbus)
templ = yield cg.templatable(config[CONF_CAN_DATA], args, cg.float_) lambda_ = yield cg.process_lambda(config[CONF_CAN_DATA], args, return_type=cg.float_)
send_id_ = yield cg.templatable(config[CONF_CAN_ID], args, cg.uint16) can_id = yield cg.templatable(config[CONF_CAN_ID], args, cg.uint16)
var = yield cg.new_Pvariable(action_id, template_arg, canbus, send_id_) cg.add(var.set_can_id(can_id))
cg.add(var.set_data(templ)) cg.add(var.set_data(lambda_))
yield var
@coroutine_with_priority(100.0) @coroutine_with_priority(100.0)
def to_code(config): def to_code(config):
cg.add_global(canbus_ns.using) cg.add_global(canbus_ns.using)
cg.add_define("USE_CANBUS")

View File

@ -9,7 +9,8 @@ namespace canbus {
template<typename... Ts> class SendAction : public Action<Ts...> { template<typename... Ts> class SendAction : public Action<Ts...> {
public: public:
explicit SendAction(Canbus *parent, int can_id) : parent_(parent), can_id_(can_id) {} explicit SendAction(Canbus *parent) : parent_(parent){}
void set_can_id(int can_id) {this->can_id_ = can_id;}
TEMPLATABLE_VALUE(float, data) TEMPLATABLE_VALUE(float, data)

View File

@ -96,6 +96,29 @@ class Canbus : public Component {
virtual ERROR set_bitrate_(const CAN_SPEED canSpeed); virtual ERROR set_bitrate_(const CAN_SPEED canSpeed);
}; };
class MQTTMessageTrigger : public Trigger<std::string>, public Component {
public:
explicit MQTTMessageTrigger(const std::string &topic);
void set_qos(uint8_t qos);
void set_payload(const std::string &payload);
void setup() override;
void dump_config() override;
float get_setup_priority() const override;
protected:
std::string topic_;
uint8_t qos_{0};
optional<std::string> payload_;
};
class MQTTJsonMessageTrigger : public Trigger<const JsonObject &> {
public:
explicit MQTTJsonMessageTrigger(const std::string &topic, uint8_t qos) {
global_mqtt_client->subscribe_json(
topic, [this](const std::string &topic, JsonObject &root) { this->trigger(root); }, qos);
}
};
} // namespace canbus } // namespace canbus
} // namespace esphome } // namespace esphome

View File

@ -1,8 +1,10 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.components import spi, canbus from esphome.components import spi, canbus
from esphome.const import CONF_ID from esphome.automation import LambdaAction
from esphome.components.canbus import CanbusComponent from esphome import automation
from esphome.const import CONF_ID, CONF_TRIGGER_ID
from esphome.components.canbus import CanbusComponent,CONF_ON_RECEIVE, CONF_CAN_ID
print("mcp2515.canbus.py") print("mcp2515.canbus.py")
AUTO_LOAD = ['canbus'] AUTO_LOAD = ['canbus']
@ -21,3 +23,7 @@ def to_code(config):
var = cg.Pvariable(config[CONF_ID], rhs) var = cg.Pvariable(config[CONF_ID], rhs)
yield canbus.register_canbus(var, config) yield canbus.register_canbus(var, config)
yield spi.register_spi_device(var, config) yield spi.register_spi_device(var, config)
for conf in config.get(CONF_ON_RECEIVE, []):
trig = cg.new_Pvariable(conf[CONF_TRIGGER_ID], conf[CONF_CAN_ID])
yield cg.register_component(trig, conf)
yield automation.build_automation(trig, [(cg.std_string, 'x')], conf)