mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	Allow WIFI to be disabled and enabled (#4810)
Co-authored-by: Péter Sárközi <xmisterhu@gmail.com> Co-authored-by: Ash McKenzie <ash@the-rebellion.net>
This commit is contained in:
		| @@ -53,6 +53,9 @@ WIFI_POWER_SAVE_MODES = { | |||||||
|     "HIGH": WiFiPowerSaveMode.WIFI_POWER_SAVE_HIGH, |     "HIGH": WiFiPowerSaveMode.WIFI_POWER_SAVE_HIGH, | ||||||
| } | } | ||||||
| WiFiConnectedCondition = wifi_ns.class_("WiFiConnectedCondition", Condition) | WiFiConnectedCondition = wifi_ns.class_("WiFiConnectedCondition", Condition) | ||||||
|  | WiFiEnabledCondition = wifi_ns.class_("WiFiEnabledCondition", Condition) | ||||||
|  | WiFiEnableAction = wifi_ns.class_("WiFiEnableAction", automation.Action) | ||||||
|  | WiFiDisableAction = wifi_ns.class_("WiFiDisableAction", automation.Action) | ||||||
|  |  | ||||||
|  |  | ||||||
| def validate_password(value): | def validate_password(value): | ||||||
| @@ -253,6 +256,7 @@ def _validate(config): | |||||||
|  |  | ||||||
| CONF_OUTPUT_POWER = "output_power" | CONF_OUTPUT_POWER = "output_power" | ||||||
| CONF_PASSIVE_SCAN = "passive_scan" | CONF_PASSIVE_SCAN = "passive_scan" | ||||||
|  | CONF_ENABLE_ON_BOOT = "enable_on_boot" | ||||||
| CONFIG_SCHEMA = cv.All( | CONFIG_SCHEMA = cv.All( | ||||||
|     cv.Schema( |     cv.Schema( | ||||||
|         { |         { | ||||||
| @@ -286,6 +290,7 @@ CONFIG_SCHEMA = cv.All( | |||||||
|                 "This option has been removed. Please use the [disabled] option under the " |                 "This option has been removed. Please use the [disabled] option under the " | ||||||
|                 "new mdns component instead." |                 "new mdns component instead." | ||||||
|             ), |             ), | ||||||
|  |             cv.Optional(CONF_ENABLE_ON_BOOT, default=True): cv.boolean, | ||||||
|         } |         } | ||||||
|     ), |     ), | ||||||
|     _validate, |     _validate, | ||||||
| @@ -385,6 +390,8 @@ async def to_code(config): | |||||||
|     if CONF_OUTPUT_POWER in config: |     if CONF_OUTPUT_POWER in config: | ||||||
|         cg.add(var.set_output_power(config[CONF_OUTPUT_POWER])) |         cg.add(var.set_output_power(config[CONF_OUTPUT_POWER])) | ||||||
|  |  | ||||||
|  |     cg.add(var.set_enable_on_boot(config[CONF_ENABLE_ON_BOOT])) | ||||||
|  |  | ||||||
|     if CORE.is_esp8266: |     if CORE.is_esp8266: | ||||||
|         cg.add_library("ESP8266WiFi", None) |         cg.add_library("ESP8266WiFi", None) | ||||||
|     elif CORE.is_esp32 and CORE.using_arduino: |     elif CORE.is_esp32 and CORE.using_arduino: | ||||||
| @@ -410,3 +417,18 @@ async def to_code(config): | |||||||
| @automation.register_condition("wifi.connected", WiFiConnectedCondition, cv.Schema({})) | @automation.register_condition("wifi.connected", WiFiConnectedCondition, cv.Schema({})) | ||||||
| async def wifi_connected_to_code(config, condition_id, template_arg, args): | async def wifi_connected_to_code(config, condition_id, template_arg, args): | ||||||
|     return cg.new_Pvariable(condition_id, template_arg) |     return cg.new_Pvariable(condition_id, template_arg) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @automation.register_condition("wifi.enabled", WiFiEnabledCondition, cv.Schema({})) | ||||||
|  | async def wifi_enabled_to_code(config, condition_id, template_arg, args): | ||||||
|  |     return cg.new_Pvariable(condition_id, template_arg) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @automation.register_action("wifi.enable", WiFiEnableAction, cv.Schema({})) | ||||||
|  | async def wifi_enable_to_code(config, action_id, template_arg, args): | ||||||
|  |     return cg.new_Pvariable(action_id, template_arg) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @automation.register_action("wifi.disable", WiFiDisableAction, cv.Schema({})) | ||||||
|  | async def wifi_disable_to_code(config, action_id, template_arg, args): | ||||||
|  |     return cg.new_Pvariable(action_id, template_arg) | ||||||
|   | |||||||
| @@ -36,9 +36,18 @@ float WiFiComponent::get_setup_priority() const { return setup_priority::WIFI; } | |||||||
|  |  | ||||||
| void WiFiComponent::setup() { | void WiFiComponent::setup() { | ||||||
|   ESP_LOGCONFIG(TAG, "Setting up WiFi..."); |   ESP_LOGCONFIG(TAG, "Setting up WiFi..."); | ||||||
|  |   this->wifi_pre_setup_(); | ||||||
|  |   if (this->enable_on_boot_) { | ||||||
|  |     this->start(); | ||||||
|  |   } else { | ||||||
|  |     this->state_ = WIFI_COMPONENT_STATE_DISABLED; | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void WiFiComponent::start() { | ||||||
|  |   ESP_LOGCONFIG(TAG, "Starting WiFi..."); | ||||||
|   ESP_LOGCONFIG(TAG, "  Local MAC: %s", get_mac_address_pretty().c_str()); |   ESP_LOGCONFIG(TAG, "  Local MAC: %s", get_mac_address_pretty().c_str()); | ||||||
|   this->last_connected_ = millis(); |   this->last_connected_ = millis(); | ||||||
|   this->wifi_pre_setup_(); |  | ||||||
|  |  | ||||||
|   uint32_t hash = this->has_sta() ? fnv1_hash(App.get_compilation_time()) : 88491487UL; |   uint32_t hash = this->has_sta() ? fnv1_hash(App.get_compilation_time()) : 88491487UL; | ||||||
|  |  | ||||||
| @@ -135,6 +144,8 @@ void WiFiComponent::loop() { | |||||||
|       case WIFI_COMPONENT_STATE_OFF: |       case WIFI_COMPONENT_STATE_OFF: | ||||||
|       case WIFI_COMPONENT_STATE_AP: |       case WIFI_COMPONENT_STATE_AP: | ||||||
|         break; |         break; | ||||||
|  |       case WIFI_COMPONENT_STATE_DISABLED: | ||||||
|  |         return; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     if (this->has_ap() && !this->ap_setup_) { |     if (this->has_ap() && !this->ap_setup_) { | ||||||
| @@ -387,6 +398,28 @@ void WiFiComponent::print_connect_params_() { | |||||||
| #endif | #endif | ||||||
| } | } | ||||||
|  |  | ||||||
|  | void WiFiComponent::enable() { | ||||||
|  |   if (this->state_ != WIFI_COMPONENT_STATE_DISABLED) | ||||||
|  |     return; | ||||||
|  |  | ||||||
|  |   ESP_LOGD(TAG, "Enabling WIFI..."); | ||||||
|  |   this->error_from_callback_ = false; | ||||||
|  |   this->state_ = WIFI_COMPONENT_STATE_OFF; | ||||||
|  |   this->start(); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void WiFiComponent::disable() { | ||||||
|  |   if (this->state_ == WIFI_COMPONENT_STATE_DISABLED) | ||||||
|  |     return; | ||||||
|  |  | ||||||
|  |   ESP_LOGD(TAG, "Disabling WIFI..."); | ||||||
|  |   this->state_ = WIFI_COMPONENT_STATE_DISABLED; | ||||||
|  |   this->wifi_disconnect_(); | ||||||
|  |   this->wifi_mode_(false, false); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | bool WiFiComponent::is_disabled() { return this->state_ == WIFI_COMPONENT_STATE_DISABLED; } | ||||||
|  |  | ||||||
| void WiFiComponent::start_scanning() { | void WiFiComponent::start_scanning() { | ||||||
|   this->action_started_ = millis(); |   this->action_started_ = millis(); | ||||||
|   ESP_LOGD(TAG, "Starting scan..."); |   ESP_LOGD(TAG, "Starting scan..."); | ||||||
| @@ -608,7 +641,7 @@ void WiFiComponent::retry_connect() { | |||||||
| } | } | ||||||
|  |  | ||||||
| bool WiFiComponent::can_proceed() { | bool WiFiComponent::can_proceed() { | ||||||
|   if (!this->has_sta()) { |   if (!this->has_sta() || this->state_ == WIFI_COMPONENT_STATE_DISABLED) { | ||||||
|     return true; |     return true; | ||||||
|   } |   } | ||||||
|   return this->is_connected(); |   return this->is_connected(); | ||||||
|   | |||||||
| @@ -47,6 +47,8 @@ struct SavedWifiSettings { | |||||||
| enum WiFiComponentState { | enum WiFiComponentState { | ||||||
|   /** Nothing has been initialized yet. Internal AP, if configured, is disabled at this point. */ |   /** Nothing has been initialized yet. Internal AP, if configured, is disabled at this point. */ | ||||||
|   WIFI_COMPONENT_STATE_OFF = 0, |   WIFI_COMPONENT_STATE_OFF = 0, | ||||||
|  |   /** WiFi is disabled. */ | ||||||
|  |   WIFI_COMPONENT_STATE_DISABLED, | ||||||
|   /** WiFi is in cooldown mode because something went wrong, scanning will begin after a short period of time. */ |   /** WiFi is in cooldown mode because something went wrong, scanning will begin after a short period of time. */ | ||||||
|   WIFI_COMPONENT_STATE_COOLDOWN, |   WIFI_COMPONENT_STATE_COOLDOWN, | ||||||
|   /** WiFi is in STA-only mode and currently scanning for APs. */ |   /** WiFi is in STA-only mode and currently scanning for APs. */ | ||||||
| @@ -198,6 +200,9 @@ class WiFiComponent : public Component { | |||||||
|   void set_ap(const WiFiAP &ap); |   void set_ap(const WiFiAP &ap); | ||||||
|   WiFiAP get_ap() { return this->ap_; } |   WiFiAP get_ap() { return this->ap_; } | ||||||
|  |  | ||||||
|  |   void enable(); | ||||||
|  |   void disable(); | ||||||
|  |   bool is_disabled(); | ||||||
|   void start_scanning(); |   void start_scanning(); | ||||||
|   void check_scanning_finished(); |   void check_scanning_finished(); | ||||||
|   void start_connecting(const WiFiAP &ap, bool two); |   void start_connecting(const WiFiAP &ap, bool two); | ||||||
| @@ -224,6 +229,7 @@ class WiFiComponent : public Component { | |||||||
|   // (In most use cases you won't need these) |   // (In most use cases you won't need these) | ||||||
|   /// Setup WiFi interface. |   /// Setup WiFi interface. | ||||||
|   void setup() override; |   void setup() override; | ||||||
|  |   void start(); | ||||||
|   void dump_config() override; |   void dump_config() override; | ||||||
|   /// WIFI setup_priority. |   /// WIFI setup_priority. | ||||||
|   float get_setup_priority() const override; |   float get_setup_priority() const override; | ||||||
| @@ -282,6 +288,8 @@ class WiFiComponent : public Component { | |||||||
|  |  | ||||||
|   int8_t wifi_rssi(); |   int8_t wifi_rssi(); | ||||||
|  |  | ||||||
|  |   void set_enable_on_boot(bool enable_on_boot) { this->enable_on_boot_ = enable_on_boot; } | ||||||
|  |  | ||||||
|  protected: |  protected: | ||||||
|   static std::string format_mac_addr(const uint8_t mac[6]); |   static std::string format_mac_addr(const uint8_t mac[6]); | ||||||
|   void setup_ap_config_(); |   void setup_ap_config_(); | ||||||
| @@ -359,18 +367,30 @@ class WiFiComponent : public Component { | |||||||
|   bool btm_{false}; |   bool btm_{false}; | ||||||
|   bool rrm_{false}; |   bool rrm_{false}; | ||||||
| #endif | #endif | ||||||
|  |   bool enable_on_boot_; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| extern WiFiComponent *global_wifi_component;  // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) | extern WiFiComponent *global_wifi_component;  // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) | ||||||
|  |  | ||||||
| template<typename... Ts> class WiFiConnectedCondition : public Condition<Ts...> { | template<typename... Ts> class WiFiConnectedCondition : public Condition<Ts...> { | ||||||
|  public: |  public: | ||||||
|   bool check(Ts... x) override; |   bool check(Ts... x) override { return global_wifi_component->is_connected(); } | ||||||
| }; | }; | ||||||
|  |  | ||||||
| template<typename... Ts> bool WiFiConnectedCondition<Ts...>::check(Ts... x) { | template<typename... Ts> class WiFiEnabledCondition : public Condition<Ts...> { | ||||||
|   return global_wifi_component->is_connected(); |  public: | ||||||
| } |   bool check(Ts... x) override { return !global_wifi_component->is_disabled(); } | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | template<typename... Ts> class WiFiEnableAction : public Action<Ts...> { | ||||||
|  |  public: | ||||||
|  |   void play(Ts... x) override { global_wifi_component->enable(); } | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | template<typename... Ts> class WiFiDisableAction : public Action<Ts...> { | ||||||
|  |  public: | ||||||
|  |   void play(Ts... x) override { global_wifi_component->disable(); } | ||||||
|  | }; | ||||||
|  |  | ||||||
| }  // namespace wifi | }  // namespace wifi | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user