[wifi] ap_active condition (#11852)

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
This commit is contained in:
Flo
2025-11-24 18:19:38 +01:00
committed by GitHub
parent 0764f4da86
commit 66cda04664
4 changed files with 17 additions and 0 deletions

View File

@@ -97,6 +97,7 @@ WIFI_MIN_AUTH_MODES = {
VALIDATE_WIFI_MIN_AUTH_MODE = cv.enum(WIFI_MIN_AUTH_MODES, upper=True)
WiFiConnectedCondition = wifi_ns.class_("WiFiConnectedCondition", Condition)
WiFiEnabledCondition = wifi_ns.class_("WiFiEnabledCondition", Condition)
WiFiAPActiveCondition = wifi_ns.class_("WiFiAPActiveCondition", Condition)
WiFiEnableAction = wifi_ns.class_("WiFiEnableAction", automation.Action)
WiFiDisableAction = wifi_ns.class_("WiFiDisableAction", automation.Action)
WiFiConfigureAction = wifi_ns.class_(
@@ -590,6 +591,11 @@ async def wifi_enabled_to_code(config, condition_id, template_arg, args):
return cg.new_Pvariable(condition_id, template_arg)
@automation.register_condition("wifi.ap_active", WiFiAPActiveCondition, cv.Schema({}))
async def wifi_ap_active_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)

View File

@@ -530,6 +530,7 @@ void WiFiComponent::loop() {
WiFiComponent::WiFiComponent() { global_wifi_component = this; }
bool WiFiComponent::has_ap() const { return this->has_ap_; }
bool WiFiComponent::is_ap_active() const { return this->state_ == WIFI_COMPONENT_STATE_AP; }
bool WiFiComponent::has_sta() const { return !this->sta_.empty(); }
#ifdef USE_WIFI_11KV_SUPPORT
void WiFiComponent::set_btm(bool btm) { this->btm_ = btm; }

View File

@@ -308,6 +308,7 @@ class WiFiComponent : public Component {
bool has_sta() const;
bool has_ap() const;
bool is_ap_active() const;
#ifdef USE_WIFI_11KV_SUPPORT
void set_btm(bool btm);
@@ -557,6 +558,11 @@ template<typename... Ts> class WiFiEnabledCondition : public Condition<Ts...> {
bool check(const Ts &...x) override { return !global_wifi_component->is_disabled(); }
};
template<typename... Ts> class WiFiAPActiveCondition : public Condition<Ts...> {
public:
bool check(const Ts &...x) override { return global_wifi_component->is_ap_active(); }
};
template<typename... Ts> class WiFiEnableAction : public Action<Ts...> {
public:
void play(const Ts &...x) override { global_wifi_component->enable(); }

View File

@@ -10,6 +10,10 @@ esphome:
- logger.log: "Connected to WiFi!"
on_error:
- logger.log: "Failed to connect to WiFi!"
- if:
condition: wifi.ap_active
then:
- logger.log: "WiFi AP is active!"
wifi:
networks: