Skip to content

Commit

Permalink
Fix potential MQTT discovery race condition (#17208)
Browse files Browse the repository at this point in the history
* Fix potential MQTT discovery race condition

* Rename data key
  • Loading branch information
OttoWinter authored and balloob committed Oct 8, 2018
1 parent ee5e1fa commit 3f498bd
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions homeassistant/components/mqtt/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/mqtt/#discovery
"""
import asyncio
import json
import logging
import re
Expand Down Expand Up @@ -51,6 +52,7 @@
}

ALREADY_DISCOVERED = 'mqtt_discovered_components'
DATA_CONFIG_ENTRY_LOCK = 'mqtt_config_entry_lock'
CONFIG_ENTRY_IS_SETUP = 'mqtt_config_entry_is_setup'
MQTT_DISCOVERY_UPDATED = 'mqtt_discovery_updated_{}'
MQTT_DISCOVERY_NEW = 'mqtt_discovery_new_{}_{}'
Expand Down Expand Up @@ -119,14 +121,16 @@ async def async_device_message_received(topic, payload, qos):
return

config_entries_key = '{}.{}'.format(component, platform)
if config_entries_key not in hass.data[CONFIG_ENTRY_IS_SETUP]:
hass.data[CONFIG_ENTRY_IS_SETUP].add(config_entries_key)
await hass.config_entries.async_forward_entry_setup(
config_entry, component)
async with hass.data[DATA_CONFIG_ENTRY_LOCK]:
if config_entries_key not in hass.data[CONFIG_ENTRY_IS_SETUP]:
await hass.config_entries.async_forward_entry_setup(
config_entry, component)
hass.data[CONFIG_ENTRY_IS_SETUP].add(config_entries_key)

async_dispatcher_send(hass, MQTT_DISCOVERY_NEW.format(
component, platform), payload)

hass.data[DATA_CONFIG_ENTRY_LOCK] = asyncio.Lock()
hass.data[CONFIG_ENTRY_IS_SETUP] = set()

await mqtt.async_subscribe(
Expand Down

0 comments on commit 3f498bd

Please sign in to comment.