Otto Winter 2019-11-12 17:39:27 +01:00
parent 2ef64b55c5
commit e3640e710f
No known key found for this signature in database
GPG Key ID: DB66C0BE6013F97E
6 changed files with 23 additions and 0 deletions

View File

@ -110,6 +110,7 @@ def validate(config):
return config
CONF_OUTPUT_POWER = 'output_power'
CONFIG_SCHEMA = cv.All(cv.Schema({
cv.GenerateID(): cv.declare_id(WiFiComponent),
cv.Optional(CONF_NETWORKS): cv.ensure_list(WIFI_NETWORK_STA),
@ -125,6 +126,7 @@ CONFIG_SCHEMA = cv.All(cv.Schema({
cv.enum(WIFI_POWER_SAVE_MODES, upper=True),
cv.Optional(CONF_FAST_CONNECT, default=False): cv.boolean,
cv.Optional(CONF_USE_ADDRESS): cv.string_strict,
cv.Optional(CONF_OUTPUT_POWER): cv.All(cv.decibel, cv.float_range(min=10.0, max=20.5)),
cv.Optional('hostname'): cv.invalid("The hostname option has been removed in 1.11.0"),
}), validate)
@ -186,6 +188,8 @@ def to_code(config):
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]))
if CONF_OUTPUT_POWER in config:
cg.add(var.set_output_power(config[CONF_OUTPUT_POWER]))
if CORE.is_esp8266:
cg.add_library('ESP8266WiFi', None)

View File

@ -36,6 +36,9 @@ void WiFiComponent::setup() {
if (this->has_sta()) {
this->wifi_sta_pre_setup_();
if (this->output_power_.has_value() && !this->wifi_apply_output_power_(*this->output_power_)) {
ESP_LOGV(TAG, "Setting Power Save Option failed!");
}
if (!this->wifi_apply_power_save_()) {
ESP_LOGV(TAG, "Setting Power Save Option failed!");
@ -49,6 +52,9 @@ void WiFiComponent::setup() {
}
} else if (this->has_ap()) {
this->setup_ap_config_();
if (this->output_power_.has_value() && !this->wifi_apply_output_power_(*this->output_power_)) {
ESP_LOGV(TAG, "Setting Power Save Option failed!");
}
#ifdef USE_CAPTIVE_PORTAL
if (captive_portal::global_captive_portal != nullptr)
captive_portal::global_captive_portal->start();

View File

@ -162,6 +162,7 @@ class WiFiComponent : public Component {
bool is_connected();
void set_power_save_mode(WiFiPowerSaveMode power_save);
void set_output_power(float output_power) { output_power_ = output_power; }
// ========== INTERNAL METHODS ==========
// (In most use cases you won't need these)
@ -217,6 +218,7 @@ class WiFiComponent : public Component {
bool wifi_mode_(optional<bool> sta, optional<bool> ap);
bool wifi_sta_pre_setup_();
bool wifi_apply_output_power_(float output_power);
bool wifi_apply_power_save_();
bool wifi_sta_ip_config_(optional<ManualIP> manual_ip);
IPAddress wifi_sta_ip_();
@ -260,6 +262,7 @@ class WiFiComponent : public Component {
std::vector<WiFiScanResult> scan_result_;
bool scan_done_{false};
bool ap_setup_{false};
optional<float> output_power_;
};
extern WiFiComponent *global_wifi_component;

View File

@ -53,6 +53,10 @@ bool WiFiComponent::wifi_mode_(optional<bool> sta, optional<bool> ap) {
return ret;
}
bool WiFiComponent::wifi_apply_output_power_(float output_power) {
int8_t val = static_cast<uint8_t>(output_power * 4);
return esp_wifi_set_max_tx_power(val) == ESP_OK;
}
bool WiFiComponent::wifi_sta_pre_setup_() {
if (!this->wifi_mode_(true, {}))
return false;

View File

@ -390,6 +390,11 @@ void WiFiComponent::wifi_event_callback(System_Event_t *event) {
WiFiMockClass::_event_callback(event);
}
bool WiFiComponent::wifi_apply_output_power_(float output_power) {
uint8_t val = static_cast<uint8_t>(output_power * 4);
system_phy_set_max_tpw(val);
return true;
}
bool WiFiComponent::wifi_sta_pre_setup_() {
if (!this->wifi_mode_(true, {}))
return false;

View File

@ -616,6 +616,7 @@ angle = float_with_unit("angle", u"(°|deg)", optional_unit=True)
_temperature_c = float_with_unit("temperature", u"(°C|° C|°|C)?")
_temperature_k = float_with_unit("temperature", u"(° K|° K|K)?")
_temperature_f = float_with_unit("temperature", u"(°F|° F|F)?")
decibel = float_with_unit("decibel", u"(dB|dBm|db|dbm)", optional_unit=True)
if IS_PY2:
# Override voluptuous invalid to unicode for py2