mirror of
https://github.com/esphome/esphome.git
synced 2024-11-22 11:47:30 +01:00
Wifi scan results (#1605)
* adding a scan results wifi text sensor * Code comment * Adding scan results to test * Removing redundant call * linting * Better method to update wifi info Co-authored-by: Otto Winter <otto@otto-winter.com> * Getting loop back At least for now. * Trying out suggestion again * Applying cr suggestions Co-authored-by: Otto Winter <otto@otto-winter.com>
This commit is contained in:
parent
fd836e982e
commit
8bebf138ee
@ -5,6 +5,7 @@ from esphome.const import (
|
||||
CONF_BSSID,
|
||||
CONF_ID,
|
||||
CONF_IP_ADDRESS,
|
||||
CONF_SCAN_RESULTS,
|
||||
CONF_SSID,
|
||||
CONF_MAC_ADDRESS,
|
||||
)
|
||||
@ -15,6 +16,9 @@ wifi_info_ns = cg.esphome_ns.namespace("wifi_info")
|
||||
IPAddressWiFiInfo = wifi_info_ns.class_(
|
||||
"IPAddressWiFiInfo", text_sensor.TextSensor, cg.Component
|
||||
)
|
||||
ScanResultsWiFiInfo = wifi_info_ns.class_(
|
||||
"ScanResultsWiFiInfo", text_sensor.TextSensor, cg.PollingComponent
|
||||
)
|
||||
SSIDWiFiInfo = wifi_info_ns.class_("SSIDWiFiInfo", text_sensor.TextSensor, cg.Component)
|
||||
BSSIDWiFiInfo = wifi_info_ns.class_(
|
||||
"BSSIDWiFiInfo", text_sensor.TextSensor, cg.Component
|
||||
@ -30,6 +34,11 @@ CONFIG_SCHEMA = cv.Schema(
|
||||
cv.GenerateID(): cv.declare_id(IPAddressWiFiInfo),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_SCAN_RESULTS): text_sensor.TEXT_SENSOR_SCHEMA.extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(ScanResultsWiFiInfo),
|
||||
}
|
||||
).extend(cv.polling_component_schema("60s")),
|
||||
cv.Optional(CONF_SSID): text_sensor.TEXT_SENSOR_SCHEMA.extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(SSIDWiFiInfo),
|
||||
@ -62,3 +71,4 @@ async def to_code(config):
|
||||
await setup_conf(config, CONF_SSID)
|
||||
await setup_conf(config, CONF_BSSID)
|
||||
await setup_conf(config, CONF_MAC_ADDRESS)
|
||||
await setup_conf(config, CONF_SCAN_RESULTS)
|
||||
|
@ -7,6 +7,7 @@ namespace wifi_info {
|
||||
static const char *const TAG = "wifi_info";
|
||||
|
||||
void IPAddressWiFiInfo::dump_config() { LOG_TEXT_SENSOR("", "WifiInfo IPAddress", this); }
|
||||
void ScanResultsWiFiInfo::dump_config() { LOG_TEXT_SENSOR("", "WifiInfo Scan Results", 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); }
|
||||
|
@ -24,6 +24,35 @@ class IPAddressWiFiInfo : public Component, public text_sensor::TextSensor {
|
||||
network::IPAddress last_ip_;
|
||||
};
|
||||
|
||||
class ScanResultsWiFiInfo : public PollingComponent, public text_sensor::TextSensor {
|
||||
public:
|
||||
void update() override {
|
||||
std::string scan_results;
|
||||
for (auto &scan : wifi::global_wifi_component->get_scan_result()) {
|
||||
if (scan.get_is_hidden())
|
||||
continue;
|
||||
|
||||
scan_results += scan.get_ssid();
|
||||
scan_results += ": ";
|
||||
scan_results += esphome::to_string(scan.get_rssi());
|
||||
scan_results += "dB\n";
|
||||
}
|
||||
|
||||
if (this->last_scan_results_ != scan_results) {
|
||||
this->last_scan_results_ = scan_results;
|
||||
// There's a limit of 255 characters per state.
|
||||
// Longer states just don't get sent so we truncate it.
|
||||
this->publish_state(scan_results.substr(0, 255));
|
||||
}
|
||||
}
|
||||
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
|
||||
std::string unique_id() override { return get_mac_address() + "-wifiinfo-scanresults"; }
|
||||
void dump_config() override;
|
||||
|
||||
protected:
|
||||
std::string last_scan_results_;
|
||||
};
|
||||
|
||||
class SSIDWiFiInfo : public Component, public text_sensor::TextSensor {
|
||||
public:
|
||||
void loop() override {
|
||||
|
@ -558,6 +558,7 @@ CONF_SAFE_MODE = "safe_mode"
|
||||
CONF_SAMSUNG = "samsung"
|
||||
CONF_SATELLITES = "satellites"
|
||||
CONF_SCAN = "scan"
|
||||
CONF_SCAN_RESULTS = "scan_results"
|
||||
CONF_SCL = "scl"
|
||||
CONF_SCL_PIN = "scl_pin"
|
||||
CONF_SDA = "sda"
|
||||
|
@ -2359,6 +2359,8 @@ text_sensor:
|
||||
name: Template Text Sensor
|
||||
id: ${textname}_text
|
||||
- platform: wifi_info
|
||||
scan_results:
|
||||
name: 'Scan Results'
|
||||
ip_address:
|
||||
name: 'IP Address'
|
||||
ssid:
|
||||
|
Loading…
Reference in New Issue
Block a user