Add version and hash to docker container (#167)

* Add version and git hash to image
* Add version reporting to HA via api
This commit is contained in:
Florian Asche authored and GitHub committed 2026-02-21 20:38:46 +01:00
1 parent 76308cc209
commit 7de510cb1f
13 files changed
+123 -12

No files matched your search

+60
View File
@@ -4,8 +4,68 @@ import uuid
# netifaces lib is from netifaces2
import netifaces
from collections.abc import Callable
from importlib.metadata import version, PackageNotFoundError
from pathlib import Path
from typing import Optional
# Cache for version to avoid repeated file reading
_version_cache: Optional[str] = None
_esphome_version_cache: Optional[str] = None
def get_version() -> str:
"""
Read the version from version.txt file.
This function reads the content safely without risk of code injection,
as it only reads raw text and performs no evaluation.
Returns:
str: The version from version.txt or 'unknown' if the file
does not exist or cannot be read.
"""
global _version_cache
if _version_cache is not None:
return _version_cache
version_file = Path(__file__).parent.parent / "version.txt"
try:
# Sicher lesen: nur Rohtext, keine Evaluierung
version = version_file.read_text(encoding="utf-8").strip()
_version_cache = version if version else "unknown"
except (FileNotFoundError, PermissionError, OSError):
_version_cache = "unknown"
return _version_cache
def get_esphome_version() -> str:
"""
Read the version of the installed aioesphomeapi package.
This function uses importlib.metadata to safely retrieve the version
of an installed Python package without executing any code from the
package itself.
Returns:
str: The version of aioesphomeapi (e.g., '42.7.0'), or 'unknown'
if the package is not installed or the version cannot be read.
"""
global _esphome_version_cache
if _esphome_version_cache is not None:
return _esphome_version_cache
try:
_esphome_version_cache = version("aioesphomeapi")
except PackageNotFoundError:
_esphome_version_cache = "unknown"
return _esphome_version_cache
def call_all(*callables: Optional[Callable[[], None]]) -> None:
for item in filter(None, callables):
item()