diff --git a/README.md b/README.md index f5bfee8..8daf7f9 100644 --- a/README.md +++ b/README.md @@ -617,13 +617,54 @@ Experienced programmers can use these public methods: To use the display without homeassistant automations, you may use the [advanced functionality](#change-configuration-during-runtime) with triggers. The triggers can be fired by sensors, time or by the ehmtx component. +#### on_add_screen + +There is a trigger available to do some local magic. The trigger ```on_add_screen``` is triggered every time a new screen with icon is added to the queue. In lambda's you can use two local variables: + +**icon** (Name of the icon, std::string): value to use in lambda + +**mode** ([mode](#modes) of the screen, uint8_t): value to use in lambda + +See the examples: + +##### Write information to esphome log + +```yaml +ehmtxv2: + .... + on_add_screen: + then: + - logger.log: + format: 'add screen: %s, mode: %d' + tag: "EHMTXv2 sample" + args: + - icon.c_str() + - mode +``` + +#### on_icon_error + +The trigger ```on_icon_error``` is triggered if you try to add a screen with a non defined icon. In lambda's you can use one local string variable: + +**icon** (Name of the icon, std::string): value to use in lambda + +See the examples: + +```yaml +ehmtxv2: + .... + on_next_screen: + lambda: |- + ESP_LOGD("Check CONFIG","Iconname: %s",icon.c_str()); +``` + #### on_next_screen -There is a trigger available to do some local magic. The trigger ```on_next_screen``` is triggered every time a new screen is displayed (it doesn't trigger on the clock/date display!!). In lambda's you can use two local string variables: +The trigger ```on_next_screen``` is triggered every time a new screen is displayed (it doesn't trigger on the clock/date display!!). In lambda's you can use two local string variables: -**x** (Name of the icon, std::string): value to use in lambda +**icon** (Name of the icon, std::string): value to use in lambda -**y** (displayed text, std::string): value to use in lambda +**text** (displayed text, std::string): value to use in lambda See the examples: @@ -634,8 +675,8 @@ ehmtxv2: .... on_next_screen: lambda: |- - ESP_LOGD("TriggerTest","Iconname: %s",x.c_str()); - ESP_LOGI("TriggerTest","Text: %s",y.c_str()); + ESP_LOGD("TriggerTest","Iconname: %s",icon.c_str()); + ESP_LOGI("TriggerTest","Text: %s",text.c_str()); ``` ##### Send an event to Home Assistant @@ -649,8 +690,8 @@ ehmtxv2: - homeassistant.event: event: esphome.next_screen data_template: - iconname: !lambda "return x.c_str();" - text: !lambda "return y.c_str();" + iconname: !lambda "return icon.c_str();" + text: !lambda "return text.c_str();" ``` ***Result*** diff --git a/components/ehmtxv2/EHMTX.cpp b/components/ehmtxv2/EHMTX.cpp index a643f32..6050cba 100644 --- a/components/ehmtxv2/EHMTX.cpp +++ b/components/ehmtxv2/EHMTX.cpp @@ -518,6 +518,10 @@ namespace esphome screen->default_font = default_font; screen->mode = MODE_ICON_SCREEN; screen->icon_name = iconname; + for (auto *t : on_add_screen_triggers_) + { + t->process(screen->icon_name,(uint8_t)screen->mode); + } ESP_LOGD(TAG, "icon screen icon: %d iconname: %s text: %s lifetime: %d screen_time: %d", icon, iconname.c_str(), text.c_str(), lifetime, screen_time); screen->status(); } @@ -894,6 +898,11 @@ namespace esphome this->trigger(iconname, text); } + void EHMTXAddScreenTrigger::process(std::string iconname, uint8_t mode) + { + this->trigger(iconname, mode); + } + void EHMTXIconErrorTrigger::process(std::string iconname) { this->trigger(iconname); diff --git a/components/ehmtxv2/EHMTX.h b/components/ehmtxv2/EHMTX.h index fd6a7a8..7ed5507 100644 --- a/components/ehmtxv2/EHMTX.h +++ b/components/ehmtxv2/EHMTX.h @@ -30,6 +30,7 @@ namespace esphome class EHMTX_queue; class EHMTX_Icon; class EHMTXNextScreenTrigger; + class EHMTXAddScreenTrigger; class EHMTXIconErrorTrigger; class EHMTXExpiredScreenTrigger; class EHMTXNextClockTrigger; @@ -47,6 +48,7 @@ namespace esphome std::vector on_icon_error_triggers_; std::vector on_expired_screen_triggers_; std::vector on_next_clock_triggers_; + std::vector on_add_screen_triggers_; EHMTX_queue *find_icon_queue_element(uint8_t icon); EHMTX_queue *find_free_queue_element(); @@ -152,6 +154,7 @@ namespace esphome void draw_indicator(); void add_on_next_screen_trigger(EHMTXNextScreenTrigger *t) { this->on_next_screen_triggers_.push_back(t); } + void add_on_add_screen_trigger(EHMTXAddScreenTrigger *t) { this->on_add_screen_triggers_.push_back(t); } void add_on_icon_error_trigger(EHMTXIconErrorTrigger *t) { this->on_icon_error_triggers_.push_back(t); } void add_on_expired_screen_trigger(EHMTXExpiredScreenTrigger *t) { this->on_expired_screen_triggers_.push_back(t); } void add_on_next_clock_trigger(EHMTXNextClockTrigger *t) { this->on_next_clock_triggers_.push_back(t); } @@ -200,6 +203,13 @@ namespace esphome void process(std::string, std::string); }; + class EHMTXAddScreenTrigger : public Trigger + { + public: + explicit EHMTXAddScreenTrigger(EHMTX *parent) { parent->add_on_add_screen_trigger(this); } + void process(std::string, uint8_t); + }; + class EHMTXIconErrorTrigger : public Trigger { public: diff --git a/components/ehmtxv2/__init__.py b/components/ehmtxv2/__init__.py index 7bc8467..add1e5d 100644 --- a/components/ehmtxv2/__init__.py +++ b/components/ehmtxv2/__init__.py @@ -53,6 +53,10 @@ NextClockTrigger = ehmtx_ns.class_( "EHMTXNextClockTrigger", automation.Trigger.template(cg.std_string) ) +AddScreenTrigger = ehmtx_ns.class_( + "EHMTXAddScreenTrigger", automation.Trigger.template(cg.std_string) +) + CONF_URL = "url" CONF_FLAG = "flag" CONF_TIMECOMPONENT = "time_component" @@ -81,6 +85,7 @@ CONF_DATE_FORMAT = "date_format" CONF_ON_NEXT_SCREEN = "on_next_screen" CONF_ON_NEXT_CLOCK = "on_next_clock" CONF_ON_ICON_ERROR = "on_icon_error" +CONF_ON_ADD_SCREEN = "on_add_screen" CONF_ON_EXPIRED_SCREEN= "on_expired_screen" CONF_SHOW_SECONDS = "show_seconds" CONF_WEEK_START_MONDAY = "week_start_monday" @@ -150,6 +155,11 @@ EHMTX_SCHEMA = cv.Schema({ cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(IconErrorTrigger), } ), + cv.Optional(CONF_ON_ADD_SCREEN): automation.validate_automation( + { + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(AddScreenTrigger), + } + ), cv.Optional(CONF_ON_EXPIRED_SCREEN): automation.validate_automation( { cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(NextScreenTrigger), @@ -360,7 +370,7 @@ async def to_code(config): cg.add(var.set_special_font_offset(config[CONF_special_FONT_XOFFSET], config[CONF_special_FONT_YOFFSET] )) for conf in config.get(CONF_ON_NEXT_SCREEN, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) - await automation.build_automation(trigger, [(cg.std_string, "x"), (cg.std_string, "y")], conf) + await automation.build_automation(trigger, [(cg.std_string, "icon"), (cg.std_string, "text")], conf) for conf in config.get(CONF_ON_NEXT_CLOCK, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) @@ -368,10 +378,14 @@ async def to_code(config): for conf in config.get(CONF_ON_EXPIRED_SCREEN, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) - await automation.build_automation(trigger, [(cg.std_string, "x"), (cg.std_string, "y")] , conf) + await automation.build_automation(trigger, [(cg.std_string, "icon"), (cg.std_string, "text")] , conf) for conf in config.get(CONF_ON_ICON_ERROR, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) - await automation.build_automation(trigger, [(cg.std_string, "x")] , conf) + await automation.build_automation(trigger, [(cg.std_string, "icon")] , conf) + + for conf in config.get(CONF_ON_ADD_SCREEN, []): + trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) + await automation.build_automation(trigger, [(cg.std_string, "icon"), (cg.uint8 , "mode")] , conf) await cg.register_component(var, config)