mirror of
https://github.com/esphome/esphome.git
synced 2024-11-22 15:08:10 +01:00
Add wifi output_power setting (#853)
* Add wifi output_power setting
See also:
- https://github.com/esphome/feature-requests/issues/471#issuecomment-552350467
- https://github.com/esp8266/Arduino/issues/6366
- https://github.com/esp8266/Arduino/issues/6471
- 849f8cf920/code/espurna/config/general.h (L593-L599)
- https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/network/esp_wifi.html#_CPPv425esp_wifi_set_max_tx_power6int8_t
* Lint
This commit is contained in:
parent
9580b13b9f
commit
fad05d5a2e
6 changed files with 23 additions and 0 deletions
|
@ -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)
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue