Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
Allow self signed certs on octoprint server (home-assistant#59213)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfleming71 authored Jan 10, 2022
1 parent 69c8a02 commit 77d22c8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
8 changes: 7 additions & 1 deletion homeassistant/components/octoprint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
CONF_PORT,
CONF_SENSORS,
CONF_SSL,
CONF_VERIFY_SSL,
Platform,
)
from homeassistant.core import HomeAssistant
Expand Down Expand Up @@ -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,
Expand Down
37 changes: 20 additions & 17 deletions homeassistant/components/octoprint/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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
Expand All @@ -23,14 +24,17 @@
_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,
vol.Required(CONF_HOST, default=host): str,
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,
)
Expand Down Expand Up @@ -80,6 +84,7 @@ async def async_step_user(self, user_input=None):
user_input[CONF_PORT],
user_input[CONF_PATH],
user_input[CONF_SSL],
user_input[CONF_VERIFY_SSL],
),
)

Expand Down Expand Up @@ -111,14 +116,7 @@ async def async_step_get_api_key(self, user_input):

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:
Expand Down Expand Up @@ -183,14 +181,7 @@ async def async_step_ssdp(

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(
Expand All @@ -204,6 +195,18 @@ async def _async_get_auth_key(self, user_input: dict):
)
)

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."""
1 change: 1 addition & 0 deletions homeassistant/components/octoprint/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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%]"
}
}
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/octoprint/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"path": "Application Path",
"port": "Port Number",
"ssl": "Use SSL",
"username": "Username"
"username": "Username",
"verify_ssl": "Verify SSL certificate"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions tests/components/octoprint/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
},
Expand Down Expand Up @@ -157,6 +159,7 @@ async def test_form_unknown_exception(hass):
"ssl": True,
"path": "/",
"api_key": "test-key",
"verify_ssl": True,
},
)

Expand Down

0 comments on commit 77d22c8

Please sign in to comment.