Skip to content
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

Use voluptuous for input_slider, input_boolean, input_select #3256

Merged
merged 9 commits into from
Sep 23, 2016

Conversation

kellerza
Copy link
Member

@kellerza kellerza commented Sep 7, 2016

Description:
#2800

@fabaff fabaff mentioned this pull request Sep 7, 2016
cv.slug: {
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_INITIAL, default=False): cv.boolean,
vol.Optional(CONF_ICON): cv.icon,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CONF_NAME and CONF_ICON are present in const.py.

if object_id != slugify(object_id):
_LOGGER.warning("Found invalid key for boolean input: %s. "
"Use %s instead", object_id, slugify(object_id))
for object_id, cfg, duplicate in dict_items_from_list(config[DOMAIN]):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this doing? We should never be able to get a duplicate keys because we iterate over a dictionary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I get it. You have added a PLATFORM_SCHEMA instead of CONFIG_SCHEMA, so bootstrap is collecting your config. Change that and you can use the original for object_id, cfg in config[DOMAIN].items():

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multiple platform style was nice for split config though, but removing it will make it simpler

@balloob
Copy link
Member

balloob commented Sep 14, 2016

I think that your changes to bootstrap are causing the conflicts

@kellerza
Copy link
Member Author

Yeah, I must probably rebase after the async PR to fix these, but the log_error(...p_config...) is a bug copied over from that first bootstrap PR of mine. It never seemed to fire during unit testing though

try:
message += humanize_error(config, ex)
except TypeError:
# Shorten ex.path - can be caused by a vol.All(ensure_list, <fail>)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But what if the user did provide a list in his original config, did we just lose some path?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validating {Key: None} with vol.Schema({Key: vol.All(ensure_list, [str])})

The None will not be a string so the path=['Key', 0] -- the first element in the array of key
So None[0] raises TypeError. By popping 1 valie off the end of path, we get a error on Key instead of a unhandled TypeError exception

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened alecthomas/voluptuous#206 that will address the TypeError exception by humanize_error

message += humanize_error(config, ex)
try:
message += humanize_error(config, ex)
except TypeError:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What can cause a TypeError to be raised here?

[cv.string]),
vol.Optional(CONF_INITIAL): cv.string,
vol.Optional(CONF_ICON): cv.icon,
}, _cv_input_select)}}, required=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add extra=vol.ALLOW_EXTRA or else this config will not work in conjunction with other components in the config.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, unit tests completely missed this and did not do a manual test after changing from PATFORM_SCHEMA

vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_INITIAL, default=False): cv.boolean,
vol.Optional(CONF_ICON): cv.icon,
})}}, required=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

vol.Range(min=1e-3)),
vol.Optional(CONF_ICON): cv.icon,
vol.Optional(ATTR_UNIT_OF_MEASUREMENT): cv.string
}, _cv_input_slider)}}, required=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

raise vol.Invalid('Maximum ({}) is not greater than minimum ({})'
.format(minimum, maximum))
state = cfg.get(CONF_INITIAL, minimum)
state = max(min(state, maximum), minimum)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For all other checks we raise if the value is invalid, but here we actually change the value. Let's raise here too

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be a change in current behaviour, but in the end more consistent. Will do the change

}))
for cfg in invalid_configs:
self.assertFalse(
_setup_component(self.hass, DOMAIN, {DOMAIN: cfg}))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use setup_component. The other method is an implementation detail.

"""Wrap value in list if it is not one."""
return value if isinstance(value, list) else [value]
return (value if isinstance(value, list) else
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like that ensure_list now has a default value ([]). I would prefer instead to keep it to the original implementation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will roll back this change.
Ok if I comment out the failing unit tests with [None] with a comment linking to the voluptuous PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just remove it.

@balloob balloob merged commit 9631179 into home-assistant:dev Sep 23, 2016
@kellerza kellerza deleted the input branch October 28, 2016 21:58
@home-assistant home-assistant locked and limited conversation to collaborators Mar 17, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants