Add verify SSL option to Mealie (#121767)

pull/121706/head^2
Joost Lekkerkerker 2024-07-11 17:23:26 +02:00 committed by GitHub
parent ede130aa53
commit 3be95ebc87
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 24 additions and 8 deletions

View File

@ -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()

View File

@ -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],
},

View File

@ -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%]"
}
}
},

View File

@ -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(