From 3be95ebc87948354ec21e9199ffc2b659e0a5da7 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker <joostlek@outlook.com> Date: Thu, 11 Jul 2024 17:23:26 +0200 Subject: [PATCH] Add verify SSL option to Mealie (#121767) --- homeassistant/components/mealie/__init__.py | 6 ++++-- homeassistant/components/mealie/config_flow.py | 10 ++++++++-- homeassistant/components/mealie/strings.json | 6 ++++-- tests/components/mealie/test_config_flow.py | 10 ++++++++-- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/mealie/__init__.py b/homeassistant/components/mealie/__init__.py index 0e1ea080f46..87b3e3988a2 100644 --- a/homeassistant/components/mealie/__init__.py +++ b/homeassistant/components/mealie/__init__.py @@ -4,7 +4,7 @@ from __future__ import annotations from aiomealie import MealieAuthenticationError, MealieClient, MealieConnectionError -from homeassistant.const import CONF_API_TOKEN, CONF_HOST, Platform +from homeassistant.const import CONF_API_TOKEN, CONF_HOST, CONF_VERIFY_SSL, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import ( ConfigEntryAuthFailed, @@ -42,7 +42,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: MealieConfigEntry) -> bo client = MealieClient( entry.data[CONF_HOST], token=entry.data[CONF_API_TOKEN], - session=async_get_clientsession(hass), + session=async_get_clientsession( + hass, verify_ssl=entry.data.get(CONF_VERIFY_SSL, True) + ), ) try: about = await client.get_about() diff --git a/homeassistant/components/mealie/config_flow.py b/homeassistant/components/mealie/config_flow.py index 110599928c5..6b75f57313c 100644 --- a/homeassistant/components/mealie/config_flow.py +++ b/homeassistant/components/mealie/config_flow.py @@ -7,7 +7,7 @@ from aiomealie import MealieAuthenticationError, MealieClient, MealieConnectionE import voluptuous as vol from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult -from homeassistant.const import CONF_API_TOKEN, CONF_HOST +from homeassistant.const import CONF_API_TOKEN, CONF_HOST, CONF_VERIFY_SSL from homeassistant.helpers.aiohttp_client import async_get_clientsession from .const import DOMAIN, LOGGER, MIN_REQUIRED_MEALIE_VERSION @@ -17,6 +17,7 @@ USER_SCHEMA = vol.Schema( { vol.Required(CONF_HOST): str, vol.Required(CONF_API_TOKEN): str, + vol.Optional(CONF_VERIFY_SSL, default=True): bool, } ) REAUTH_SCHEMA = vol.Schema( @@ -30,6 +31,7 @@ class MealieConfigFlow(ConfigFlow, domain=DOMAIN): """Mealie config flow.""" host: str | None = None + verify_ssl: bool = True entry: ConfigEntry | None = None async def check_connection( @@ -40,7 +42,7 @@ class MealieConfigFlow(ConfigFlow, domain=DOMAIN): client = MealieClient( self.host, token=api_token, - session=async_get_clientsession(self.hass), + session=async_get_clientsession(self.hass, verify_ssl=self.verify_ssl), ) try: info = await client.get_user_info() @@ -64,6 +66,7 @@ class MealieConfigFlow(ConfigFlow, domain=DOMAIN): errors: dict[str, str] = {} if user_input: self.host = user_input[CONF_HOST] + self.verify_ssl = user_input[CONF_VERIFY_SSL] errors, user_id = await self.check_connection( user_input[CONF_API_TOKEN], ) @@ -85,6 +88,7 @@ class MealieConfigFlow(ConfigFlow, domain=DOMAIN): ) -> ConfigFlowResult: """Perform reauth upon an API authentication error.""" self.host = entry_data[CONF_HOST] + self.verify_ssl = entry_data.get(CONF_VERIFY_SSL, True) self.entry = self.hass.config_entries.async_get_entry(self.context["entry_id"]) return await self.async_step_reauth_confirm() @@ -128,6 +132,7 @@ class MealieConfigFlow(ConfigFlow, domain=DOMAIN): errors: dict[str, str] = {} if user_input: self.host = user_input[CONF_HOST] + self.verify_ssl = user_input[CONF_VERIFY_SSL] errors, user_id = await self.check_connection( user_input[CONF_API_TOKEN], ) @@ -138,6 +143,7 @@ class MealieConfigFlow(ConfigFlow, domain=DOMAIN): self.entry, data={ **self.entry.data, + CONF_VERIFY_SSL: user_input[CONF_VERIFY_SSL], CONF_HOST: user_input[CONF_HOST], CONF_API_TOKEN: user_input[CONF_API_TOKEN], }, diff --git a/homeassistant/components/mealie/strings.json b/homeassistant/components/mealie/strings.json index 43f6cde80b2..a0b0dcbfc4f 100644 --- a/homeassistant/components/mealie/strings.json +++ b/homeassistant/components/mealie/strings.json @@ -4,7 +4,8 @@ "user": { "data": { "host": "[%key:common::config_flow::data::url%]", - "api_token": "[%key:common::config_flow::data::api_token%]" + "api_token": "[%key:common::config_flow::data::api_token%]", + "verify_ssl": "[%key:common::config_flow::data::verify_ssl%]" }, "data_description": { "host": "The URL of your Mealie instance." @@ -20,7 +21,8 @@ "description": "Please reconfigure with Mealie.", "data": { "host": "[%key:common::config_flow::data::url%]", - "api_token": "[%key:common::config_flow::data::api_token%]" + "api_token": "[%key:common::config_flow::data::api_token%]", + "verify_ssl": "[%key:common::config_flow::data::verify_ssl%]" } } }, diff --git a/tests/components/mealie/test_config_flow.py b/tests/components/mealie/test_config_flow.py index c08a52394d7..8edc89c3213 100644 --- a/tests/components/mealie/test_config_flow.py +++ b/tests/components/mealie/test_config_flow.py @@ -7,7 +7,7 @@ import pytest from homeassistant.components.mealie.const import DOMAIN from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_RECONFIGURE, SOURCE_USER -from homeassistant.const import CONF_API_TOKEN, CONF_HOST +from homeassistant.const import CONF_API_TOKEN, CONF_HOST, CONF_VERIFY_SSL from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -38,6 +38,7 @@ async def test_full_flow( assert result["data"] == { CONF_HOST: "demo.mealie.io", CONF_API_TOKEN: "token", + CONF_VERIFY_SSL: True, } assert result["result"].unique_id == "bf1c62fe-4941-4332-9886-e54e88dbdba0" @@ -264,13 +265,18 @@ async def test_reconfigure_flow( result = await hass.config_entries.flow.async_configure( result["flow_id"], - {CONF_HOST: "http://test:9090", CONF_API_TOKEN: "token2"}, + { + CONF_HOST: "http://test:9090", + CONF_API_TOKEN: "token2", + CONF_VERIFY_SSL: False, + }, ) assert result["type"] is FlowResultType.ABORT assert result["reason"] == "reconfigure_successful" assert mock_config_entry.data[CONF_API_TOKEN] == "token2" assert mock_config_entry.data[CONF_HOST] == "http://test:9090" + assert mock_config_entry.data[CONF_VERIFY_SSL] is False async def test_reconfigure_flow_wrong_account( -- GitLab