1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-04 19:03:47 +01:00

Move fnv1a_32bit_hash to helpers

This commit is contained in:
Daniel Vikstrom
2025-06-02 14:42:39 +02:00
parent f4a9221232
commit 57f4067fbf
3 changed files with 17 additions and 12 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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: