diff --git a/homeassistant/components/telegram_bot/__init__.py b/homeassistant/components/telegram_bot/__init__.py index 655dfb7a260..f9472c50cae 100644 --- a/homeassistant/components/telegram_bot/__init__.py +++ b/homeassistant/components/telegram_bot/__init__.py @@ -374,6 +374,10 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: ) elif msgtype == SERVICE_DELETE_MESSAGE: await notify_service.delete_message(context=service.context, **kwargs) + elif msgtype == SERVICE_LEAVE_CHAT: + messages = await notify_service.leave_chat( + context=service.context, **kwargs + ) else: await notify_service.edit_message( msgtype, context=service.context, **kwargs diff --git a/homeassistant/components/telegram_bot/icons.json b/homeassistant/components/telegram_bot/icons.json index 0acf20d561a..8deecfb9c27 100644 --- a/homeassistant/components/telegram_bot/icons.json +++ b/homeassistant/components/telegram_bot/icons.json @@ -41,6 +41,9 @@ }, "delete_message": { "service": "mdi:delete" + }, + "leave_chat": { + "service": "mdi:exit-run" } } } diff --git a/homeassistant/components/telegram_bot/services.yaml b/homeassistant/components/telegram_bot/services.yaml index 581e7f2e350..75f7a39a25f 100644 --- a/homeassistant/components/telegram_bot/services.yaml +++ b/homeassistant/components/telegram_bot/services.yaml @@ -774,3 +774,15 @@ delete_message: example: 12345 selector: text: + +leave_chat: + fields: + config_entry_id: + selector: + config_entry: + integration: telegram_bot + chat_id: + required: true + example: 12345 + selector: + text: diff --git a/homeassistant/components/telegram_bot/strings.json b/homeassistant/components/telegram_bot/strings.json index 1fb0ea30475..3dddcd6e4e9 100644 --- a/homeassistant/components/telegram_bot/strings.json +++ b/homeassistant/components/telegram_bot/strings.json @@ -842,6 +842,20 @@ "description": "ID of the chat where to delete the message." } } + }, + "leave_chat": { + "name": "Leave chat", + "description": "Removes the bot from the chat.", + "fields": { + "config_entry_id": { + "name": "[%key:component::telegram_bot::services::send_message::fields::config_entry_id::name%]", + "description": "The config entry representing the Telegram bot to leave the chat." + }, + "chat_id": { + "name": "[%key:component::telegram_bot::services::edit_message::fields::chat_id::name%]", + "description": "Chat ID of the group from which the bot should be removed." + } + } } }, "exceptions": { diff --git a/tests/components/telegram_bot/test_telegram_bot.py b/tests/components/telegram_bot/test_telegram_bot.py index 60e85394f58..595ef2882fd 100644 --- a/tests/components/telegram_bot/test_telegram_bot.py +++ b/tests/components/telegram_bot/test_telegram_bot.py @@ -39,6 +39,7 @@ from homeassistant.components.telegram_bot import ( SERVICE_EDIT_CAPTION, SERVICE_EDIT_MESSAGE, SERVICE_EDIT_REPLYMARKUP, + SERVICE_LEAVE_CHAT, SERVICE_SEND_ANIMATION, SERVICE_SEND_DOCUMENT, SERVICE_SEND_LOCATION, @@ -754,3 +755,30 @@ async def test_answer_callback_query( await hass.async_block_till_done() mock.assert_called_once() + + +async def test_leave_chat( + hass: HomeAssistant, + mock_broadcast_config_entry: MockConfigEntry, + mock_external_calls: None, +) -> None: + """Test answer callback query.""" + mock_broadcast_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_broadcast_config_entry.entry_id) + await hass.async_block_till_done() + + with patch( + "homeassistant.components.telegram_bot.bot.TelegramNotificationService.leave_chat", + AsyncMock(return_value=True), + ) as mock: + await hass.services.async_call( + DOMAIN, + SERVICE_LEAVE_CHAT, + { + ATTR_CHAT_ID: 12345, + }, + blocking=True, + ) + + await hass.async_block_till_done() + mock.assert_called_once()