From 9508c5140306b47e5b821951d41bdc047c982e48 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 30 Mar 2020 23:26:59 +0200 Subject: [PATCH] Fix change of entity_id for discovered MQTT entities (#33444) --- homeassistant/components/mqtt/__init__.py | 12 +++-- homeassistant/components/mqtt/discovery.py | 5 ++ .../mqtt/test_alarm_control_panel.py | 16 +++++-- tests/components/mqtt/test_binary_sensor.py | 16 +++++-- tests/components/mqtt/test_camera.py | 16 +++++-- tests/components/mqtt/test_climate.py | 16 +++++-- .../mqtt/{common.py => test_common.py} | 48 ++++++++++++++++++- tests/components/mqtt/test_cover.py | 18 +++++-- tests/components/mqtt/test_fan.py | 18 +++++-- tests/components/mqtt/test_legacy_vacuum.py | 16 +++++-- tests/components/mqtt/test_light.py | 18 +++++-- tests/components/mqtt/test_light_json.py | 18 +++++-- tests/components/mqtt/test_light_template.py | 18 +++++-- tests/components/mqtt/test_lock.py | 18 +++++-- tests/components/mqtt/test_sensor.py | 18 +++++-- tests/components/mqtt/test_state_vacuum.py | 18 +++++-- tests/components/mqtt/test_switch.py | 18 +++++-- 17 files changed, 246 insertions(+), 61 deletions(-) rename tests/components/mqtt/{common.py => test_common.py} (90%) diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index 90dd21ae307..ee14fe432b5 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -56,7 +56,7 @@ from .const import ( DEFAULT_QOS, PROTOCOL_311, ) -from .discovery import MQTT_DISCOVERY_UPDATED, clear_discovery_hash +from .discovery import MQTT_DISCOVERY_UPDATED, clear_discovery_hash, set_discovery_hash from .models import Message, MessageCallbackType, PublishPayloadType from .subscription import async_subscribe_topics, async_unsubscribe_topics @@ -1181,10 +1181,12 @@ class MqttDiscoveryUpdate(Entity): self._discovery_data = discovery_data self._discovery_update = discovery_update self._remove_signal = None + self._removed_from_hass = False async def async_added_to_hass(self) -> None: """Subscribe to discovery updates.""" await super().async_added_to_hass() + self._removed_from_hass = False discovery_hash = ( self._discovery_data[ATTR_DISCOVERY_HASH] if self._discovery_data else None ) @@ -1217,6 +1219,8 @@ class MqttDiscoveryUpdate(Entity): await self._discovery_update(payload) if discovery_hash: + # Set in case the entity has been removed and is re-added + set_discovery_hash(self.hass, discovery_hash) self._remove_signal = async_dispatcher_connect( self.hass, MQTT_DISCOVERY_UPDATED.format(discovery_hash), @@ -1225,7 +1229,7 @@ class MqttDiscoveryUpdate(Entity): async def async_removed_from_registry(self) -> None: """Clear retained discovery topic in broker.""" - if self._discovery_data: + if not self._removed_from_hass: discovery_topic = self._discovery_data[ATTR_DISCOVERY_TOPIC] publish( self.hass, discovery_topic, "", retain=True, @@ -1237,9 +1241,9 @@ class MqttDiscoveryUpdate(Entity): def _cleanup_on_remove(self) -> None: """Stop listening to signal and cleanup discovery data.""" - if self._discovery_data: + if self._discovery_data and not self._removed_from_hass: clear_discovery_hash(self.hass, self._discovery_data[ATTR_DISCOVERY_HASH]) - self._discovery_data = None + self._removed_from_hass = True if self._remove_signal: self._remove_signal() diff --git a/homeassistant/components/mqtt/discovery.py b/homeassistant/components/mqtt/discovery.py index ac20ba7a4a8..3bcd8594ebe 100644 --- a/homeassistant/components/mqtt/discovery.py +++ b/homeassistant/components/mqtt/discovery.py @@ -64,6 +64,11 @@ def clear_discovery_hash(hass, discovery_hash): del hass.data[ALREADY_DISCOVERED][discovery_hash] +def set_discovery_hash(hass, discovery_hash): + """Clear entry in ALREADY_DISCOVERED list.""" + hass.data[ALREADY_DISCOVERED][discovery_hash] = {} + + class MQTTConfig(dict): """Dummy class to allow adding attributes.""" diff --git a/tests/components/mqtt/test_alarm_control_panel.py b/tests/components/mqtt/test_alarm_control_panel.py index 93036335e16..45c123fa2fe 100644 --- a/tests/components/mqtt/test_alarm_control_panel.py +++ b/tests/components/mqtt/test_alarm_control_panel.py @@ -13,7 +13,7 @@ from homeassistant.const import ( STATE_UNKNOWN, ) -from .common import ( +from .test_common import ( help_test_availability_without_topic, help_test_custom_availability_payload, help_test_default_availability_payload, @@ -25,7 +25,8 @@ from .common import ( help_test_entity_device_info_update, help_test_entity_device_info_with_connection, help_test_entity_device_info_with_identifier, - help_test_entity_id_update, + help_test_entity_id_update_discovery_update, + help_test_entity_id_update_subscriptions, help_test_setting_attribute_via_mqtt_json_message, help_test_setting_attribute_with_template, help_test_unique_id, @@ -481,8 +482,15 @@ async def test_entity_device_info_remove(hass, mqtt_mock): ) -async def test_entity_id_update(hass, mqtt_mock): +async def test_entity_id_update_subscriptions(hass, mqtt_mock): """Test MQTT subscriptions are managed when entity_id is updated.""" - await help_test_entity_id_update( + await help_test_entity_id_update_subscriptions( + hass, mqtt_mock, alarm_control_panel.DOMAIN, DEFAULT_CONFIG + ) + + +async def test_entity_id_update_discovery_update(hass, mqtt_mock): + """Test MQTT discovery update when entity_id is updated.""" + await help_test_entity_id_update_discovery_update( hass, mqtt_mock, alarm_control_panel.DOMAIN, DEFAULT_CONFIG ) diff --git a/tests/components/mqtt/test_binary_sensor.py b/tests/components/mqtt/test_binary_sensor.py index 7b104089073..a73919844c1 100644 --- a/tests/components/mqtt/test_binary_sensor.py +++ b/tests/components/mqtt/test_binary_sensor.py @@ -15,7 +15,7 @@ import homeassistant.core as ha from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util -from .common import ( +from .test_common import ( help_test_availability_without_topic, help_test_custom_availability_payload, help_test_default_availability_payload, @@ -27,7 +27,8 @@ from .common import ( help_test_entity_device_info_update, help_test_entity_device_info_with_connection, help_test_entity_device_info_with_identifier, - help_test_entity_id_update, + help_test_entity_id_update_discovery_update, + help_test_entity_id_update_subscriptions, help_test_setting_attribute_via_mqtt_json_message, help_test_setting_attribute_with_template, help_test_unique_id, @@ -495,8 +496,15 @@ async def test_entity_device_info_remove(hass, mqtt_mock): ) -async def test_entity_id_update(hass, mqtt_mock): +async def test_entity_id_update_subscriptions(hass, mqtt_mock): """Test MQTT subscriptions are managed when entity_id is updated.""" - await help_test_entity_id_update( + await help_test_entity_id_update_subscriptions( + hass, mqtt_mock, binary_sensor.DOMAIN, DEFAULT_CONFIG + ) + + +async def test_entity_id_update_discovery_update(hass, mqtt_mock): + """Test MQTT discovery update when entity_id is updated.""" + await help_test_entity_id_update_discovery_update( hass, mqtt_mock, binary_sensor.DOMAIN, DEFAULT_CONFIG ) diff --git a/tests/components/mqtt/test_camera.py b/tests/components/mqtt/test_camera.py index 96ea9b3005f..21f552b4163 100644 --- a/tests/components/mqtt/test_camera.py +++ b/tests/components/mqtt/test_camera.py @@ -5,7 +5,7 @@ from homeassistant.components import camera, mqtt from homeassistant.components.mqtt.discovery import async_start from homeassistant.setup import async_setup_component -from .common import ( +from .test_common import ( help_test_availability_without_topic, help_test_custom_availability_payload, help_test_default_availability_payload, @@ -17,7 +17,8 @@ from .common import ( help_test_entity_device_info_update, help_test_entity_device_info_with_connection, help_test_entity_device_info_with_identifier, - help_test_entity_id_update, + help_test_entity_id_update_discovery_update, + help_test_entity_id_update_subscriptions, help_test_setting_attribute_via_mqtt_json_message, help_test_setting_attribute_with_template, help_test_unique_id, @@ -194,8 +195,15 @@ async def test_entity_device_info_remove(hass, mqtt_mock): ) -async def test_entity_id_update(hass, mqtt_mock): +async def test_entity_id_update_subscriptions(hass, mqtt_mock): """Test MQTT subscriptions are managed when entity_id is updated.""" - await help_test_entity_id_update( + await help_test_entity_id_update_subscriptions( hass, mqtt_mock, camera.DOMAIN, DEFAULT_CONFIG, ["test_topic"] ) + + +async def test_entity_id_update_discovery_update(hass, mqtt_mock): + """Test MQTT discovery update when entity_id is updated.""" + await help_test_entity_id_update_discovery_update( + hass, mqtt_mock, camera.DOMAIN, DEFAULT_CONFIG + ) diff --git a/tests/components/mqtt/test_climate.py b/tests/components/mqtt/test_climate.py index 29f97af7725..ce21aa53d27 100644 --- a/tests/components/mqtt/test_climate.py +++ b/tests/components/mqtt/test_climate.py @@ -25,7 +25,7 @@ from homeassistant.components.climate.const import ( ) from homeassistant.const import STATE_OFF -from .common import ( +from .test_common import ( help_test_availability_without_topic, help_test_custom_availability_payload, help_test_default_availability_payload, @@ -37,7 +37,8 @@ from .common import ( help_test_entity_device_info_update, help_test_entity_device_info_with_connection, help_test_entity_device_info_with_identifier, - help_test_entity_id_update, + help_test_entity_id_update_discovery_update, + help_test_entity_id_update_subscriptions, help_test_setting_attribute_via_mqtt_json_message, help_test_setting_attribute_with_template, help_test_unique_id, @@ -885,7 +886,7 @@ async def test_entity_device_info_remove(hass, mqtt_mock): ) -async def test_entity_id_update(hass, mqtt_mock): +async def test_entity_id_update_subscriptions(hass, mqtt_mock): """Test MQTT subscriptions are managed when entity_id is updated.""" config = { CLIMATE_DOMAIN: { @@ -895,11 +896,18 @@ async def test_entity_id_update(hass, mqtt_mock): "availability_topic": "avty-topic", } } - await help_test_entity_id_update( + await help_test_entity_id_update_subscriptions( hass, mqtt_mock, CLIMATE_DOMAIN, config, ["test-topic", "avty-topic"] ) +async def test_entity_id_update_discovery_update(hass, mqtt_mock): + """Test MQTT discovery update when entity_id is updated.""" + await help_test_entity_id_update_discovery_update( + hass, mqtt_mock, CLIMATE_DOMAIN, DEFAULT_CONFIG + ) + + async def test_precision_default(hass, mqtt_mock): """Test that setting precision to tenths works as intended.""" assert await async_setup_component(hass, CLIMATE_DOMAIN, DEFAULT_CONFIG) diff --git a/tests/components/mqtt/common.py b/tests/components/mqtt/test_common.py similarity index 90% rename from tests/components/mqtt/common.py rename to tests/components/mqtt/test_common.py index 702a38928a2..2f437174299 100644 --- a/tests/components/mqtt/common.py +++ b/tests/components/mqtt/test_common.py @@ -440,7 +440,9 @@ async def help_test_entity_device_info_update(hass, mqtt_mock, domain, config): assert device.name == "Milk" -async def help_test_entity_id_update(hass, mqtt_mock, domain, config, topics=None): +async def help_test_entity_id_update_subscriptions( + hass, mqtt_mock, domain, config, topics=None +): """Test MQTT subscriptions are managed when entity_id is updated.""" # Add unique_id to config config = copy.deepcopy(config) @@ -473,3 +475,47 @@ async def help_test_entity_id_update(hass, mqtt_mock, domain, config, topics=Non assert state is not None for topic in topics: mock_mqtt.async_subscribe.assert_any_call(topic, ANY, ANY, ANY) + + +async def help_test_entity_id_update_discovery_update( + hass, mqtt_mock, domain, config, topic=None +): + """Test MQTT discovery update after entity_id is updated.""" + # Add unique_id to config + config = copy.deepcopy(config) + config[domain]["unique_id"] = "TOTALLY_UNIQUE" + + if topic is None: + # Add default topic to config + config[domain]["availability_topic"] = "avty-topic" + topic = "avty-topic" + + entry = MockConfigEntry(domain=mqtt.DOMAIN) + entry.add_to_hass(hass) + await async_start(hass, "homeassistant", {}, entry) + ent_registry = mock_registry(hass, {}) + + data = json.dumps(config[domain]) + async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data) + await hass.async_block_till_done() + + async_fire_mqtt_message(hass, topic, "online") + state = hass.states.get(f"{domain}.test") + assert state.state != STATE_UNAVAILABLE + + async_fire_mqtt_message(hass, topic, "offline") + state = hass.states.get(f"{domain}.test") + assert state.state == STATE_UNAVAILABLE + + ent_registry.async_update_entity(f"{domain}.test", new_entity_id=f"{domain}.milk") + await hass.async_block_till_done() + + config[domain]["availability_topic"] = f"{topic}_2" + data = json.dumps(config[domain]) + async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data) + await hass.async_block_till_done() + assert len(hass.states.async_entity_ids(domain)) == 1 + + async_fire_mqtt_message(hass, f"{topic}_2", "online") + state = hass.states.get(f"{domain}.milk") + assert state.state != STATE_UNAVAILABLE diff --git a/tests/components/mqtt/test_cover.py b/tests/components/mqtt/test_cover.py index 2e5e232cdd5..7749c419ca0 100644 --- a/tests/components/mqtt/test_cover.py +++ b/tests/components/mqtt/test_cover.py @@ -22,7 +22,7 @@ from homeassistant.const import ( ) from homeassistant.setup import async_setup_component -from .common import ( +from .test_common import ( help_test_availability_without_topic, help_test_custom_availability_payload, help_test_default_availability_payload, @@ -34,7 +34,8 @@ from .common import ( help_test_entity_device_info_update, help_test_entity_device_info_with_connection, help_test_entity_device_info_with_identifier, - help_test_entity_id_update, + help_test_entity_id_update_discovery_update, + help_test_entity_id_update_subscriptions, help_test_setting_attribute_via_mqtt_json_message, help_test_setting_attribute_with_template, help_test_unique_id, @@ -1749,6 +1750,15 @@ async def test_entity_device_info_remove(hass, mqtt_mock): ) -async def test_entity_id_update(hass, mqtt_mock): +async def test_entity_id_update_subscriptions(hass, mqtt_mock): """Test MQTT subscriptions are managed when entity_id is updated.""" - await help_test_entity_id_update(hass, mqtt_mock, cover.DOMAIN, DEFAULT_CONFIG) + await help_test_entity_id_update_subscriptions( + hass, mqtt_mock, cover.DOMAIN, DEFAULT_CONFIG + ) + + +async def test_entity_id_update_discovery_update(hass, mqtt_mock): + """Test MQTT discovery update when entity_id is updated.""" + await help_test_entity_id_update_discovery_update( + hass, mqtt_mock, cover.DOMAIN, DEFAULT_CONFIG + ) diff --git a/tests/components/mqtt/test_fan.py b/tests/components/mqtt/test_fan.py index 0ecc6a25d6f..460c99618bd 100644 --- a/tests/components/mqtt/test_fan.py +++ b/tests/components/mqtt/test_fan.py @@ -3,7 +3,7 @@ from homeassistant.components import fan from homeassistant.const import ATTR_ASSUMED_STATE, STATE_OFF, STATE_ON from homeassistant.setup import async_setup_component -from .common import ( +from .test_common import ( help_test_availability_without_topic, help_test_custom_availability_payload, help_test_default_availability_payload, @@ -15,7 +15,8 @@ from .common import ( help_test_entity_device_info_update, help_test_entity_device_info_with_connection, help_test_entity_device_info_with_identifier, - help_test_entity_id_update, + help_test_entity_id_update_discovery_update, + help_test_entity_id_update_subscriptions, help_test_setting_attribute_via_mqtt_json_message, help_test_setting_attribute_with_template, help_test_unique_id, @@ -496,6 +497,15 @@ async def test_entity_device_info_remove(hass, mqtt_mock): ) -async def test_entity_id_update(hass, mqtt_mock): +async def test_entity_id_update_subscriptions(hass, mqtt_mock): """Test MQTT subscriptions are managed when entity_id is updated.""" - await help_test_entity_id_update(hass, mqtt_mock, fan.DOMAIN, DEFAULT_CONFIG) + await help_test_entity_id_update_subscriptions( + hass, mqtt_mock, fan.DOMAIN, DEFAULT_CONFIG + ) + + +async def test_entity_id_update_discovery_update(hass, mqtt_mock): + """Test MQTT discovery update when entity_id is updated.""" + await help_test_entity_id_update_discovery_update( + hass, mqtt_mock, fan.DOMAIN, DEFAULT_CONFIG + ) diff --git a/tests/components/mqtt/test_legacy_vacuum.py b/tests/components/mqtt/test_legacy_vacuum.py index 1bbefa35478..14ab79b2d20 100644 --- a/tests/components/mqtt/test_legacy_vacuum.py +++ b/tests/components/mqtt/test_legacy_vacuum.py @@ -19,7 +19,7 @@ from homeassistant.components.vacuum import ( from homeassistant.const import CONF_NAME, CONF_PLATFORM, STATE_OFF, STATE_ON from homeassistant.setup import async_setup_component -from .common import ( +from .test_common import ( help_test_availability_without_topic, help_test_custom_availability_payload, help_test_default_availability_payload, @@ -31,7 +31,8 @@ from .common import ( help_test_entity_device_info_update, help_test_entity_device_info_with_connection, help_test_entity_device_info_with_identifier, - help_test_entity_id_update, + help_test_entity_id_update_discovery_update, + help_test_entity_id_update_subscriptions, help_test_setting_attribute_via_mqtt_json_message, help_test_setting_attribute_with_template, help_test_unique_id, @@ -606,7 +607,7 @@ async def test_entity_device_info_remove(hass, mqtt_mock): ) -async def test_entity_id_update(hass, mqtt_mock): +async def test_entity_id_update_subscriptions(hass, mqtt_mock): """Test MQTT subscriptions are managed when entity_id is updated.""" config = { vacuum.DOMAIN: { @@ -618,6 +619,13 @@ async def test_entity_id_update(hass, mqtt_mock): "availability_topic": "avty-topic", } } - await help_test_entity_id_update( + await help_test_entity_id_update_subscriptions( hass, mqtt_mock, vacuum.DOMAIN, config, ["test-topic", "avty-topic"] ) + + +async def test_entity_id_update_discovery_update(hass, mqtt_mock): + """Test MQTT discovery update when entity_id is updated.""" + await help_test_entity_id_update_discovery_update( + hass, mqtt_mock, vacuum.DOMAIN, DEFAULT_CONFIG_2 + ) diff --git a/tests/components/mqtt/test_light.py b/tests/components/mqtt/test_light.py index ba4078f5374..bc4f5fc3393 100644 --- a/tests/components/mqtt/test_light.py +++ b/tests/components/mqtt/test_light.py @@ -162,7 +162,7 @@ from homeassistant.const import ATTR_ASSUMED_STATE, STATE_OFF, STATE_ON import homeassistant.core as ha from homeassistant.setup import async_setup_component -from .common import ( +from .test_common import ( help_test_availability_without_topic, help_test_custom_availability_payload, help_test_default_availability_payload, @@ -174,7 +174,8 @@ from .common import ( help_test_entity_device_info_update, help_test_entity_device_info_with_connection, help_test_entity_device_info_with_identifier, - help_test_entity_id_update, + help_test_entity_id_update_discovery_update, + help_test_entity_id_update_subscriptions, help_test_setting_attribute_via_mqtt_json_message, help_test_setting_attribute_with_template, help_test_unique_id, @@ -1345,6 +1346,15 @@ async def test_entity_device_info_remove(hass, mqtt_mock): ) -async def test_entity_id_update(hass, mqtt_mock): +async def test_entity_id_update_subscriptions(hass, mqtt_mock): """Test MQTT subscriptions are managed when entity_id is updated.""" - await help_test_entity_id_update(hass, mqtt_mock, light.DOMAIN, DEFAULT_CONFIG) + await help_test_entity_id_update_subscriptions( + hass, mqtt_mock, light.DOMAIN, DEFAULT_CONFIG + ) + + +async def test_entity_id_update_discovery_update(hass, mqtt_mock): + """Test MQTT discovery update when entity_id is updated.""" + await help_test_entity_id_update_discovery_update( + hass, mqtt_mock, light.DOMAIN, DEFAULT_CONFIG + ) diff --git a/tests/components/mqtt/test_light_json.py b/tests/components/mqtt/test_light_json.py index c07cec47ecc..f71791e019f 100644 --- a/tests/components/mqtt/test_light_json.py +++ b/tests/components/mqtt/test_light_json.py @@ -101,7 +101,7 @@ from homeassistant.const import ( import homeassistant.core as ha from homeassistant.setup import async_setup_component -from .common import ( +from .test_common import ( help_test_availability_without_topic, help_test_custom_availability_payload, help_test_default_availability_payload, @@ -113,7 +113,8 @@ from .common import ( help_test_entity_device_info_update, help_test_entity_device_info_with_connection, help_test_entity_device_info_with_identifier, - help_test_entity_id_update, + help_test_entity_id_update_discovery_update, + help_test_entity_id_update_subscriptions, help_test_setting_attribute_via_mqtt_json_message, help_test_setting_attribute_with_template, help_test_unique_id, @@ -1121,6 +1122,15 @@ async def test_entity_device_info_remove(hass, mqtt_mock): ) -async def test_entity_id_update(hass, mqtt_mock): +async def test_entity_id_update_subscriptions(hass, mqtt_mock): """Test MQTT subscriptions are managed when entity_id is updated.""" - await help_test_entity_id_update(hass, mqtt_mock, light.DOMAIN, DEFAULT_CONFIG) + await help_test_entity_id_update_subscriptions( + hass, mqtt_mock, light.DOMAIN, DEFAULT_CONFIG + ) + + +async def test_entity_id_update_discovery_update(hass, mqtt_mock): + """Test MQTT discovery update when entity_id is updated.""" + await help_test_entity_id_update_discovery_update( + hass, mqtt_mock, light.DOMAIN, DEFAULT_CONFIG + ) diff --git a/tests/components/mqtt/test_light_template.py b/tests/components/mqtt/test_light_template.py index 4f89ca77847..c9612a7ded7 100644 --- a/tests/components/mqtt/test_light_template.py +++ b/tests/components/mqtt/test_light_template.py @@ -38,7 +38,7 @@ from homeassistant.const import ( import homeassistant.core as ha from homeassistant.setup import async_setup_component -from .common import ( +from .test_common import ( help_test_availability_without_topic, help_test_custom_availability_payload, help_test_default_availability_payload, @@ -50,7 +50,8 @@ from .common import ( help_test_entity_device_info_update, help_test_entity_device_info_with_connection, help_test_entity_device_info_with_identifier, - help_test_entity_id_update, + help_test_entity_id_update_discovery_update, + help_test_entity_id_update_subscriptions, help_test_setting_attribute_via_mqtt_json_message, help_test_setting_attribute_with_template, help_test_unique_id, @@ -944,6 +945,15 @@ async def test_entity_device_info_remove(hass, mqtt_mock): ) -async def test_entity_id_update(hass, mqtt_mock): +async def test_entity_id_update_subscriptions(hass, mqtt_mock): """Test MQTT subscriptions are managed when entity_id is updated.""" - await help_test_entity_id_update(hass, mqtt_mock, light.DOMAIN, DEFAULT_CONFIG) + await help_test_entity_id_update_subscriptions( + hass, mqtt_mock, light.DOMAIN, DEFAULT_CONFIG + ) + + +async def test_entity_id_update_discovery_update(hass, mqtt_mock): + """Test MQTT discovery update when entity_id is updated.""" + await help_test_entity_id_update_discovery_update( + hass, mqtt_mock, light.DOMAIN, DEFAULT_CONFIG + ) diff --git a/tests/components/mqtt/test_lock.py b/tests/components/mqtt/test_lock.py index 4c34db6ea20..151021a45f8 100644 --- a/tests/components/mqtt/test_lock.py +++ b/tests/components/mqtt/test_lock.py @@ -3,7 +3,7 @@ from homeassistant.components import lock from homeassistant.const import ATTR_ASSUMED_STATE, STATE_LOCKED, STATE_UNLOCKED from homeassistant.setup import async_setup_component -from .common import ( +from .test_common import ( help_test_availability_without_topic, help_test_custom_availability_payload, help_test_default_availability_payload, @@ -15,7 +15,8 @@ from .common import ( help_test_entity_device_info_update, help_test_entity_device_info_with_connection, help_test_entity_device_info_with_identifier, - help_test_entity_id_update, + help_test_entity_id_update_discovery_update, + help_test_entity_id_update_subscriptions, help_test_setting_attribute_via_mqtt_json_message, help_test_setting_attribute_with_template, help_test_unique_id, @@ -386,6 +387,15 @@ async def test_entity_device_info_remove(hass, mqtt_mock): ) -async def test_entity_id_update(hass, mqtt_mock): +async def test_entity_id_update_subscriptions(hass, mqtt_mock): """Test MQTT subscriptions are managed when entity_id is updated.""" - await help_test_entity_id_update(hass, mqtt_mock, lock.DOMAIN, DEFAULT_CONFIG) + await help_test_entity_id_update_subscriptions( + hass, mqtt_mock, lock.DOMAIN, DEFAULT_CONFIG + ) + + +async def test_entity_id_update_discovery_update(hass, mqtt_mock): + """Test MQTT discovery update when entity_id is updated.""" + await help_test_entity_id_update_discovery_update( + hass, mqtt_mock, lock.DOMAIN, DEFAULT_CONFIG + ) diff --git a/tests/components/mqtt/test_sensor.py b/tests/components/mqtt/test_sensor.py index 0455c5f9c7c..061e53250cb 100644 --- a/tests/components/mqtt/test_sensor.py +++ b/tests/components/mqtt/test_sensor.py @@ -11,7 +11,7 @@ import homeassistant.core as ha from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util -from .common import ( +from .test_common import ( help_test_availability_without_topic, help_test_custom_availability_payload, help_test_default_availability_payload, @@ -23,7 +23,8 @@ from .common import ( help_test_entity_device_info_update, help_test_entity_device_info_with_connection, help_test_entity_device_info_with_identifier, - help_test_entity_id_update, + help_test_entity_id_update_discovery_update, + help_test_entity_id_update_subscriptions, help_test_setting_attribute_via_mqtt_json_message, help_test_setting_attribute_with_template, help_test_unique_id, @@ -392,9 +393,18 @@ async def test_entity_device_info_remove(hass, mqtt_mock): ) -async def test_entity_id_update(hass, mqtt_mock): +async def test_entity_id_update_subscriptions(hass, mqtt_mock): """Test MQTT subscriptions are managed when entity_id is updated.""" - await help_test_entity_id_update(hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG) + await help_test_entity_id_update_subscriptions( + hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG + ) + + +async def test_entity_id_update_discovery_update(hass, mqtt_mock): + """Test MQTT discovery update when entity_id is updated.""" + await help_test_entity_id_update_discovery_update( + hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG + ) async def test_entity_device_info_with_hub(hass, mqtt_mock): diff --git a/tests/components/mqtt/test_state_vacuum.py b/tests/components/mqtt/test_state_vacuum.py index 1b1150985a2..ecb38ef5774 100644 --- a/tests/components/mqtt/test_state_vacuum.py +++ b/tests/components/mqtt/test_state_vacuum.py @@ -30,7 +30,7 @@ from homeassistant.const import ( ) from homeassistant.setup import async_setup_component -from .common import ( +from .test_common import ( help_test_availability_without_topic, help_test_custom_availability_payload, help_test_default_availability_payload, @@ -42,7 +42,8 @@ from .common import ( help_test_entity_device_info_update, help_test_entity_device_info_with_connection, help_test_entity_device_info_with_identifier, - help_test_entity_id_update, + help_test_entity_id_update_discovery_update, + help_test_entity_id_update_subscriptions, help_test_setting_attribute_via_mqtt_json_message, help_test_setting_attribute_with_template, help_test_unique_id, @@ -441,6 +442,15 @@ async def test_entity_device_info_remove(hass, mqtt_mock): ) -async def test_entity_id_update(hass, mqtt_mock): +async def test_entity_id_update_subscriptions(hass, mqtt_mock): """Test MQTT subscriptions are managed when entity_id is updated.""" - await help_test_entity_id_update(hass, mqtt_mock, vacuum.DOMAIN, DEFAULT_CONFIG_2) + await help_test_entity_id_update_subscriptions( + hass, mqtt_mock, vacuum.DOMAIN, DEFAULT_CONFIG_2 + ) + + +async def test_entity_id_update_discovery_update(hass, mqtt_mock): + """Test MQTT discovery update when entity_id is updated.""" + await help_test_entity_id_update_discovery_update( + hass, mqtt_mock, vacuum.DOMAIN, DEFAULT_CONFIG_2 + ) diff --git a/tests/components/mqtt/test_switch.py b/tests/components/mqtt/test_switch.py index 5f5c69d5a22..d8ca8031390 100644 --- a/tests/components/mqtt/test_switch.py +++ b/tests/components/mqtt/test_switch.py @@ -7,7 +7,7 @@ from homeassistant.const import ATTR_ASSUMED_STATE, STATE_OFF, STATE_ON import homeassistant.core as ha from homeassistant.setup import async_setup_component -from .common import ( +from .test_common import ( help_test_availability_without_topic, help_test_custom_availability_payload, help_test_default_availability_payload, @@ -19,7 +19,8 @@ from .common import ( help_test_entity_device_info_update, help_test_entity_device_info_with_connection, help_test_entity_device_info_with_identifier, - help_test_entity_id_update, + help_test_entity_id_update_discovery_update, + help_test_entity_id_update_subscriptions, help_test_setting_attribute_via_mqtt_json_message, help_test_setting_attribute_with_template, help_test_unique_id, @@ -353,6 +354,15 @@ async def test_entity_device_info_remove(hass, mqtt_mock): ) -async def test_entity_id_update(hass, mqtt_mock): +async def test_entity_id_update_subscriptions(hass, mqtt_mock): """Test MQTT subscriptions are managed when entity_id is updated.""" - await help_test_entity_id_update(hass, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG) + await help_test_entity_id_update_subscriptions( + hass, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG + ) + + +async def test_entity_id_update_discovery_update(hass, mqtt_mock): + """Test MQTT discovery update when entity_id is updated.""" + await help_test_entity_id_update_discovery_update( + hass, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG + )