mirror of
https://github.com/esphome/esphome.git
synced 2024-12-25 23:14:54 +01:00
Logger on_message trigger (#729)
* on_message * Lint fix * Lint fix (2) * Lint fix (<3) * Replace cg.int_ with int * Revert * Removed strdup Co-authored-by: Otto Winter <otto@otto-winter.com>
This commit is contained in:
parent
4c49beb3c7
commit
e4f055597c
4 changed files with 33 additions and 2 deletions
|
@ -19,7 +19,7 @@ from esphome.cpp_helpers import ( # noqa
|
|||
gpio_pin_expression, register_component, build_registry_entry,
|
||||
build_registry_list, extract_registry_entry_config, register_parented)
|
||||
from esphome.cpp_types import ( # noqa
|
||||
global_ns, void, nullptr, float_, double, bool_, std_ns, std_string,
|
||||
global_ns, void, nullptr, float_, double, bool_, int_, std_ns, std_string,
|
||||
std_vector, uint8, uint16, uint32, int32, const_char_ptr, NAN,
|
||||
esphome_ns, App, Nameable, Component, ComponentPtr,
|
||||
PollingComponent, Application, optional, arduino_json_ns, JsonObject,
|
||||
|
|
|
@ -5,7 +5,7 @@ import esphome.config_validation as cv
|
|||
from esphome import automation
|
||||
from esphome.automation import LambdaAction
|
||||
from esphome.const import CONF_ARGS, CONF_BAUD_RATE, CONF_FORMAT, CONF_HARDWARE_UART, CONF_ID, \
|
||||
CONF_LEVEL, CONF_LOGS, CONF_TAG, CONF_TX_BUFFER_SIZE
|
||||
CONF_LEVEL, CONF_LOGS, CONF_ON_MESSAGE, CONF_TAG, CONF_TRIGGER_ID, CONF_TX_BUFFER_SIZE
|
||||
from esphome.core import CORE, EsphomeError, Lambda, coroutine_with_priority
|
||||
from esphome.py_compat import text_type
|
||||
|
||||
|
@ -70,6 +70,9 @@ def validate_local_no_higher_than_global(value):
|
|||
|
||||
|
||||
Logger = logger_ns.class_('Logger', cg.Component)
|
||||
LoggerMessageTrigger = logger_ns.class_('LoggerMessageTrigger',
|
||||
automation.Trigger.template(cg.int_, cg.const_char_ptr,
|
||||
cg.const_char_ptr))
|
||||
|
||||
CONF_ESP8266_STORE_LOG_STRINGS_IN_FLASH = 'esp8266_store_log_strings_in_flash'
|
||||
CONFIG_SCHEMA = cv.All(cv.Schema({
|
||||
|
@ -81,6 +84,10 @@ CONFIG_SCHEMA = cv.All(cv.Schema({
|
|||
cv.Optional(CONF_LOGS, default={}): cv.Schema({
|
||||
cv.string: is_log_level,
|
||||
}),
|
||||
cv.Optional(CONF_ON_MESSAGE): automation.validate_automation({
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(LoggerMessageTrigger),
|
||||
cv.Optional(CONF_LEVEL, default='WARN'): is_log_level,
|
||||
}),
|
||||
|
||||
cv.SplitDefault(CONF_ESP8266_STORE_LOG_STRINGS_IN_FLASH, esp8266=True):
|
||||
cv.All(cv.only_on_esp8266, cv.boolean),
|
||||
|
@ -138,6 +145,13 @@ def to_code(config):
|
|||
# Register at end for safe mode
|
||||
yield cg.register_component(log, config)
|
||||
|
||||
for conf in config.get(CONF_ON_MESSAGE, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], log,
|
||||
LOG_LEVEL_SEVERITY.index(conf[CONF_LEVEL]))
|
||||
yield automation.build_automation(trigger, [(cg.int_, 'level'),
|
||||
(cg.const_char_ptr, 'tag'),
|
||||
(cg.const_char_ptr, 'message')], conf)
|
||||
|
||||
|
||||
def maybe_simple_message(schema):
|
||||
def validator(value):
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
|
@ -114,6 +115,21 @@ class Logger : public Component {
|
|||
|
||||
extern Logger *global_logger;
|
||||
|
||||
class LoggerMessageTrigger : public Trigger<int, const char *, const char *> {
|
||||
public:
|
||||
explicit LoggerMessageTrigger(Logger *parent, int level) {
|
||||
this->level_ = level;
|
||||
parent->add_on_log_callback([this](int level, const char *tag, const char *message) {
|
||||
if (level <= this->level_) {
|
||||
this->trigger(level, tag, message);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected:
|
||||
int level_;
|
||||
};
|
||||
|
||||
} // namespace logger
|
||||
|
||||
} // namespace esphome
|
||||
|
|
|
@ -6,6 +6,7 @@ nullptr = global_ns.namespace('nullptr')
|
|||
float_ = global_ns.namespace('float')
|
||||
double = global_ns.namespace('double')
|
||||
bool_ = global_ns.namespace('bool')
|
||||
int_ = global_ns.namespace('int')
|
||||
std_ns = global_ns.namespace('std')
|
||||
std_string = std_ns.class_('string')
|
||||
std_vector = std_ns.class_('vector')
|
||||
|
|
Loading…
Reference in a new issue