diff --git a/homeassistant/components/ntfy/__init__.py b/homeassistant/components/ntfy/__init__.py index 44a8a7e00d9..cd9c35ca4e6 100644 --- a/homeassistant/components/ntfy/__init__.py +++ b/homeassistant/components/ntfy/__init__.py @@ -13,7 +13,7 @@ from aiontfy.exceptions import ( ) from homeassistant.config_entries import ConfigEntry -from homeassistant.const import CONF_TOKEN, CONF_URL, Platform +from homeassistant.const import CONF_TOKEN, CONF_URL, CONF_VERIFY_SSL, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.helpers.aiohttp_client import async_get_clientsession @@ -30,7 +30,7 @@ type NtfyConfigEntry = ConfigEntry[Ntfy] async def async_setup_entry(hass: HomeAssistant, entry: NtfyConfigEntry) -> bool: """Set up ntfy from a config entry.""" - session = async_get_clientsession(hass) + session = async_get_clientsession(hass, entry.data.get(CONF_VERIFY_SSL, True)) ntfy = Ntfy(entry.data[CONF_URL], session, token=entry.data.get(CONF_TOKEN)) try: diff --git a/homeassistant/components/ntfy/config_flow.py b/homeassistant/components/ntfy/config_flow.py index ffbb1c762ed..04a6730aa73 100644 --- a/homeassistant/components/ntfy/config_flow.py +++ b/homeassistant/components/ntfy/config_flow.py @@ -33,6 +33,7 @@ from homeassistant.const import ( CONF_TOKEN, CONF_URL, CONF_USERNAME, + CONF_VERIFY_SSL, ) from homeassistant.core import callback from homeassistant.helpers.aiohttp_client import async_get_clientsession @@ -54,6 +55,7 @@ STEP_USER_DATA_SCHEMA = vol.Schema( autocomplete="url", ), ), + vol.Required(CONF_VERIFY_SSL, default=True): bool, vol.Required(SECTION_AUTH): data_entry_flow.section( vol.Schema( { @@ -123,7 +125,7 @@ class NtfyConfigFlow(ConfigFlow, domain=DOMAIN): CONF_USERNAME: username, } ) - session = async_get_clientsession(self.hass) + session = async_get_clientsession(self.hass, user_input[CONF_VERIFY_SSL]) if username: ntfy = Ntfy( user_input[CONF_URL], @@ -160,6 +162,7 @@ class NtfyConfigFlow(ConfigFlow, domain=DOMAIN): CONF_URL: url.human_repr(), CONF_USERNAME: username, CONF_TOKEN: token, + CONF_VERIFY_SSL: user_input[CONF_VERIFY_SSL], }, ) diff --git a/homeassistant/components/ntfy/strings.json b/homeassistant/components/ntfy/strings.json index c60f618ed66..a48d158c896 100644 --- a/homeassistant/components/ntfy/strings.json +++ b/homeassistant/components/ntfy/strings.json @@ -8,10 +8,12 @@ "user": { "description": "Set up **ntfy** push notification service", "data": { - "url": "Service URL" + "url": "Service URL", + "verify_ssl": "[%key:common::config_flow::data::verify_ssl%]" }, "data_description": { - "url": "Address of the ntfy service. Modify this if you want to use a different server" + "url": "Address of the ntfy service. Modify this if you want to use a different server", + "verify_ssl": "Enable SSL certificate verification for secure connections. Disable only if connecting to a ntfy instance using a self-signed certificate" }, "sections": { "auth": { diff --git a/tests/components/ntfy/conftest.py b/tests/components/ntfy/conftest.py index 52d6e413c4e..d9bc620b464 100644 --- a/tests/components/ntfy/conftest.py +++ b/tests/components/ntfy/conftest.py @@ -9,7 +9,7 @@ import pytest from homeassistant.components.ntfy.const import CONF_TOPIC, DOMAIN from homeassistant.config_entries import ConfigSubentryData -from homeassistant.const import CONF_TOKEN, CONF_URL, CONF_USERNAME +from homeassistant.const import CONF_TOKEN, CONF_URL, CONF_USERNAME, CONF_VERIFY_SSL from tests.common import MockConfigEntry, load_fixture @@ -64,6 +64,7 @@ def mock_config_entry() -> MockConfigEntry: CONF_URL: "https://ntfy.sh/", CONF_USERNAME: None, CONF_TOKEN: "token", + CONF_VERIFY_SSL: True, }, entry_id="123456789", subentries_data=[ diff --git a/tests/components/ntfy/test_config_flow.py b/tests/components/ntfy/test_config_flow.py index e846b805298..2d3656536a9 100644 --- a/tests/components/ntfy/test_config_flow.py +++ b/tests/components/ntfy/test_config_flow.py @@ -20,6 +20,7 @@ from homeassistant.const import ( CONF_TOKEN, CONF_URL, CONF_USERNAME, + CONF_VERIFY_SSL, ) from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -33,17 +34,24 @@ from tests.common import MockConfigEntry ( { CONF_URL: "https://ntfy.sh", + CONF_VERIFY_SSL: True, SECTION_AUTH: {CONF_USERNAME: "username", CONF_PASSWORD: "password"}, }, { CONF_URL: "https://ntfy.sh/", + CONF_VERIFY_SSL: True, CONF_USERNAME: "username", CONF_TOKEN: "token", }, ), ( - {CONF_URL: "https://ntfy.sh", SECTION_AUTH: {}}, - {CONF_URL: "https://ntfy.sh/", CONF_USERNAME: None, CONF_TOKEN: "token"}, + {CONF_URL: "https://ntfy.sh", CONF_VERIFY_SSL: True, SECTION_AUTH: {}}, + { + CONF_URL: "https://ntfy.sh/", + CONF_VERIFY_SSL: True, + CONF_USERNAME: None, + CONF_TOKEN: "token", + }, ), ], ) @@ -109,6 +117,7 @@ async def test_form_errors( result["flow_id"], { CONF_URL: "https://ntfy.sh", + CONF_VERIFY_SSL: True, SECTION_AUTH: {CONF_USERNAME: "username", CONF_PASSWORD: "password"}, }, ) @@ -121,6 +130,7 @@ async def test_form_errors( result["flow_id"], { CONF_URL: "https://ntfy.sh", + CONF_VERIFY_SSL: True, SECTION_AUTH: {CONF_USERNAME: "username", CONF_PASSWORD: "password"}, }, ) @@ -130,6 +140,7 @@ async def test_form_errors( assert result["title"] == "ntfy.sh" assert result["data"] == { CONF_URL: "https://ntfy.sh/", + CONF_VERIFY_SSL: True, CONF_USERNAME: "username", CONF_TOKEN: "token", } @@ -151,7 +162,11 @@ async def test_form_already_configured( result = await hass.config_entries.flow.async_configure( result["flow_id"], - user_input={CONF_URL: "https://ntfy.sh", SECTION_AUTH: {}}, + user_input={ + CONF_URL: "https://ntfy.sh", + CONF_VERIFY_SSL: True, + SECTION_AUTH: {}, + }, ) assert result["type"] is FlowResultType.ABORT @@ -163,7 +178,7 @@ async def test_add_topic_flow(hass: HomeAssistant) -> None: """Test add topic subentry flow.""" config_entry = MockConfigEntry( domain=DOMAIN, - data={CONF_URL: "https://ntfy.sh/", CONF_USERNAME: None}, + data={CONF_URL: "https://ntfy.sh/", CONF_VERIFY_SSL: True, CONF_USERNAME: None}, ) config_entry.add_to_hass(hass) @@ -211,7 +226,7 @@ async def test_generated_topic(hass: HomeAssistant, mock_random: AsyncMock) -> N """Test add topic subentry flow with generated topic name.""" config_entry = MockConfigEntry( domain=DOMAIN, - data={CONF_URL: "https://ntfy.sh/"}, + data={CONF_URL: "https://ntfy.sh/", CONF_VERIFY_SSL: True}, ) config_entry.add_to_hass(hass) @@ -265,7 +280,7 @@ async def test_invalid_topic(hass: HomeAssistant, mock_random: AsyncMock) -> Non """Test add topic subentry flow with invalid topic name.""" config_entry = MockConfigEntry( domain=DOMAIN, - data={CONF_URL: "https://ntfy.sh/"}, + data={CONF_URL: "https://ntfy.sh/", CONF_VERIFY_SSL: True}, ) config_entry.add_to_hass(hass)