1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-06 05:12:21 +01:00

Limit hostnames to 31 characters (#2531)

This commit is contained in:
Oxan van Leeuwen
2021-10-22 12:09:47 +02:00
committed by GitHub
parent 9220d9fc52
commit f7b3f52731
3 changed files with 62 additions and 73 deletions

View File

@@ -903,21 +903,9 @@ def validate_bytes(value):
def hostname(value):
value = string(value)
warned_underscore = False
if len(value) > 63:
raise Invalid("Hostnames can only be 63 characters long")
for c in value:
if not (c.isalnum() or c in "-_"):
raise Invalid("Hostname can only have alphanumeric characters and -")
if c in "_" and not warned_underscore:
_LOGGER.warning(
"'%s': Using the '_' (underscore) character in the hostname is discouraged "
"as it can cause problems with some DHCP and local name services. "
"For more information, see https://esphome.io/guides/faq.html#why-shouldn-t-i-use-underscores-in-my-device-name",
value,
)
warned_underscore = True
return value
if re.match(r"^[a-z0-9-]{1,63}$", value, re.IGNORECASE) is not None:
return value
raise Invalid(f"Invalid hostname: {value}")
def domain(value):