Skip to content

Commit

Permalink
Merge pull request lubeda#105 from andrewjswan/2023.9.1-103-Night_Mode
Browse files Browse the repository at this point in the history
2023.9.1-103: Night Mode
  • Loading branch information
lubeda authored Oct 11, 2023
2 parents 99d3c4c + 2db8dfd commit ea80495
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 3 deletions.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
[![Build](https://github.com/lubeda/EspHoMaTriXv2/actions/workflows/main.yml/badge.svg)](https://github.com/lubeda/EspHoMaTriXv2/actions/workflows/main.yml)

### EspHoMaTriX 2023.9.1
- Added night mode
- `night_mode_on`
- `night_mode_off`
- New Yaml option `night_mode_screens: [2, 3, 16]` - list of screens to be displayed in night mode.
- `on_night_mode` trigger
- Added `on_show_display` trigger
- Added the ability to specify a screen ID - `icon_name|screen_id`
- Added icon and date output screen
- `icon_date(iconname, lifetime, screen_time, default_font, r, g, b)`
Expand Down Expand Up @@ -673,6 +679,8 @@ Example:

**clock_interval** (optional, s): the interval in seconds to force the clock display. By default, the clock screen, if any, will be displayed according to the position in the queue. **If you set the clock_interval close to the screen_time of the clock, you will only see the clock!** (default=0)

**night_mode_screens** (optional, screen array, default [2, 3, 16]): List of screens displayed in [night mode](#night-mode).

**boot_logo** (optional, string , only on ESP32): Display a fullscreen logo defined as rgb565 array.

```yaml
Expand Down Expand Up @@ -725,6 +733,8 @@ Numerous features are accessible with services from home assistant and lambdas t
|`hide_rindicator`|none|hides the rindicator|
|`hide_gauge`|none|hides the gauge|
|`hide_alarm`|none|hides the alarm|
|`night_mode_on`|none|turn [night mode](#night-mode) on|
|`night_mode_off`|none|turn [night mode](#night-mode) off|
|`show_gauge"`|"percent", "r", "g", "b"|set the height of the gauge according to the percentage in the given color|
|`show_alarm`|"r", "g", "b", "size"|shows the color with the given size in the upper-right corner|
|`show_rindicator`|"r", "g", "b", "size"|shows the color with the given size in the lower-right corner|
Expand Down Expand Up @@ -763,6 +773,10 @@ Numerous features are accessible with services from home assistant and lambdas t
- **progress**: сan take a value from -100 to 100, the color of the progress bar is calculated automatically, if the progress is in the range `0..100`, then `from red to green`, if in the range `-100..0`, then from `green to red`.
- **value**: the brightness 0..255

### Night mode

When night mode is enabled, only the screens specified in `night_mode_screens` are displayed (**default:** 2, 3, 16) the other screens can be added, deleted, and will follow their life cycle but will not be displayed. Screen numbers in the table [mode](#modes).

### Local lambdas

#### Add screen to your display queue
Expand Down Expand Up @@ -925,9 +939,21 @@ ehmtxv2:

#### on_next_clock

The trigger ```on_next_clock``` is triggered every time a new clock display circle starts.
The trigger ```on_next_clock``` is triggered every time a new clock display circle starts.
See the examples:

#### on_show_display

The trigger ```on_show_display``` is triggered when the screen is turned on or off. In lambda's you can use one local boolean variable:

**state** (Display state, bool): value to use in lambda

#### on_night_mode

The trigger ```on_night_mode``` is triggered when the night mode is turned on or off. In lambda's you can use one local boolean variable:

**state** (Night mode state, bool): value to use in lambda

##### Change something for each clock circle

```yaml
Expand Down
95 changes: 93 additions & 2 deletions components/ehmtxv2/EHMTX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace esphome
this->is_running = false;
this->set_today_color();
this->set_weekday_color();
this->night_mode = false;

for (uint8_t i = 0; i < MAXQUEUE; i++)
{
Expand Down Expand Up @@ -77,12 +78,44 @@ namespace esphome
{
this->show_display = false;
ESP_LOGD(TAG, "display off");

for (auto *t : on_show_display_triggers_)
{
t->process(this->show_display);
}
}

void EHMTX::set_display_on()
{
this->show_display = true;
ESP_LOGD(TAG, "display on");

for (auto *t : on_show_display_triggers_)
{
t->process(this->show_display);
}
}

void EHMTX::set_night_mode_off()
{
this->night_mode = false;
ESP_LOGD(TAG, "night mode off");

for (auto *t : on_night_mode_triggers_)
{
t->process(this->night_mode);
}
}

void EHMTX::set_night_mode_on()
{
this->night_mode = true;
ESP_LOGD(TAG, "night mode on");

for (auto *t : on_night_mode_triggers_)
{
t->process(this->night_mode);
}
}

void EHMTX::set_today_color(int r, int g, int b)
Expand Down Expand Up @@ -364,6 +397,9 @@ namespace esphome
register_service(&EHMTX::set_text_color, "set_text_color", {"r", "g", "b"});
register_service(&EHMTX::set_infotext_color, "set_infotext_color", {"left_r", "left_g", "left_b", "right_r", "right_g", "right_b", "default_font", "y_offset"});

register_service(&EHMTX::set_night_mode_on, "night_mode_on");
register_service(&EHMTX::set_night_mode_off, "night_mode_off");

register_service(&EHMTX::del_screen, "del_screen", {"icon_name", "mode"});
register_service(&EHMTX::force_screen, "force_screen", {"icon_name", "mode"});

Expand Down Expand Up @@ -513,6 +549,22 @@ namespace esphome
time_t last_time = this->clock->now().timestamp;
for (size_t i = 0; i < MAXQUEUE; i++)
{
if (this->night_mode)
{
bool skip = true;
for (auto id : EHMTXv2_CONF_NIGNT_MODE_SCREENS)
{
if (this->queue[i]->mode == id)
{
skip = false;
}
}
if (skip)
{
continue;
}
}

if ((this->queue[i]->endtime > 0) && (this->queue[i]->last_time < last_time))
{
hit = i;
Expand All @@ -535,6 +587,22 @@ namespace esphome
time_t ts = this->clock->now().timestamp;
for (size_t i = 0; i < MAXQUEUE; i++)
{
if (this->night_mode)
{
bool skip = true;
for (auto id : EHMTXv2_CONF_NIGNT_MODE_SCREENS)
{
if (this->queue[i]->mode == id)
{
skip = false;
}
}
if (skip)
{
continue;
}
}

if ((this->queue[i]->mode == MODE_CLOCK) || (this->queue[i]->mode == MODE_RAINBOW_CLOCK) || (this->queue[i]->mode == MODE_ICON_CLOCK))
{
if (ts > (this->queue[i]->last_time + EHMTXv2_CLOCK_INTERVALL))
Expand Down Expand Up @@ -746,6 +814,14 @@ namespace esphome
{
ESP_LOGI(TAG, "status display off");
}
if (this->night_mode)
{
ESP_LOGI(TAG, "status night mode on");
}
else
{
ESP_LOGI(TAG, "status night mode off");
}

this->queue_status();
}
Expand Down Expand Up @@ -1330,6 +1406,7 @@ namespace esphome
{
this->display = disp;
this->show_display = true;
this->night_mode = false;
ESP_LOGD(TAG, "set_display");
}

Expand Down Expand Up @@ -1552,6 +1629,9 @@ namespace esphome
{
ESP_LOGCONFIG(TAG, "weekstart: sunday");
}
ESP_LOGCONFIG(TAG, "Weekdays: %s Count: %d", EHMTXv2_WEEKDAYTEXT, this->GetWeekdayCharCount());
ESP_LOGCONFIG(TAG, "Display: %s", this->show_display ? "On" : "Off");
ESP_LOGCONFIG(TAG, "Night mode: %s", this->night_mode ? "On" : "Off");
}

void EHMTX::add_icon(EHMTX_Icon *icon)
Expand Down Expand Up @@ -1620,7 +1700,8 @@ namespace esphome
{
if ((this->is_running) && (this->show_display) )
{
if (this->screen_pointer != MAXQUEUE) {
if (this->screen_pointer != MAXQUEUE)
{
this->queue[this->screen_pointer]->draw();
}
if (this->queue[this->screen_pointer]->mode != MODE_FULL_SCREEN &&
Expand All @@ -1636,8 +1717,8 @@ namespace esphome
this->queue[this->screen_pointer]->mode != MODE_BITMAP_SCREEN)
{
#endif

this->draw_rindicator();

#ifndef EHMTXv2_ALWAYS_SHOW_RLINDICATORS
if (this->queue[this->screen_pointer]->mode != MODE_ICON_SCREEN &&
this->queue[this->screen_pointer]->mode != MODE_RAINBOW_ICON &&
Expand Down Expand Up @@ -1687,4 +1768,14 @@ namespace esphome
{
this->trigger();
}

void EHMTXShowDisplayTrigger::process(bool state)
{
this->trigger(state);
}

void EHMTXNightModeTrigger::process(bool state)
{
this->trigger(state);
}
}
23 changes: 23 additions & 0 deletions components/ehmtxv2/EHMTX.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ namespace esphome
class EHMTXExpiredScreenTrigger;
class EHMTXNextClockTrigger;
class EHMTXStartRunningTrigger;
class EHMTXShowDisplayTrigger;
class EHMTXNightModeTrigger;

class EHMTX : public PollingComponent, public api::CustomAPIDevice
{
Expand All @@ -79,6 +81,8 @@ namespace esphome
std::vector<EHMTXNextClockTrigger *> on_next_clock_triggers_;
std::vector<EHMTXStartRunningTrigger *> on_start_running_triggers_;
std::vector<EHMTXAddScreenTrigger *> on_add_screen_triggers_;
std::vector<EHMTXShowDisplayTrigger *> on_show_display_triggers_;
std::vector<EHMTXNightModeTrigger *> on_night_mode_triggers_;
EHMTX_queue *find_icon_queue_element(uint8_t icon);
EHMTX_queue *find_mode_queue_element(uint8_t mode);
EHMTX_queue *find_mode_icon_queue_element(uint8_t mode, std::string name);
Expand Down Expand Up @@ -144,6 +148,7 @@ namespace esphome
void force_screen(std::string name, int mode = MODE_ICON_SCREEN);
void add_icon(EHMTX_Icon *icon);
bool show_display = false;
bool night_mode = false;
uint8_t find_icon(std::string name);
uint8_t find_last_clock();
bool string_has_ending(std::string const &fullString, std::string const &ending);
Expand All @@ -162,6 +167,8 @@ namespace esphome
void set_brightness(int b);
void set_display_on();
void set_display_off();
void set_night_mode_off();
void set_night_mode_on();
void set_clock(esphome::time::RealTimeClock *clock);
#ifdef USE_GRAPH
void set_graph(esphome::graph::Graph *graph);
Expand Down Expand Up @@ -229,6 +236,8 @@ namespace esphome
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); }
void add_on_start_running_trigger(EHMTXStartRunningTrigger *t) { this->on_start_running_triggers_.push_back(t); }
void add_on_show_display_trigger(EHMTXShowDisplayTrigger *t) { this->on_show_display_triggers_.push_back(t); }
void add_on_night_mode_trigger(EHMTXNightModeTrigger *t) { this->on_night_mode_triggers_.push_back(t); }
void update();

uint8_t get_brightness();
Expand Down Expand Up @@ -321,6 +330,20 @@ namespace esphome
void process();
};

class EHMTXShowDisplayTrigger : public Trigger<bool>
{
public:
explicit EHMTXShowDisplayTrigger(EHMTX *parent) { parent->add_on_show_display_trigger(this); }
void process(bool);
};

class EHMTXNightModeTrigger : public Trigger<bool>
{
public:
explicit EHMTXNightModeTrigger(EHMTX *parent) { parent->add_on_night_mode_trigger(this); }
void process(bool);
};

class EHMTX_Icon : public animation::Animation
{
protected:
Expand Down
29 changes: 29 additions & 0 deletions components/ehmtxv2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ def rgb565_888(v565):
"EHMTXAddScreenTrigger", automation.Trigger.template(cg.std_string)
)

ShowDisplayTrigger = ehmtx_ns.class_(
"EHMTXShowDisplayTrigger", automation.Trigger.template(cg.std_string)
)

NightModeTrigger = ehmtx_ns.class_(
"EHMTXNightModeTrigger", automation.Trigger.template(cg.std_string)
)

CONF_URL = "url"
CONF_CLOCKINTERVAL = "clock_interval"
CONF_ALWAYS_SHOW_RLINDICATORS = "always_show_rl_indicators"
Expand Down Expand Up @@ -108,6 +116,8 @@ def rgb565_888(v565):
CONF_ON_NEXT_CLOCK = "on_next_clock"
CONF_ON_ICON_ERROR = "on_icon_error"
CONF_ON_ADD_SCREEN = "on_add_screen"
CONF_ON_SHOW_DISPLAY = "on_show_display"
CONF_ON_NIGHT_MODE = "on_night_mode"
CONF_WEEKDAYTEXT = "weekdays"
CONF_REPLACE_TIME_DATE_FROM = "replace_time_date_from"
CONF_REPLACE_TIME_DATE_TO = "replace_time_date_to"
Expand All @@ -118,6 +128,9 @@ def rgb565_888(v565):
CONF_ICON = "icon_name"
CONF_TEXT = "text"
CONF_GRAPH = "display_graph"
CONF_NIGNT_MODE_SCREENS = "night_mode_screens"

DAFAULT_NIGNT_MODE_SCREENS = [2,3,16]

EHMTX_SCHEMA = cv.Schema({
cv.Required(CONF_ID): cv.declare_id(EHMTX_),
Expand Down Expand Up @@ -229,6 +242,19 @@ def rgb565_888(v565):
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ExpiredScreenTrigger),
}
),
cv.Optional(CONF_ON_SHOW_DISPLAY): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ShowDisplayTrigger),
}
),
cv.Optional(CONF_ON_NIGHT_MODE): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(NightModeTrigger),
}
),
cv.Optional(CONF_NIGNT_MODE_SCREENS, default=DAFAULT_NIGNT_MODE_SCREENS): cv.All(
cv.ensure_list(cv.one_of(1, 2, 3, 4, 5, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19)), cv.Length(min=1, max=5)
),
cv.Required(CONF_ICONS): cv.All(
cv.ensure_list(
{
Expand Down Expand Up @@ -481,6 +507,9 @@ def thumbnails(frames):
if config[CONF_RTL]:
cg.add_define("EHMTXv2_USE_RTL")

if config[CONF_NIGNT_MODE_SCREENS]:
cg.add_define("EHMTXv2_CONF_NIGNT_MODE_SCREENS",config[CONF_NIGNT_MODE_SCREENS])

cg.add(var.set_show_day_of_week(config[CONF_SHOWDOW]))

cg.add(var.set_show_seconds(config[CONF_SHOW_SECONDS]))
Expand Down

0 comments on commit ea80495

Please sign in to comment.