Add cv.require_esphome_version helper (#3103)

This commit is contained in:
Oxan van Leeuwen 2022-01-24 20:10:27 +01:00 committed by GitHub
parent cdda648360
commit 2f46267994
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View file

@ -63,7 +63,7 @@ from esphome.jsonschema import (
jschema_registry, jschema_registry,
jschema_typed, jschema_typed,
) )
from esphome.util import parse_esphome_version
from esphome.voluptuous_schema import _Schema from esphome.voluptuous_schema import _Schema
from esphome.yaml_util import make_data_base from esphome.yaml_util import make_data_base
@ -1742,6 +1742,19 @@ def require_framework_version(
return validator return validator
def require_esphome_version(year, month, patch):
def validator(value):
esphome_version = parse_esphome_version()
if esphome_version < (year, month, patch):
requires_version = f"{year}.{month}.{patch}"
raise Invalid(
f"This component requires at least ESPHome version {requires_version}"
)
return value
return validator
@contextmanager @contextmanager
def suppress_invalid(): def suppress_invalid():
try: try:

View file

@ -1,3 +1,4 @@
import typing
from typing import Union, List from typing import Union, List
import collections import collections
@ -242,6 +243,13 @@ def is_dev_esphome_version():
return "dev" in const.__version__ return "dev" in const.__version__
def parse_esphome_version() -> typing.Tuple[int, int, int]:
match = re.match(r"^(\d+).(\d+).(\d+)(-dev\d*|b\d*)?$", const.__version__)
if match is None:
raise ValueError(f"Failed to parse ESPHome version '{const.__version__}'")
return int(match.group(1)), int(match.group(2)), int(match.group(3))
# Custom OrderedDict with nicer repr method for debugging # Custom OrderedDict with nicer repr method for debugging
class OrderedDict(collections.OrderedDict): class OrderedDict(collections.OrderedDict):
def __repr__(self): def __repr__(self):