Skip to content

File tree

6 files changed

+23
-0
lines changed

6 files changed

+23
-0
lines changed

esphome/components/wifi/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ def validate(config):
110110
return config
111111

112112

113+
CONF_OUTPUT_POWER = 'output_power'
113114
CONFIG_SCHEMA = cv.All(cv.Schema({
114115
cv.GenerateID(): cv.declare_id(WiFiComponent),
115116
cv.Optional(CONF_NETWORKS): cv.ensure_list(WIFI_NETWORK_STA),
@@ -125,6 +126,7 @@ def validate(config):
125126
cv.enum(WIFI_POWER_SAVE_MODES, upper=True),
126127
cv.Optional(CONF_FAST_CONNECT, default=False): cv.boolean,
127128
cv.Optional(CONF_USE_ADDRESS): cv.string_strict,
129+
cv.Optional(CONF_OUTPUT_POWER): cv.All(cv.decibel, cv.float_range(min=10.0, max=20.5)),
128130

129131
cv.Optional('hostname'): cv.invalid("The hostname option has been removed in 1.11.0"),
130132
}), validate)
@@ -186,6 +188,8 @@ def to_code(config):
186188
cg.add(var.set_reboot_timeout(config[CONF_REBOOT_TIMEOUT]))
187189
cg.add(var.set_power_save_mode(config[CONF_POWER_SAVE_MODE]))
188190
cg.add(var.set_fast_connect(config[CONF_FAST_CONNECT]))
191+
if CONF_OUTPUT_POWER in config:
192+
cg.add(var.set_output_power(config[CONF_OUTPUT_POWER]))
189193

190194
if CORE.is_esp8266:
191195
cg.add_library('ESP8266WiFi', None)

esphome/components/wifi/wifi_component.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ void WiFiComponent::setup() {
3636

3737
if (this->has_sta()) {
3838
this->wifi_sta_pre_setup_();
39+
if (this->output_power_.has_value() && !this->wifi_apply_output_power_(*this->output_power_)) {
40+
ESP_LOGV(TAG, "Setting Power Save Option failed!");
41+
}
3942

4043
if (!this->wifi_apply_power_save_()) {
4144
ESP_LOGV(TAG, "Setting Power Save Option failed!");
@@ -49,6 +52,9 @@ void WiFiComponent::setup() {
4952
}
5053
} else if (this->has_ap()) {
5154
this->setup_ap_config_();
55+
if (this->output_power_.has_value() && !this->wifi_apply_output_power_(*this->output_power_)) {
56+
ESP_LOGV(TAG, "Setting Power Save Option failed!");
57+
}
5258
#ifdef USE_CAPTIVE_PORTAL
5359
if (captive_portal::global_captive_portal != nullptr)
5460
captive_portal::global_captive_portal->start();

esphome/components/wifi/wifi_component.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ class WiFiComponent : public Component {
162162
bool is_connected();
163163

164164
void set_power_save_mode(WiFiPowerSaveMode power_save);
165+
void set_output_power(float output_power) { output_power_ = output_power; }
165166

166167
// ========== INTERNAL METHODS ==========
167168
// (In most use cases you won't need these)
@@ -217,6 +218,7 @@ class WiFiComponent : public Component {
217218

218219
bool wifi_mode_(optional<bool> sta, optional<bool> ap);
219220
bool wifi_sta_pre_setup_();
221+
bool wifi_apply_output_power_(float output_power);
220222
bool wifi_apply_power_save_();
221223
bool wifi_sta_ip_config_(optional<ManualIP> manual_ip);
222224
IPAddress wifi_sta_ip_();
@@ -260,6 +262,7 @@ class WiFiComponent : public Component {
260262
std::vector<WiFiScanResult> scan_result_;
261263
bool scan_done_{false};
262264
bool ap_setup_{false};
265+
optional<float> output_power_;
263266
};
264267

265268
extern WiFiComponent *global_wifi_component;

esphome/components/wifi/wifi_component_esp32.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ bool WiFiComponent::wifi_mode_(optional<bool> sta, optional<bool> ap) {
5353

5454
return ret;
5555
}
56+
bool WiFiComponent::wifi_apply_output_power_(float output_power) {
57+
int8_t val = static_cast<uint8_t>(output_power * 4);
58+
return esp_wifi_set_max_tx_power(val) == ESP_OK;
59+
}
5660
bool WiFiComponent::wifi_sta_pre_setup_() {
5761
if (!this->wifi_mode_(true, {}))
5862
return false;

esphome/components/wifi/wifi_component_esp8266.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,11 @@ void WiFiComponent::wifi_event_callback(System_Event_t *event) {
390390
WiFiMockClass::_event_callback(event);
391391
}
392392

393+
bool WiFiComponent::wifi_apply_output_power_(float output_power) {
394+
uint8_t val = static_cast<uint8_t>(output_power * 4);
395+
system_phy_set_max_tpw(val);
396+
return true;
397+
}
393398
bool WiFiComponent::wifi_sta_pre_setup_() {
394399
if (!this->wifi_mode_(true, {}))
395400
return false;

esphome/config_validation.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ def validator(value):
616616
_temperature_c = float_with_unit("temperature", u"(°C|° C|°|C)?")
617617
_temperature_k = float_with_unit("temperature", u"(° K|° K|K)?")
618618
_temperature_f = float_with_unit("temperature", u"(°F|° F|F)?")
619+
decibel = float_with_unit("decibel", u"(dB|dBm|db|dbm)", optional_unit=True)
619620

620621
if IS_PY2:
621622
# Override voluptuous invalid to unicode for py2

0 commit comments

Comments
 (0)