Remove all unique_id overriding

This commit is contained in:
Jesse Hills 2023-09-01 13:31:27 +12:00
parent f614655af5
commit 5fd8676151
No known key found for this signature in database
GPG key ID: BEAAE804EFD8E83A
23 changed files with 33 additions and 98 deletions

View file

@ -1,6 +1,6 @@
#include "adc_sensor.h" #include "adc_sensor.h"
#include "esphome/core/log.h"
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#ifdef USE_ESP8266 #ifdef USE_ESP8266
#ifdef USE_ADC_SENSOR_VCC #ifdef USE_ADC_SENSOR_VCC
@ -254,9 +254,5 @@ float ADCSensor::sample() {
} }
#endif #endif
#ifdef USE_ESP8266
std::string ADCSensor::unique_id() { return get_mac_address() + "-adc"; }
#endif
} // namespace adc } // namespace adc
} // namespace esphome } // namespace esphome

View file

@ -1,14 +1,14 @@
#pragma once #pragma once
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/core/defines.h"
#include "esphome/components/sensor/sensor.h" #include "esphome/components/sensor/sensor.h"
#include "esphome/components/voltage_sampler/voltage_sampler.h" #include "esphome/components/voltage_sampler/voltage_sampler.h"
#include "esphome/core/component.h"
#include "esphome/core/defines.h"
#include "esphome/core/hal.h"
#ifdef USE_ESP32 #ifdef USE_ESP32
#include "driver/adc.h"
#include <esp_adc_cal.h> #include <esp_adc_cal.h>
#include "driver/adc.h"
#endif #endif
namespace esphome { namespace esphome {
@ -41,10 +41,6 @@ class ADCSensor : public sensor::Sensor, public PollingComponent, public voltage
void set_output_raw(bool output_raw) { output_raw_ = output_raw; } void set_output_raw(bool output_raw) { output_raw_ = output_raw; }
float sample() override; float sample() override;
#ifdef USE_ESP8266
std::string unique_id() override;
#endif
#ifdef USE_RP2040 #ifdef USE_RP2040
void set_is_temperature() { is_temperature_ = true; } void set_is_temperature() { is_temperature_ = true; }
#endif #endif

View file

@ -273,7 +273,6 @@ float DallasTemperatureSensor::get_temp_c() {
return temp / 128.0f; return temp / 128.0f;
} }
std::string DallasTemperatureSensor::unique_id() { return "dallas-" + str_lower_case(format_hex(this->address_)); }
} // namespace dallas } // namespace dallas
} // namespace esphome } // namespace esphome

View file

@ -1,8 +1,8 @@
#pragma once #pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esp_one_wire.h" #include "esp_one_wire.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/core/component.h"
#include <vector> #include <vector>
@ -60,8 +60,6 @@ class DallasTemperatureSensor : public sensor::Sensor {
float get_temp_c(); float get_temp_c();
std::string unique_id() override;
protected: protected:
DallasComponent *parent_; DallasComponent *parent_;
uint64_t address_; uint64_t address_;

View file

@ -1,8 +1,8 @@
#ifdef USE_ESP32 #ifdef USE_ESP32
#include "esp32_hall.h" #include "esp32_hall.h"
#include "esphome/core/log.h"
#include "esphome/core/hal.h"
#include <driver/adc.h> #include <driver/adc.h>
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
namespace esphome { namespace esphome {
namespace esp32_hall { namespace esp32_hall {
@ -16,7 +16,6 @@ void ESP32HallSensor::update() {
ESP_LOGD(TAG, "'%s': Got reading %.0f µT", this->name_.c_str(), value); ESP_LOGD(TAG, "'%s': Got reading %.0f µT", this->name_.c_str(), value);
this->publish_state(value); this->publish_state(value);
} }
std::string ESP32HallSensor::unique_id() { return get_mac_address() + "-hall"; }
void ESP32HallSensor::dump_config() { LOG_SENSOR("", "ESP32 Hall Sensor", this); } void ESP32HallSensor::dump_config() { LOG_SENSOR("", "ESP32 Hall Sensor", this); }
} // namespace esp32_hall } // namespace esp32_hall

View file

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h" #include "esphome/components/sensor/sensor.h"
#include "esphome/core/component.h"
#ifdef USE_ESP32 #ifdef USE_ESP32
@ -13,8 +13,6 @@ class ESP32HallSensor : public sensor::Sensor, public PollingComponent {
void dump_config() override; void dump_config() override;
void update() override; void update() override;
std::string unique_id() override;
}; };
} // namespace esp32_hall } // namespace esp32_hall

View file

@ -1,8 +1,8 @@
#pragma once #pragma once
#include "esphome/core/component.h"
#include "esphome/components/text_sensor/text_sensor.h"
#include "esphome/components/ethernet/ethernet_component.h" #include "esphome/components/ethernet/ethernet_component.h"
#include "esphome/components/text_sensor/text_sensor.h"
#include "esphome/core/component.h"
#ifdef USE_ESP32 #ifdef USE_ESP32
@ -20,7 +20,6 @@ class IPAddressEthernetInfo : public PollingComponent, public text_sensor::TextS
} }
float get_setup_priority() const override { return setup_priority::ETHERNET; } float get_setup_priority() const override { return setup_priority::ETHERNET; }
std::string unique_id() override { return get_mac_address() + "-ethernetinfo"; }
void dump_config() override; void dump_config() override;
protected: protected:

