From 5e4b16c69a77f4238f0434ea985e8eb5a0d096b2 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 28 Feb 2022 14:50:49 +0000 Subject: [PATCH] Remove custom WS command for removing MQTT devices (#67381) * Remove custom WS command for removing MQTT devices * Re-add removed test --- homeassistant/components/mqtt/__init__.py | 39 +------- .../mqtt/test_device_tracker_discovery.py | 7 +- tests/components/mqtt/test_device_trigger.py | 8 +- tests/components/mqtt/test_discovery.py | 8 +- tests/components/mqtt/test_init.py | 97 ++----------------- tests/components/mqtt/test_tag.py | 9 +- 6 files changed, 35 insertions(+), 133 deletions(-) diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index 107bc4660c2..b98fe990fe8 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -50,12 +50,7 @@ from homeassistant.core import ( ) from homeassistant.data_entry_flow import BaseServiceInfo from homeassistant.exceptions import HomeAssistantError, TemplateError, Unauthorized -from homeassistant.helpers import ( - config_validation as cv, - device_registry as dr, - event, - template, -) +from homeassistant.helpers import config_validation as cv, event, template from homeassistant.helpers.device_registry import DeviceEntry from homeassistant.helpers.dispatcher import async_dispatcher_connect, dispatcher_send from homeassistant.helpers.entity import Entity @@ -588,7 +583,6 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: conf: ConfigType | None = config.get(DOMAIN) websocket_api.async_register_command(hass, websocket_subscribe) - websocket_api.async_register_command(hass, websocket_remove_device) websocket_api.async_register_command(hass, websocket_mqtt_info) debug_info.initialize(hass) @@ -1198,37 +1192,6 @@ def websocket_mqtt_info(hass, connection, msg): connection.send_result(msg["id"], mqtt_info) -@websocket_api.websocket_command( - {vol.Required("type"): "mqtt/device/remove", vol.Required("device_id"): str} -) -@websocket_api.async_response -async def websocket_remove_device(hass, connection, msg): - """Delete device.""" - device_id = msg["device_id"] - device_registry = dr.async_get(hass) - - if not (device := device_registry.async_get(device_id)): - connection.send_error( - msg["id"], websocket_api.const.ERR_NOT_FOUND, "Device not found" - ) - return - - for config_entry in device.config_entries: - config_entry = hass.config_entries.async_get_entry(config_entry) - # Only delete the device if it belongs to an MQTT device entry - if config_entry.domain == DOMAIN: - await async_remove_config_entry_device(hass, config_entry, device) - device_registry.async_update_device( - device_id, remove_config_entry_id=config_entry.entry_id - ) - connection.send_message(websocket_api.result_message(msg["id"])) - return - - connection.send_error( - msg["id"], websocket_api.const.ERR_NOT_FOUND, "Non MQTT device" - ) - - @websocket_api.websocket_command( { vol.Required("type"): "mqtt/subscribe", diff --git a/tests/components/mqtt/test_device_tracker_discovery.py b/tests/components/mqtt/test_device_tracker_discovery.py index 3b83581b86a..f8ee94b58f9 100644 --- a/tests/components/mqtt/test_device_tracker_discovery.py +++ b/tests/components/mqtt/test_device_tracker_discovery.py @@ -3,6 +3,7 @@ import pytest from homeassistant.components import device_tracker +from homeassistant.components.mqtt.const import DOMAIN as MQTT_DOMAIN from homeassistant.components.mqtt.discovery import ALREADY_DISCOVERED from homeassistant.const import STATE_HOME, STATE_NOT_HOME, STATE_UNKNOWN from homeassistant.setup import async_setup_component @@ -187,7 +188,7 @@ async def test_device_tracker_discovery_update(hass, mqtt_mock, caplog): async def test_cleanup_device_tracker( hass, hass_ws_client, device_reg, entity_reg, mqtt_mock ): - """Test discvered device is cleaned up when removed from registry.""" + """Test discovered device is cleaned up when removed from registry.""" assert await async_setup_component(hass, "config", {}) ws_client = await hass_ws_client(hass) @@ -210,10 +211,12 @@ async def test_cleanup_device_tracker( assert state is not None # Remove MQTT from the device + mqtt_config_entry = hass.config_entries.async_entries(MQTT_DOMAIN)[0] await ws_client.send_json( { "id": 6, - "type": "mqtt/device/remove", + "type": "config/device_registry/remove_config_entry", + "config_entry_id": mqtt_config_entry.entry_id, "device_id": device_entry.id, } ) diff --git a/tests/components/mqtt/test_device_trigger.py b/tests/components/mqtt/test_device_trigger.py index 8a3719f1707..8cfac3bf993 100644 --- a/tests/components/mqtt/test_device_trigger.py +++ b/tests/components/mqtt/test_device_trigger.py @@ -692,10 +692,12 @@ async def test_not_fires_on_mqtt_message_after_remove_from_registry( assert len(calls) == 1 # Remove MQTT from the device + mqtt_config_entry = hass.config_entries.async_entries(DOMAIN)[0] await ws_client.send_json( { "id": 6, - "type": "mqtt/device/remove", + "type": "config/device_registry/remove_config_entry", + "config_entry_id": mqtt_config_entry.entry_id, "device_id": device_entry.id, } ) @@ -1005,10 +1007,12 @@ async def test_cleanup_trigger(hass, hass_ws_client, device_reg, entity_reg, mqt assert triggers[0]["type"] == "foo" # Remove MQTT from the device + mqtt_config_entry = hass.config_entries.async_entries(DOMAIN)[0] await ws_client.send_json( { "id": 6, - "type": "mqtt/device/remove", + "type": "config/device_registry/remove_config_entry", + "config_entry_id": mqtt_config_entry.entry_id, "device_id": device_entry.id, } ) diff --git a/tests/components/mqtt/test_discovery.py b/tests/components/mqtt/test_discovery.py index 463f3d03fff..d0c00f6b2ce 100644 --- a/tests/components/mqtt/test_discovery.py +++ b/tests/components/mqtt/test_discovery.py @@ -592,10 +592,12 @@ async def test_cleanup_device(hass, hass_ws_client, device_reg, entity_reg, mqtt assert state is not None # Remove MQTT from the device + mqtt_config_entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] await ws_client.send_json( { "id": 6, - "type": "mqtt/device/remove", + "type": "config/device_registry/remove_config_entry", + "config_entry_id": mqtt_config_entry.entry_id, "device_id": device_entry.id, } ) @@ -717,10 +719,12 @@ async def test_cleanup_device_multiple_config_entries( assert state is not None # Remove MQTT from the device + mqtt_config_entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] await ws_client.send_json( { "id": 6, - "type": "mqtt/device/remove", + "type": "config/device_registry/remove_config_entry", + "config_entry_id": mqtt_config_entry.entry_id, "device_id": device_entry.id, } ) diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index 7296d4e8101..b770122d39a 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -12,7 +12,7 @@ import voluptuous as vol import yaml from homeassistant import config as hass_config -from homeassistant.components import mqtt, websocket_api +from homeassistant.components import mqtt from homeassistant.components.mqtt import debug_info from homeassistant.components.mqtt.mixins import MQTT_ENTITY_DEVICE_INFO_SCHEMA from homeassistant.components.mqtt.models import ReceiveMessage @@ -1698,6 +1698,8 @@ async def test_mqtt_ws_remove_discovered_device( hass, device_reg, entity_reg, hass_ws_client, mqtt_mock ): """Test MQTT websocket device removal.""" + assert await async_setup_component(hass, "config", {}) + data = ( '{ "device":{"identifiers":["0AFFD2"]},' ' "state_topic": "foobar/sensor",' @@ -1712,8 +1714,14 @@ async def test_mqtt_ws_remove_discovered_device( assert device_entry is not None client = await hass_ws_client(hass) + mqtt_config_entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] await client.send_json( - {"id": 5, "type": "mqtt/device/remove", "device_id": device_entry.id} + { + "id": 5, + "type": "config/device_registry/remove_config_entry", + "config_entry_id": mqtt_config_entry.entry_id, + "device_id": device_entry.id, + } ) response = await client.receive_json() assert response["success"] @@ -1723,91 +1731,6 @@ async def test_mqtt_ws_remove_discovered_device( assert device_entry is None -async def test_mqtt_ws_remove_discovered_device_twice( - hass, device_reg, hass_ws_client, mqtt_mock -): - """Test MQTT websocket device removal.""" - data = ( - '{ "device":{"identifiers":["0AFFD2"]},' - ' "state_topic": "foobar/sensor",' - ' "unique_id": "unique" }' - ) - - async_fire_mqtt_message(hass, "homeassistant/sensor/bla/config", data) - await hass.async_block_till_done() - - device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) - assert device_entry is not None - - client = await hass_ws_client(hass) - await client.send_json( - {"id": 5, "type": "mqtt/device/remove", "device_id": device_entry.id} - ) - response = await client.receive_json() - assert response["success"] - - await client.send_json( - {"id": 6, "type": "mqtt/device/remove", "device_id": device_entry.id} - ) - response = await client.receive_json() - assert not response["success"] - assert response["error"]["code"] == websocket_api.const.ERR_NOT_FOUND - - -async def test_mqtt_ws_remove_discovered_device_same_topic( - hass, device_reg, hass_ws_client, mqtt_mock -): - """Test MQTT websocket device removal.""" - data = ( - '{ "device":{"identifiers":["0AFFD2"]},' - ' "state_topic": "foobar/sensor",' - ' "availability_topic": "foobar/sensor",' - ' "unique_id": "unique" }' - ) - - async_fire_mqtt_message(hass, "homeassistant/sensor/bla/config", data) - await hass.async_block_till_done() - - device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) - assert device_entry is not None - - client = await hass_ws_client(hass) - await client.send_json( - {"id": 5, "type": "mqtt/device/remove", "device_id": device_entry.id} - ) - response = await client.receive_json() - assert response["success"] - - await client.send_json( - {"id": 6, "type": "mqtt/device/remove", "device_id": device_entry.id} - ) - response = await client.receive_json() - assert not response["success"] - assert response["error"]["code"] == websocket_api.const.ERR_NOT_FOUND - - -async def test_mqtt_ws_remove_non_mqtt_device( - hass, device_reg, hass_ws_client, mqtt_mock -): - """Test MQTT websocket device removal of device belonging to other domain.""" - config_entry = MockConfigEntry(domain="test") - config_entry.add_to_hass(hass) - - device_entry = device_reg.async_get_or_create( - config_entry_id=config_entry.entry_id, - connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, - ) - assert device_entry is not None - - client = await hass_ws_client(hass) - await client.send_json( - {"id": 5, "type": "mqtt/device/remove", "device_id": device_entry.id} - ) - response = await client.receive_json() - assert not response["success"] - assert response["error"]["code"] == websocket_api.const.ERR_NOT_FOUND - - async def test_mqtt_ws_get_device_debug_info( hass, device_reg, hass_ws_client, mqtt_mock ): diff --git a/tests/components/mqtt/test_tag.py b/tests/components/mqtt/test_tag.py index 7d3b4f2e1b2..99e2fffc085 100644 --- a/tests/components/mqtt/test_tag.py +++ b/tests/components/mqtt/test_tag.py @@ -6,6 +6,7 @@ from unittest.mock import ANY, patch import pytest from homeassistant.components.device_automation import DeviceAutomationType +from homeassistant.components.mqtt.const import DOMAIN as MQTT_DOMAIN from homeassistant.helpers import device_registry as dr from homeassistant.setup import async_setup_component @@ -378,10 +379,12 @@ async def test_not_fires_on_mqtt_message_after_remove_from_registry( tag_mock.assert_called_once_with(ANY, DEFAULT_TAG_ID, device_entry.id) # Remove MQTT from the device + mqtt_config_entry = hass.config_entries.async_entries(MQTT_DOMAIN)[0] await ws_client.send_json( { "id": 6, - "type": "mqtt/device/remove", + "type": "config/device_registry/remove_config_entry", + "config_entry_id": mqtt_config_entry.entry_id, "device_id": device_entry.id, } ) @@ -537,10 +540,12 @@ async def test_cleanup_tag(hass, hass_ws_client, device_reg, entity_reg, mqtt_mo mqtt_mock.async_publish.assert_not_called() # Remove MQTT from the device + mqtt_config_entry = hass.config_entries.async_entries(MQTT_DOMAIN)[0] await ws_client.send_json( { "id": 6, - "type": "mqtt/device/remove", + "type": "config/device_registry/remove_config_entry", + "config_entry_id": mqtt_config_entry.entry_id, "device_id": device_entry1.id, } )