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

Mqtt light refactor #18227

Merged
merged 4 commits into from
Nov 27, 2018
Merged

Conversation

emontnemery
Copy link
Contributor

@emontnemery emontnemery commented Nov 5, 2018

Description:

Refactor of MQTT light as discussed in #18180 to add support for config entry to mqtt_json and mqtt_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):

light:
  - platform: mqtt_json
    command_topic: "home/rgb1/set"

After change (mqtt_json):

light:
  - platform: mqtt
    schema: json
    command_topic: "home/rgb1/set"

Before change (mqtt_template):

light:
  - platform: mqtt_template
    command_topic: "home/rgb1/set"
    command_on_template: "on"
    command_off_template: "off"

After change (mqtt_template):

light:
  - platform: mqtt
    schema: template
    command_topic: "home/rgb1/set"
    command_on_template: "on"
    command_off_template: "off"

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass

If user exposed functionality or configuration variables are added/changed:

@emontnemery emontnemery force-pushed the mqtt_light_refactor branch 2 times, most recently from 95cb9ca to 55ba5e1 Compare November 5, 2018 16:10
Copy link
Member

@OttoWinter OttoWinter left a 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)
Copy link
Member

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)

Copy link
Contributor Author

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?

Copy link
Member

@OttoWinter OttoWinter Nov 10, 2018

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)

Copy link
Member

@OttoWinter OttoWinter Nov 10, 2018

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.

Copy link
Contributor Author

@emontnemery emontnemery Nov 10, 2018

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.

homeassistant/components/light/mqtt/__init__.py Outdated Show resolved Hide resolved
homeassistant/components/mqtt/discovery.py Show resolved Hide resolved
homeassistant/components/mqtt/discovery.py Outdated Show resolved Hide resolved
@emontnemery
Copy link
Contributor Author

@OttoWinter Thanks a lot for reviewing! I added backwards compatibility by replacing platform in the discovery message with schema as you suggest.

@emontnemery
Copy link
Contributor Author

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).
Should the documentation source files also be renamed in some way, or it does not matter?

@balloob
Copy link
Member

balloob commented Nov 27, 2018

Docs should be renamed to just be light.mqtt. You can add redirect_from to make the old urls redirect to the new page.

@balloob balloob merged commit 16e3ff2 into home-assistant:dev Nov 27, 2018
@ghost ghost removed the in progress label Nov 27, 2018
balloob pushed a commit to home-assistant/home-assistant.io that referenced this pull request Nov 27, 2018
* Documentation for home-assistant/core#18227

* Merge documentation for the three shcemas
@emontnemery emontnemery deleted the mqtt_light_refactor branch December 2, 2018 16:29
debsahu added a commit to toblum/McLighting that referenced this pull request Dec 11, 2018
@balloob balloob mentioned this pull request Dec 12, 2018
@ghost ghost removed the platform: light.mqtt label Mar 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants