mirror of
https://github.com/esphome/esphome.git
synced 2024-12-22 16:37:52 +01:00
Add Ethernet MAC address to ethernet_info (#6835)
This commit is contained in:
parent
05491e756b
commit
78b48209aa
@ -28,6 +28,13 @@ EthernetComponent *global_eth_component; // NOLINT(cppcoreguidelines-avoid-non-
|
|||||||
return; \
|
return; \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define ESPHL_ERROR_CHECK_RET(err, message, ret) \
|
||||||
|
if ((err) != ESP_OK) { \
|
||||||
|
ESP_LOGE(TAG, message ": (%d) %s", err, esp_err_to_name(err)); \
|
||||||
|
this->mark_failed(); \
|
||||||
|
return ret; \
|
||||||
|
}
|
||||||
|
|
||||||
EthernetComponent::EthernetComponent() { global_eth_component = this; }
|
EthernetComponent::EthernetComponent() { global_eth_component = this; }
|
||||||
|
|
||||||
void EthernetComponent::setup() {
|
void EthernetComponent::setup() {
|
||||||
@ -498,22 +505,9 @@ void EthernetComponent::dump_connect_params_() {
|
|||||||
}
|
}
|
||||||
#endif /* USE_NETWORK_IPV6 */
|
#endif /* USE_NETWORK_IPV6 */
|
||||||
|
|
||||||
esp_err_t err;
|
ESP_LOGCONFIG(TAG, " MAC Address: %s", this->get_eth_mac_address_pretty().c_str());
|
||||||
|
ESP_LOGCONFIG(TAG, " Is Full Duplex: %s", YESNO(this->get_duplex_mode() == ETH_DUPLEX_FULL));
|
||||||
uint8_t mac[6];
|
ESP_LOGCONFIG(TAG, " Link Speed: %u", this->get_link_speed() == ETH_SPEED_100M ? 100 : 10);
|
||||||
err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_G_MAC_ADDR, &mac);
|
|
||||||
ESPHL_ERROR_CHECK(err, "ETH_CMD_G_MAC error");
|
|
||||||
ESP_LOGCONFIG(TAG, " MAC Address: %02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
|
||||||
|
|
||||||
eth_duplex_t duplex_mode;
|
|
||||||
err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_G_DUPLEX_MODE, &duplex_mode);
|
|
||||||
ESPHL_ERROR_CHECK(err, "ETH_CMD_G_DUPLEX_MODE error");
|
|
||||||
ESP_LOGCONFIG(TAG, " Is Full Duplex: %s", YESNO(duplex_mode == ETH_DUPLEX_FULL));
|
|
||||||
|
|
||||||
eth_speed_t speed;
|
|
||||||
err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_G_SPEED, &speed);
|
|
||||||
ESPHL_ERROR_CHECK(err, "ETH_CMD_G_SPEED error");
|
|
||||||
ESP_LOGCONFIG(TAG, " Link Speed: %u", speed == ETH_SPEED_100M ? 100 : 10);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef USE_ETHERNET_SPI
|
#ifdef USE_ETHERNET_SPI
|
||||||
@ -546,6 +540,34 @@ std::string EthernetComponent::get_use_address() const {
|
|||||||
|
|
||||||
void EthernetComponent::set_use_address(const std::string &use_address) { this->use_address_ = use_address; }
|
void EthernetComponent::set_use_address(const std::string &use_address) { this->use_address_ = use_address; }
|
||||||
|
|
||||||
|
void EthernetComponent::get_eth_mac_address_raw(uint8_t *mac) {
|
||||||
|
esp_err_t err;
|
||||||
|
err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_G_MAC_ADDR, mac);
|
||||||
|
ESPHL_ERROR_CHECK(err, "ETH_CMD_G_MAC error");
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string EthernetComponent::get_eth_mac_address_pretty() {
|
||||||
|
uint8_t mac[6];
|
||||||
|
get_mac_address_raw(mac);
|
||||||
|
return str_snprintf("%02X:%02X:%02X:%02X:%02X:%02X", 17, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||||
|
}
|
||||||
|
|
||||||
|
eth_duplex_t EthernetComponent::get_duplex_mode() {
|
||||||
|
esp_err_t err;
|
||||||
|
eth_duplex_t duplex_mode;
|
||||||
|
err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_G_DUPLEX_MODE, &duplex_mode);
|
||||||
|
ESPHL_ERROR_CHECK_RET(err, "ETH_CMD_G_DUPLEX_MODE error", ETH_DUPLEX_HALF);
|
||||||
|
return duplex_mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
eth_speed_t EthernetComponent::get_link_speed() {
|
||||||
|
esp_err_t err;
|
||||||
|
eth_speed_t speed;
|
||||||
|
err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_G_SPEED, &speed);
|
||||||
|
ESPHL_ERROR_CHECK_RET(err, "ETH_CMD_G_SPEED error", ETH_SPEED_10M);
|
||||||
|
return speed;
|
||||||
|
}
|
||||||
|
|
||||||
bool EthernetComponent::powerdown() {
|
bool EthernetComponent::powerdown() {
|
||||||
ESP_LOGI(TAG, "Powering down ethernet PHY");
|
ESP_LOGI(TAG, "Powering down ethernet PHY");
|
||||||
if (this->phy_ == nullptr) {
|
if (this->phy_ == nullptr) {
|
||||||
|
@ -74,6 +74,10 @@ class EthernetComponent : public Component {
|
|||||||
network::IPAddress get_dns_address(uint8_t num);
|
network::IPAddress get_dns_address(uint8_t num);
|
||||||
std::string get_use_address() const;
|
std::string get_use_address() const;
|
||||||
void set_use_address(const std::string &use_address);
|
void set_use_address(const std::string &use_address);
|
||||||
|
void get_eth_mac_address_raw(uint8_t *mac);
|
||||||
|
std::string get_eth_mac_address_pretty();
|
||||||
|
eth_duplex_t get_duplex_mode();
|
||||||
|
eth_speed_t get_link_speed();
|
||||||
bool powerdown();
|
bool powerdown();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -10,6 +10,7 @@ static const char *const TAG = "ethernet_info";
|
|||||||
|
|
||||||
void IPAddressEthernetInfo::dump_config() { LOG_TEXT_SENSOR("", "EthernetInfo IPAddress", this); }
|
void IPAddressEthernetInfo::dump_config() { LOG_TEXT_SENSOR("", "EthernetInfo IPAddress", this); }
|
||||||
void DNSAddressEthernetInfo::dump_config() { LOG_TEXT_SENSOR("", "EthernetInfo DNS Address", this); }
|
void DNSAddressEthernetInfo::dump_config() { LOG_TEXT_SENSOR("", "EthernetInfo DNS Address", this); }
|
||||||
|
void MACAddressEthernetInfo::dump_config() { LOG_TEXT_SENSOR("", "EthernetInfo MAC Address", this); }
|
||||||
|
|
||||||
} // namespace ethernet_info
|
} // namespace ethernet_info
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
@ -59,6 +59,13 @@ class DNSAddressEthernetInfo : public PollingComponent, public text_sensor::Text
|
|||||||
std::string last_results_;
|
std::string last_results_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class MACAddressEthernetInfo : public Component, public text_sensor::TextSensor {
|
||||||
|
public:
|
||||||
|
void setup() override { this->publish_state(ethernet::global_eth_component->get_eth_mac_address_pretty()); }
|
||||||
|
std::string unique_id() override { return get_mac_address() + "-ethernetinfo-mac"; }
|
||||||
|
void dump_config() override;
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace ethernet_info
|
} // namespace ethernet_info
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ from esphome.components import text_sensor
|
|||||||
from esphome.const import (
|
from esphome.const import (
|
||||||
CONF_IP_ADDRESS,
|
CONF_IP_ADDRESS,
|
||||||
CONF_DNS_ADDRESS,
|
CONF_DNS_ADDRESS,
|
||||||
|
CONF_MAC_ADDRESS,
|
||||||
ENTITY_CATEGORY_DIAGNOSTIC,
|
ENTITY_CATEGORY_DIAGNOSTIC,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -19,6 +20,10 @@ DNSAddressEthernetInfo = ethernet_info_ns.class_(
|
|||||||
"DNSAddressEthernetInfo", text_sensor.TextSensor, cg.PollingComponent
|
"DNSAddressEthernetInfo", text_sensor.TextSensor, cg.PollingComponent
|
||||||
)
|
)
|
||||||
|
|
||||||
|
MACAddressEthernetInfo = ethernet_info_ns.class_(
|
||||||
|
"MACAddressEthernetInfo", text_sensor.TextSensor, cg.PollingComponent
|
||||||
|
)
|
||||||
|
|
||||||
CONFIG_SCHEMA = cv.Schema(
|
CONFIG_SCHEMA = cv.Schema(
|
||||||
{
|
{
|
||||||
cv.Optional(CONF_IP_ADDRESS): text_sensor.text_sensor_schema(
|
cv.Optional(CONF_IP_ADDRESS): text_sensor.text_sensor_schema(
|
||||||
@ -36,6 +41,9 @@ CONFIG_SCHEMA = cv.Schema(
|
|||||||
cv.Optional(CONF_DNS_ADDRESS): text_sensor.text_sensor_schema(
|
cv.Optional(CONF_DNS_ADDRESS): text_sensor.text_sensor_schema(
|
||||||
DNSAddressEthernetInfo, entity_category=ENTITY_CATEGORY_DIAGNOSTIC
|
DNSAddressEthernetInfo, entity_category=ENTITY_CATEGORY_DIAGNOSTIC
|
||||||
).extend(cv.polling_component_schema("1s")),
|
).extend(cv.polling_component_schema("1s")),
|
||||||
|
cv.Optional(CONF_MAC_ADDRESS): text_sensor.text_sensor_schema(
|
||||||
|
MACAddressEthernetInfo, entity_category=ENTITY_CATEGORY_DIAGNOSTIC
|
||||||
|
),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -51,3 +59,6 @@ async def to_code(config):
|
|||||||
if conf := config.get(CONF_DNS_ADDRESS):
|
if conf := config.get(CONF_DNS_ADDRESS):
|
||||||
dns_info = await text_sensor.new_text_sensor(config[CONF_DNS_ADDRESS])
|
dns_info = await text_sensor.new_text_sensor(config[CONF_DNS_ADDRESS])
|
||||||
await cg.register_component(dns_info, config[CONF_DNS_ADDRESS])
|
await cg.register_component(dns_info, config[CONF_DNS_ADDRESS])
|
||||||
|
if conf := config.get(CONF_MAC_ADDRESS):
|
||||||
|
mac_info = await text_sensor.new_text_sensor(config[CONF_MAC_ADDRESS])
|
||||||
|
await cg.register_component(mac_info, config[CONF_MAC_ADDRESS])
|
||||||
|
@ -17,3 +17,5 @@ text_sensor:
|
|||||||
name: IP Address
|
name: IP Address
|
||||||
dns_address:
|
dns_address:
|
||||||
name: DNS Address
|
name: DNS Address
|
||||||
|
mac_address:
|
||||||
|
name: MAC Address
|
||||||
|
Loading…
Reference in New Issue
Block a user