-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
Mqtt light refactor #18227
Mqtt light refactor #18227
Conversation
95cb9ca
to
55ba5e1
Compare
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 would be a breaking change. And especially for the MQTT discovered platforms I really do not like breaking changes as everyone would need to update their firmwares/wait for them to update too. It's not the end of the world, but maybe we could be a bit smarter with working around this?
I'm thinking: now that the platform
option in discovery payloads has no use anymore, maybe we could use it as an alias for schema
? This way we could have a gradual deprecation/removal cycle.
schema_basic.PLATFORM_SCHEMA_BASIC.schema).extend( | ||
schema_json.PLATFORM_SCHEMA_JSON.schema).extend( | ||
schema_template.PLATFORM_SCHEMA_TEMPLATE.schema).extend( | ||
mqtt.MQTT_AVAILABILITY_SCHEMA.schema) |
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 is not the way this should be done. If we already have a schema option, why not use it?
def validate_mqtt_light(value):
SCHEMAS = {
'basic': schema_basic.PLATFORM_SCHEMA_BASIC,
'json': schema_json.PLATFORM_SCHEMA_JSON,
...
}
return SCHEMAS[value[CONF_SCHEMA]](value)
PLATFORM_SCHEMA = vol.All(vol.Schema({
vol.Optional(CONF_SCHEMA, default='basic'): vol.All(vol.Lower, vol.Any('basic', 'json', 'template'))
}, allow_extra=True), validate_mqtt_light)
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.
Thanks!
However, what should the custom validator do if it finds a violation?
Print error messsage? Throw?
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.
Voluptuous already takes care of that using the code above. (Internally, the schema will throw an vol.Invalid
or vol.MultipleInvalid
which will propagate up through the custom validator into a validation error)
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.
Alternatively, we could do
PLATFORM_SCHEMA = vol.Any(schema_basic.PLATFORM_SCHEMA_BASIC, schema_json. ...)
# in basic schema
PLATFORM_SCHEMA = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend({
vol.Optional(CONF_SCHEMA, default='basic'): 'basic',
# ...
# in other schemas (for example json)
PLATFORM_SCHEMA = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend({
vol.Required(CONF_SCHEMA): 'json',
but the validation error messages this alternative approach would produce are quite bad.
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.
Thanks a lot! Rewritten as you suggest.
1a5bb43
to
b39be91
Compare
@OttoWinter Thanks a lot for reviewing! I added backwards compatibility by replacing |
How should the documentation be changed to reflect these changes? Obviously, the configuration.yaml samples need to be updated and some wording rephrased (it's now one platform, supporting multiple schemas). |
Refactor according to review comments.
b39be91
to
4605e14
Compare
Docs should be renamed to just be |
* Documentation for home-assistant/core#18227 * Merge documentation for the three shcemas
Changes as per home-assistant/core#18227 and suggestion from #286
Description:
Refactor of MQTT light as discussed in #18180 to add support for config entry to
mqtt_json
andmqtt_template
Pull request in home-assistant.io with documentation (if applicable): home-assistant/home-assistant.io#7601
Example entry for
configuration.yaml
(if applicable):Before change (mqtt_json):
After change (mqtt_json):
Before change (mqtt_template):
After change (mqtt_template):
Checklist:
tox
. Your PR cannot be merged unless tests passIf user exposed functionality or configuration variables are added/changed: