ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
util.cpp
Go to the documentation of this file.
1#include "util.h"
3#ifdef USE_NETWORK
4#ifdef USE_WIFI
6#endif
7
8#ifdef USE_ETHERNET
10#endif
11
12#ifdef USE_OPENTHREAD
14#endif
15
16#ifdef USE_MODEM
17#include "esphome/components/modem/modem_component.h"
18#endif
19
20namespace esphome::network {
21
22// The order of the components is important: WiFi should come after any possible main interfaces (it may be used as
23// an AP that use a previous interface for NAT).
24
26#ifdef USE_ETHERNET
28 return true;
29#endif
30
31#ifdef USE_MODEM
32 if (modem::global_modem_component != nullptr)
33 return modem::global_modem_component->is_connected();
34#endif
35
36#ifdef USE_WIFI
37 if (wifi::global_wifi_component != nullptr)
39#endif
40
41#ifdef USE_OPENTHREAD
44#endif
45
46#ifdef USE_HOST
47 return true; // Assume its connected
48#endif
49 return false;
50}
51
53#ifdef USE_MODEM
54 if (modem::global_modem_component != nullptr)
55 return modem::global_modem_component->is_disabled();
56#endif
57
58#ifdef USE_WIFI
59 if (wifi::global_wifi_component != nullptr)
61#endif
62 return false;
63}
64
66#ifdef USE_ETHERNET
67 if (ethernet::global_eth_component != nullptr)
69#endif
70
71#ifdef USE_MODEM
72 if (modem::global_modem_component != nullptr)
73 return modem::global_modem_component->get_ip_addresses();
74#endif
75
76#ifdef USE_WIFI
77 if (wifi::global_wifi_component != nullptr)
79#endif
80#ifdef USE_OPENTHREAD
83#endif
84 return {};
85}
86
87const char *get_use_address() {
88 // Global component pointers are guaranteed to be set by component constructors when USE_* is defined
89#ifdef USE_ETHERNET
91#endif
92
93#ifdef USE_MODEM
94 return modem::global_modem_component->get_use_address();
95#endif
96
97#ifdef USE_WIFI
99#endif
100
101#ifdef USE_OPENTHREAD
103#endif
104
105#if !defined(USE_ETHERNET) && !defined(USE_MODEM) && !defined(USE_WIFI) && !defined(USE_OPENTHREAD)
106 // Fallback when no network component is defined (e.g., host platform)
107 return "";
108#endif
109}
110
111} // namespace esphome::network
112#endif
network::IPAddresses get_ip_addresses()
const char * get_use_address() const
EthernetComponent * global_eth_component
std::array< IPAddress, 5 > IPAddresses
Definition ip_address.h:187
const char * get_use_address()
Get the active network hostname.
Definition util.cpp:87
bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
Definition util.cpp:25
network::IPAddresses get_ip_addresses()
Definition util.cpp:65
bool is_disabled()
Return whether the network is disabled (only wifi for now)
Definition util.cpp:52
OpenThreadComponent * global_openthread_component
WiFiComponent * global_wifi_component