ESPHome  2023.5.4
wifi_info_text_sensor.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 namespace esphome {
8 namespace wifi_info {
9 
11  public:
12  void update() override {
14  if (ip != this->last_ip_) {
15  this->last_ip_ = ip;
16  this->publish_state(ip.str());
17  }
18  }
19  float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
20  std::string unique_id() override { return get_mac_address() + "-wifiinfo-ip"; }
21  void dump_config() override;
22 
23  protected:
25 };
26 
28  public:
29  void update() override {
30  std::string scan_results;
31  for (auto &scan : wifi::global_wifi_component->get_scan_result()) {
32  if (scan.get_is_hidden())
33  continue;
34 
35  scan_results += scan.get_ssid();
36  scan_results += ": ";
37  scan_results += esphome::to_string(scan.get_rssi());
38  scan_results += "dB\n";
39  }
40 
41  if (this->last_scan_results_ != scan_results) {
42  this->last_scan_results_ = scan_results;
43  // There's a limit of 255 characters per state.
44  // Longer states just don't get sent so we truncate it.
45  this->publish_state(scan_results.substr(0, 255));
46  }
47  }
48  float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
49  std::string unique_id() override { return get_mac_address() + "-wifiinfo-scanresults"; }
50  void dump_config() override;
51 
52  protected:
53  std::string last_scan_results_;
54 };
55 
57  public:
58  void update() override {
59  std::string ssid = wifi::global_wifi_component->wifi_ssid();
60  if (this->last_ssid_ != ssid) {
61  this->last_ssid_ = ssid;
62  this->publish_state(this->last_ssid_);
63  }
64  }
65  float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
66  std::string unique_id() override { return get_mac_address() + "-wifiinfo-ssid"; }
67  void dump_config() override;
68 
69  protected:
70  std::string last_ssid_;
71 };
72 
74  public:
75  void update() override {
77  if (memcmp(bssid.data(), last_bssid_.data(), 6) != 0) {
78  std::copy(bssid.begin(), bssid.end(), last_bssid_.begin());
79  char buf[30];
80  sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
81  this->publish_state(buf);
82  }
83  }
84  float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
85  std::string unique_id() override { return get_mac_address() + "-wifiinfo-bssid"; }
86  void dump_config() override;
87 
88  protected:
90 };
91 
93  public:
94  void setup() override { this->publish_state(get_mac_address_pretty()); }
95  std::string unique_id() override { return get_mac_address() + "-wifiinfo-macadr"; }
96  void dump_config() override;
97 };
98 
99 } // namespace wifi_info
100 } // namespace esphome
float get_setup_priority() const override
std::array< uint8_t, 6 > bssid_t
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition: component.cpp:25
This class simplifies creating components that periodically check a state.
Definition: component.h:282
void publish_state(const std::string &state)
Definition: text_sensor.cpp:9
std::string get_mac_address()
Get the device MAC address as a string, in lowercase hex notation.
Definition: helpers.cpp:480
WiFiComponent * global_wifi_component
float get_setup_priority() const override
std::string to_string(int value)
Definition: helpers.cpp:50
Definition: a4988.cpp:4
std::string get_mac_address_pretty()
Get the device MAC address as a string, in colon-separated uppercase hex notation.
Definition: helpers.cpp:485