View file

@ -2,9 +2,9 @@
#ifdef USE_MQTT #ifdef USE_MQTT
#include "esphome/core/log.h"
#include "esphome/core/application.h" #include "esphome/core/application.h"
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include "esphome/core/version.h" #include "esphome/core/version.h"
#include "mqtt_const.h" #include "mqtt_const.h"
@ -111,21 +111,16 @@ bool MQTTComponent::send_discovery_() {
root[MQTT_PAYLOAD_NOT_AVAILABLE] = this->availability_->payload_not_available; root[MQTT_PAYLOAD_NOT_AVAILABLE] = this->availability_->payload_not_available;
} }
std::string unique_id = this->unique_id();
const MQTTDiscoveryInfo &discovery_info = global_mqtt_client->get_discovery_info(); const MQTTDiscoveryInfo &discovery_info = global_mqtt_client->get_discovery_info();
if (!unique_id.empty()) { if (discovery_info.unique_id_generator == MQTT_MAC_ADDRESS_UNIQUE_ID_GENERATOR) {
root[MQTT_UNIQUE_ID] = unique_id; char friendly_name_hash[9];
sprintf(friendly_name_hash, "%08" PRIx32, fnv1_hash(this->friendly_name()));
friendly_name_hash[8] = 0; // ensure the hash-string ends with null
root[MQTT_UNIQUE_ID] = get_mac_address() + "-" + this->component_type() + "-" + friendly_name_hash;
} else { } else {
if (discovery_info.unique_id_generator == MQTT_MAC_ADDRESS_UNIQUE_ID_GENERATOR) { // default to almost-unique ID. It's a hack but the only way to get that
char friendly_name_hash[9]; // gorgeous device registry view.
sprintf(friendly_name_hash, "%08" PRIx32, fnv1_hash(this->friendly_name())); root[MQTT_UNIQUE_ID] = "ESP" + this->component_type() + this->get_default_object_id_();
friendly_name_hash[8] = 0; // ensure the hash-string ends with null
root[MQTT_UNIQUE_ID] = get_mac_address() + "-" + this->component_type() + "-" + friendly_name_hash;
} else {
// default to almost-unique ID. It's a hack but the only way to get that
// gorgeous device registry view.
root[MQTT_UNIQUE_ID] = "ESP" + this->component_type() + this->get_default_object_id_();
}
} }
const std::string &node_name = App.get_name(); const std::string &node_name = App.get_name();
@ -233,7 +228,6 @@ void MQTTComponent::call_dump_config() {
this->dump_config(); this->dump_config();
} }
void MQTTComponent::schedule_resend_state() { this->resend_state_ = true; } void MQTTComponent::schedule_resend_state() { this->resend_state_ = true; }
std::string MQTTComponent::unique_id() { return ""; }
bool MQTTComponent::is_connected_() const { return global_mqtt_client->is_connected(); } bool MQTTComponent::is_connected_() const { return global_mqtt_client->is_connected(); }
// Pull these properties from EntityBase if not overridden // Pull these properties from EntityBase if not overridden

View file

