Add Wifi info text sensor (#473)

* Add WiFi Info text sensor

* Lint

* Fix register

* Add newline at end of file
This commit is contained in:
Otto Winter 2019-03-05 14:05:51 +01:00 committed by GitHub
parent 0fc6a027a7
commit 5c14ca030a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 3 deletions

View file

@ -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'

View file

@ -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'

View file

@ -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'

View file

@ -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"