From 4501bdb4a0dd7dccb1bde69e15c1878d2c4480a8 Mon Sep 17 00:00:00 2001 From: emontnemery Date: Tue, 25 Sep 2018 19:32:42 +0200 Subject: [PATCH] Remove discovered MQTT cover device when discovery topic is cleared (#16857) --- homeassistant/components/cover/mqtt.py | 26 +++++++++++----- tests/components/cover/test_mqtt.py | 41 ++++++++++++++++++++------ 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/cover/mqtt.py b/homeassistant/components/cover/mqtt.py index 977353cb318..ae1162f7120 100644 --- a/homeassistant/components/cover/mqtt.py +++ b/homeassistant/components/cover/mqtt.py @@ -20,9 +20,10 @@ from homeassistant.const import ( CONF_NAME, CONF_VALUE_TEMPLATE, CONF_OPTIMISTIC, STATE_OPEN, STATE_CLOSED, STATE_UNKNOWN) from homeassistant.components.mqtt import ( - CONF_AVAILABILITY_TOPIC, CONF_STATE_TOPIC, CONF_COMMAND_TOPIC, - CONF_PAYLOAD_AVAILABLE, CONF_PAYLOAD_NOT_AVAILABLE, CONF_QOS, CONF_RETAIN, - valid_publish_topic, valid_subscribe_topic, MqttAvailability) + ATTR_DISCOVERY_HASH, CONF_AVAILABILITY_TOPIC, CONF_STATE_TOPIC, + CONF_COMMAND_TOPIC, CONF_PAYLOAD_AVAILABLE, CONF_PAYLOAD_NOT_AVAILABLE, + CONF_QOS, CONF_RETAIN, valid_publish_topic, valid_subscribe_topic, + MqttAvailability, MqttDiscoveryUpdate) import homeassistant.helpers.config_validation as cv _LOGGER = logging.getLogger(__name__) @@ -105,6 +106,10 @@ async def async_setup_platform(hass, config, async_add_entities, if set_position_template is not None: set_position_template.hass = hass + discovery_hash = None + if discovery_info is not None and ATTR_DISCOVERY_HASH in discovery_info: + discovery_hash = discovery_info[ATTR_DISCOVERY_HASH] + async_add_entities([MqttCover( config.get(CONF_NAME), config.get(CONF_STATE_TOPIC), @@ -131,10 +136,11 @@ async def async_setup_platform(hass, config, async_add_entities, config.get(CONF_TILT_INVERT_STATE), config.get(CONF_POSITION_TOPIC), set_position_template, + discovery_hash, )]) -class MqttCover(MqttAvailability, CoverDevice): +class MqttCover(MqttAvailability, MqttDiscoveryUpdate, CoverDevice): """Representation of a cover that can be controlled using MQTT.""" def __init__(self, name, state_topic, command_topic, availability_topic, @@ -143,10 +149,12 @@ class MqttCover(MqttAvailability, CoverDevice): payload_stop, payload_available, payload_not_available, optimistic, value_template, tilt_open_position, tilt_closed_position, tilt_min, tilt_max, tilt_optimistic, - tilt_invert, position_topic, set_position_template): + tilt_invert, position_topic, set_position_template, + discovery_hash): """Initialize the cover.""" - super().__init__(availability_topic, qos, payload_available, - payload_not_available) + MqttAvailability.__init__(self, availability_topic, qos, + payload_available, payload_not_available) + MqttDiscoveryUpdate.__init__(self, discovery_hash) self._position = None self._state = None self._name = name @@ -172,10 +180,12 @@ class MqttCover(MqttAvailability, CoverDevice): self._tilt_invert = tilt_invert self._position_topic = position_topic self._set_position_template = set_position_template + self._discovery_hash = discovery_hash async def async_added_to_hass(self): """Subscribe MQTT events.""" - await super().async_added_to_hass() + await MqttAvailability.async_added_to_hass(self) + await MqttDiscoveryUpdate.async_added_to_hass(self) @callback def tilt_updated(topic, payload, qos): diff --git a/tests/components/cover/test_mqtt.py b/tests/components/cover/test_mqtt.py index ad68c2416ca..e75178bf3d7 100644 --- a/tests/components/cover/test_mqtt.py +++ b/tests/components/cover/test_mqtt.py @@ -6,9 +6,11 @@ from homeassistant.const import STATE_OPEN, STATE_CLOSED, STATE_UNKNOWN, \ STATE_UNAVAILABLE, ATTR_ASSUMED_STATE import homeassistant.components.cover as cover from homeassistant.components.cover.mqtt import MqttCover +from homeassistant.components.mqtt.discovery import async_start from tests.common import ( - get_test_home_assistant, mock_mqtt_component, fire_mqtt_message) + get_test_home_assistant, mock_mqtt_component, async_fire_mqtt_message, + fire_mqtt_message) class TestCoverMQTT(unittest.TestCase): @@ -579,7 +581,7 @@ class TestCoverMQTT(unittest.TestCase): 'cover.test', 'state-topic', 'command-topic', None, 'tilt-command-topic', 'tilt-status-topic', 0, False, 'OPEN', 'CLOSE', 'OPEN', 'CLOSE', 'STOP', None, None, - False, None, 100, 0, 0, 100, False, False, None, None) + False, None, 100, 0, 0, 100, False, False, None, None, None) self.assertEqual(44, mqtt_cover.find_percentage_in_range(44)) @@ -589,7 +591,7 @@ class TestCoverMQTT(unittest.TestCase): 'cover.test', 'state-topic', 'command-topic', None, 'tilt-command-topic', 'tilt-status-topic', 0, False, 'OPEN', 'CLOSE', 'OPEN', 'CLOSE', 'STOP', None, None, - False, None, 180, 80, 80, 180, False, False, None, None) + False, None, 180, 80, 80, 180, False, False, None, None, None) self.assertEqual(40, mqtt_cover.find_percentage_in_range(120)) @@ -599,7 +601,7 @@ class TestCoverMQTT(unittest.TestCase): 'cover.test', 'state-topic', 'command-topic', None, 'tilt-command-topic', 'tilt-status-topic', 0, False, 'OPEN', 'CLOSE', 'OPEN', 'CLOSE', 'STOP', None, None, - False, None, 100, 0, 0, 100, False, True, None, None) + False, None, 100, 0, 0, 100, False, True, None, None, None) self.assertEqual(56, mqtt_cover.find_percentage_in_range(44)) @@ -609,7 +611,7 @@ class TestCoverMQTT(unittest.TestCase): 'cover.test', 'state-topic', 'command-topic', None, 'tilt-command-topic', 'tilt-status-topic', 0, False, 'OPEN', 'CLOSE', 'OPEN', 'CLOSE', 'STOP', None, None, - False, None, 180, 80, 80, 180, False, True, None, None) + False, None, 180, 80, 80, 180, False, True, None, None, None) self.assertEqual(60, mqtt_cover.find_percentage_in_range(120)) @@ -619,7 +621,7 @@ class TestCoverMQTT(unittest.TestCase): 'cover.test', 'state-topic', 'command-topic', None, 'tilt-command-topic', 'tilt-status-topic', 0, False, 'OPEN', 'CLOSE', 'OPEN', 'CLOSE', 'STOP', None, None, - False, None, 100, 0, 0, 100, False, False, None, None) + False, None, 100, 0, 0, 100, False, False, None, None, None) self.assertEqual(44, mqtt_cover.find_in_range_from_percent(44)) @@ -629,7 +631,7 @@ class TestCoverMQTT(unittest.TestCase): 'cover.test', 'state-topic', 'command-topic', None, 'tilt-command-topic', 'tilt-status-topic', 0, False, 'OPEN', 'CLOSE', 'OPEN', 'CLOSE', 'STOP', None, None, - False, None, 180, 80, 80, 180, False, False, None, None) + False, None, 180, 80, 80, 180, False, False, None, None, None) self.assertEqual(120, mqtt_cover.find_in_range_from_percent(40)) @@ -639,7 +641,7 @@ class TestCoverMQTT(unittest.TestCase): 'cover.test', 'state-topic', 'command-topic', None, 'tilt-command-topic', 'tilt-status-topic', 0, False, 'OPEN', 'CLOSE', 'OPEN', 'CLOSE', 'STOP', None, None, - False, None, 100, 0, 0, 100, False, True, None, None) + False, None, 100, 0, 0, 100, False, True, None, None, None) self.assertEqual(44, mqtt_cover.find_in_range_from_percent(56)) @@ -649,7 +651,7 @@ class TestCoverMQTT(unittest.TestCase): 'cover.test', 'state-topic', 'command-topic', None, 'tilt-command-topic', 'tilt-status-topic', 0, False, 'OPEN', 'CLOSE', 'OPEN', 'CLOSE', 'STOP', None, None, - False, None, 180, 80, 80, 180, False, True, None, None) + False, None, 180, 80, 80, 180, False, True, None, None, None) self.assertEqual(120, mqtt_cover.find_in_range_from_percent(60)) @@ -722,3 +724,24 @@ class TestCoverMQTT(unittest.TestCase): state = self.hass.states.get('cover.test') self.assertEqual(STATE_UNAVAILABLE, state.state) + + +async def test_discovery_removal_cover(hass, mqtt_mock, caplog): + """Test removal of discovered cover.""" + await async_start(hass, 'homeassistant', {}) + data = ( + '{ "name": "Beer",' + ' "command_topic": "test_topic" }' + ) + async_fire_mqtt_message(hass, 'homeassistant/cover/bla/config', + data) + await hass.async_block_till_done() + state = hass.states.get('cover.beer') + assert state is not None + assert state.name == 'Beer' + async_fire_mqtt_message(hass, 'homeassistant/cover/bla/config', + '') + await hass.async_block_till_done() + await hass.async_block_till_done() + state = hass.states.get('cover.beer') + assert state is None