From 75fafceb76b02f5873c840662403805fecaf68ed Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Wed, 10 Jan 2024 15:38:01 +0100 Subject: [PATCH] Add template method: sha1 --- .../homeassistant/templating/sha1.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 custom_components/spook/ectoplasms/homeassistant/templating/sha1.py diff --git a/custom_components/spook/ectoplasms/homeassistant/templating/sha1.py b/custom_components/spook/ectoplasms/homeassistant/templating/sha1.py new file mode 100644 index 00000000..a685bb12 --- /dev/null +++ b/custom_components/spook/ectoplasms/homeassistant/templating/sha1.py @@ -0,0 +1,32 @@ +"""Spook - Not your homie.""" +from __future__ import annotations + +import hashlib +from typing import TYPE_CHECKING, Any + +from ....templating import AbstractSpookTemplateFunction + +if TYPE_CHECKING: + from collections.abc import Callable + + +class SpookTemplateFunction(AbstractSpookTemplateFunction): + """Spook template function to generate sha1 hashes.""" + + name = "sha1" + + requires_hass_object = False + is_available_in_limited_environment = True + is_filter = True + is_global = True + + def _function( + self, + value: str, + ) -> str: + """Generate sha1 hash from a string.""" + return hashlib.sha1(value.encode()).hexdigest() # noqa: S324 + + def function(self) -> Callable[..., Any]: + """Return the python method that runs this template function.""" + return self._function