mirror of
https://github.com/esphome/esphome.git
synced 2024-11-26 17:05:21 +01:00
Fix mqtt_text_sensor to honor unique_id when set. (#698)
* Fix mqtt_text_sensor to honor unique_id when set. * Remove setting of unique_id in json tree, as the mqtt_component already does this, and in fact overrides what we do here. * Add unqiue_id() and dump_config() to the wifi_info sensors.
This commit is contained in:
parent
655327a8b1
commit
071272a27f
3 changed files with 11 additions and 3 deletions
|
@ -15,9 +15,6 @@ void MQTTTextSensor::send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig
|
||||||
if (!this->sensor_->get_icon().empty())
|
if (!this->sensor_->get_icon().empty())
|
||||||
root["icon"] = this->sensor_->get_icon();
|
root["icon"] = this->sensor_->get_icon();
|
||||||
|
|
||||||
if (!this->sensor_->unique_id().empty())
|
|
||||||
root["unique_id"] = this->sensor_->unique_id();
|
|
||||||
|
|
||||||
config.command_topic = false;
|
config.command_topic = false;
|
||||||
}
|
}
|
||||||
void MQTTTextSensor::setup() {
|
void MQTTTextSensor::setup() {
|
||||||
|
@ -40,6 +37,7 @@ bool MQTTTextSensor::send_initial_state() {
|
||||||
bool MQTTTextSensor::is_internal() { return this->sensor_->is_internal(); }
|
bool MQTTTextSensor::is_internal() { return this->sensor_->is_internal(); }
|
||||||
std::string MQTTTextSensor::component_type() const { return "sensor"; }
|
std::string MQTTTextSensor::component_type() const { return "sensor"; }
|
||||||
std::string MQTTTextSensor::friendly_name() const { return this->sensor_->get_name(); }
|
std::string MQTTTextSensor::friendly_name() const { return this->sensor_->get_name(); }
|
||||||
|
std::string MQTTTextSensor::unique_id() { return this->sensor_->unique_id(); }
|
||||||
|
|
||||||
} // namespace mqtt
|
} // namespace mqtt
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|
|
@ -31,6 +31,8 @@ class MQTTTextSensor : public mqtt::MQTTComponent {
|
||||||
|
|
||||||
std::string friendly_name() const override;
|
std::string friendly_name() const override;
|
||||||
|
|
||||||
|
std::string unique_id() override;
|
||||||
|
|
||||||
text_sensor::TextSensor *sensor_;
|
text_sensor::TextSensor *sensor_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,8 @@
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace wifi_info {
|
namespace wifi_info {
|
||||||
|
|
||||||
|
static const char TAG[] = "wifi_info.text_sensor";
|
||||||
|
|
||||||
class IPAddressWiFiInfo : public Component, public text_sensor::TextSensor {
|
class IPAddressWiFiInfo : public Component, public text_sensor::TextSensor {
|
||||||
public:
|
public:
|
||||||
void loop() override {
|
void loop() override {
|
||||||
|
@ -16,7 +18,9 @@ class IPAddressWiFiInfo : public Component, public text_sensor::TextSensor {
|
||||||
this->publish_state(ip.toString().c_str());
|
this->publish_state(ip.toString().c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
void dump_config() override { LOG_TEXT_SENSOR("", "WifiInfo IPAddress Text Sensor", this); }
|
||||||
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
|
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
|
||||||
|
std::string unique_id() override { return get_mac_address() + "-wifiinfo-ip"; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
IPAddress last_ip_;
|
IPAddress last_ip_;
|
||||||
|
@ -31,7 +35,9 @@ class SSIDWiFiInfo : public Component, public text_sensor::TextSensor {
|
||||||
this->publish_state(this->last_ssid_);
|
this->publish_state(this->last_ssid_);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
void dump_config() override { LOG_TEXT_SENSOR("", "WifiInfo SSDID Text Sensor", this); }
|
||||||
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
|
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
|
||||||
|
std::string unique_id() override { return get_mac_address() + "-wifiinfo-ssid"; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::string last_ssid_;
|
std::string last_ssid_;
|
||||||
|
@ -48,7 +54,9 @@ class BSSIDWiFiInfo : public Component, public text_sensor::TextSensor {
|
||||||
this->publish_state(buf);
|
this->publish_state(buf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
void dump_config() override { LOG_TEXT_SENSOR("", "WifiInfo BSSID Text Sensor", this); }
|
||||||
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
|
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
|
||||||
|
std::string unique_id() override { return get_mac_address() + "-wifiinfo-bssid"; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
wifi::bssid_t last_bssid_;
|
wifi::bssid_t last_bssid_;
|
||||||
|
|
Loading…
Reference in a new issue