Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Commit

Permalink
Add WiFi/MQTT/API connected condition (#536)
Browse files Browse the repository at this point in the history
* Add WiFi/MQTT/API connected condition

* Lint
  • Loading branch information
OttoWinter committed Mar 3, 2019
1 parent d012b82 commit 554c8ee
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/esphome/api/api_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void APIServer::loop() {

if (this->reboot_timeout_ != 0) {
const uint32_t now = millis();
if (this->clients_.empty()) {
if (!this->is_connected()) {
if (now - this->last_connected_ > this->reboot_timeout_) {
ESP_LOGE(TAG, "No client connected to API. Rebooting...");
reboot("api");
Expand Down Expand Up @@ -211,6 +211,7 @@ void APIServer::request_time() {
}
}
#endif
bool APIServer::is_connected() const { return !this->clients_.empty(); }

// APIConnection
APIConnection::APIConnection(AsyncClient *client, APIServer *parent)
Expand Down
9 changes: 9 additions & 0 deletions src/esphome/api/api_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ class APIServer : public Component, public StoringUpdateListenerController {
void request_time();
#endif

bool is_connected() const;

struct HomeAssistantStateSubscription {
std::string entity_id;
std::function<void(std::string)> callback;
Expand Down Expand Up @@ -218,6 +220,11 @@ template<typename... Ts> class HomeAssistantServiceCallAction : public Action<Ts
ServiceCallResponse resp_;
};

template<typename... Ts> class APIConnectedCondition : public Condition<Ts...> {
public:
bool check(Ts... x) override;
};

template<typename... Ts> HomeAssistantServiceCallAction<Ts...> *APIServer::make_home_assistant_service_call_action() {
return new HomeAssistantServiceCallAction<Ts...>(this);
}
Expand Down Expand Up @@ -248,6 +255,8 @@ UserService<Ts...> *APIServer::make_user_service_trigger(const std::string &name
return service;
}

template<typename... Ts> bool APIConnectedCondition<Ts...>::check(Ts... x) { return global_api_server->is_connected(); }

} // namespace api

ESPHOME_NAMESPACE_END
Expand Down
9 changes: 9 additions & 0 deletions src/esphome/mqtt/mqtt_client_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,11 @@ template<typename... Ts> class MQTTPublishJsonAction : public Action<Ts...> {
bool retain_{false};
};

template<typename... Ts> class MQTTConnectedCondition : public Condition<Ts...> {
public:
bool check(Ts... x) override;
};

// =============== TEMPLATE DEFINITIONS ===============

template<typename... Ts>
Expand Down Expand Up @@ -378,6 +383,10 @@ template<typename... Ts> MQTTPublishAction<Ts...> *MQTTClientComponent::make_pub
return new MQTTPublishAction<Ts...>();
}

template<typename... Ts> bool MQTTConnectedCondition<Ts...>::check(Ts... x) {
return global_mqtt_client->is_connected();
}

} // namespace mqtt

ESPHOME_NAMESPACE_END
Expand Down
10 changes: 10 additions & 0 deletions src/esphome/wifi_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <ESP8266WiFi.h>
#endif

#include "esphome/automation.h"
#include "esphome/component.h"
#include "esphome/helpers.h"
#include "esphome/defines.h"
Expand Down Expand Up @@ -224,8 +225,17 @@ class WiFiComponent : public Component {
bool ap_setup_{false};
};

template<typename... Ts> class WiFiConnectedCondition : public Condition<Ts...> {
public:
bool check(Ts... x) override;
};

extern WiFiComponent *global_wifi_component;

template<typename... Ts> bool WiFiConnectedCondition<Ts...>::check(Ts... x) {
return global_wifi_component->is_connected();
}

ESPHOME_NAMESPACE_END

#endif // ESPHOME_WIFI_COMPONENT_H

0 comments on commit 554c8ee

Please sign in to comment.