Skip to content

Commit

Permalink
update components for 2023.5
Browse files Browse the repository at this point in the history
  • Loading branch information
KaufHA committed May 17, 2023
1 parent 32c9215 commit 164ece2
Show file tree
Hide file tree
Showing 19 changed files with 139 additions and 100 deletions.
4 changes: 2 additions & 2 deletions components/esp8266/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def _parse_platform_version(value):
try:
# if platform version is a valid version constraint, prefix the default package
cv.platformio_version_constraint(value)
return f"platformio/espressif8266 @ {value}"
return f"platformio/espressif8266@{value}"
except cv.Invalid:
return value

Expand Down Expand Up @@ -183,7 +183,7 @@ async def to_code(config):
cg.add_platformio_option("platform", conf[CONF_PLATFORM_VERSION])
cg.add_platformio_option(
"platform_packages",
[f"platformio/framework-arduinoespressif8266 @ {conf[CONF_SOURCE]}"],
[f"platformio/framework-arduinoespressif8266@{conf[CONF_SOURCE]}"],
)

# Default for platformio is LWIP2_LOW_MEMORY with:
Expand Down
9 changes: 8 additions & 1 deletion components/light/base_light_effects.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PulseLightEffect : public LightEffect {
return;
}
auto call = this->state_->turn_on();
float out = this->on_ ? 1.0 : 0.0;
float out = this->on_ ? this->max_brightness : this->min_brightness;
call.set_brightness_if_supported(out);
this->on_ = !this->on_;
call.set_transition_length_if_supported(this->transition_length_);
Expand All @@ -41,11 +41,18 @@ class PulseLightEffect : public LightEffect {

void set_update_interval(uint32_t update_interval) { this->update_interval_ = update_interval; }

void set_min_max_brightness(float min, float max) {
this->min_brightness = min;
this->max_brightness = max;
}

protected:
bool on_ = false;
uint32_t last_color_change_{0};
uint32_t transition_length_{};
uint32_t update_interval_{};
float min_brightness{0.0};
float max_brightness{1.0};
};

/// Random effect. Sets random colors every 10 seconds and slowly transitions between them.
Expand Down
9 changes: 9 additions & 0 deletions components/light/effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
CONF_NUM_LEDS,
CONF_RANDOM,
CONF_SEQUENCE,
CONF_MAX_BRIGHTNESS,
CONF_MIN_BRIGHTNESS,
)
from esphome.util import Registry
from .types import (
Expand Down Expand Up @@ -174,12 +176,19 @@ async def automation_effect_to_code(config, effect_id):
cv.Optional(
CONF_UPDATE_INTERVAL, default="1s"
): cv.positive_time_period_milliseconds,
cv.Optional(CONF_MIN_BRIGHTNESS, default="0%"): cv.percentage,
cv.Optional(CONF_MAX_BRIGHTNESS, default="100%"): cv.percentage,
},
)
async def pulse_effect_to_code(config, effect_id):
effect = cg.new_Pvariable(effect_id, config[CONF_NAME])
cg.add(effect.set_transition_length(config[CONF_TRANSITION_LENGTH]))
cg.add(effect.set_update_interval(config[CONF_UPDATE_INTERVAL]))
cg.add(
effect.set_min_max_brightness(
config[CONF_MIN_BRIGHTNESS], config[CONF_MAX_BRIGHTNESS]
)
)
return effect


Expand Down
27 changes: 25 additions & 2 deletions components/ota/ota_backend_esp_idf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include <esp_ota_ops.h>
#include "esphome/components/md5/md5.h"

#if ESP_IDF_VERSION_MAJOR >= 5
#include <spi_flash_mmap.h>
#endif

