From 6123cb7c69464ff4af4f5aa3cd9020b4590820ee Mon Sep 17 00:00:00 2001 From: Guillermo Ruffino Date: Fri, 24 Apr 2020 20:58:32 -0300 Subject: [PATCH] add mac address to wifi info (#1030) * add mac address to wifi info * add test * lint --- esphome/components/wifi_info/text_sensor.py | 7 ++++++- esphome/components/wifi_info/wifi_info_text_sensor.cpp | 1 + esphome/components/wifi_info/wifi_info_text_sensor.h | 7 +++++++ tests/test1.yaml | 2 ++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/esphome/components/wifi_info/text_sensor.py b/esphome/components/wifi_info/text_sensor.py index 81ee787848..56670b4173 100644 --- a/esphome/components/wifi_info/text_sensor.py +++ b/esphome/components/wifi_info/text_sensor.py @@ -1,7 +1,7 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import text_sensor -from esphome.const import CONF_BSSID, CONF_ID, CONF_IP_ADDRESS, CONF_SSID +from esphome.const import CONF_BSSID, CONF_ID, CONF_IP_ADDRESS, CONF_SSID, CONF_MAC_ADDRESS from esphome.core import coroutine DEPENDENCIES = ['wifi'] @@ -10,6 +10,7 @@ wifi_info_ns = cg.esphome_ns.namespace('wifi_info') IPAddressWiFiInfo = wifi_info_ns.class_('IPAddressWiFiInfo', text_sensor.TextSensor, cg.Component) SSIDWiFiInfo = wifi_info_ns.class_('SSIDWiFiInfo', text_sensor.TextSensor, cg.Component) BSSIDWiFiInfo = wifi_info_ns.class_('BSSIDWiFiInfo', text_sensor.TextSensor, cg.Component) +MacAddressWifiInfo = wifi_info_ns.class_('MacAddressWifiInfo', text_sensor.TextSensor, cg.Component) CONFIG_SCHEMA = cv.Schema({ cv.Optional(CONF_IP_ADDRESS): text_sensor.TEXT_SENSOR_SCHEMA.extend({ @@ -21,6 +22,9 @@ CONFIG_SCHEMA = cv.Schema({ cv.Optional(CONF_BSSID): text_sensor.TEXT_SENSOR_SCHEMA.extend({ cv.GenerateID(): cv.declare_id(BSSIDWiFiInfo), }), + cv.Optional(CONF_MAC_ADDRESS): text_sensor.TEXT_SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_id(MacAddressWifiInfo), + }) }) @@ -37,3 +41,4 @@ def to_code(config): yield setup_conf(config, CONF_IP_ADDRESS) yield setup_conf(config, CONF_SSID) yield setup_conf(config, CONF_BSSID) + yield setup_conf(config, CONF_MAC_ADDRESS) diff --git a/esphome/components/wifi_info/wifi_info_text_sensor.cpp b/esphome/components/wifi_info/wifi_info_text_sensor.cpp index 704d9b3099..08a69998fb 100644 --- a/esphome/components/wifi_info/wifi_info_text_sensor.cpp +++ b/esphome/components/wifi_info/wifi_info_text_sensor.cpp @@ -9,6 +9,7 @@ static const char *TAG = "wifi_info"; void IPAddressWiFiInfo::dump_config() { LOG_TEXT_SENSOR("", "WifiInfo IPAddress", this); } void SSIDWiFiInfo::dump_config() { LOG_TEXT_SENSOR("", "WifiInfo SSID", this); } void BSSIDWiFiInfo::dump_config() { LOG_TEXT_SENSOR("", "WifiInfo BSSID", this); } +void MacAddressWifiInfo::dump_config() { LOG_TEXT_SENSOR("", "WifiInfo Mac Address", this); } } // namespace wifi_info } // namespace esphome diff --git a/esphome/components/wifi_info/wifi_info_text_sensor.h b/esphome/components/wifi_info/wifi_info_text_sensor.h index 9dfa684b4b..6d2be08fa0 100644 --- a/esphome/components/wifi_info/wifi_info_text_sensor.h +++ b/esphome/components/wifi_info/wifi_info_text_sensor.h @@ -60,5 +60,12 @@ class BSSIDWiFiInfo : public Component, public text_sensor::TextSensor { wifi::bssid_t last_bssid_; }; +class MacAddressWifiInfo : public Component, public text_sensor::TextSensor { + public: + void setup() override { this->publish_state(get_mac_address_pretty()); } + std::string unique_id() override { return get_mac_address() + "-wifiinfo-macadr"; } + void dump_config() override; +}; + } // namespace wifi_info } // namespace esphome diff --git a/tests/test1.yaml b/tests/test1.yaml index dcc6700b45..dbb3a4bdb3 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -1644,3 +1644,5 @@ text_sensor: name: "SSID" bssid: name: "BSSID" + mac_address: + name: "Mac Address"