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

Move more MQTT platforms to config entries #18180

Merged
merged 5 commits into from
Nov 6, 2018

Conversation

emontnemery
Copy link
Contributor

@emontnemery emontnemery commented Nov 4, 2018

Description:

#16904, but for this new platform:

  • lock

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox.


async def _async_setup_entity(hass, config, async_add_entities,
discovery_hash=None):
"""Set up the MQTT JSON Light."""
Copy link
Member

@OttoWinter OttoWinter Nov 4, 2018

Choose a reason for hiding this comment

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

Have you tested this? I believe the setup entry will never be called because the config entry is forwarded to the platform with the same name as the component (mqtt).

So all light.mqtt_json payloads will be redirected to light.mqtt.

That's a limitation of the config entry system and we should not change the config entry core just to work around this quirk.

There's been some discussion before (can't find the link rn), but the conclusion was that the light.mqtt platform would need to redirect the entity setup to mqtt_json if the platform doesn't match.

Copy link
Contributor Author

@emontnemery emontnemery Nov 4, 2018

Choose a reason for hiding this comment

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

I've only tested the unit tests, I'll do some "real" testing too.

Edit: Fixed now, thanks!

@emontnemery
Copy link
Contributor Author

emontnemery commented Nov 4, 2018

@OttoWinter Updated, I hope it's what you intended.

from homeassistant.components.light.mqtt_json import (
PLATFORM_SCHEMA as PLATFORM_SCHEMA_JSON)
from homeassistant.components.light.mqtt_json import (
_async_setup_entity as _async_setup_entity_json)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe consider using the get_platform import instead of this absolute one to support loading custom_components. I don't know how much this is enforced, but it would be best to call:

# in async_discover
if discovery_payload['platform'] == 'mqtt_json':
    mqtt_json = get_platform(hass, 'light', 'mqtt_json')
    config = mqtt_json.PLATFORM_SCHEMA_JSON(discovery_payload)
    await mqtt_json._async_setup_entity_json(...)

See https://github.com/home-assistant/home-assistant/blob/ee696643cd7c63bf0cc88f5111c64b6e2d836640/homeassistant/loader.py#L8-L11

Copy link
Contributor Author

@emontnemery emontnemery Nov 4, 2018

Choose a reason for hiding this comment

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

Great, thanks!
Edit: Implemented as you suggested.

@@ -244,15 +245,15 @@
hass, component, platform, payload, hass_config)
return

config_entries_key = '{}.{}'.format(component, platform)
config_entries_key = '{}.{}'.format(component, 'mqtt')
Copy link
Member

Choose a reason for hiding this comment

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

Revert this to platform?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, can't be reverted due to the limitations in the config entry system as you pointed out.

homeassistant/setup.py Outdated Show resolved Hide resolved
tests/components/light/test_mqtt_json.py Outdated Show resolved Hide resolved
discovery_payload[ATTR_DISCOVERY_HASH])

if discovery_payload['platform'] == 'mqtt_json':
mqtt_json = get_platform(hass, 'light', 'mqtt_json')
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 wrong. Platforms should not know about other platforms.

We have two choices:

  • don't support mqtt_json with config entries
  • move MQTT JSON into the MQTT platform and add a config to switch to "mode". To keep code manageable we could move the files so we have light/mqtt/__init__.py and light/mqtt/json.py

Copy link
Contributor Author

@emontnemery emontnemery Nov 4, 2018

Choose a reason for hiding this comment

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

Hmm, too bad, I'll remove the mqtt_json changes from this PR.

There's a third MQTT light variant. mqtt_template, which has the same issue then.

When you say, "move MQTT JSON into the MQTT platform", does that affect the configuration.yaml entry?

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

Edit: As explained by @balloob on the chat, yes, that would affect the configuration and be a breaking change.

@emontnemery emontnemery mentioned this pull request Nov 5, 2018
3 tasks
@cgarwood cgarwood mentioned this pull request Nov 6, 2018
2 tasks
@balloob balloob merged commit 5897649 into home-assistant:dev Nov 6, 2018
@ghost ghost removed the in progress label Nov 6, 2018
@balloob balloob mentioned this pull request Nov 29, 2018
@emontnemery emontnemery deleted the mqtt_discovery_info branch December 2, 2018 16:38
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.

4 participants