From 5fd86761516b4fc72cad72211acaddbcfea2cb01 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Fri, 1 Sep 2023 13:31:27 +1200 Subject: [PATCH] Remove all unique_id overriding --- esphome/components/adc/adc_sensor.cpp | 6 +---- esphome/components/adc/adc_sensor.h | 12 ++++------ .../components/dallas/dallas_component.cpp | 1 - esphome/components/dallas/dallas_component.h | 6 ++--- esphome/components/esp32_hall/esp32_hall.cpp | 5 ++-- esphome/components/esp32_hall/esp32_hall.h | 4 +--- .../ethernet_info/ethernet_info_text_sensor.h | 5 ++-- esphome/components/mqtt/mqtt_component.cpp | 24 +++++++------------ esphome/components/mqtt/mqtt_component.h | 7 ------ esphome/components/mqtt/mqtt_sensor.cpp | 3 +-- esphome/components/mqtt/mqtt_sensor.h | 1 - esphome/components/mqtt/mqtt_text_sensor.cpp | 1 - esphome/components/mqtt/mqtt_text_sensor.h | 1 - esphome/components/sensor/sensor.cpp | 1 - esphome/components/sensor/sensor.h | 13 ++-------- .../components/text_sensor/text_sensor.cpp | 1 - esphome/components/text_sensor/text_sensor.h | 11 +-------- esphome/components/uptime/uptime_sensor.cpp | 5 ++-- esphome/components/uptime/uptime_sensor.h | 4 +--- .../version/version_text_sensor.cpp | 3 +-- .../components/version/version_text_sensor.h | 3 +-- .../wifi_info/wifi_info_text_sensor.h | 8 +------ .../wifi_signal/wifi_signal_sensor.h | 6 ++--- 23 files changed, 33 insertions(+), 98 deletions(-) diff --git a/esphome/components/adc/adc_sensor.cpp b/esphome/components/adc/adc_sensor.cpp index 665ecfd6b5..d78cd53e3c 100644 --- a/esphome/components/adc/adc_sensor.cpp +++ b/esphome/components/adc/adc_sensor.cpp @@ -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 diff --git a/esphome/components/adc/adc_sensor.h b/esphome/components/adc/adc_sensor.h index 7d9c8959da..cb1b6d4093 100644 --- a/esphome/components/adc/adc_sensor.h +++ b/esphome/components/adc/adc_sensor.h @@ -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 +#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 diff --git a/esphome/components/dallas/dallas_component.cpp b/esphome/components/dallas/dallas_component.cpp index 302422d6c7..b2aadabffe 100644 --- a/esphome/components/dallas/dallas_component.cpp +++ b/esphome/components/dallas/dallas_component.cpp @@ -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 diff --git a/esphome/components/dallas/dallas_component.h b/esphome/components/dallas/dallas_component.h index b21bc02e54..fed07cc82d 100644 --- a/esphome/components/dallas/dallas_component.h +++ b/esphome/components/dallas/dallas_component.h @@ -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 @@ -60,8 +60,6 @@ class DallasTemperatureSensor : public sensor::Sensor { float get_temp_c(); - std::string unique_id() override; - protected: DallasComponent *parent_; uint64_t address_; diff --git a/esphome/components/esp32_hall/esp32_hall.cpp b/esphome/components/esp32_hall/esp32_hall.cpp index 762497aedc..3489498edb 100644 --- a/esphome/components/esp32_hall/esp32_hall.cpp +++ b/esphome/components/esp32_hall/esp32_hall.cpp @@ -1,8 +1,8 @@ #ifdef USE_ESP32 #include "esp32_hall.h" -#include "esphome/core/log.h" -#include "esphome/core/hal.h" #include +#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 diff --git a/esphome/components/esp32_hall/esp32_hall.h b/esphome/components/esp32_hall/esp32_hall.h index 8db50c4667..aa4039b8bb 100644 --- a/esphome/components/esp32_hall/esp32_hall.h +++ b/esphome/components/esp32_hall/esp32_hall.h @@ -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 diff --git a/esphome/components/ethernet_info/ethernet_info_text_sensor.h b/esphome/components/ethernet_info/ethernet_info_text_sensor.h index 2d46fe18eb..0c407e912b 100644 --- a/esphome/components/ethernet_info/ethernet_info_text_sensor.h +++ b/esphome/components/ethernet_info/ethernet_info_text_sensor.h @@ -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: diff --git a/esphome/components/mqtt/mqtt_component.cpp b/esphome/components/mqtt/mqtt_component.cpp index 1c7d9f86dd..ff97744b01 100644 --- a/esphome/components/mqtt/mqtt_component.cpp +++ b/esphome/components/mqtt/mqtt_component.cpp @@ -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 diff --git a/esphome/components/mqtt/mqtt_component.h b/esphome/components/mqtt/mqtt_component.h index 16a00cfdde..9242669e16 100644 --- a/esphome/components/mqtt/mqtt_component.h +++ b/esphome/components/mqtt/mqtt_component.h @@ -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; diff --git a/esphome/components/mqtt/mqtt_sensor.cpp b/esphome/components/mqtt/mqtt_sensor.cpp index fff75a3c00..868f741d67 100644 --- a/esphome/components/mqtt/mqtt_sensor.cpp +++ b/esphome/components/mqtt/mqtt_sensor.cpp @@ -1,5 +1,5 @@ -#include #include "mqtt_sensor.h" +#include #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 diff --git a/esphome/components/mqtt/mqtt_sensor.h b/esphome/components/mqtt/mqtt_sensor.h index adc201736a..15ea703ad4 100644 --- a/esphome/components/mqtt/mqtt_sensor.h +++ b/esphome/components/mqtt/mqtt_sensor.h @@ -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 expire_after_; // Override the expire after advertised to Home Assistant diff --git a/esphome/components/mqtt/mqtt_text_sensor.cpp b/esphome/components/mqtt/mqtt_text_sensor.cpp index d0d3174bfe..cb6ee5da63 100644 --- a/esphome/components/mqtt/mqtt_text_sensor.cpp +++ b/esphome/components/mqtt/mqtt_text_sensor.cpp @@ -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 diff --git a/esphome/components/mqtt/mqtt_text_sensor.h b/esphome/components/mqtt/mqtt_text_sensor.h index fe53a6fefd..9a14efdd16 100644 --- a/esphome/components/mqtt/mqtt_text_sensor.h +++ b/esphome/components/mqtt/mqtt_text_sensor.h @@ -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_; }; diff --git a/esphome/components/sensor/sensor.cpp b/esphome/components/sensor/sensor.cpp index 14a8b3d490..ed0a090866 100644 --- a/esphome/components/sensor/sensor.cpp +++ b/esphome/components/sensor/sensor.cpp @@ -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; diff --git a/esphome/components/sensor/sensor.h b/esphome/components/sensor/sensor.h index 98356c943d..32ba73c878 100644 --- a/esphome/components/sensor/sensor.h +++ b/esphome/components/sensor/sensor.h @@ -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 @@ -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: diff --git a/esphome/components/text_sensor/text_sensor.cpp b/esphome/components/text_sensor/text_sensor.cpp index f10cd50267..736145e233 100644 --- a/esphome/components/text_sensor/text_sensor.cpp +++ b/esphome/components/text_sensor/text_sensor.cpp @@ -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 diff --git a/esphome/components/text_sensor/text_sensor.h b/esphome/components/text_sensor/text_sensor.h index 996af02f7e..be5c255ca2 100644 --- a/esphome/components/text_sensor/text_sensor.h +++ b/esphome/components/text_sensor/text_sensor.h @@ -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 @@ -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); diff --git a/esphome/components/uptime/uptime_sensor.cpp b/esphome/components/uptime/uptime_sensor.cpp index 40325d2a36..f3b93b5ac9 100644 --- a/esphome/components/uptime/uptime_sensor.cpp +++ b/esphome/components/uptime/uptime_sensor.cpp @@ -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); } diff --git a/esphome/components/uptime/uptime_sensor.h b/esphome/components/uptime/uptime_sensor.h index dab380d2d9..515d6f2945 100644 --- a/esphome/components/uptime/uptime_sensor.h +++ b/esphome/components/uptime/uptime_sensor.h @@ -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}; }; diff --git a/esphome/components/version/version_text_sensor.cpp b/esphome/components/version/version_text_sensor.cpp index 5b2437ab62..af781fd8dd 100644 --- a/esphome/components/version/version_text_sensor.cpp +++ b/esphome/components/version/version_text_sensor.cpp @@ -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 diff --git a/esphome/components/version/version_text_sensor.h b/esphome/components/version/version_text_sensor.h index 9355e78442..5e0a69e45b 100644 --- a/esphome/components/version/version_text_sensor.h +++ b/esphome/components/version/version_text_sensor.h @@ -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}; diff --git a/esphome/components/wifi_info/wifi_info_text_sensor.h b/esphome/components/wifi_info/wifi_info_text_sensor.h index 35ce108c86..02c95b8db8 100644 --- a/esphome/components/wifi_info/wifi_info_text_sensor.h +++ b/esphome/components/wifi_info/wifi_info_text_sensor.h @@ -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; }; diff --git a/esphome/components/wifi_signal/wifi_signal_sensor.h b/esphome/components/wifi_signal/wifi_signal_sensor.h index f797aaa590..3ff5f292f9 100644 --- a/esphome/components/wifi_signal/wifi_signal_sensor.h +++ b/esphome/components/wifi_signal/wifi_signal_sensor.h @@ -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; } };