mirror of
https://github.com/esphome/esphome.git
synced 2026-02-08 00:31:58 +00:00
[ethernet] Add on_connect and on_disconnect triggers (#13677)
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: J. Nick Koston <nick@home-assistant.io>
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from esphome import pins
|
from esphome import automation, pins
|
||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
from esphome.components.esp32 import (
|
from esphome.components.esp32 import (
|
||||||
VARIANT_ESP32,
|
VARIANT_ESP32,
|
||||||
@@ -35,6 +35,8 @@ from esphome.const import (
|
|||||||
CONF_MODE,
|
CONF_MODE,
|
||||||
CONF_MOSI_PIN,
|
CONF_MOSI_PIN,
|
||||||
CONF_NUMBER,
|
CONF_NUMBER,
|
||||||
|
CONF_ON_CONNECT,
|
||||||
|
CONF_ON_DISCONNECT,
|
||||||
CONF_PAGE_ID,
|
CONF_PAGE_ID,
|
||||||
CONF_PIN,
|
CONF_PIN,
|
||||||
CONF_POLLING_INTERVAL,
|
CONF_POLLING_INTERVAL,
|
||||||
@@ -237,6 +239,8 @@ BASE_SCHEMA = cv.Schema(
|
|||||||
cv.Optional(CONF_DOMAIN, default=".local"): cv.domain_name,
|
cv.Optional(CONF_DOMAIN, default=".local"): cv.domain_name,
|
||||||
cv.Optional(CONF_USE_ADDRESS): cv.string_strict,
|
cv.Optional(CONF_USE_ADDRESS): cv.string_strict,
|
||||||
cv.Optional(CONF_MAC_ADDRESS): cv.mac_address,
|
cv.Optional(CONF_MAC_ADDRESS): cv.mac_address,
|
||||||
|
cv.Optional(CONF_ON_CONNECT): automation.validate_automation(single=True),
|
||||||
|
cv.Optional(CONF_ON_DISCONNECT): automation.validate_automation(single=True),
|
||||||
}
|
}
|
||||||
).extend(cv.COMPONENT_SCHEMA)
|
).extend(cv.COMPONENT_SCHEMA)
|
||||||
|
|
||||||
@@ -430,6 +434,18 @@ async def to_code(config):
|
|||||||
if CORE.using_arduino:
|
if CORE.using_arduino:
|
||||||
cg.add_library("WiFi", None)
|
cg.add_library("WiFi", None)
|
||||||
|
|
||||||
|
if on_connect_config := config.get(CONF_ON_CONNECT):
|
||||||
|
cg.add_define("USE_ETHERNET_CONNECT_TRIGGER")
|
||||||
|
await automation.build_automation(
|
||||||
|
var.get_connect_trigger(), [], on_connect_config
|
||||||
|
)
|
||||||
|
|
||||||
|
if on_disconnect_config := config.get(CONF_ON_DISCONNECT):
|
||||||
|
cg.add_define("USE_ETHERNET_DISCONNECT_TRIGGER")
|
||||||
|
await automation.build_automation(
|
||||||
|
var.get_disconnect_trigger(), [], on_disconnect_config
|
||||||
|
)
|
||||||
|
|
||||||
CORE.add_job(final_step)
|
CORE.add_job(final_step)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -309,6 +309,9 @@ void EthernetComponent::loop() {
|
|||||||
|
|
||||||
this->dump_connect_params_();
|
this->dump_connect_params_();
|
||||||
this->status_clear_warning();
|
this->status_clear_warning();
|
||||||
|
#ifdef USE_ETHERNET_CONNECT_TRIGGER
|
||||||
|
this->connect_trigger_.trigger();
|
||||||
|
#endif
|
||||||
} else if (now - this->connect_begin_ > 15000) {
|
} else if (now - this->connect_begin_ > 15000) {
|
||||||
ESP_LOGW(TAG, "Connecting failed; reconnecting");
|
ESP_LOGW(TAG, "Connecting failed; reconnecting");
|
||||||
this->start_connect_();
|
this->start_connect_();
|
||||||
@@ -318,10 +321,16 @@ void EthernetComponent::loop() {
|
|||||||
if (!this->started_) {
|
if (!this->started_) {
|
||||||
ESP_LOGI(TAG, "Stopped connection");
|
ESP_LOGI(TAG, "Stopped connection");
|
||||||
this->state_ = EthernetComponentState::STOPPED;
|
this->state_ = EthernetComponentState::STOPPED;
|
||||||
|
#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
|
||||||
|
this->disconnect_trigger_.trigger();
|
||||||
|
#endif
|
||||||
} else if (!this->connected_) {
|
} else if (!this->connected_) {
|
||||||
ESP_LOGW(TAG, "Connection lost; reconnecting");
|
ESP_LOGW(TAG, "Connection lost; reconnecting");
|
||||||
this->state_ = EthernetComponentState::CONNECTING;
|
this->state_ = EthernetComponentState::CONNECTING;
|
||||||
this->start_connect_();
|
this->start_connect_();
|
||||||
|
#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
|
||||||
|
this->disconnect_trigger_.trigger();
|
||||||
|
#endif
|
||||||
} else {
|
} else {
|
||||||
this->finish_connect_();
|
this->finish_connect_();
|
||||||
// When connected and stable, disable the loop to save CPU cycles
|
// When connected and stable, disable the loop to save CPU cycles
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include "esphome/core/defines.h"
|
#include "esphome/core/defines.h"
|
||||||
#include "esphome/core/hal.h"
|
#include "esphome/core/hal.h"
|
||||||
#include "esphome/core/helpers.h"
|
#include "esphome/core/helpers.h"
|
||||||
|
#include "esphome/core/automation.h"
|
||||||
#include "esphome/components/network/ip_address.h"
|
#include "esphome/components/network/ip_address.h"
|
||||||
|
|
||||||
#ifdef USE_ESP32
|
#ifdef USE_ESP32
|
||||||
@@ -119,6 +120,12 @@ class EthernetComponent : public Component {
|
|||||||
void add_ip_state_listener(EthernetIPStateListener *listener) { this->ip_state_listeners_.push_back(listener); }
|
void add_ip_state_listener(EthernetIPStateListener *listener) { this->ip_state_listeners_.push_back(listener); }
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef USE_ETHERNET_CONNECT_TRIGGER
|
||||||
|
Trigger<> *get_connect_trigger() { return &this->connect_trigger_; }
|
||||||
|
#endif
|
||||||
|
#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
|
||||||
|
Trigger<> *get_disconnect_trigger() { return &this->disconnect_trigger_; }
|
||||||
|
#endif
|
||||||
protected:
|
protected:
|
||||||
static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
||||||
static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
||||||
@@ -190,6 +197,12 @@ class EthernetComponent : public Component {
|
|||||||
StaticVector<EthernetIPStateListener *, ESPHOME_ETHERNET_IP_STATE_LISTENERS> ip_state_listeners_;
|
StaticVector<EthernetIPStateListener *, ESPHOME_ETHERNET_IP_STATE_LISTENERS> ip_state_listeners_;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef USE_ETHERNET_CONNECT_TRIGGER
|
||||||
|
Trigger<> connect_trigger_;
|
||||||
|
#endif
|
||||||
|
#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
|
||||||
|
Trigger<> disconnect_trigger_;
|
||||||
|
#endif
|
||||||
private:
|
private:
|
||||||
// Stores a pointer to a string literal (static storage duration).
|
// Stores a pointer to a string literal (static storage duration).
|
||||||
// ONLY set from Python-generated code with string literals - never dynamic strings.
|
// ONLY set from Python-generated code with string literals - never dynamic strings.
|
||||||
|
|||||||
@@ -240,6 +240,8 @@
|
|||||||
#define USE_ETHERNET_KSZ8081
|
#define USE_ETHERNET_KSZ8081
|
||||||
#define USE_ETHERNET_MANUAL_IP
|
#define USE_ETHERNET_MANUAL_IP
|
||||||
#define USE_ETHERNET_IP_STATE_LISTENERS
|
#define USE_ETHERNET_IP_STATE_LISTENERS
|
||||||
|
#define USE_ETHERNET_CONNECT_TRIGGER
|
||||||
|
#define USE_ETHERNET_DISCONNECT_TRIGGER
|
||||||
#define ESPHOME_ETHERNET_IP_STATE_LISTENERS 2
|
#define ESPHOME_ETHERNET_IP_STATE_LISTENERS 2
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -13,3 +13,7 @@ ethernet:
|
|||||||
subnet: 255.255.255.0
|
subnet: 255.255.255.0
|
||||||
domain: .local
|
domain: .local
|
||||||
mac_address: "02:AA:BB:CC:DD:01"
|
mac_address: "02:AA:BB:CC:DD:01"
|
||||||
|
on_connect:
|
||||||
|
- logger.log: "Ethernet connected!"
|
||||||
|
on_disconnect:
|
||||||
|
- logger.log: "Ethernet disconnected!"
|
||||||
|
|||||||
@@ -13,3 +13,7 @@ ethernet:
|
|||||||
subnet: 255.255.255.0
|
subnet: 255.255.255.0
|
||||||
domain: .local
|
domain: .local
|
||||||
mac_address: "02:AA:BB:CC:DD:01"
|
mac_address: "02:AA:BB:CC:DD:01"
|
||||||
|
on_connect:
|
||||||
|
- logger.log: "Ethernet connected!"
|
||||||
|
on_disconnect:
|
||||||
|
- logger.log: "Ethernet disconnected!"
|
||||||
|
|||||||
@@ -13,3 +13,7 @@ ethernet:
|
|||||||
subnet: 255.255.255.0
|
subnet: 255.255.255.0
|
||||||
domain: .local
|
domain: .local
|
||||||
mac_address: "02:AA:BB:CC:DD:01"
|
mac_address: "02:AA:BB:CC:DD:01"
|
||||||
|
on_connect:
|
||||||
|
- logger.log: "Ethernet connected!"
|
||||||
|
on_disconnect:
|
||||||
|
- logger.log: "Ethernet disconnected!"
|
||||||
|
|||||||
@@ -13,3 +13,7 @@ ethernet:
|
|||||||
subnet: 255.255.255.0
|
subnet: 255.255.255.0
|
||||||
domain: .local
|
domain: .local
|
||||||
mac_address: "02:AA:BB:CC:DD:01"
|
mac_address: "02:AA:BB:CC:DD:01"
|
||||||
|
on_connect:
|
||||||
|
- logger.log: "Ethernet connected!"
|
||||||
|
on_disconnect:
|
||||||
|
- logger.log: "Ethernet disconnected!"
|
||||||
|
|||||||
@@ -13,3 +13,7 @@ ethernet:
|
|||||||
subnet: 255.255.255.0
|
subnet: 255.255.255.0
|
||||||
domain: .local
|
domain: .local
|
||||||
mac_address: "02:AA:BB:CC:DD:01"
|
mac_address: "02:AA:BB:CC:DD:01"
|
||||||
|
on_connect:
|
||||||
|
- logger.log: "Ethernet connected!"
|
||||||
|
on_disconnect:
|
||||||
|
- logger.log: "Ethernet disconnected!"
|
||||||
|
|||||||
@@ -13,3 +13,7 @@ ethernet:
|
|||||||
subnet: 255.255.255.0
|
subnet: 255.255.255.0
|
||||||
domain: .local
|
domain: .local
|
||||||
mac_address: "02:AA:BB:CC:DD:01"
|
mac_address: "02:AA:BB:CC:DD:01"
|
||||||
|
on_connect:
|
||||||
|
- logger.log: "Ethernet connected!"
|
||||||
|
on_disconnect:
|
||||||
|
- logger.log: "Ethernet disconnected!"
|
||||||
|
|||||||
@@ -12,3 +12,7 @@ ethernet:
|
|||||||
gateway: 192.168.178.1
|
gateway: 192.168.178.1
|
||||||
subnet: 255.255.255.0
|
subnet: 255.255.255.0
|
||||||
domain: .local
|
domain: .local
|
||||||
|
on_connect:
|
||||||
|
- logger.log: "Ethernet connected!"
|
||||||
|
on_disconnect:
|
||||||
|
- logger.log: "Ethernet disconnected!"
|
||||||
|
|||||||
@@ -13,3 +13,7 @@ ethernet:
|
|||||||
subnet: 255.255.255.0
|
subnet: 255.255.255.0
|
||||||
domain: .local
|
domain: .local
|
||||||
mac_address: "02:AA:BB:CC:DD:01"
|
mac_address: "02:AA:BB:CC:DD:01"
|
||||||
|
on_connect:
|
||||||
|
- logger.log: "Ethernet connected!"
|
||||||
|
on_disconnect:
|
||||||
|
- logger.log: "Ethernet disconnected!"
|
||||||
|
|||||||
@@ -1,2 +1,6 @@
|
|||||||
ethernet:
|
ethernet:
|
||||||
type: OPENETH
|
type: OPENETH
|
||||||
|
on_connect:
|
||||||
|
- logger.log: "Ethernet connected!"
|
||||||
|
on_disconnect:
|
||||||
|
- logger.log: "Ethernet disconnected!"
|
||||||
|
|||||||
@@ -13,3 +13,7 @@ ethernet:
|
|||||||
subnet: 255.255.255.0
|
subnet: 255.255.255.0
|
||||||
domain: .local
|
domain: .local
|
||||||
mac_address: "02:AA:BB:CC:DD:01"
|
mac_address: "02:AA:BB:CC:DD:01"
|
||||||
|
on_connect:
|
||||||
|
- logger.log: "Ethernet connected!"
|
||||||
|
on_disconnect:
|
||||||
|
- logger.log: "Ethernet disconnected!"
|
||||||
|
|||||||
@@ -13,3 +13,7 @@ ethernet:
|
|||||||
subnet: 255.255.255.0
|
subnet: 255.255.255.0
|
||||||
domain: .local
|
domain: .local
|
||||||
mac_address: "02:AA:BB:CC:DD:01"
|
mac_address: "02:AA:BB:CC:DD:01"
|
||||||
|
on_connect:
|
||||||
|
- logger.log: "Ethernet connected!"
|
||||||
|
on_disconnect:
|
||||||
|
- logger.log: "Ethernet disconnected!"
|
||||||
|
|||||||
Reference in New Issue
Block a user