diff --git a/homeassistant/components/tasmota/__init__.py b/homeassistant/components/tasmota/__init__.py index 44dd2489177..b6e92301554 100644 --- a/homeassistant/components/tasmota/__init__.py +++ b/homeassistant/components/tasmota/__init__.py @@ -15,15 +15,13 @@ from hatasmota.const import ( from hatasmota.discovery import clear_discovery_topic from hatasmota.models import TasmotaDeviceConfig from hatasmota.mqtt import TasmotaMQTTClient -import voluptuous as vol -from homeassistant.components import mqtt, websocket_api +from homeassistant.components import mqtt from homeassistant.components.mqtt.subscription import ( async_prepare_subscribe_topics, async_subscribe_topics, async_unsubscribe_topics, ) -from homeassistant.components.websocket_api.connection import ActiveConnection from homeassistant.config_entries import ConfigEntry from homeassistant.core import Event, HomeAssistant, callback from homeassistant.helpers import device_registry as dr @@ -39,7 +37,6 @@ from .const import ( CONF_DISCOVERY_PREFIX, DATA_REMOVE_DISCOVER_COMPONENT, DATA_UNSUB, - DOMAIN, PLATFORMS, ) @@ -48,7 +45,6 @@ _LOGGER = logging.getLogger(__name__) async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Tasmota from a config entry.""" - websocket_api.async_register_command(hass, websocket_remove_device) hass.data[DATA_UNSUB] = [] async def _publish( @@ -218,37 +214,6 @@ async def async_setup_device( _update_device(hass, config_entry, config, device_registry) -@websocket_api.websocket_command( - {vol.Required("type"): "tasmota/device/remove", vol.Required("device_id"): str} -) -@callback -def websocket_remove_device( - hass: HomeAssistant, connection: ActiveConnection, msg: dict -) -> None: - """Delete device.""" - device_id = msg["device_id"] - dev_registry = dr.async_get(hass) - - if not (device := dev_registry.async_get(device_id)): - connection.send_error( - msg["id"], websocket_api.const.ERR_NOT_FOUND, "Device not found" - ) - return - - for config_entry_id in device.config_entries: - config_entry = hass.config_entries.async_get_entry(config_entry_id) - assert config_entry - # Only delete the device if it belongs to a Tasmota device entry - if config_entry.domain == DOMAIN: - dev_registry.async_remove_device(device_id) - connection.send_message(websocket_api.result_message(msg["id"])) - return - - connection.send_error( - msg["id"], websocket_api.const.ERR_NOT_FOUND, "Non Tasmota device" - ) - - async def async_remove_config_entry_device( hass: HomeAssistant, config_entry: ConfigEntry, device_entry: dr.DeviceEntry ) -> bool: diff --git a/tests/components/tasmota/test_init.py b/tests/components/tasmota/test_init.py index 1b9c88ee4b1..7cbb12c49fc 100644 --- a/tests/components/tasmota/test_init.py +++ b/tests/components/tasmota/test_init.py @@ -3,9 +3,9 @@ import copy import json from unittest.mock import call -from homeassistant.components import websocket_api -from homeassistant.components.tasmota.const import DEFAULT_PREFIX +from homeassistant.components.tasmota.const import DEFAULT_PREFIX, DOMAIN from homeassistant.helpers import device_registry as dr +from homeassistant.setup import async_setup_component from .test_common import DEFAULT_CONFIG @@ -111,6 +111,7 @@ async def test_tasmota_ws_remove_discovered_device( hass, device_reg, entity_reg, hass_ws_client, mqtt_mock, setup_tasmota ): """Test Tasmota websocket device removal.""" + assert await async_setup_component(hass, "config", {}) config = copy.deepcopy(DEFAULT_CONFIG) mac = config["mac"] @@ -124,8 +125,14 @@ async def test_tasmota_ws_remove_discovered_device( assert device_entry is not None client = await hass_ws_client(hass) + tasmota_config_entry = hass.config_entries.async_entries(DOMAIN)[0] await client.send_json( - {"id": 5, "type": "tasmota/device/remove", "device_id": device_entry.id} + { + "id": 5, + "config_entry_id": tasmota_config_entry.entry_id, + "type": "config/device_registry/remove_config_entry", + "device_id": device_entry.id, + } ) response = await client.receive_json() assert response["success"] @@ -135,57 +142,3 @@ async def test_tasmota_ws_remove_discovered_device( set(), {(dr.CONNECTION_NETWORK_MAC, mac)} ) assert device_entry is None - - -async def test_tasmota_ws_remove_discovered_device_twice( - hass, device_reg, hass_ws_client, mqtt_mock, setup_tasmota -): - """Test Tasmota websocket device removal.""" - config = copy.deepcopy(DEFAULT_CONFIG) - mac = config["mac"] - - async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/{mac}/config", json.dumps(config)) - await hass.async_block_till_done() - - # Verify device entry is created - device_entry = device_reg.async_get_device( - set(), {(dr.CONNECTION_NETWORK_MAC, mac)} - ) - assert device_entry is not None - - client = await hass_ws_client(hass) - await client.send_json( - {"id": 5, "type": "tasmota/device/remove", "device_id": device_entry.id} - ) - response = await client.receive_json() - assert response["success"] - - await client.send_json( - {"id": 6, "type": "tasmota/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 - assert response["error"]["message"] == "Device not found" - - -async def test_tasmota_ws_remove_non_tasmota_device( - hass, device_reg, hass_ws_client, mqtt_mock, setup_tasmota -): - """Test Tasmota 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": "tasmota/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