namespace esphome {
namespace ota {

Expand All @@ -16,9 +20,28 @@ OTAResponseTypes IDFOTABackend::begin(size_t image_size) {
if (this->partition_ == nullptr) {
return OTA_RESPONSE_ERROR_NO_UPDATE_PARTITION;
}
esp_task_wdt_init(15, false); // The following function takes longer than the 5 seconds timeout of WDT

// The following function takes longer than the 5 seconds timeout of WDT
#if ESP_IDF_VERSION_MAJOR >= 5
esp_task_wdt_config_t wdtc;
wdtc.timeout_ms = 15000;
wdtc.idle_core_mask = 0;
wdtc.trigger_panic = false;
esp_task_wdt_reconfigure(&wdtc);
#else
esp_task_wdt_init(15, false);
#endif

esp_err_t err = esp_ota_begin(this->partition_, image_size, &this->update_handle_);
esp_task_wdt_init(CONFIG_ESP_TASK_WDT_TIMEOUT_S, false); // Set the WDT back to the configured timeout

// Set the WDT back to the configured timeout
#if ESP_IDF_VERSION_MAJOR >= 5
wdtc.timeout_ms = CONFIG_ESP_TASK_WDT_TIMEOUT_S;
esp_task_wdt_reconfigure(&wdtc);
#else
esp_task_wdt_init(CONFIG_ESP_TASK_WDT_TIMEOUT_S, false);
#endif

if (err != ESP_OK) {
esp_ota_abort(this->update_handle_);
this->update_handle_ = 0;
Expand Down
8 changes: 0 additions & 8 deletions components/switch/switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ void Switch::publish_state(bool state, bool force_save) {
ESP_LOGD(TAG, "'%s': Sending state %s", this->name_.c_str(), ONOFF(this->state));
this->state_callback_.call(this->state);
}

bool Switch::assumed_state() { return false; }

void Switch::add_on_state_callback(std::function<void(bool)> &&callback) {
Expand All @@ -75,13 +74,6 @@ void Switch::add_on_state_callback(std::function<void(bool)> &&callback) {
void Switch::set_inverted(bool inverted) { this->inverted_ = inverted; }
bool Switch::is_inverted() const { return this->inverted_; }

std::string Switch::get_device_class() {
if (this->device_class_.has_value())
return *this->device_class_;
return "";
}
void Switch::set_device_class(const std::string &device_class) { this->device_class_ = device_class; }

void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj) {
if (obj != nullptr) {
ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());
Expand Down
7 changes: 1 addition & 6 deletions components/switch/switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ enum SwitchRestoreMode {
* A switch is basically just a combination of a binary sensor (for reporting switch values)
* and a write_state method that writes a state to the hardware.
*/
class Switch : public EntityBase {
class Switch : public EntityBase, public EntityBase_DeviceClass {
public:
explicit Switch();

Expand Down Expand Up @@ -105,10 +105,6 @@ class Switch : public EntityBase {

bool is_inverted() const;

/// Get the device class for this switch.
std::string get_device_class();
/// Set the Home Assistant device class for this switch.
void set_device_class(const std::string &device_class);
void set_restore_mode(SwitchRestoreMode restore_mode) { this->restore_mode = restore_mode; }

bool has_forced_hash = false;
Expand Down Expand Up @@ -145,7 +141,6 @@ class Switch : public EntityBase {
bool inverted_{false};
Deduplicator<bool> publish_dedup_;
ESPPreferenceObject rtc_;
optional<std::string> device_class_;
};

#define LOG_SWITCH(prefix, type, obj) log_switch((TAG), (prefix), LOG_STR_LITERAL(type), (obj))
Expand Down
1 change: 1 addition & 0 deletions components/template/cover/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ async def to_code(config):
await automation.build_automation(
var.get_stop_trigger(), [], config[CONF_STOP_ACTION]
)
cg.add(var.set_has_stop(True))
if CONF_TILT_ACTION in config:
await automation.build_automation(
var.get_tilt_trigger(), [(float, "tilt")], config[CONF_TILT_ACTION]
Expand Down
2 changes: 2 additions & 0 deletions components/template/cover/template_cover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,15 @@ void TemplateCover::control(const CoverCall &call) {
CoverTraits TemplateCover::get_traits() {
auto traits = CoverTraits();
traits.set_is_assumed_state(this->assumed_state_);
traits.set_supports_stop(this->has_stop_);
traits.set_supports_position(this->has_position_);
traits.set_supports_tilt(this->has_tilt_);
return traits;
}
Trigger<float> *TemplateCover::get_position_trigger() const { return this->position_trigger_; }
Trigger<float> *TemplateCover::get_tilt_trigger() const { return this->tilt_trigger_; }
void TemplateCover::set_tilt_lambda(std::function<optional<float>()> &&tilt_f) { this->tilt_f_ = tilt_f; }
void TemplateCover::set_has_stop(bool has_stop) { this->has_stop_ = has_stop; }
void TemplateCover::set_has_position(bool has_position) { this->has_position_ = has_position; }
void TemplateCover::set_has_tilt(bool has_tilt) { this->has_tilt_ = has_tilt; }
void TemplateCover::stop_prev_trigger_() {
Expand Down
2 changes: 2 additions & 0 deletions components/template/cover/template_cover.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class TemplateCover : public cover::Cover, public Component {
void set_optimistic(bool optimistic);
void set_assumed_state(bool assumed_state);
void set_tilt_lambda(std::function<optional<float>()> &&tilt_f);
void set_has_stop(bool has_stop);
void set_has_position(bool has_position);
void set_has_tilt(bool has_tilt);
void set_restore_mode(TemplateCoverRestoreMode restore_mode) { restore_mode_ = restore_mode; }
Expand All @@ -48,6 +49,7 @@ class TemplateCover : public cover::Cover, public Component {
bool optimistic_{false};
Trigger<> *open_trigger_;
Trigger<> *close_trigger_;
bool has_stop_{false};
Trigger<> *stop_trigger_;
Trigger<> *prev_command_trigger_{nullptr};
Trigger<float> *position_trigger_;
Expand Down
13 changes: 6 additions & 7 deletions components/template/sensor/template_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ namespace template_ {
static const char *const TAG = "template.sensor";

void TemplateSensor::update() {
if (this->f_.has_value()) {
auto val = (*this->f_)();
if (val.has_value()) {
this->publish_state(*val);
}
} else if (!std::isnan(this->get_raw_state())) {
this->publish_state(this->get_raw_state());
if (!this->f_.has_value())
return;

auto val = (*this->f_)();
if (val.has_value()) {
this->publish_state(*val);
}
}
float TemplateSensor::get_setup_priority() const { return setup_priority::HARDWARE; }
Expand Down
13 changes: 6 additions & 7 deletions components/template/text_sensor/template_text_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ namespace template_ {
static const char *const TAG = "template.text_sensor";

void TemplateTextSensor::update() {
if (this->f_.has_value()) {
auto val = (*this->f_)();
if (val.has_value()) {
this->publish_state(*val);
}
} else if (this->has_state()) {
this->publish_state(this->state);
if (!this->f_.has_value())
return;

auto val = (*this->f_)();
if (val.has_value()) {
this->publish_state(*val);
}
}
float TemplateTextSensor::get_setup_priority() const { return setup_priority::HARDWARE; }
Expand Down
3 changes: 3 additions & 0 deletions components/web_server/web_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,9 @@ void WebServer::on_switch_update(switch_::Switch *obj, bool state) {
std::string WebServer::switch_json(switch_::Switch *obj, bool value, JsonDetail start_config) {
return json::build_json([obj, value, start_config](JsonObject root) {
set_json_icon_state_value(root, obj, "switch-" + obj->get_object_id(), value ? "ON" : "OFF", value, start_config);
if (start_config == DETAIL_ALL) {
root["assumed_state"] = obj->assumed_state();
}
});
}
void WebServer::handle_switch_request(AsyncWebServerRequest *request, const UrlMatch &match) {
Expand Down
3 changes: 3 additions & 0 deletions components/wifi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ def _validate(config):


CONF_OUTPUT_POWER = "output_power"
CONF_PASSIVE_SCAN = "passive_scan"
CONFIG_SCHEMA = cv.All(
cv.Schema(
{
Expand Down Expand Up @@ -300,6 +301,7 @@ def _validate(config):
cv.SplitDefault(CONF_ENABLE_RRM, esp32_idf=False): cv.All(
cv.boolean, cv.only_with_esp_idf
),
cv.Optional(CONF_PASSIVE_SCAN, default=False): cv.boolean,
cv.Optional("enable_mdns"): cv.invalid(
"This option has been removed. Please use the [disabled] option under the "
"new mdns component instead."
Expand Down Expand Up @@ -403,6 +405,7 @@ def add_sta(ap, network):
cg.add(var.set_reboot_timeout(config[CONF_REBOOT_TIMEOUT]))
cg.add(var.set_power_save_mode(config[CONF_POWER_SAVE_MODE]))
cg.add(var.set_fast_connect(config[CONF_FAST_CONNECT]))
cg.add(var.set_passive_scan(config[CONF_PASSIVE_SCAN]))
if CONF_OUTPUT_POWER in config:
cg.add(var.set_output_power(config[CONF_OUTPUT_POWER]))

Expand Down
4 changes: 3 additions & 1 deletion components/wifi/wifi_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ void WiFiComponent::print_connect_params_() {
void WiFiComponent::start_scanning() {
this->action_started_ = millis();
ESP_LOGD(TAG, "Starting scan...");
this->wifi_scan_start_();
this->wifi_scan_start_(this->passive_scan_);
this->state_ = WIFI_COMPONENT_STATE_STA_SCANNING;
}

Expand Down Expand Up @@ -677,6 +677,8 @@ bool WiFiComponent::is_connected() {
}
void WiFiComponent::set_power_save_mode(WiFiPowerSaveMode power_save) { this->power_save_ = power_save; }

void WiFiComponent::set_passive_scan(bool passive) { this->passive_scan_ = passive; }

std::string WiFiComponent::format_mac_addr(const uint8_t *mac) {
char buf[20];
sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Expand Down
5 changes: 4 additions & 1 deletion components/wifi/wifi_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ class WiFiComponent : public Component {
void set_power_save_mode(WiFiPowerSaveMode power_save);
void set_output_power(float output_power) { output_power_ = output_power; }

void set_passive_scan(bool passive);

void save_wifi_sta(const std::string &ssid, const std::string &password);
// ========== INTERNAL METHODS ==========
// (In most use cases you won't need these)
Expand Down Expand Up @@ -330,7 +332,7 @@ class WiFiComponent : public Component {
bool wifi_sta_connect_(const WiFiAP &ap);
void wifi_pre_setup_();
WiFiSTAConnectStatus wifi_sta_connect_status_();
bool wifi_scan_start_();
bool wifi_scan_start_(bool passive);
bool wifi_ap_ip_config_(optional<ManualIP> manual_ip);
bool wifi_start_ap_(const WiFiAP &ap);
bool wifi_disconnect_();
Expand Down Expand Up @@ -385,6 +387,7 @@ class WiFiComponent : public Component {
bool scan_done_{false};
bool ap_setup_{false};
optional<float> output_power_;
bool passive_scan_{false};
ESPPreferenceObject pref_;
bool has_saved_wifi_settings_{false};
#ifdef USE_WIFI_11KV_SUPPORT
Expand Down
4 changes: 2 additions & 2 deletions components/wifi/wifi_component_esp32_arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,13 +618,13 @@ WiFiSTAConnectStatus WiFiComponent::wifi_sta_connect_status_() {
}
return WiFiSTAConnectStatus::IDLE;
}
bool WiFiComponent::wifi_scan_start_() {
bool WiFiComponent::wifi_scan_start_(bool passive) {
// enable STA
if (!this->wifi_mode_(true, {}))
return false;

// need to use WiFi because of WiFiScanClass allocations :(
int16_t err = WiFi.scanNetworks(true, true, false, 200);
int16_t err = WiFi.scanNetworks(true, true, passive, 200);
if (err != WIFI_SCAN_RUNNING) {
ESP_LOGV(TAG, "WiFi.scanNetworks failed! %d", err);
return false;
Expand Down
21 changes: 15 additions & 6 deletions components/wifi/wifi_component_esp8266.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ WiFiSTAConnectStatus WiFiComponent::wifi_sta_connect_status_() {
return WiFiSTAConnectStatus::IDLE;
}
}
bool WiFiComponent::wifi_scan_start_() {
bool WiFiComponent::wifi_scan_start_(bool passive) {
static bool first_scan = false;

// enable STA
Expand All @@ -615,13 +615,21 @@ bool WiFiComponent::wifi_scan_start_() {
config.channel = 0;
config.show_hidden = 1;
#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 4, 0)
config.scan_type = WIFI_SCAN_TYPE_ACTIVE;
config.scan_type = passive ? WIFI_SCAN_TYPE_PASSIVE : WIFI_SCAN_TYPE_ACTIVE;
if (first_scan) {
config.scan_time.active.min = 100;
config.scan_time.active.max = 200;
if (passive) {
config.scan_time.passive = 200;
} else {
config.scan_time.active.min = 100;
config.scan_time.active.max = 200;
}
} else {
config.scan_time.active.min = 400;
config.scan_time.active.max = 500;
if (passive) {
config.scan_time.passive = 500;
} else {
config.scan_time.active.min = 400;
config.scan_time.active.max = 500;
}
}
#endif
first_scan = false;
Expand Down Expand Up @@ -698,6 +706,7 @@ bool WiFiComponent::wifi_ap_ip_config_(optional<ManualIP> manual_ip) {
#endif

struct dhcps_lease lease {};
lease.enable = true;
network::IPAddress start_address = info.ip.addr;
start_address[3] += 99;
lease.start_ip.addr = static_cast<uint32_t>(start_address);
Expand Down
Loading

0 comments on commit 164ece2

Please sign in to comment.