From 57f4067fbf425c4a456b928e2ee97cc5d28e4a6b Mon Sep 17 00:00:00 2001 From: Daniel Vikstrom Date: Mon, 2 Jun 2025 14:42:39 +0200 Subject: [PATCH] Move fnv1a_32bit_hash to helpers --- esphome/core/config.py | 16 ++++++---------- esphome/cpp_helpers.py | 4 ++-- esphome/helpers.py | 9 +++++++++ 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/esphome/core/config.py b/esphome/core/config.py index d27ec1d6bf..22bb7b0472 100644 --- a/esphome/core/config.py +++ b/esphome/core/config.py @@ -34,7 +34,12 @@ from esphome.const import ( __version__ as ESPHOME_VERSION, ) from esphome.core import CORE, coroutine_with_priority -from esphome.helpers import copy_file_if_changed, get_str_env, walk_files +from esphome.helpers import ( + copy_file_if_changed, + fnv1a_32bit_hash, + get_str_env, + walk_files, +) _LOGGER = logging.getLogger(__name__) @@ -340,15 +345,6 @@ async def _add_automations(config): await automation.build_automation(trigger, [], conf) -def fnv1a_32bit_hash(string: str) -> int: - """FNV-1a 32-bit hash function.""" - hash_value = 2166136261 - for char in string: - hash_value ^= ord(char) - hash_value = (hash_value * 16777619) & 0xFFFFFFFF - return hash_value - - @coroutine_with_priority(100.0) async def to_code(config): cg.add_global(cg.global_ns.namespace("esphome").using) diff --git a/esphome/cpp_helpers.py b/esphome/cpp_helpers.py index 7a8ad060e4..66ff58f4a7 100644 --- a/esphome/cpp_helpers.py +++ b/esphome/cpp_helpers.py @@ -13,11 +13,11 @@ from esphome.const import ( CONF_UPDATE_INTERVAL, KEY_PAST_SAFE_MODE, ) -from esphome.core import CORE, ID, coroutine, fnv1a_32bit_hash +from esphome.core import CORE, ID, coroutine from esphome.coroutine import FakeAwaitable from esphome.cpp_generator import add, get_variable from esphome.cpp_types import App -from esphome.helpers import sanitize, snake_case +from esphome.helpers import fnv1a_32bit_hash, sanitize, snake_case from esphome.types import ConfigFragmentType, ConfigType from esphome.util import Registry, RegistryEntry diff --git a/esphome/helpers.py b/esphome/helpers.py index d95546ac94..242c05e892 100644 --- a/esphome/helpers.py +++ b/esphome/helpers.py @@ -29,6 +29,15 @@ def ensure_unique_string(preferred_string, current_strings): return test_string +def fnv1a_32bit_hash(string: str) -> int: + """FNV-1a 32-bit hash function.""" + hash_value = 2166136261 + for char in string: + hash_value ^= ord(char) + hash_value = (hash_value * 16777619) & 0xFFFFFFFF + return hash_value + + def indent_all_but_first_and_last(text, padding=" "): lines = text.splitlines(True) if len(lines) <= 2: