diff options
-rw-r--r-- | kauf-plug.yaml | 207 |
1 files changed, 115 insertions, 92 deletions
diff --git a/kauf-plug.yaml b/kauf-plug.yaml index 47c115b..bafd239 100644 --- a/kauf-plug.yaml +++ b/kauf-plug.yaml @@ -15,7 +15,7 @@ substitutions: # https://esphome.io/components/esphome.html#esphome-creators-project project_name: Kauf.PLF10 - project_ver_num: "1.97" + project_ver_num: "1.964" project_ver_let: y # https://esphome.io/components/switch/gpio.html?highlight=restore_mode @@ -90,10 +90,6 @@ globals: type: int restore_value: no initial_value: '0' - - id: global_has_debounce - type: boolean - restore_value: no - initial_value: "false" # https://esphome.io/components/esphome.html#adjusting-flash-writes @@ -109,13 +105,18 @@ esphome: name: $project_name version: $project_ver_num($project_ver_let) + on_boot: + priority: 700 + then: + # implementing on_boot function as a script so that any other on_boot automations in other files + # will execute in parallel and not be blocked by this one. + - script.execute: on_boot_main # https://esphome.io/components/external_components.html external_components: - source: type: git url: https://github.com/KaufHA/common - ref: v2022.06.21 refresh: 0s @@ -303,6 +304,99 @@ script: then: - lambda: return; + - id: on_boot_main + then: + + # this on_boot script has to execute after select and number entities are set up, so + # priority < 800. Adding a check here to make sure all needed entities are set up just + # in case another yaml file makes on_boot priority higher than 800 the following code + # will still work right. If on_boot priority becomes <= 600, the code for setting up + # power monitoring mode won't work because HLW will already be set up before values are + # changed in this script. + - wait_until: + lambda: return (id(select_monitor_mode).has_state() && + id(select_button_old).has_state() && + id(select_button).has_state() && + id(number_debounce).has_state() ); + + - lambda: |- + + ////////////////////////////////////////////////////////////////////// + // implement power monitoring mode per select entity + + if ( id(select_monitor_mode).state == "10s P / 40s V,I") { + id(hlw_main).set_update_interval(10000); + id(hlw_main).set_change_mode_every(1); } + + else if ( id(select_monitor_mode).state == "10s P,I Only" ) { + id(hlw_main).set_update_interval(10000); + id(hlw_main).set_change_mode_every(4294967295); + id(hlw_main).set_initial_mode(hlw8012::HLW8012_INITIAL_MODE_CURRENT); } + + else if ( id(select_monitor_mode).state == "10s P,V Only" ) { + id(hlw_main).set_update_interval(10000); + id(hlw_main).set_change_mode_every(4294967295); + id(hlw_main).set_initial_mode(hlw8012::HLW8012_INITIAL_MODE_VOLTAGE); } + + else if ( id(select_monitor_mode).state == " 2s P,I Only" ) { + id(hlw_main).set_update_interval(2000); + id(hlw_main).set_change_mode_every(4294967295); + id(hlw_main).set_initial_mode(hlw8012::HLW8012_INITIAL_MODE_CURRENT); } + + else if ( id(select_monitor_mode).state == " 2s P,V Only" ) { + id(hlw_main).set_update_interval(2000); + id(hlw_main).set_change_mode_every(4294967295); + id(hlw_main).set_initial_mode(hlw8012::HLW8012_INITIAL_MODE_VOLTAGE); } + + ////////////////////////////////////////////////////////////////////// + // Recover legacy button config if present, delete June 1, 2023. If + // you haven't updated in 1 year, you are going to lose your config, sorry. + + if ( id(select_button_old).state == "Didn't Restore" ) { + ESP_LOGD("Kauf Config Migration", "No legacy button configuration to migrate."); + } else { + ESP_LOGD("Kauf Config Migration", "Found legacy button configuration, migrating to new format."); + + // set button config select entity based on press/release/disabled + auto call_sel = id(select_button).make_call(); + if ( id(select_button_old).active_index() == 1 ) { call_sel.set_option("Don't Toggle"); } + else if ( id(select_button_old).active_index() >= 5 ) { call_sel.set_option("Toggle on Release"); } + else { call_sel.set_option("Toggle on Press"); } + call_sel.perform(); + + // set number_debounce if configured for longer than 100ms. + std::size_t found = id(select_button_old).state.find("250ms"); + if (found != std::string::npos) { + auto call_num = id(number_debounce).make_call(); + call_num.set_value(250); + call_num.perform(); } + found = id(select_button_old).state.find("1s"); + if (found != std::string::npos) { + auto call_num = id(number_debounce).make_call(); + call_num.set_value(1000); + call_num.perform(); } + + } + + // keep old format select entity from ever trying to save over the new one. + id(select_button_old).set_restore_value(false); + + // END SECTION TO DELETE JUNE 1, 2023 + ////////////////////////////////////////////////////////////////////// + + ////////////////////////////////////////////////////////////////////// + // add a delayed_on filter to the button binary_sensor with time argument from number_debounce. + // this is placed after settings recovery so that it will take into account recovered setting + // on first boot. In any case, needs to be after priority 800 so that number_debounce is set up. + + binary_sensor::DelayedOnFilter *button_delayedonfilter; + button_delayedonfilter = new binary_sensor::DelayedOnFilter(id(number_debounce).state); + button_delayedonfilter->set_component_source("binary_sensor"); + App.register_component(button_delayedonfilter); + id(button_in).add_filters({button_delayedonfilter}); + + + # pwm outputs for LEDs so they can be dimmed # https://esphome.io/components/output/esp8266_pwm.html @@ -587,24 +681,8 @@ number: # used as a threshold for whether the plugged-in devices is running forced_hash: 2232959069 forced_addr: 40 global_addr: global_forced_addr - set_action: # currently rebooting plug to apply new delayed_on time + set_action: # currently, have to reboot plug to apply new delayed_on time - script.execute: script_new_debounce - on_value: - - wait_until: - lambda: return ( id(select_button_old).state == "Didn't Restore" ); - - lambda: |- - // add a delayed_on filter to the button binary_sensor with time argument from number_debounce. - // we wait until any setting recovery has completed so that this will take into account recovered - // setting. - - if ( !id(global_has_debounce) ) { - binary_sensor::DelayedOnFilter *button_delayedonfilter; - button_delayedonfilter = new binary_sensor::DelayedOnFilter(id(number_debounce).state); - button_delayedonfilter->set_component_source("binary_sensor"); - App.register_component(button_delayedonfilter); - id(button_in).add_filters({button_delayedonfilter}); - id(global_has_debounce) = true; - } # https://esphome.io/components/select/index.html # https://esphome.io/components/select/template.html @@ -634,42 +712,6 @@ select: forced_hash: 3616613942 forced_addr: 34 global_addr: global_forced_addr - on_value: - - wait_until: # wait until new select entity is setup - lambda: return ( id(select_button).has_state() ); - - lambda: |- - if ( x == "Didn't Restore" ) { - ESP_LOGD("Kauf Config Migration", "No legacy button configuration to migrate."); - } else { - ESP_LOGD("Kauf Config Migration", "Found legacy button configuration, migrating to new format."); - - // set button config select entity based on press/release/disabled - auto call_sel = id(select_button).make_call(); - if ( i == 1 ) { call_sel.set_option("Don't Toggle"); } - else if ( i >= 5 ) { call_sel.set_option("Toggle on Release"); } - else { call_sel.set_option("Toggle on Press"); } - call_sel.perform(); - - // set number_debounce if configured for longer than 100ms. - std::size_t found = x.find("250ms"); - if (found != std::string::npos) { - auto call_num = id(number_debounce).make_call(); - call_num.set_value(250); - call_num.perform(); } - found = x.find("1s"); - if (found != std::string::npos) { - auto call_num = id(number_debounce).make_call(); - call_num.set_value(1000); - call_num.perform(); } - - // change legacy select entity to "Didn't Restore" to indicate we did restore - auto call_sel2 = id(select_button_old).make_call(); - call_sel2.set_option("Didn't Restore"); - call_sel2.perform(); - } - - delay: 5s # delay long enough to save values to flash - # then stop lambda from ever saving new value to flash - - lambda: id(select_button_old).set_restore_value(false); - platform: template name: $friendly_name Button Config @@ -706,12 +748,13 @@ select: forced_addr: 36 global_addr: global_forced_addr on_value: - - if: - condition: - - lambda: return ( x == "Error Status" ); - then: - - light.turn_off: blue_led - - script.execute: script_set_power_leds + then: + - if: + condition: + - lambda: return ( x == "Error Status" ); + then: + - light.turn_off: blue_led + - script.execute: script_set_power_leds - platform: template name: $friendly_name Red LED Config @@ -732,12 +775,13 @@ select: forced_addr: 38 global_addr: global_forced_addr on_value: - - if: - condition: - - lambda: return ( x == "Error Status" ); - then: - - light.turn_off: red_led - - script.execute: script_set_power_leds + then: + - if: + condition: + - lambda: return ( x == "Error Status" ); + then: + - light.turn_off: red_led + - script.execute: script_set_power_leds # change mode of power monitoring - platform: template @@ -764,27 +808,6 @@ select: - lambda: global_preferences->sync(); - delay: 2s - button.press: restart_button - on_value: - - lambda: |- - if ( x == "10s P / 40s V,I") { - id(hlw_main).set_update_interval(10000); - id(hlw_main).set_change_mode_every(1); } - else if ( x == "10s P,I Only" ) { - id(hlw_main).set_update_interval(10000); - id(hlw_main).set_change_mode_every(4294967295); - id(hlw_main).set_initial_mode(hlw8012::HLW8012_INITIAL_MODE_CURRENT); } - else if ( x == "10s P,V Only" ) { - id(hlw_main).set_update_interval(10000); - id(hlw_main).set_change_mode_every(4294967295); - id(hlw_main).set_initial_mode(hlw8012::HLW8012_INITIAL_MODE_VOLTAGE); } - else if ( x == " 2s P,I Only" ) { - id(hlw_main).set_update_interval(2000); - id(hlw_main).set_change_mode_every(4294967295); - id(hlw_main).set_initial_mode(hlw8012::HLW8012_INITIAL_MODE_CURRENT); } - else if ( x == " 2s P,V Only" ) { - id(hlw_main).set_update_interval(2000); - id(hlw_main).set_change_mode_every(4294967295); - id(hlw_main).set_initial_mode(hlw8012::HLW8012_INITIAL_MODE_VOLTAGE); } # Send IP Address to HA |