diff --git a/CODEOWNERS b/CODEOWNERS index 4ac394345c..d3151c3f02 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -76,6 +76,7 @@ esphome/components/esp32_camera_web_server/* @ayufan esphome/components/esp32_can/* @Sympatron esphome/components/esp32_improv/* @jesserockz esphome/components/esp8266/* @esphome/core +esphome/components/ethernet_info/* @gtjadsonsantos esphome/components/exposure_notifications/* @OttoWinter esphome/components/ezo/* @ssieb esphome/components/factory_reset/* @anatoly-savchenkov diff --git a/esphome/components/ethernet_info/__init__.py b/esphome/components/ethernet_info/__init__.py new file mode 100644 index 0000000000..312688d3fd --- /dev/null +++ b/esphome/components/ethernet_info/__init__.py @@ -0,0 +1 @@ +CODEOWNERS = ["@gtjadsonsantos"] diff --git a/esphome/components/ethernet_info/ethernet_info_text_sensor.cpp b/esphome/components/ethernet_info/ethernet_info_text_sensor.cpp new file mode 100644 index 0000000000..e69872c290 --- /dev/null +++ b/esphome/components/ethernet_info/ethernet_info_text_sensor.cpp @@ -0,0 +1,16 @@ +#include "ethernet_info_text_sensor.h" +#include "esphome/core/log.h" + +#ifdef USE_ESP32_FRAMEWORK_ARDUINO + +namespace esphome { +namespace ethernet_info { + +static const char *const TAG = "ethernet_info"; + +void IPAddressEthernetInfo::dump_config() { LOG_TEXT_SENSOR("", "EthernetInfo IPAddress", this); } + +} // namespace ethernet_info +} // namespace esphome + +#endif // USE_ESP32_FRAMEWORK_ARDUINO diff --git a/esphome/components/ethernet_info/ethernet_info_text_sensor.h b/esphome/components/ethernet_info/ethernet_info_text_sensor.h new file mode 100644 index 0000000000..c683b68080 --- /dev/null +++ b/esphome/components/ethernet_info/ethernet_info_text_sensor.h @@ -0,0 +1,35 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/components/text_sensor/text_sensor.h" +#include "esphome/components/ethernet/ethernet_component.h" + +#ifdef USE_ESP32_FRAMEWORK_ARDUINO + +namespace esphome { +namespace ethernet_info { + +class IPAddressEthernetInfo : public PollingComponent, public text_sensor::TextSensor { + public: + void update() override { + tcpip_adapter_ip_info_t tcpip; + tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &tcpip); + auto ip = tcpip.ip.addr; + if (ip != this->last_ip_) { + this->last_ip_ = ip; + this->publish_state(network::IPAddress(ip).str()); + } + } + + float get_setup_priority() const override { return setup_priority::ETHERNET; } + std::string unique_id() override { return get_mac_address() + "-ethernetinfo"; } + void dump_config() override; + + protected: + network::IPAddress last_ip_; +}; + +} // namespace ethernet_info +} // namespace esphome + +#endif // USE_ESP32_FRAMEWORK_ARDUINO diff --git a/esphome/components/ethernet_info/text_sensor.py b/esphome/components/ethernet_info/text_sensor.py new file mode 100644 index 0000000000..7cb9944c92 --- /dev/null +++ b/esphome/components/ethernet_info/text_sensor.py @@ -0,0 +1,34 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.components import text_sensor +from esphome.const import ( + CONF_IP_ADDRESS, + ENTITY_CATEGORY_DIAGNOSTIC, +) + +DEPENDENCIES = ["ethernet"] + +ethernet_info_ns = cg.esphome_ns.namespace("ethernet_info") + +IPAddressEsthernetInfo = ethernet_info_ns.class_( + "IPAddressEthernetInfo", text_sensor.TextSensor, cg.PollingComponent +) + +CONFIG_SCHEMA = cv.Schema( + { + cv.Optional(CONF_IP_ADDRESS): text_sensor.text_sensor_schema( + IPAddressEsthernetInfo, entity_category=ENTITY_CATEGORY_DIAGNOSTIC + ).extend(cv.polling_component_schema("1s")) + } +) + + +async def setup_conf(config, key): + if key in config: + conf = config[key] + var = await text_sensor.new_text_sensor(conf) + await cg.register_component(var, conf) + + +async def to_code(config): + await setup_conf(config, CONF_IP_ADDRESS) diff --git a/esphome/core/component.cpp b/esphome/core/component.cpp index 5cb063cbec..7505e1f39b 100644 --- a/esphome/core/component.cpp +++ b/esphome/core/component.cpp @@ -20,6 +20,7 @@ const float PROCESSOR = 400.0; const float BLUETOOTH = 350.0f; const float AFTER_BLUETOOTH = 300.0f; const float WIFI = 250.0f; +const float ETHERNET = 250.0f; const float BEFORE_CONNECTION = 220.0f; const float AFTER_WIFI = 200.0f; const float AFTER_CONNECTION = 100.0f; diff --git a/esphome/core/component.h b/esphome/core/component.h index cb97a93d21..1d8499e262 100644 --- a/esphome/core/component.h +++ b/esphome/core/component.h @@ -29,6 +29,7 @@ extern const float PROCESSOR; extern const float BLUETOOTH; extern const float AFTER_BLUETOOTH; extern const float WIFI; +extern const float ETHERNET; /// For components that should be initialized after WiFi and before API is connected. extern const float BEFORE_CONNECTION; /// For components that should be initialized after WiFi is connected. diff --git a/tests/test4.yaml b/tests/test4.yaml index cf517bb391..a8077c625f 100644 --- a/tests/test4.yaml +++ b/tests/test4.yaml @@ -532,6 +532,9 @@ text_sensor: - platform: copy source_id: inverter0_device_mode name: Inverter Text Sensor Copy + - platform: ethernet_info + ip_address: + name: IP Address output: - platform: pipsolar