add test script

This commit is contained in:
NP v/d Spek 2023-11-21 15:25:19 +01:00
parent cc4cc4774f
commit 9ac02ea20c
7 changed files with 188 additions and 46 deletions

View file

@ -12,23 +12,6 @@
namespace esphome {
namespace network {
bool has_network() {
#ifdef USE_ETHERNET
if (ethernet::global_eth_component != nullptr)
return true;
#endif
#ifdef USE_WIFI
if (wifi::global_wifi_component != nullptr)
return true;
#endif
#ifdef USE_HOST
return true; // Assume its connected
#endif
return false;
}
bool is_connected() {
#ifdef USE_ETHERNET
if (ethernet::global_eth_component != nullptr && ethernet::global_eth_component->is_connected())

View file

@ -5,8 +5,7 @@
namespace esphome {
namespace network {
/// Return whether has network components enabled (through wifi, eth, ...)
bool has_network();
/// Return whether the node is connected to the network (through wifi, eth, ...)
bool is_connected();
/// Return whether the network is disabled (only wifi for now)

View file

@ -31,6 +31,7 @@ CONF_TRIGGER_LIST = {
"on_mgtt_disconnected": True,
"on_custom_status": False,
}
CONF_ON_TURN_OFF = "on_turn_off"
def trigger_setup(Single):
@ -56,6 +57,7 @@ CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(CONF_ID): cv.declare_id(StatusLED),
cv.Required(CONF_ON_TURN_OFF): trigger_setup(True),
}
)
.extend(cv.COMPONENT_SCHEMA)
@ -71,22 +73,24 @@ async def add_trigger(var, conf, key):
conf[CONF_PRIORITY],
)
await auto.build_automation(trigger, [], conf)
cg.add(var.set_trigger(key, trigger))
if key is not None:
cg.add(var.set_trigger(key, trigger))
@coroutine_with_priority(80.0)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await add_trigger(var, config.get(CONF_ON_TURN_OFF), CONF_ON_TURN_OFF)
for trigger_name, single in CONF_TRIGGER_LIST.items():
conf = config.get(trigger_name, None)
if conf is None:
continue
if single:
await add_trigger(var, conf, trigger_name)
else:
for conf in config.get(trigger_name, []):
await add_trigger(var, conf, conf[CONF_TRIGGER_ID].id)
if conf is not None:
if single:
await add_trigger(var, conf, trigger_name)
else:
for conf in config.get(trigger_name, []):
await add_trigger(var, conf, None)
@auto.register_action(

View file

@ -0,0 +1,5 @@
# Gitignore settings for ESPHome
# This is an example and may include too much for your use-case.
# You can modify this file to suit your needs.
/.esphome/
/secrets.yaml

View file

@ -0,0 +1,107 @@
---
esphome:
name: m5stack-atom-echo
esp32:
board: m5stack-atom
light:
- platform: esp32_rmt_led_strip
id: led
name: None
disabled_by_default: true
entity_category: config
pin: GPIO27
default_transition_length: 0s
chipset: SK6812
num_leds: 1
rgb_order: grb
rmt_channel: 0
effects:
- pulse:
name: "Slow Pulse"
transition_length: 250ms
update_interval: 250ms
min_brightness: 50%
max_brightness: 100%
- pulse:
name: "Fast Pulse"
transition_length: 100ms
update_interval: 100ms
min_brightness: 50%
max_brightness: 100%
binary_sensor:
- platform: gpio
pin:
number: GPIO39
inverted: true
name: Button
disabled_by_default: true
entity_category: diagnostic
id: button
on_press:
- status.push:
trigger_id: custom_status
on_release:
- status.pop:
trigger_id: custom_status
on_click:
- min_length: 50ms
max_length: 350ms
then:
- status.push:
trigger_id: custom_status
- min_length: 500ms
max_length: 1000ms
then:
- status.pop:
trigger_id: custom_status
status_indicator:
on_clear_all: #Manditory
- light.turn_off:
id: led
on_app_error:
then:
- light.turn_on:
id: led
red: 100%
green: 0%
blue: 0%
brightness: 100%
effect: Fast Pulse
on_app_warning:
then:
- light.turn_on:
id: led
red: 100%
green: 0%
blue: 0%
brightness: 100%
effect: None
on_custom_status:
- trigger_id: custom_status
then:
- light.turn_on:
id: led
red: 0%
green: 100%
blue: 0%
brightness: 100%
effect: None
- trigger_id: custom_status2
then:
- light.turn_on:
id: led
red: 0%
green: 100%
blue: 50%
brightness: 100%
effect: None

View file

@ -2,12 +2,9 @@
#include "esphome/core/log.h"
#include "esphome/core/application.h"
#include "esphome/components/network/util.h"
#ifdef USE_WIFI
#include "esphome/components/wifi/wifi_component.h"
#endif
#ifdef USE_MQTT
#include "esphome/components/mqtt/mqtt_client.h"
#endif
@ -18,6 +15,40 @@
namespace esphome {
namespace status_indicator {
bool has_network() {
#ifdef USE_ETHERNET
if (ethernet::global_eth_component != nullptr)
return true;
#endif
#ifdef USE_WIFI
if (wifi::global_wifi_component != nullptr)
return true;
#endif
#ifdef USE_HOST
return true; // Assume its connected
#endif
return false;
}
bool is_connected() {
#ifdef USE_ETHERNET
if (ethernet::global_eth_component != nullptr && ethernet::global_eth_component->is_connected())
return true;
#endif
#ifdef USE_WIFI
if (wifi::global_wifi_component != nullptr)
return wifi::global_wifi_component->is_connected();
#endif
#ifdef USE_HOST
return true; // Assume its connected
#endif
return false;
}
static const char *const TAG = "status_indicator";
void StatusIndicator::dump_config() { ESP_LOGCONFIG(TAG, "Status Indicator:"); }
@ -44,7 +75,7 @@ void StatusIndicator::loop() {
this->current_trigger_->stop_action();
}
}
if (network::has_network()) {
if (has_network()) {
#ifdef USE_WIFI
if (status == "" && wifi::global_wifi_component->is_ap_enabled()) {
status = "on_wifi_ap_enabled";
@ -55,7 +86,7 @@ void StatusIndicator::loop() {
}
#endif
if (status == "" && not network::is_connected()) {
if (status == "" && not is_connected()) {
status = "on_network_disconnected";
this->status_.on_network = 1;
} else if (this->status_.on_network == 1) {
@ -83,17 +114,19 @@ void StatusIndicator::loop() {
#endif
}
if (this->current_status_ != status) {
StatusTrigger *oldtrigger = this->current_trigger_;
if (status != "") {
this->current_trigger_ = get_trigger(status);
if (this->current_trigger_ != nullptr) {
this->current_trigger_->trigger();
this->current_trigger_ = get_trigger("on_turn_off");
}
} else if (!this->custom_triggers_.empty()) {
this->current_trigger_ = this->custom_triggers_[0];
} else {
this->current_trigger_ = nullptr;
if (!this->custom_triggers_.empty()) {
this->custom_triggers_[0]->trigger();
}
this->current_trigger_ = get_trigger("on_turn_off");
}
if (oldtrigger != this->current_trigger_)
this->current_trigger_->trigger();
this->current_status_ = status;
}
}
@ -115,12 +148,18 @@ void StatusIndicator::set_trigger(std::string key, StatusTrigger *trigger) { thi
void StatusIndicator::push_trigger(StatusTrigger *trigger) {
this->pop_trigger(trigger, true);
uint32_t x = 0;
while (this->custom_triggers_.size() > x) {
if (trigger->get_priority() <= this->custom_triggers_[x]->get_priority()) {
this->custom_triggers_.insert(this->custom_triggers_.begin() + x, trigger);
break;
} else {
x++;
if (this->custom_triggers_.empty()) {
this->custom_triggers_.push_back(trigger);
this->current_status_ = "update me";
} else {
while (this->custom_triggers_.size() > x) {
if (trigger->get_priority() <= this->custom_triggers_[x]->get_priority()) {
this->custom_triggers_.insert(this->custom_triggers_.begin() + x, trigger);
this->current_status_ = "update me";
break;
} else {
x++;
}
}
}
}
@ -130,8 +169,11 @@ void StatusIndicator::pop_trigger(StatusTrigger *trigger, bool incl_group) {
while (this->custom_triggers_.size() > x) {
if (trigger == this->custom_triggers_[x]) {
this->custom_triggers_.erase(this->custom_triggers_.begin() + x);
} else if (incl_group && trigger->get_group() != "" && trigger->get_group() == this->custom_triggers_[x]->get_group()) {
this->current_status_ = "update me";
} else if (incl_group && trigger->get_group() != "" &&
trigger->get_group() == this->custom_triggers_[x]->get_group()) {
this->custom_triggers_.erase(this->custom_triggers_.begin() + x);
this->current_status_ = "update me";
} else {
x++;
}
@ -143,10 +185,12 @@ void StatusIndicator::pop_trigger(std::string group) {
while (this->custom_triggers_.size() > x) {
if ( group == this->custom_triggers_[x]->get_group()) {
this->custom_triggers_.erase(this->custom_triggers_.begin() + x);
this->current_status_ = "update me";
} else {
x++;
}
}
}
} // namespace status_indicator

View file

@ -48,7 +48,7 @@ class StatusTrigger : public Trigger<> {
explicit StatusTrigger(StatusIndicator *parent, std::string group, uint32_t priority)
: parent_(parent), group_(group), priority_(priority) {}
std::string get_group() { return this->group_; }
uint32 get_priority() { return this->priority_; }
uint32_t get_priority() { return this->priority_; }
void push_me() { parent_->push_trigger(this); }
void pop_me() { parent_->pop_trigger(this, false); }
@ -74,7 +74,7 @@ template<typename... Ts> class StatusAction : public Action<Ts...> {
TEMPLATABLE_VALUE(bool, state)
void play(Ts... x) override {
if (this->state_) {
if (false) {
} else {