-
-
Notifications
You must be signed in to change notification settings - Fork 32k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a mac_address config validation helper #18571
Conversation
This adds a helper to verify that a given string is indeed a MAC address. While this is possible with the regex config validation helper, this purpose-specific helper has the following benefits: - You don't have to write a regex yourself! - Allows you to easily specify allowed MAC address separators, and also allows you to easily specify if lower/uppercase hex characters are allowed. The latter may seem like a strange 'feature', but various platforms specify (and require that MAC addresses *be* specified) in a variety of formats. In my own personal experience, I've seen the following be considered "valid": - 01:23:45:67:89:AB - 01:23:45:67:89:ab - 01.23.45.67.89.AB - 01.23.45.67.89.ab - 01-23-45-67-89-AB - 01-23-45-67-89-ab - 01 23 45 67 89 AB - 01 23 45 67 89 ab - 0123456789AB - 0123456789ab - 0123.4567.89AB - 0123.4567.89ab
@@ -474,6 +474,60 @@ def validator(config): | |||
return validator | |||
|
|||
|
|||
def is_mac_address(value=None, separators=None, allow_lowercase=True, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method seems overkill and is not in line with our other config validation. What we do instead is that we pick a format and normalize everything to be that specific format. A method to format mac addresses is already available here and could be moved to config_validation.
Also this method does weird things by accepting value
(unused) and raising Invalid
(but that is raised outside of the scope of actual validating config)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@balloob I think you're right - I reflected on it this morning and it does indeed feel like overkill. I'll close this and reconfigure the PR that uses it to use other validators. I don't think normalizing MAC addresses in the whole codebase is something I could do soon, though. 😄
FWIW - The reason value was accepted was because it seems to be passed implicitly by voluptuous when you call one of these helpers. Without it (leaving separators=None
as the first parameter), you'd end up with weird behavior. For example, calling cv.is_mac_address
without arguments would end up calling cv.is_mac_address(separator="whatever you wanted to validate")
, and your regular expression would be ^[0-9a-zA-Z]{2}[whatever you wanted to validate]{1}[0-9a-zA-Z{2}[whatever you wanted to validate]{1} ...
. It was odd.
In any case, thank you for the feedback! 😄
Closing per @balloob's feedback! |
Description:
This adds a helper to verify that a given string is indeed a MAC
address. While this is possible with the regex config validation
helper, this purpose-specific helper has the following benefits:
and also allows you to easily specify if lower/uppercase hex
characters are allowed.
The latter may seem like a strange 'feature', but various platforms
specify (and require that MAC addresses be specified) in a variety
of formats. In my own personal experience, I've seen the following
be considered "valid":
Checklist:
tox
. Your PR cannot be merged unless tests passIf the code does not interact with devices: