Skip to content

Migrate from get_object_id_hash() to make_entity_preference() for preferences #48

@bdraco

Description

@bdraco

Summary

ESPHome has added a new make_entity_preference<T>() helper on EntityBase (PR #13505) that encapsulates preference creation and will handle automatic preference migration when the hash algorithm is updated to fix collisions.

This component uses get_object_id_hash() for preference keys, which is the old API that predates even get_preference_hash() (which added device-awareness to avoid collisions across devices). Components using get_object_id_hash() directly are vulnerable to hash collisions when different entity names sanitize to the same string (e.g. "Living Room" and "living_room" both become "living_room"), and will not participate in automatic preference migration.

Current usage

In components/pool_controller/pool_select.cpp:

this->pref_ = global_preferences->make_preference<size_t>(this->get_object_id_hash());

Recommended migration

// Before:
this->pref_ = global_preferences->make_preference<size_t>(this->get_object_id_hash());

// After:
this->pref_ = this->make_entity_preference<size_t>();

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions