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 "esphome/core/log.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#ifdef USE_ESP8266
#ifdef USE_ADC_SENSOR_VCC
@ -254,9 +254,5 @@ float ADCSensor::sample() {
}
#endif
#ifdef USE_ESP8266
std::string ADCSensor::unique_id() { return get_mac_address() + "-adc"; }
#endif
} // namespace adc
} // namespace esphome

View File

@ -1,14 +1,14 @@
#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/voltage_sampler/voltage_sampler.h"
#include "esphome/core/component.h"
#include "esphome/core/defines.h"
#include "esphome/core/hal.h"
#ifdef USE_ESP32
#include "driver/adc.h"
#include <esp_adc_cal.h>
#include "driver/adc.h"
#endif
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; }
float sample() override;
#ifdef USE_ESP8266
std::string unique_id() override;
#endif
#ifdef USE_RP2040
void set_is_temperature() { is_temperature_ = true; }
#endif

View File

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

View File

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

View File

@ -1,8 +1,8 @@
#ifdef USE_ESP32
#include "esp32_hall.h"
#include "esphome/core/log.h"
#include "esphome/core/hal.h"
#include <driver/adc.h>
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
namespace esphome {
namespace esp32_hall {
@ -16,7 +16,6 @@ void ESP32HallSensor::update() {
ESP_LOGD(TAG, "'%s': Got reading %.0f µT", this->name_.c_str(), 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); }
} // namespace esp32_hall

View File

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

View File

@ -1,8 +1,8 @@
#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/text_sensor/text_sensor.h"
#include "esphome/core/component.h"
#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; }
std::string unique_id() override { return get_mac_address() + "-ethernetinfo"; }
void dump_config() override;
protected:

View File

@ -2,9 +2,9 @@
#ifdef USE_MQTT
#include "esphome/core/log.h"
#include "esphome/core/application.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include "esphome/core/version.h"
#include "mqtt_const.h"
@ -111,21 +111,16 @@ bool MQTTComponent::send_discovery_() {
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();
if (!unique_id.empty()) {
root[MQTT_UNIQUE_ID] = unique_id;
if (discovery_info.unique_id_generator == MQTT_MAC_ADDRESS_UNIQUE_ID_GENERATOR) {
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 {
if (discovery_info.unique_id_generator == MQTT_MAC_ADDRESS_UNIQUE_ID_GENERATOR) {
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 {
// 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_();
}
// 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();
@ -233,7 +228,6 @@ void MQTTComponent::call_dump_config() {
this->dump_config();
}
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(); }
// 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;
/** 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.
virtual std::string friendly_name() const;

View File

@ -1,5 +1,5 @@
#include <cinttypes>
#include "mqtt_sensor.h"
#include <cinttypes>
#include "esphome/core/log.h"
#include "mqtt_const.h"
@ -72,7 +72,6 @@ bool MQTTSensorComponent::publish_state(float value) {
int8_t accuracy = this->sensor_->get_accuracy_decimals();
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 esphome

View File

@ -46,7 +46,6 @@ class MQTTSensorComponent : public mqtt::MQTTComponent {
/// Override for MQTTComponent, returns "sensor".
std::string component_type() const override;
const EntityBase *get_entity() const override;
std::string unique_id() override;
sensor::Sensor *sensor_;
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"; }
const EntityBase *MQTTTextSensor::get_entity() const { return this->sensor_; }
std::string MQTTTextSensor::unique_id() { return this->sensor_->unique_id(); }
} // namespace mqtt
} // namespace esphome

View File

@ -28,7 +28,6 @@ class MQTTTextSensor : public mqtt::MQTTComponent {
protected:
std::string component_type() const override;
const EntityBase *get_entity() const override;
std::string unique_id() override;
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_raw_state() const { return this->raw_state; }
std::string Sensor::unique_id() { return ""; }
void Sensor::internal_send_state_to_frontend(float state) {
this->has_state_ = true;

View File

@ -1,10 +1,10 @@
#pragma once
#include "esphome/core/log.h"
#include "esphome/components/sensor/filter.h"
#include "esphome/core/component.h"
#include "esphome/core/entity_base.h"
#include "esphome/core/helpers.h"
#include "esphome/components/sensor/filter.h"
#include "esphome/core/log.h"
#include <vector>
@ -23,9 +23,6 @@ namespace sensor {
if (!(obj)->get_icon().empty()) { \
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()) { \
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.
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);
protected:

View File

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

View File

@ -1,9 +1,9 @@
#pragma once
#include "esphome/components/text_sensor/filter.h"
#include "esphome/core/component.h"
#include "esphome/core/entity_base.h"
#include "esphome/core/helpers.h"
#include "esphome/components/text_sensor/filter.h"
#include <vector>
@ -16,9 +16,6 @@ namespace text_sensor {
if (!(obj)->get_icon().empty()) { \
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) \
@ -58,12 +55,6 @@ class TextSensor : public EntityBase {
// ========== INTERNAL METHODS ==========
// (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();
void internal_send_state_to_frontend(const std::string &state);

View File

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

View File

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

View File

@ -1,6 +1,6 @@
#include "version_text_sensor.h"
#include "esphome/core/log.h"
#include "esphome/core/application.h"
#include "esphome/core/log.h"
#include "esphome/core/version.h"
namespace esphome {
@ -17,7 +17,6 @@ void VersionTextSensor::setup() {
}
float VersionTextSensor::get_setup_priority() const { return setup_priority::DATA; }
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); }
} // namespace version

View File

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

View File

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

View File

@ -1,9 +1,9 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/helpers.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/wifi/wifi_component.h"
#include "esphome/core/component.h"
#include "esphome/core/helpers.h"
namespace esphome {
namespace wifi_signal {
@ -12,8 +12,6 @@ class WiFiSignalSensor : public sensor::Sensor, public PollingComponent {
public:
void update() override { this->publish_state(wifi::global_wifi_component->wifi_rssi()); }
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; }
};