From 570d10875fd969d7a0c747040e0626542c7cf745 Mon Sep 17 00:00:00 2001 From: DanielV Date: Mon, 26 May 2025 13:08:42 +0000 Subject: [PATCH 1/2] Add config option to use SSL --- custom_components/poollab/__init__.py | 11 +++++--- custom_components/poollab/config_flow.py | 28 +++++++++++++------ custom_components/poollab/poollab.py | 5 ++-- .../poollab/translations/en.json | 12 +++++++- 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/custom_components/poollab/__init__.py b/custom_components/poollab/__init__.py index b72525e..8f8d488 100644 --- a/custom_components/poollab/__init__.py +++ b/custom_components/poollab/__init__.py @@ -9,7 +9,7 @@ from gql.client import TransportQueryError from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry -from homeassistant.const import CONF_API_KEY, CONF_URL +from homeassistant.const import CONF_API_KEY, CONF_SSL, CONF_URL from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed @@ -81,7 +81,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b hass.data[DOMAIN][config_entry.entry_id] = poollab = PoolLabCoordinator( hass, PoolLabApi( - token=config_entry.data[CONF_API_KEY], url=config_entry.data[CONF_URL] + token=config_entry.data[CONF_API_KEY], + url=config_entry.data[CONF_URL], + ssl=config_entry.data.get("ssl", True), ), ) else: @@ -128,6 +130,9 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> if CONF_URL not in config_entry.data: new_data[CONF_URL] = API_ENDPOINT + if CONF_SSL not in config_entry.data: + new_data[CONF_SSL] = True + hass.config_entries.async_update_entry( config_entry, data=new_data, @@ -141,5 +146,3 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> installed_minor_version, ) return True - - return False diff --git a/custom_components/poollab/config_flow.py b/custom_components/poollab/config_flow.py index 4712979..e12a362 100644 --- a/custom_components/poollab/config_flow.py +++ b/custom_components/poollab/config_flow.py @@ -9,7 +9,7 @@ import voluptuous as vol from homeassistant import config_entries -from homeassistant.const import CONF_API_KEY, CONF_URL +from homeassistant.const import CONF_API_KEY, CONF_SSL, CONF_URL from homeassistant.data_entry_flow import FlowResult import homeassistant.helpers.config_validation as cv @@ -18,17 +18,18 @@ _LOGGER = logging.getLogger(__name__) -PLACEHOLDERS = { - CONF_API_KEY: "API key", - CONF_URL: "API endpoint URL", -} +# PLACEHOLDERS = { +# CONF_API_KEY: "API key", +# CONF_URL: "API endpoint URL", +# CONF_SSL: "Use SSL (recommended)", +# } class PoolLabConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): """PoolLab config flow.""" VERSION = 1 - MINOR_VERSION = 2 + MINOR_VERSION = 3 data = None options = None _reauth_entry: config_entries.ConfigEntry | None = None @@ -41,6 +42,7 @@ async def async_step_user( defaults = { CONF_API_KEY: "", CONF_URL: API_ENDPOINT, + CONF_SSL: True, } if user_input is not None: @@ -79,12 +81,15 @@ async def async_step_user( vol.Optional( CONF_URL, default=defaults.get(CONF_URL, API_ENDPOINT) ): cv.string, + vol.Optional( + CONF_SSL, default=defaults.get(CONF_SSL, True) + ): cv.boolean, } ) return self.async_show_form( step_id="user", data_schema=user_schema, - description_placeholders=PLACEHOLDERS, + # description_placeholders=PLACEHOLDERS, errors=errors, ) @@ -122,12 +127,15 @@ async def async_step_reconfigure( vol.Optional( CONF_URL, default=config_entry.data.get(CONF_URL, API_ENDPOINT) ): cv.string, + vol.Optional( + CONF_SSL, default=config_entry.data.get(CONF_SSL, True) + ): cv.boolean, } ) return self.async_show_form( step_id="reconfigure", data_schema=user_schema, - description_placeholders=PLACEHOLDERS, + # description_placeholders=PLACEHOLDERS, errors=errors, ) @@ -145,7 +153,9 @@ async def async_step_reauth(self, user_input: Mapping[str, Any]) -> FlowResult: async def is_valid(self, user_input): """Check for user input errors.""" poollab_api = PoolLabApi( - token=user_input[CONF_API_KEY], url=user_input[CONF_URL] + token=user_input[CONF_API_KEY], + url=user_input[CONF_URL], + ssl=user_input[CONF_SSL], ) if not await poollab_api.test(): raise InvalidAuth diff --git a/custom_components/poollab/poollab.py b/custom_components/poollab/poollab.py index e641542..ef088ec 100644 --- a/custom_components/poollab/poollab.py +++ b/custom_components/poollab/poollab.py @@ -240,11 +240,12 @@ def as_dict(self) -> dict[str, Any]: class PoolLabApi: """Public API class for PoolLab.""" - def __init__(self, token: str, url=API_ENDPOINT) -> None: + def __init__(self, token: str, url: str = API_ENDPOINT, ssl: bool = True) -> None: """Init the cloud api object.""" self._token = token self._data = None self._url = url + self._ssl = ssl def _build_schema(self) -> str: schema = "\n" @@ -259,7 +260,7 @@ async def _update(self, schema: str | None = None) -> bool: if schema is None: schema = self._build_schema() transport = AIOHTTPTransport( - url=self._url, headers={"Authorization": self._token}, ssl=True + url=self._url, headers={"Authorization": self._token}, ssl=self._ssl ) async with Client( transport=transport, diff --git a/custom_components/poollab/translations/en.json b/custom_components/poollab/translations/en.json index 38f3f0d..0c8ffd1 100644 --- a/custom_components/poollab/translations/en.json +++ b/custom_components/poollab/translations/en.json @@ -6,7 +6,17 @@ "description": "Setup a PoolLab integration, get key from https://labcom.cloud/pages/user-setting", "data": { "api_key": "API key", - "url": "API endpoint URL" + "url": "API endpoint URL", + "ssl": "Use SSL" + } + }, + "reconfigure": { + "title": "PoolLab", + "description": "Reconfigure a PoolLab integration, get key from https://labcom.cloud/pages/user-setting", + "data": { + "api_key": "API key", + "url": "API endpoint URL", + "ssl": "Use SSL" } } }, From b6b6126bd40ac01a991eee4357506d587e0540b6 Mon Sep 17 00:00:00 2001 From: DanielV Date: Mon, 26 May 2025 13:10:39 +0000 Subject: [PATCH 2/2] Clean-up --- custom_components/poollab/config_flow.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/custom_components/poollab/config_flow.py b/custom_components/poollab/config_flow.py index e12a362..4bf0921 100644 --- a/custom_components/poollab/config_flow.py +++ b/custom_components/poollab/config_flow.py @@ -18,12 +18,6 @@ _LOGGER = logging.getLogger(__name__) -# PLACEHOLDERS = { -# CONF_API_KEY: "API key", -# CONF_URL: "API endpoint URL", -# CONF_SSL: "Use SSL (recommended)", -# } - class PoolLabConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): """PoolLab config flow.""" @@ -89,7 +83,6 @@ async def async_step_user( return self.async_show_form( step_id="user", data_schema=user_schema, - # description_placeholders=PLACEHOLDERS, errors=errors, ) @@ -135,7 +128,6 @@ async def async_step_reconfigure( return self.async_show_form( step_id="reconfigure", data_schema=user_schema, - # description_placeholders=PLACEHOLDERS, errors=errors, )