Cleanup if discovered mqtt vacuum can't be added (#20549)

This commit is contained in:
emontnemery 2019-01-29 00:56:47 +01:00 committed by Paulus Schoutsen
parent bd335e1ac1
commit bb4ca1f525
2 changed files with 44 additions and 4 deletions

View file

@ -12,7 +12,8 @@ from homeassistant.components import mqtt
from homeassistant.components.mqtt import ( from homeassistant.components.mqtt import (
ATTR_DISCOVERY_HASH, CONF_UNIQUE_ID, MqttAttributes, MqttAvailability, ATTR_DISCOVERY_HASH, CONF_UNIQUE_ID, MqttAttributes, MqttAvailability,
MqttDiscoveryUpdate, MqttEntityDeviceInfo, subscription) MqttDiscoveryUpdate, MqttEntityDeviceInfo, subscription)
from homeassistant.components.mqtt.discovery import MQTT_DISCOVERY_NEW from homeassistant.components.mqtt.discovery import (
MQTT_DISCOVERY_NEW, clear_discovery_hash)
from homeassistant.components.vacuum import ( from homeassistant.components.vacuum import (
DOMAIN, SUPPORT_BATTERY, SUPPORT_CLEAN_SPOT, SUPPORT_FAN_SPEED, DOMAIN, SUPPORT_BATTERY, SUPPORT_CLEAN_SPOT, SUPPORT_FAN_SPEED,
SUPPORT_LOCATE, SUPPORT_PAUSE, SUPPORT_RETURN_HOME, SUPPORT_SEND_COMMAND, SUPPORT_LOCATE, SUPPORT_PAUSE, SUPPORT_RETURN_HOME, SUPPORT_SEND_COMMAND,
@ -160,9 +161,15 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up MQTT vacuum dynamically through MQTT discovery.""" """Set up MQTT vacuum dynamically through MQTT discovery."""
async def async_discover(discovery_payload): async def async_discover(discovery_payload):
"""Discover and add a MQTT vacuum.""" """Discover and add a MQTT vacuum."""
config = PLATFORM_SCHEMA(discovery_payload) try:
await _async_setup_entity(config, async_add_entities, config_entry, discovery_hash = discovery_payload[ATTR_DISCOVERY_HASH]
discovery_payload[ATTR_DISCOVERY_HASH]) config = PLATFORM_SCHEMA(discovery_payload)
await _async_setup_entity(config, async_add_entities, config_entry,
discovery_hash)
except Exception:
if discovery_hash:
clear_discovery_hash(hass, discovery_hash)
raise
async_dispatcher_connect( async_dispatcher_connect(
hass, MQTT_DISCOVERY_NEW.format(DOMAIN, 'mqtt'), async_discover) hass, MQTT_DISCOVERY_NEW.format(DOMAIN, 'mqtt'), async_discover)

View file

@ -299,6 +299,39 @@ async def test_discovery_removal_vacuum(hass, mock_publish):
assert state is None assert state is None
async def test_discovery_broken(hass, mqtt_mock, caplog):
"""Test handling of bad discovery message."""
entry = MockConfigEntry(domain=mqtt.DOMAIN)
await async_start(hass, 'homeassistant', {}, entry)
data1 = (
'{ "name": "Beer",'
' "command_topic": "test_topic#" }'
)
data2 = (
'{ "name": "Milk",'
' "command_topic": "test_topic" }'
)
async_fire_mqtt_message(hass, 'homeassistant/vacuum/bla/config',
data1)
await hass.async_block_till_done()
state = hass.states.get('vacuum.beer')
assert state is None
async_fire_mqtt_message(hass, 'homeassistant/vacuum/bla/config',
data2)
await hass.async_block_till_done()
await hass.async_block_till_done()
state = hass.states.get('vacuum.milk')
assert state is not None
assert state.name == 'Milk'
state = hass.states.get('vacuum.beer')
assert state is None
async def test_discovery_update_vacuum(hass, mock_publish): async def test_discovery_update_vacuum(hass, mock_publish):
"""Test update of discovered vacuum.""" """Test update of discovered vacuum."""
entry = MockConfigEntry(domain=mqtt.DOMAIN) entry = MockConfigEntry(domain=mqtt.DOMAIN)