mirror of
https://github.com/esphome/esphome.git
synced 2025-10-04 11:02:19 +01:00
Change hash method
This commit is contained in:
@@ -340,6 +340,15 @@ 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)
|
||||
@@ -420,7 +429,7 @@ async def to_code(config):
|
||||
if config[CONF_SUB_DEVICES]:
|
||||
for dev_conf in config[CONF_SUB_DEVICES]:
|
||||
dev = cg.new_Pvariable(dev_conf[CONF_ID])
|
||||
cg.add(dev.set_uid(hash(str(dev_conf[CONF_ID])) % 0xFFFFFFFF))
|
||||
cg.add(dev.set_uid(fnv1a_32bit_hash(str(dev_conf[CONF_ID]))))
|
||||
cg.add(dev.set_name(dev_conf[CONF_NAME]))
|
||||
cg.add(dev.set_area(dev_conf[CONF_AREA]))
|
||||
cg.add(cg.App.register_sub_device(dev))
|
||||
|
@@ -13,7 +13,7 @@ from esphome.const import (
|
||||
CONF_UPDATE_INTERVAL,
|
||||
KEY_PAST_SAFE_MODE,
|
||||
)
|
||||
from esphome.core import CORE, ID, coroutine
|
||||
from esphome.core import CORE, ID, coroutine, fnv1a_32bit_hash
|
||||
from esphome.coroutine import FakeAwaitable
|
||||
from esphome.cpp_generator import add, get_variable
|
||||
from esphome.cpp_types import App
|
||||
@@ -113,7 +113,7 @@ async def setup_entity(var, config):
|
||||
add(var.set_entity_category(config[CONF_ENTITY_CATEGORY]))
|
||||
if CONF_DEVICE_ID in config:
|
||||
device = await get_variable(config[CONF_DEVICE_ID])
|
||||
add(var.set_device_uid(hash(str(device)) % 0xFFFFFFFF))
|
||||
add(var.set_device_uid(fnv1a_32bit_hash(str(device))))
|
||||
|
||||
|
||||
def extract_registry_entry_config(
|
||||
|
Reference in New Issue
Block a user