[wifi_signal] Update signal strength immediately on WiFi connect/disconnect (#12347)
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
from esphome.components import sensor
|
from esphome.components import sensor, wifi
|
||||||
import esphome.config_validation as cv
|
import esphome.config_validation as cv
|
||||||
from esphome.const import (
|
from esphome.const import (
|
||||||
DEVICE_CLASS_SIGNAL_STRENGTH,
|
DEVICE_CLASS_SIGNAL_STRENGTH,
|
||||||
@@ -25,5 +25,6 @@ CONFIG_SCHEMA = sensor.sensor_schema(
|
|||||||
|
|
||||||
|
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
|
wifi.request_wifi_listeners()
|
||||||
var = await sensor.new_sensor(config)
|
var = await sensor.new_sensor(config)
|
||||||
await cg.register_component(var, config)
|
await cg.register_component(var, config)
|
||||||
|
|||||||
@@ -2,13 +2,11 @@
|
|||||||
#ifdef USE_WIFI
|
#ifdef USE_WIFI
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome::wifi_signal {
|
||||||
namespace wifi_signal {
|
|
||||||
|
|
||||||
static const char *const TAG = "wifi_signal.sensor";
|
static const char *const TAG = "wifi_signal.sensor";
|
||||||
|
|
||||||
void WiFiSignalSensor::dump_config() { LOG_SENSOR("", "WiFi Signal", this); }
|
void WiFiSignalSensor::dump_config() { LOG_SENSOR("", "WiFi Signal", this); }
|
||||||
|
|
||||||
} // namespace wifi_signal
|
} // namespace esphome::wifi_signal
|
||||||
} // namespace esphome
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -5,17 +5,19 @@
|
|||||||
#include "esphome/components/sensor/sensor.h"
|
#include "esphome/components/sensor/sensor.h"
|
||||||
#include "esphome/components/wifi/wifi_component.h"
|
#include "esphome/components/wifi/wifi_component.h"
|
||||||
#ifdef USE_WIFI
|
#ifdef USE_WIFI
|
||||||
namespace esphome {
|
namespace esphome::wifi_signal {
|
||||||
namespace wifi_signal {
|
|
||||||
|
|
||||||
class WiFiSignalSensor : public sensor::Sensor, public PollingComponent {
|
class WiFiSignalSensor : public sensor::Sensor, public PollingComponent, public wifi::WiFiConnectStateListener {
|
||||||
public:
|
public:
|
||||||
|
void setup() override { wifi::global_wifi_component->add_connect_state_listener(this); }
|
||||||
void update() override { this->publish_state(wifi::global_wifi_component->wifi_rssi()); }
|
void update() override { this->publish_state(wifi::global_wifi_component->wifi_rssi()); }
|
||||||
void dump_config() override;
|
void dump_config() override;
|
||||||
|
|
||||||
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
|
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
|
||||||
|
|
||||||
|
// WiFiConnectStateListener interface - update RSSI immediately on connect
|
||||||
|
void on_wifi_connect_state(const std::string &ssid, const wifi::bssid_t &bssid) override { this->update(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace wifi_signal
|
} // namespace esphome::wifi_signal
|
||||||
} // namespace esphome
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user