Skip to content

Commit

Permalink
esphome 2022.10
Browse files Browse the repository at this point in the history
  • Loading branch information
KaufHA committed Oct 18, 2022
1 parent 90b2999 commit af55b42
Show file tree
Hide file tree
Showing 13 changed files with 814 additions and 803 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__pycache__
_static
node_modules
esphome-webserver/captive-portal/package-lock.json
241 changes: 122 additions & 119 deletions components/captive_portal/captive_index.h

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions components/esp8266/gpio.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
from dataclasses import dataclass
from typing import List

from esphome.const import (
CONF_ID,
Expand Down Expand Up @@ -200,7 +199,7 @@ async def esp8266_pin_to_code(config):
@coroutine_with_priority(-999.0)
async def add_pin_initial_states_array():
# Add includes at the very end, so that they override everything
initial_states: List[PinInitialState] = CORE.data[KEY_ESP8266][
initial_states: list[PinInitialState] = CORE.data[KEY_ESP8266][
KEY_PIN_INITIAL_STATES
]
initial_modes_s = ", ".join(str(x.mode) for x in initial_states)
Expand Down
13 changes: 0 additions & 13 deletions components/esp8266/preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,6 @@ class ESP8266Preferences : public ESPPreferences {

if (in_flash) {

// looking for type 2729014980 and length 2 to erase all flash. Length 2 just to make it less likely to occur accidentally.
if ( (type==2729014980) && (length==2) && (current_flash_offset!=0)) {
auto *pref = new ESP8266PreferenceBackend(); // NOLINT(cppcoreguidelines-owning-memory)
ESP_LOGD("KAUF Preferences", " !!!! ERASING FLASH THROUGH ADDR %d !!!!", current_flash_offset - 1);
pref->offset = 0;
pref->type = type;
pref->length_words = current_flash_offset - 1;
pref->in_flash = true;
uint8_t fake_data = 0;
pref->save(&fake_data,(current_flash_offset-1)*4);
return {};
}

uint32_t start;
uint32_t end;

Expand Down
7 changes: 3 additions & 4 deletions components/select/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from typing import List
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation
Expand Down Expand Up @@ -63,7 +62,7 @@
)


async def setup_select_core_(var, config, *, options: List[str]):
async def setup_select_core_(var, config, *, options: list[str]):
await setup_entity(var, config)

cg.add(var.traits.set_options(options))
Expand All @@ -88,14 +87,14 @@ async def setup_select_core_(var, config, *, options: List[str]):
cg.add(var.set_global_addr(ga))


async def register_select(var, config, *, options: List[str]):
async def register_select(var, config, *, options: list[str]):
if not CORE.has_id(config[CONF_ID]):
var = cg.Pvariable(config[CONF_ID], var)
cg.add(cg.App.register_select(var))
await setup_select_core_(var, config, options=options)


async def new_select(config, *, options: List[str]):
async def new_select(config, *, options: list[str]):
var = cg.new_Pvariable(config[CONF_ID])
await register_select(var, config, options=options)
return var
Expand Down
1,255 changes: 626 additions & 629 deletions components/web_server/server_index.h

Large diffs are not rendered by default.

45 changes: 20 additions & 25 deletions components/web_server/web_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1204,9 +1204,7 @@ void WebServer::reset_flash(AsyncWebServerRequest *request) {

ESP_LOGD("kauf web server", "erasing flash");

// using hash of 2729014980 and length 2 erases all used flash.
ESPPreferenceObject pref;
pref = global_preferences->make_preference(2,2729014980,true);
global_preferences->reset();

AsyncResponseStream *stream = request->beginResponseStream("text/html");
stream->addHeader("Access-Control-Allow-Origin", "*");
Expand All @@ -1228,37 +1226,34 @@ void WebServer::clear_wifi(AsyncWebServerRequest *request) {
stream->addHeader("Access-Control-Allow-Origin", "*");
stream->print(F("<!DOCTYPE html><html><head><meta charset=UTF-8><link rel=icon href=data:></head><body>"));

// if there is no soft ssid, provide message nothing to clear
if ( strcmp(wifi::global_wifi_component->soft_ssid.c_str(),"") == 0 ) {
stream->print(F("This utility currently only clears Wi-Fi credentials that were configured using the captive portal after "));
stream->print(F("the firmware was compiled. Since this device has no such Wi-Fi credentials, nothing has been cleared. "));
stream->print(F("<br /><br />This device will continue to try to connect to the hard-coded Wi-Fi network with SSID <b>"));
stream->print(wifi::global_wifi_component->hard_ssid.c_str());
stream->print(F("</b>.<br /><br />"));
stream->print(F("</body></html>"));
request->send(stream);
} else {
wifi::global_wifi_component->clear_stored_creds();
stream->print(F("The Wi-Fi credentials with SSID <b>"));
stream->print(wifi::global_wifi_component->soft_ssid.c_str());
stream->print(F("</b>, which were configured via the captive portal, are being cleared. "));
stream->print(F("<br /><br />This device will continue to try to connect to the hard-coded Wi-Fi network with SSID <b>"));
stream->print(wifi::global_wifi_component->hard_ssid.c_str());
stream->print(F("</b>.<br /><br />"));

stream->print(F("This device is now restarting itself automatically."));
if ( wifi::global_wifi_component->has_ap() ) {

// store default credentials in flash, overwrites both soft and hard-coded credentials to ensure that Wi-Fi AP comes up.
stream->print(F("The following Wi-Fi credentials are being saved into flash memory:<br>"));
stream->print(F("SSID: <b>initial_ap</b><br>"));
stream->print(F("Password: <b>asdfasdfasdfasdf</b><br><br>"));

stream->print(F("This will overwrite any previous credentials that were stored in flash memory via the captive portal. "));
stream->print(F("This will also take precedence over any credentials that were hard-coded in yaml. "));
stream->print(F("This device will now reboot and put up it's Wi-Fi AP to allow setting of new credentials. However, if a network with the above credentials exists, this device will connect to that network instead of putting up its Wi-Fi AP."));
stream->print(F("</body></html>"));
request->send(stream);

wifi::global_wifi_component->save_wifi_sta("initial_ap","asdfasdfasdfasdf");

this->set_timeout(100, []() { App.safe_reboot(); });

}

else {
stream->print(F("This function is only available for devices that have the Wi-Fi AP enabled."));
stream->print(F("</body></html>"));
request->send(stream);
}

return;
}




} // namespace web_server
} // namespace esphome

Expand Down
1 change: 1 addition & 0 deletions components/web_server_base/web_server_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class WebServerBase : public Component {
return;
}
this->server_ = std::make_shared<AsyncWebServer>(this->port_);
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*");
this->server_->begin();

for (auto *handler : this->handlers_)
Expand Down
18 changes: 12 additions & 6 deletions components/wifi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,7 @@ def manual_ip(config):
)


def wifi_network(config, static_ip):
ap = cg.variable(config[CONF_ID], WiFiAP())
def wifi_network(config, ap, static_ip):
if CONF_SSID in config:
cg.add(ap.set_ssid(config[CONF_SSID]))
if CONF_PASSWORD in config:
Expand All @@ -363,14 +362,21 @@ async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
cg.add(var.set_use_address(config[CONF_USE_ADDRESS]))

for network in config.get(CONF_NETWORKS, []):
def add_sta(ap, network):
ip_config = network.get(CONF_MANUAL_IP, config.get(CONF_MANUAL_IP))
cg.add(var.add_sta(wifi_network(network, ip_config)))
cg.add(var.add_sta(wifi_network(network, ap, ip_config)))

for network in config.get(CONF_NETWORKS, []):
cg.with_local_variable(network[CONF_ID], WiFiAP(), add_sta, network)

if CONF_AP in config:
conf = config[CONF_AP]
ip_config = conf.get(CONF_MANUAL_IP, config.get(CONF_MANUAL_IP))
cg.add(var.set_ap(wifi_network(conf, ip_config)))
ip_config = conf.get(CONF_MANUAL_IP)
cg.with_local_variable(
conf[CONF_ID],
WiFiAP(),
lambda ap: cg.add(var.set_ap(wifi_network(conf, ap, ip_config))),
)
cg.add(var.set_ap_timeout(conf[CONF_AP_TIMEOUT]))

cg.add(var.set_reboot_timeout(config[CONF_REBOOT_TIMEOUT]))
Expand Down
6 changes: 6 additions & 0 deletions components/wifi/wifi_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ void WiFiComponent::loop() {
this->wifi_loop_();
const uint32_t now = millis();

// hard code an AP timeout of 15 seconds for all devices if Wi-Fi credentials are not configured.
if ( (this->soft_ssid == "initial_ap") ||
((this->hard_ssid == "initial_ap") && this->tried_loading_creds && !this->loaded_creds ) ) {
this->set_ap_timeout(15000);
}

if (this->has_sta()) {
switch (this->state_) {
case WIFI_COMPONENT_STATE_COOLDOWN: {
Expand Down
2 changes: 1 addition & 1 deletion esphome-webserver/captive-portal/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ <h3>Wi-Fi Credentials:</h3>
<br>
<div style="display:flex">
<p>Show password:</p>
<p><input type="checkbox" onclick="togglepass()" style="width:auto"></p>
<p><input type="checkbox" onclick="togglepass();document.getElementById('psk').focus()" style="width:auto"></p>
</div>
<hr>
<br>
Expand Down
8 changes: 8 additions & 0 deletions esphome-webserver/scripts/make_header.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ cat <<EOT >> ./$1/$2
} // namespace esphome
EOT
ls -l ./$1/$2

if [ "web_server" = "$3" ]; then
cp ./_static/v2/server_index.h ../components/web_server/
fi

if [ "captive_portal" = "$3" ]; then
cp ./dist/captive_index.h ../../components/captive_portal/
fi
14 changes: 10 additions & 4 deletions esphome-webserver/v2/esp-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ export default class EspApp extends LitElement {
kauf_p_name() {
if ( this.config.proj_n == "Kauf.PLF10")
return "Plug";
else if ( this.config.proj_n == "Kauf.RGBWW")
else if ( this.config.proj_n == "Kauf.PLF12")
return "Plug";
else if ( this.config.proj_n == "Kauf.RGBWW")
return "RGBWW Bulb";
else if ( this.config.proj_n == "Kauf.RGBSw")
return "RGB Switch";
Expand All @@ -90,6 +92,8 @@ export default class EspApp extends LitElement {
kauf_p_url() {
if ( this.config.proj_n == "Kauf.PLF10")
return "plf10";
else if ( this.config.proj_n == "Kauf.PLF12")
return "plf12";
else if ( this.config.proj_n == "Kauf.RGBWW")
return "blf10";
else if ( this.config.proj_n == "Kauf.RGBSw")
Expand All @@ -101,6 +105,8 @@ export default class EspApp extends LitElement {
kauf_p_up() {
if ( this.config.proj_n == "Kauf.PLF10")
return html`<br><a href="https://github.com/KaufHA/PLF10/releases" target="_blank" rel="noopener noreferrer">Check for Updates</a>`;
else if ( this.config.proj_n == "Kauf.PLF12")
return html`<br><a href="https://github.com/KaufHA/PLF12/releases" target="_blank" rel="noopener noreferrer">Check for Updates</a>`;
else if ( this.config.proj_n == "Kauf.RGBWW")
return html`<br><a href="https://github.com/KaufHA/kauf-rgbww-bulbs/releases" target="_blank" rel="noopener noreferrer">Check for Updates</a>`;
else if ( this.config.proj_n == "Kauf.RGBSw")
Expand Down Expand Up @@ -131,12 +137,12 @@ export default class EspApp extends LitElement {

factory_reset() {
if ( this.config.proj_l == "f" )
return html `<p><b> Important note for factory images, with version suffix (f)</b> - /reset will place the firmware into factory test mode. Factory test mode can typically be cleared easily by pressing a button on the device. For bulbs, factory test mode cannot be cleared and will cause the bulb to cycle through colors for 10 minutes with no way to stop the routine. <b>It is strongly recommended</b> to update the firmware with a bin.gz file downloaded from the "check for updates" link above, even if the version number is the same, in order to get rid of the factory test routine before trying to reset the flash memory using this method.</p>`
return html `<p><b> For factory images, with version suffix (f)</b>, /reset will place the firmware into factory test mode. Factory test mode can typically be cleared easily by pressing a button on the device after a few seconds. For bulbs, factory test mode will automatically stop after 10 minutes or can be cleared through the web interface by pressing the "Stop Factory Routine" button once you get the bulb connected back to Wi-Fi. You may need to refresh the page to see this button.</p>`
}

clear() {
if ( this.config.soft_ssid != "" )
return html `<p><a href="/clear" target="_blank">/clear</a> - Clears the software-configured Wi-Fi credentials. The device will reboot and try to connect to the hard-coded Wi-Fi credentials. If this firwmare has the AP enabled, and the hard-coded Wi-Fi credentials cannot be connected to, then this device will put up a Wi-Fi AP allowing new software-configured Wi-Fi credential to be entered.</p>`
if ( this.config.has_ap )
return html `<p><a href="/clear" target="_blank">/clear</a> - Writes new software-configured Wi-Fi credentials (SSID: initial_ap, password: asdfasdfasdfasdf). The device will reboot and try to connect to a network with those credentials. If those credentials cannot be connected to, then this device will put up a Wi-Fi AP allowing new software-configured Wi-Fi credential to be entered.</p>`
}

render() {
Expand Down

0 comments on commit af55b42

Please sign in to comment.