home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
71.41k stars 29.9k forks source link

MQTT Discovery broken after merge of #16904 / #16918 #16968

Closed emontnemery closed 5 years ago

emontnemery commented 5 years ago

MQTT Discovery broken after merge of #16904 + 16918

Home Assistant release with the issue: SHA: af89e7c50f65afb233341525eb81d6dcc6ba599f

Last working Home Assistant release (if known): 720b05c301f1a22128839b71e8589b26623c33a1

Operating environment (Hass.io/Docker/Windows/etc.): WSL

Component/platform: MQTT Discovery

Description of problem: MQTT Discovery fails to discover more than one device per platform.

Reproduction steps:

  1. Publish retained discovery topics:
    mosquitto_pub -h <your_broker_here> -r -m '{"name":"Lab BTN","state_topic":"tasmota/sonoff_BTN_A1AE96/cmnd/POWER","payload_on":"TOGGLE","availability_topic":"tasmota/sonoff_A1AE96/tele/LWT","payload_available":"Online","payload_not_available":"Offline","force_update":true}' -t 'homeassistant2/binary_sensor/sonoff_BTN_A1AE96_1/config'
    mosquitto_pub -h <your_broker_here> -r -m '{"name":"Multical 402 BTN","state_topic":"tasmota/sonoff_BTN_0FAC39/cmnd/POWER","payload_on":"TOGGLE","availability_topic":"tasmota/sonoff_0FAC39/tele/LWT","payload_available":"Online","payload_not_available":"Offline"}' -t 'homeassistant2/binary_sensor/sonoff_BTN_0FAC39_1/config'
  2. Start hass: One of the binary sensors is discovered, the 2nd one errors as below

Problem-relevant configuration.yaml entries and (fill out even if it seems unimportant):

mqtt:
  broker: <your_broker_here>
  discovery: true
  discovery_prefix: homeassistant2

Traceback (if applicable):

2018-09-29 19:04:54 ERROR (MainThread) [homeassistant.config_entries] Error setting up entry configuration.yaml for binary_sensor
Traceback (most recent call last):
  File "/mnt/d/development/github/home-assistant_fork/homeassistant/config_entries.py", line 225, in async_setup
    result = await component.async_setup_entry(hass, self)
  File "/mnt/d/development/github/home-assistant_fork/homeassistant/components/binary_sensor/__init__.py", line 62, in async_setup_entry
    return await hass.data[DOMAIN].async_setup_entry(entry)
  File "/mnt/d/development/github/home-assistant_fork/homeassistant/helpers/entity_component.py", line 108, in async_setup_entry
    raise ValueError('Config entry has already been setup!')
ValueError: Config entry has already been setup!
emontnemery commented 5 years ago

The problem is these lines in async_device_message_received in mqtt/discovery.py:

            if platform not in CONFIG_ENTRY_PLATFORMS.get(component, []):
                await async_load_platform(
                    hass, component, platform, payload, hass_config)
                return

This will cause all but the 1st device that is discovered before async_load_platform() is finished to fail. @balloob How to fix this in a proper way?