From 5c14ca030a7d89c1b0917ff4556d37fbea28c0bb Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Tue, 5 Mar 2019 14:05:51 +0100 Subject: [PATCH] Add Wifi info text sensor (#473) * Add WiFi Info text sensor * Lint * Fix register * Add newline at end of file --- esphome/components/text_sensor/__init__.py | 7 ++-- esphome/components/text_sensor/wifi_info.py | 45 +++++++++++++++++++++ esphome/const.py | 1 + tests/test1.yaml | 7 ++++ 4 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 esphome/components/text_sensor/wifi_info.py diff --git a/esphome/components/text_sensor/__init__.py b/esphome/components/text_sensor/__init__.py index c250ccdc66..ce5468346d 100644 --- a/esphome/components/text_sensor/__init__.py +++ b/esphome/components/text_sensor/__init__.py @@ -56,9 +56,10 @@ def setup_text_sensor(text_sensor_obj, config): def register_text_sensor(var, config): - text_sensor_var = Pvariable(config[CONF_ID], var, has_side_effects=True) - add(App.register_text_sensor(text_sensor_var)) - CORE.add_job(setup_text_sensor_core_, text_sensor_var, config) + if not CORE.has_id(config[CONF_ID]): + var = Pvariable(config[CONF_ID], var, has_side_effects=True) + add(App.register_text_sensor(var)) + CORE.add_job(setup_text_sensor_core_, var, config) BUILD_FLAGS = '-DUSE_TEXT_SENSOR' diff --git a/esphome/components/text_sensor/wifi_info.py b/esphome/components/text_sensor/wifi_info.py new file mode 100644 index 0000000000..f8b2039d04 --- /dev/null +++ b/esphome/components/text_sensor/wifi_info.py @@ -0,0 +1,45 @@ +import voluptuous as vol + +from esphome.components import text_sensor +import esphome.config_validation as cv +from esphome.const import CONF_BSSID, CONF_ID, CONF_IP_ADDRESS, CONF_NAME, CONF_SSID +from esphome.cpp_generator import Pvariable +from esphome.cpp_types import App, Component + +DEPENDENCIES = ['wifi'] + +IPAddressWiFiInfo = text_sensor.text_sensor_ns.class_('IPAddressWiFiInfo', + text_sensor.TextSensor, Component) +SSIDWiFiInfo = text_sensor.text_sensor_ns.class_('SSIDWiFiInfo', + text_sensor.TextSensor, Component) +BSSIDWiFiInfo = text_sensor.text_sensor_ns.class_('BSSIDWiFiInfo', + text_sensor.TextSensor, Component) + +PLATFORM_SCHEMA = text_sensor.PLATFORM_SCHEMA.extend({ + vol.Optional(CONF_IP_ADDRESS): cv.nameable(text_sensor.TEXT_SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(IPAddressWiFiInfo), + })), + vol.Optional(CONF_SSID): cv.nameable(text_sensor.TEXT_SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(SSIDWiFiInfo), + })), + vol.Optional(CONF_BSSID): cv.nameable(text_sensor.TEXT_SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(BSSIDWiFiInfo), + })), +}) + + +def setup_conf(config, key, klass): + if key in config: + conf = config[key] + rhs = App.register_component(klass.new(conf[CONF_NAME])) + sensor_ = Pvariable(conf[CONF_ID], rhs) + text_sensor.register_text_sensor(sensor_, conf) + + +def to_code(config): + setup_conf(config, CONF_IP_ADDRESS, IPAddressWiFiInfo) + setup_conf(config, CONF_SSID, SSIDWiFiInfo) + setup_conf(config, CONF_BSSID, BSSIDWiFiInfo) + + +BUILD_FLAGS = '-DUSE_WIFI_INFO_TEXT_SENSOR' diff --git a/esphome/const.py b/esphome/const.py index 6d955c650d..ff816bcc6d 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -29,6 +29,7 @@ CONF_BRANCH = 'branch' CONF_LOGGER = 'logger' CONF_WIFI = 'wifi' CONF_SSID = 'ssid' +CONF_IP_ADDRESS = 'ip_address' CONF_BSSID = 'bssid' CONF_PASSWORD = 'password' CONF_MANUAL_IP = 'manual_ip' diff --git a/tests/test1.yaml b/tests/test1.yaml index 8b66d15e1a..b5877d2c36 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -1214,3 +1214,10 @@ text_sensor: - platform: template name: Template Text Sensor id: template_text +- platform: wifi_info + ip_address: + name: "IP Address" + ssid: + name: "SSID" + bssid: + name: "BSSID"