mirror of
https://github.com/esphome/esphome.git
synced 2024-12-22 13:34:54 +01:00
Warn if underscore character is used in hostname (#2079)
* Prevent underscore character being used in 'name'. * Restrict underscores in hostnames, not all names. * Use hostname validator for node name. * Allow underscore in hostname but warn once. * Add renaming instructions link to warning. * Point underscore warning to FAQ section Co-authored-by: Otto Winter <otto@otto-winter.com> Co-authored-by: Otto Winter <otto@otto-winter.com>
This commit is contained in:
parent
d258e06fd7
commit
1771e673d2
3 changed files with 34 additions and 3 deletions
|
@ -891,11 +891,20 @@ def validate_bytes(value):
|
||||||
|
|
||||||
def hostname(value):
|
def hostname(value):
|
||||||
value = string(value)
|
value = string(value)
|
||||||
|
warned_underscore = False
|
||||||
if len(value) > 63:
|
if len(value) > 63:
|
||||||
raise Invalid("Hostnames can only be 63 characters long")
|
raise Invalid("Hostnames can only be 63 characters long")
|
||||||
for c in value:
|
for c in value:
|
||||||
if not (c.isalnum() or c in "_-"):
|
if not (c.isalnum() or c in "-_"):
|
||||||
raise Invalid("Hostname can only have alphanumeric characters and _ or -")
|
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
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -158,7 +158,7 @@ def valid_project_name(value: str):
|
||||||
|
|
||||||
CONFIG_SCHEMA = cv.Schema(
|
CONFIG_SCHEMA = cv.Schema(
|
||||||
{
|
{
|
||||||
cv.Required(CONF_NAME): cv.valid_name,
|
cv.Required(CONF_NAME): cv.hostname,
|
||||||
cv.Required(CONF_PLATFORM): cv.one_of("ESP8266", "ESP32", upper=True),
|
cv.Required(CONF_PLATFORM): cv.one_of("ESP8266", "ESP32", upper=True),
|
||||||
cv.Required(CONF_BOARD): validate_board,
|
cv.Required(CONF_BOARD): validate_board,
|
||||||
cv.Optional(CONF_COMMENT): cv.string,
|
cv.Optional(CONF_COMMENT): cv.string,
|
||||||
|
|
|
@ -40,6 +40,28 @@ def test_valid_name__invalid(value):
|
||||||
config_validation.valid_name(value)
|
config_validation.valid_name(value)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("value", ("foo", "bar123", "foo-bar"))
|
||||||
|
def test_hostname__valid(value):
|
||||||
|
actual = config_validation.hostname(value)
|
||||||
|
|
||||||
|
assert actual == value
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("value", ("foo bar", "foobar ", "foo#bar"))
|
||||||
|
def test_hostname__invalid(value):
|
||||||
|
with pytest.raises(Invalid):
|
||||||
|
config_validation.hostname(value)
|
||||||
|
|
||||||
|
|
||||||
|
def test_hostname__warning(caplog):
|
||||||
|
actual = config_validation.hostname("foo_bar")
|
||||||
|
assert actual == "foo_bar"
|
||||||
|
assert (
|
||||||
|
"Using the '_' (underscore) character in the hostname is discouraged"
|
||||||
|
in caplog.text
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@given(one_of(integers(), text()))
|
@given(one_of(integers(), text()))
|
||||||
def test_string__valid(value):
|
def test_string__valid(value):
|
||||||
actual = config_validation.string(value)
|
actual = config_validation.string(value)
|
||||||
|
|
Loading…
Reference in a new issue