@ -156,13 +156,6 @@ class MQTTComponent : public Component {
*/ */
virtual const EntityBase *get_entity() const = 0; virtual const EntityBase *get_entity() const = 0;
/** A unique ID for this MQTT component, empty for no unique id. See unique ID requirements:
* https://developers.home-assistant.io/docs/en/entity_registry_index.html#unique-id-requirements
*
* @return The unique id as a string.
*/
virtual std::string unique_id();
/// Get the friendly name of this MQTT component. /// Get the friendly name of this MQTT component.
virtual std::string friendly_name() const; virtual std::string friendly_name() const;

View file

@ -1,5 +1,5 @@
#include <cinttypes>
#include "mqtt_sensor.h" #include "mqtt_sensor.h"
#include <cinttypes>
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include "mqtt_const.h" #include "mqtt_const.h"
@ -72,7 +72,6 @@ bool MQTTSensorComponent::publish_state(float value) {
int8_t accuracy = this->sensor_->get_accuracy_decimals(); int8_t accuracy = this->sensor_->get_accuracy_decimals();
return this->publish(this->get_state_topic_(), value_accuracy_to_string(value, accuracy)); return this->publish(this->get_state_topic_(), value_accuracy_to_string(value, accuracy));
} }
std::string MQTTSensorComponent::unique_id() { return this->sensor_->unique_id(); }
} // namespace mqtt } // namespace mqtt
} // namespace esphome } // namespace esphome

View file

@ -46,7 +46,6 @@ class MQTTSensorComponent : public mqtt::MQTTComponent {
/// Override for MQTTComponent, returns "sensor". /// Override for MQTTComponent, returns "sensor".
std::string component_type() const override; std::string component_type() const override;
const EntityBase *get_entity() const override; const EntityBase *get_entity() const override;
std::string unique_id() override;
sensor::Sensor *sensor_; sensor::Sensor *sensor_;
optional<uint32_t> expire_after_; // Override the expire after advertised to Home Assistant optional<uint32_t> expire_after_; // Override the expire after advertised to Home Assistant

View file

@ -34,7 +34,6 @@ bool MQTTTextSensor::send_initial_state() {
} }
std::string MQTTTextSensor::component_type() const { return "sensor"; } std::string MQTTTextSensor::component_type() const { return "sensor"; }
const EntityBase *MQTTTextSensor::get_entity() const { return this->sensor_; } const EntityBase *MQTTTextSensor::get_entity() const { return this->sensor_; }
std::string MQTTTextSensor::unique_id() { return this->sensor_->unique_id(); }
} // namespace mqtt } // namespace mqtt
} // namespace esphome } // namespace esphome

View file

@ -28,7 +28,6 @@ class MQTTTextSensor : public mqtt::MQTTComponent {
protected: protected:
std::string component_type() const override; std::string component_type() const override;
const EntityBase *get_entity() const override; const EntityBase *get_entity() const override;
std::string unique_id() override;
text_sensor::TextSensor *sensor_; text_sensor::TextSensor *sensor_;
}; };

View file

