From a1a2b6b0478323eee34c3af8bbb930f46cf5d964 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 31 Aug 2020 13:47:58 -0500 Subject: [PATCH 1/2] Support reloading the telegram notify platform --- homeassistant/components/telegram/__init__.py | 3 ++ homeassistant/components/telegram/notify.py | 5 ++ tests/components/telegram/__init__.py | 1 + tests/components/telegram/test_notify.py | 53 +++++++++++++++++++ tests/fixtures/telegram/configuration.yaml | 4 ++ 5 files changed, 66 insertions(+) create mode 100644 tests/components/telegram/__init__.py create mode 100644 tests/components/telegram/test_notify.py create mode 100644 tests/fixtures/telegram/configuration.yaml diff --git a/homeassistant/components/telegram/__init__.py b/homeassistant/components/telegram/__init__.py index 1aca4e510c642..43a639cb2c37f 100644 --- a/homeassistant/components/telegram/__init__.py +++ b/homeassistant/components/telegram/__init__.py @@ -1 +1,4 @@ """The telegram component.""" + +DOMAIN = "telegram" +PLATFORMS = ["notify"] diff --git a/homeassistant/components/telegram/notify.py b/homeassistant/components/telegram/notify.py index 673935d8283d8..c0f3f624af95b 100644 --- a/homeassistant/components/telegram/notify.py +++ b/homeassistant/components/telegram/notify.py @@ -12,6 +12,9 @@ BaseNotificationService, ) from homeassistant.const import ATTR_LOCATION +from homeassistant.helpers.reload import setup_reload_service + +from . import DOMAIN as TELEGRAM_DOMAIN, PLATFORMS _LOGGER = logging.getLogger(__name__) @@ -29,6 +32,8 @@ def get_service(hass, config, discovery_info=None): """Get the Telegram notification service.""" + + setup_reload_service(hass, TELEGRAM_DOMAIN, PLATFORMS) chat_id = config.get(CONF_CHAT_ID) return TelegramNotificationService(hass, chat_id) diff --git a/tests/components/telegram/__init__.py b/tests/components/telegram/__init__.py new file mode 100644 index 0000000000000..feff68fd53ab9 --- /dev/null +++ b/tests/components/telegram/__init__.py @@ -0,0 +1 @@ +"""Tests for telegram component.""" diff --git a/tests/components/telegram/test_notify.py b/tests/components/telegram/test_notify.py new file mode 100644 index 0000000000000..7488db49d9ea5 --- /dev/null +++ b/tests/components/telegram/test_notify.py @@ -0,0 +1,53 @@ +"""The tests for the telegram.notify platform.""" +from os import path + +from homeassistant import config as hass_config +import homeassistant.components.notify as notify +from homeassistant.components.telegram import DOMAIN +from homeassistant.const import SERVICE_RELOAD +from homeassistant.setup import async_setup_component + +from tests.async_mock import patch + + +async def test_reload_notify(hass): + """Verify we can reload the notify service.""" + + with patch("homeassistant.components.telegram_bot.async_setup", return_value=True): + assert await async_setup_component( + hass, + notify.DOMAIN, + { + notify.DOMAIN: [ + { + "name": DOMAIN, + "platform": DOMAIN, + "chat_id": 1, + }, + ] + }, + ) + await hass.async_block_till_done() + + assert hass.services.has_service(notify.DOMAIN, DOMAIN) + + yaml_path = path.join( + _get_fixtures_base_path(), + "fixtures", + "telegram/configuration.yaml", + ) + with patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path): + await hass.services.async_call( + DOMAIN, + SERVICE_RELOAD, + {}, + blocking=True, + ) + await hass.async_block_till_done() + + assert not hass.services.has_service(notify.DOMAIN, DOMAIN) + assert hass.services.has_service(notify.DOMAIN, "telegram_reloaded") + + +def _get_fixtures_base_path(): + return path.dirname(path.dirname(path.dirname(__file__))) diff --git a/tests/fixtures/telegram/configuration.yaml b/tests/fixtures/telegram/configuration.yaml new file mode 100644 index 0000000000000..ab50b4df5eef1 --- /dev/null +++ b/tests/fixtures/telegram/configuration.yaml @@ -0,0 +1,4 @@ +notify: + - name: telegram_reloaded + platform: telegram + chat_id: 2 From 8c681a096b0ca279dc9ad2b55455855ee83d7e7f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 31 Aug 2020 16:00:52 -0500 Subject: [PATCH 2/2] services.yaml --- homeassistant/components/telegram/services.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/homeassistant/components/telegram/services.yaml b/homeassistant/components/telegram/services.yaml index e69de29bb2d1d..c467de06fe5fa 100644 --- a/homeassistant/components/telegram/services.yaml +++ b/homeassistant/components/telegram/services.yaml @@ -0,0 +1,2 @@ +reload: + description: Reload telegram notify services.