From 0cea54cea1b1c3d971913128c94c8b2668b89a84 Mon Sep 17 00:00:00 2001 From: emontnemery Date: Tue, 8 Jan 2019 16:53:02 +0100 Subject: [PATCH] Cleanup if discovered mqtt climate can't be added (#19739) * Cleanup if discovered mqtt climate can't be added --- homeassistant/components/climate/mqtt.py | 15 +++++++--- tests/components/climate/test_mqtt.py | 35 +++++++++++++++++++++++- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/climate/mqtt.py b/homeassistant/components/climate/mqtt.py index 96de0709dc8..3219b71c1bd 100644 --- a/homeassistant/components/climate/mqtt.py +++ b/homeassistant/components/climate/mqtt.py @@ -25,7 +25,8 @@ from homeassistant.components.mqtt import ( CONF_PAYLOAD_AVAILABLE, CONF_PAYLOAD_NOT_AVAILABLE, MQTT_BASE_PLATFORM_SCHEMA, MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo, subscription) -from homeassistant.components.mqtt.discovery import MQTT_DISCOVERY_NEW +from homeassistant.components.mqtt.discovery import ( + MQTT_DISCOVERY_NEW, clear_discovery_hash) import homeassistant.helpers.config_validation as cv from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.typing import HomeAssistantType, ConfigType @@ -158,9 +159,15 @@ async def async_setup_entry(hass, config_entry, async_add_entities): """Set up MQTT climate device dynamically through MQTT discovery.""" async def async_discover(discovery_payload): """Discover and add a MQTT climate device.""" - config = PLATFORM_SCHEMA(discovery_payload) - await _async_setup_entity(hass, 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(hass, config, async_add_entities, + discovery_hash) + except Exception: + if discovery_hash: + clear_discovery_hash(hass, discovery_hash) + raise async_dispatcher_connect( hass, MQTT_DISCOVERY_NEW.format(climate.DOMAIN, 'mqtt'), diff --git a/tests/components/climate/test_mqtt.py b/tests/components/climate/test_mqtt.py index 1fd45701a95..a2aa424eeee 100644 --- a/tests/components/climate/test_mqtt.py +++ b/tests/components/climate/test_mqtt.py @@ -719,7 +719,7 @@ async def test_discovery_removal_climate(hass, mqtt_mock, caplog): async def test_discovery_update_climate(hass, mqtt_mock, caplog): - """Test removal of discovered climate.""" + """Test update of discovered climate.""" entry = MockConfigEntry(domain=mqtt.DOMAIN) await async_start(hass, 'homeassistant', {}, entry) data1 = ( @@ -749,6 +749,39 @@ async def test_discovery_update_climate(hass, mqtt_mock, caplog): 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",' + ' "power_command_topic": "test_topic#" }' + ) + data2 = ( + '{ "name": "Milk", ' + ' "power_command_topic": "test_topic" }' + ) + + async_fire_mqtt_message(hass, 'homeassistant/climate/bla/config', + data1) + await hass.async_block_till_done() + + state = hass.states.get('climate.beer') + assert state is None + + async_fire_mqtt_message(hass, 'homeassistant/climate/bla/config', + data2) + await hass.async_block_till_done() + await hass.async_block_till_done() + + state = hass.states.get('climate.milk') + assert state is not None + assert state.name == 'Milk' + state = hass.states.get('climate.beer') + assert state is None + + async def test_entity_device_info_with_identifier(hass, mqtt_mock): """Test MQTT climate device registry integration.""" entry = MockConfigEntry(domain=mqtt.DOMAIN)