1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-25 05:26:47 +00:00

Cleanup if discovered mqtt binary_sensor can't be added

This commit is contained in:
emontnemery
2019-01-03 15:48:36 +01:00
committed by Erik
parent dd75c49796
commit d8370f44cb
2 changed files with 45 additions and 5 deletions

View File

@@ -20,7 +20,8 @@ from homeassistant.components.mqtt import (
CONF_PAYLOAD_AVAILABLE, CONF_PAYLOAD_NOT_AVAILABLE, CONF_QOS,
MqttAttributes, MqttAvailability, MqttDiscoveryUpdate,
MqttEntityDeviceInfo, subscription)
from homeassistant.components.mqtt.discovery import MQTT_DISCOVERY_NEW
from homeassistant.components.mqtt.discovery import (
ALREADY_DISCOVERED, MQTT_DISCOVERY_NEW)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect
import homeassistant.helpers.event as evt
@@ -63,9 +64,15 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up MQTT binary sensor dynamically through MQTT discovery."""
async def async_discover(discovery_payload):
"""Discover and add a MQTT binary sensor."""
config = PLATFORM_SCHEMA(discovery_payload)
await _async_setup_entity(config, async_add_entities,
discovery_payload[ATTR_DISCOVERY_HASH])
try:
discovery_hash = discovery_payload[ATTR_DISCOVERY_HASH]
config = PLATFORM_SCHEMA(discovery_payload)
await _async_setup_entity(config, async_add_entities,
discovery_hash)
except: # noqa: E722
if discovery_hash:
del hass.data[ALREADY_DISCOVERED][discovery_hash]
raise
async_dispatcher_connect(
hass, MQTT_DISCOVERY_NEW.format(binary_sensor.DOMAIN, 'mqtt'),