diff --git a/homeassistant/components/octoprint/__init__.py b/homeassistant/components/octoprint/__init__.py index 47d2bc51932..6c1eb62831c 100644 --- a/homeassistant/components/octoprint/__init__.py +++ b/homeassistant/components/octoprint/__init__.py @@ -18,6 +18,7 @@ from homeassistant.const import ( CONF_PORT, CONF_SENSORS, CONF_SSL, + CONF_VERIFY_SSL, Platform, ) from homeassistant.core import HomeAssistant @@ -154,7 +155,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: if DOMAIN not in hass.data: hass.data[DOMAIN] = {} - websession = async_get_clientsession(hass) + if CONF_VERIFY_SSL not in entry.data: + data = {**entry.data, CONF_VERIFY_SSL: True} + hass.config_entries.async_update_entry(entry, data=data) + + verify_ssl = entry.data[CONF_VERIFY_SSL] + websession = async_get_clientsession(hass, verify_ssl=verify_ssl) client = OctoprintClient( entry.data[CONF_HOST], websession, diff --git a/homeassistant/components/octoprint/config_flow.py b/homeassistant/components/octoprint/config_flow.py index 63b879c4f80..c2f0f96b6d5 100644 --- a/homeassistant/components/octoprint/config_flow.py +++ b/homeassistant/components/octoprint/config_flow.py @@ -14,6 +14,7 @@ from homeassistant.const import ( CONF_PORT, CONF_SSL, CONF_USERNAME, + CONF_VERIFY_SSL, ) from homeassistant.helpers.aiohttp_client import async_get_clientsession import homeassistant.helpers.config_validation as cv @@ -23,7 +24,9 @@ from .const import DOMAIN _LOGGER = logging.getLogger(__name__) -def _schema_with_defaults(username="", host="", port=80, path="/", ssl=False): +def _schema_with_defaults( + username="", host="", port=80, path="/", ssl=False, verify_ssl=True +): return vol.Schema( { vol.Required(CONF_USERNAME, default=username): str, @@ -31,6 +34,7 @@ def _schema_with_defaults(username="", host="", port=80, path="/", ssl=False): vol.Required(CONF_PORT, default=port): cv.port, vol.Required(CONF_PATH, default=path): str, vol.Required(CONF_SSL, default=ssl): bool, + vol.Required(CONF_VERIFY_SSL, default=verify_ssl): bool, }, extra=vol.ALLOW_EXTRA, ) @@ -80,6 +84,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): user_input[CONF_PORT], user_input[CONF_PATH], user_input[CONF_SSL], + user_input[CONF_VERIFY_SSL], ), ) @@ -111,14 +116,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): async def _finish_config(self, user_input): """Finish the configuration setup.""" - session = async_get_clientsession(self.hass) - octoprint = OctoprintClient( - user_input[CONF_HOST], - session, - user_input[CONF_PORT], - user_input[CONF_SSL], - user_input[CONF_PATH], - ) + octoprint = self._get_octoprint_client(user_input) octoprint.set_api_key(user_input[CONF_API_KEY]) try: @@ -183,14 +181,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): async def _async_get_auth_key(self, user_input: dict): """Get application api key.""" - session = async_get_clientsession(self.hass) - octoprint = OctoprintClient( - user_input[CONF_HOST], - session, - user_input[CONF_PORT], - user_input[CONF_SSL], - user_input[CONF_PATH], - ) + octoprint = self._get_octoprint_client(user_input) try: user_input[CONF_API_KEY] = await octoprint.request_app_key( @@ -204,6 +195,18 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): ) ) + def _get_octoprint_client(self, user_input: dict) -> OctoprintClient: + """Build an octoprint client from the user_input.""" + verify_ssl = user_input.get(CONF_VERIFY_SSL, True) + session = async_get_clientsession(self.hass, verify_ssl=verify_ssl) + return OctoprintClient( + user_input[CONF_HOST], + session, + user_input[CONF_PORT], + user_input[CONF_SSL], + user_input[CONF_PATH], + ) + class CannotConnect(exceptions.HomeAssistantError): """Error to indicate we cannot connect.""" diff --git a/homeassistant/components/octoprint/strings.json b/homeassistant/components/octoprint/strings.json index c52486d8406..89e44a6a3a6 100644 --- a/homeassistant/components/octoprint/strings.json +++ b/homeassistant/components/octoprint/strings.json @@ -8,6 +8,7 @@ "path": "Application Path", "port": "Port Number", "ssl": "Use SSL", + "verify_ssl": "[%key:common::config_flow::data::verify_ssl%]", "username": "[%key:common::config_flow::data::username%]" } } diff --git a/homeassistant/components/octoprint/translations/en.json b/homeassistant/components/octoprint/translations/en.json index 75d8355cbf3..e0729b27856 100644 --- a/homeassistant/components/octoprint/translations/en.json +++ b/homeassistant/components/octoprint/translations/en.json @@ -21,7 +21,8 @@ "path": "Application Path", "port": "Port Number", "ssl": "Use SSL", - "username": "Username" + "username": "Username", + "verify_ssl": "Verify SSL certificate" } } } diff --git a/tests/components/octoprint/test_config_flow.py b/tests/components/octoprint/test_config_flow.py index 57e89955d58..422b47668aa 100644 --- a/tests/components/octoprint/test_config_flow.py +++ b/tests/components/octoprint/test_config_flow.py @@ -63,6 +63,7 @@ async def test_form(hass): "port": 81, "ssl": True, "path": "/", + "verify_ssl": True, } assert len(mock_setup.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1 @@ -107,6 +108,7 @@ async def test_form_cannot_connect(hass): "name": "Printer", "port": 81, "ssl": True, + "verify_ssl": True, "path": "/", "api_key": "test-key", }, @@ -157,6 +159,7 @@ async def test_form_unknown_exception(hass): "ssl": True, "path": "/", "api_key": "test-key", + "verify_ssl": True, }, )