@ -85,7 +85,6 @@ void Sensor::clear_filters() {
} }
float Sensor::get_state() const { return this->state; } float Sensor::get_state() const { return this->state; }
float Sensor::get_raw_state() const { return this->raw_state; } float Sensor::get_raw_state() const { return this->raw_state; }
std::string Sensor::unique_id() { return ""; }
void Sensor::internal_send_state_to_frontend(float state) { void Sensor::internal_send_state_to_frontend(float state) {
this->has_state_ = true; this->has_state_ = true;

View file

@ -1,10 +1,10 @@
#pragma once #pragma once
#include "esphome/core/log.h" #include "esphome/components/sensor/filter.h"
#include "esphome/core/component.h" #include "esphome/core/component.h"
#include "esphome/core/entity_base.h" #include "esphome/core/entity_base.h"
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
#include "esphome/components/sensor/filter.h" #include "esphome/core/log.h"
#include <vector> #include <vector>
@ -23,9 +23,6 @@ namespace sensor {
if (!(obj)->get_icon().empty()) { \ if (!(obj)->get_icon().empty()) { \
ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \ ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \
} \ } \
if (!(obj)->unique_id().empty()) { \
ESP_LOGV(TAG, "%s Unique ID: '%s'", prefix, (obj)->unique_id().c_str()); \
} \
if ((obj)->get_force_update()) { \ if ((obj)->get_force_update()) { \
ESP_LOGV(TAG, "%s Force Update: YES", prefix); \ ESP_LOGV(TAG, "%s Force Update: YES", prefix); \
} \ } \
@ -139,12 +136,6 @@ class Sensor : public EntityBase, public EntityBase_DeviceClass, public EntityBa
/// Return whether this sensor has gotten a full state (that passed through all filters) yet. /// Return whether this sensor has gotten a full state (that passed through all filters) yet.
bool has_state() const; bool has_state() const;
/** Override this method to set the unique ID of this sensor.
*
* @deprecated Do not use for new sensors, a suitable unique ID is automatically generated (2023.4).
*/
virtual std::string unique_id();
void internal_send_state_to_frontend(float state); void internal_send_state_to_frontend(float state);
protected: protected:

View file

@ -65,7 +65,6 @@ void TextSensor::internal_send_state_to_frontend(const std::string &state) {
this->callback_.call(state); this->callback_.call(state);
} }
std::string TextSensor::unique_id() { return ""; }
bool TextSensor::has_state() { return this->has_state_; } bool TextSensor::has_state() { return this->has_state_; }
} // namespace text_sensor } // namespace text_sensor

View file

@ -1,9 +1,9 @@
#pragma once #pragma once
#include "esphome/components/text_sensor/filter.h"
#include "esphome/core/component.h" #include "esphome/core/component.h"
#include "esphome/core/entity_base.h" #include "esphome/core/entity_base.h"
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
#include "esphome/components/text_sensor/filter.h"
#include <vector> #include <vector>
@ -16,9 +16,6 @@ namespace text_sensor {
if (!(obj)->get_icon().empty()) { \ if (!(obj)->get_icon().empty()) { \
ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \ ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \
} \ } \
if (!(obj)->unique_id().empty()) { \
ESP_LOGV(TAG, "%s Unique ID: '%s'", prefix, (obj)->unique_id().c_str()); \
} \
} }
#define SUB_TEXT_SENSOR(name) \ #define SUB_TEXT_SENSOR(name) \
@ -58,12 +55,6 @@ class TextSensor : public EntityBase {
// ========== INTERNAL METHODS ========== // ========== INTERNAL METHODS ==========
// (In most use cases you won't need these) // (In most use cases you won't need these)
/** Override this method to set the unique ID of this sensor.
*
* @deprecated Do not use for new sensors, a suitable unique ID is automatically generated (2023.4).
*/
virtual std::string unique_id();
bool has_state(); bool has_state();
void internal_send_state_to_frontend(const std::string &state); void internal_send_state_to_frontend(const std::string &state);

View file

@ -1,7 +1,7 @@
#include "uptime_sensor.h" #include "uptime_sensor.h"
#include "esphome/core/log.h"
#include "esphome/core/helpers.h"
#include "esphome/core/hal.h" #include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
namespace esphome { namespace esphome {
namespace uptime { namespace uptime {
@ -26,7 +26,6 @@ void UptimeSensor::update() {
const float seconds = float(seconds_int) + (this->uptime_ % 1000ULL) / 1000.0f; const float seconds = float(seconds_int) + (this->uptime_ % 1000ULL) / 1000.0f;
this->publish_state(seconds); this->publish_state(seconds);
} }
std::string UptimeSensor::unique_id() { return get_mac_address() + "-uptime"; }
float UptimeSensor::get_setup_priority() const { return setup_priority::HARDWARE; } float UptimeSensor::get_setup_priority() const { return setup_priority::HARDWARE; }
void UptimeSensor::dump_config() { LOG_SENSOR("", "Uptime Sensor", this); } void UptimeSensor::dump_config() { LOG_SENSOR("", "Uptime Sensor", this); }

View file

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h" #include "esphome/components/sensor/sensor.h"
#include "esphome/core/component.h"
namespace esphome { namespace esphome {
namespace uptime { namespace uptime {
@ -13,8 +13,6 @@ class UptimeSensor : public sensor::Sensor, public PollingComponent {
float get_setup_priority() const override; float get_setup_priority() const override;
std::string unique_id() override;
protected: protected:
uint64_t uptime_{0}; uint64_t uptime_{0};
}; };

View file

@ -1,6 +1,6 @@
#include "version_text_sensor.h" #include "version_text_sensor.h"
#include "esphome/core/log.h"
#include "esphome/core/application.h" #include "esphome/core/application.h"
#include "esphome/core/log.h"
#include "esphome/core/version.h" #include "esphome/core/version.h"
namespace esphome { namespace esphome {
@ -17,7 +17,6 @@ void VersionTextSensor::setup() {
} }
float VersionTextSensor::get_setup_priority() const { return setup_priority::DATA; } float VersionTextSensor::get_setup_priority() const { return setup_priority::DATA; }
void VersionTextSensor::set_hide_timestamp(bool hide_timestamp) { this->hide_timestamp_ = hide_timestamp; } void VersionTextSensor::set_hide_timestamp(bool hide_timestamp) { this->hide_timestamp_ = hide_timestamp; }
std::string VersionTextSensor::unique_id() { return get_mac_address() + "-version"; }
void VersionTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Version Text Sensor", this); } void VersionTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Version Text Sensor", this); }
} // namespace version } // namespace version

View file

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "esphome/core/component.h"
#include "esphome/components/text_sensor/text_sensor.h" #include "esphome/components/text_sensor/text_sensor.h"
#include "esphome/core/component.h"
namespace esphome { namespace esphome {
namespace version { namespace version {
@ -12,7 +12,6 @@ class VersionTextSensor : public text_sensor::TextSensor, public Component {
void setup() override; void setup() override;
void dump_config() override; void dump_config() override;
float get_setup_priority() const override; float get_setup_priority() const override;
std::string unique_id() override;
protected: protected:
bool hide_timestamp_{false}; bool hide_timestamp_{false};

View file

@ -1,8 +1,8 @@
#pragma once #pragma once
#include "esphome/core/component.h"
#include "esphome/components/text_sensor/text_sensor.h" #include "esphome/components/text_sensor/text_sensor.h"
#include "esphome/components/wifi/wifi_component.h" #include "esphome/components/wifi/wifi_component.h"
#include "esphome/core/component.h"
namespace esphome { namespace esphome {
namespace wifi_info { namespace wifi_info {
@ -17,7 +17,6 @@ class IPAddressWiFiInfo : public PollingComponent, public text_sensor::TextSenso
} }
} }
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"; }
void dump_config() override; void dump_config() override;
protected: protected:
@ -43,7 +42,6 @@ class DNSAddressWifiInfo : public PollingComponent, public text_sensor::TextSens
} }
} }
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-dns"; }
void dump_config() override; void dump_config() override;
protected: protected:
@ -72,7 +70,6 @@ class ScanResultsWiFiInfo : public PollingComponent, public text_sensor::TextSen
} }
} }
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-scanresults"; }
void dump_config() override; void dump_config() override;
protected: protected:
@ -89,7 +86,6 @@ class SSIDWiFiInfo : public PollingComponent, public text_sensor::TextSensor {
} }
} }
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"; }
void dump_config() override; void dump_config() override;
protected: protected:
@ -108,7 +104,6 @@ class BSSIDWiFiInfo : public PollingComponent, public text_sensor::TextSensor {
} }
} }
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"; }
void dump_config() override; void dump_config() override;
protected: protected:
@ -118,7 +113,6 @@ class BSSIDWiFiInfo : public PollingComponent, public text_sensor::TextSensor {
class MacAddressWifiInfo : public Component, public text_sensor::TextSensor { class MacAddressWifiInfo : public Component, public text_sensor::TextSensor {
public: public:
void setup() override { this->publish_state(get_mac_address_pretty()); } void setup() override { this->publish_state(get_mac_address_pretty()); }
std::string unique_id() override { return get_mac_address() + "-wifiinfo-macadr"; }
void dump_config() override; void dump_config() override;
}; };

View file

@ -1,9 +1,9 @@
#pragma once #pragma once
#include "esphome/core/component.h"
#include "esphome/core/helpers.h"
#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"
#include "esphome/core/component.h"
#include "esphome/core/helpers.h"
namespace esphome { namespace esphome {
namespace wifi_signal { namespace wifi_signal {
@ -12,8 +12,6 @@ class WiFiSignalSensor : public sensor::Sensor, public PollingComponent {
public: public:
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;
std::string unique_id() override { return get_mac_address() + "-wifisignal"; }
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; } float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
}; };