mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 07:07:28 +00:00
Allow self signed certs on octoprint server (#59213)
This commit is contained in:
parent
69c8a02e16
commit
77d22c8542
@ -18,6 +18,7 @@ from homeassistant.const import (
|
|||||||
CONF_PORT,
|
CONF_PORT,
|
||||||
CONF_SENSORS,
|
CONF_SENSORS,
|
||||||
CONF_SSL,
|
CONF_SSL,
|
||||||
|
CONF_VERIFY_SSL,
|
||||||
Platform,
|
Platform,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
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:
|
if DOMAIN not in hass.data:
|
||||||
hass.data[DOMAIN] = {}
|
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(
|
client = OctoprintClient(
|
||||||
entry.data[CONF_HOST],
|
entry.data[CONF_HOST],
|
||||||
websession,
|
websession,
|
||||||
|
@ -14,6 +14,7 @@ from homeassistant.const import (
|
|||||||
CONF_PORT,
|
CONF_PORT,
|
||||||
CONF_SSL,
|
CONF_SSL,
|
||||||
CONF_USERNAME,
|
CONF_USERNAME,
|
||||||
|
CONF_VERIFY_SSL,
|
||||||
)
|
)
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
@ -23,7 +24,9 @@ from .const import DOMAIN
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_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(
|
return vol.Schema(
|
||||||
{
|
{
|
||||||
vol.Required(CONF_USERNAME, default=username): str,
|
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_PORT, default=port): cv.port,
|
||||||
vol.Required(CONF_PATH, default=path): str,
|
vol.Required(CONF_PATH, default=path): str,
|
||||||
vol.Required(CONF_SSL, default=ssl): bool,
|
vol.Required(CONF_SSL, default=ssl): bool,
|
||||||
|
vol.Required(CONF_VERIFY_SSL, default=verify_ssl): bool,
|
||||||
},
|
},
|
||||||
extra=vol.ALLOW_EXTRA,
|
extra=vol.ALLOW_EXTRA,
|
||||||
)
|
)
|
||||||
@ -80,6 +84,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
user_input[CONF_PORT],
|
user_input[CONF_PORT],
|
||||||
user_input[CONF_PATH],
|
user_input[CONF_PATH],
|
||||||
user_input[CONF_SSL],
|
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):
|
async def _finish_config(self, user_input):
|
||||||
"""Finish the configuration setup."""
|
"""Finish the configuration setup."""
|
||||||
session = async_get_clientsession(self.hass)
|
octoprint = self._get_octoprint_client(user_input)
|
||||||
octoprint = OctoprintClient(
|
|
||||||
user_input[CONF_HOST],
|
|
||||||
session,
|
|
||||||
user_input[CONF_PORT],
|
|
||||||
user_input[CONF_SSL],
|
|
||||||
user_input[CONF_PATH],
|
|
||||||
)
|
|
||||||
octoprint.set_api_key(user_input[CONF_API_KEY])
|
octoprint.set_api_key(user_input[CONF_API_KEY])
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -183,14 +181,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
async def _async_get_auth_key(self, user_input: dict):
|
async def _async_get_auth_key(self, user_input: dict):
|
||||||
"""Get application api key."""
|
"""Get application api key."""
|
||||||
session = async_get_clientsession(self.hass)
|
octoprint = self._get_octoprint_client(user_input)
|
||||||
octoprint = OctoprintClient(
|
|
||||||
user_input[CONF_HOST],
|
|
||||||
session,
|
|
||||||
user_input[CONF_PORT],
|
|
||||||
user_input[CONF_SSL],
|
|
||||||
user_input[CONF_PATH],
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
user_input[CONF_API_KEY] = await octoprint.request_app_key(
|
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):
|
class CannotConnect(exceptions.HomeAssistantError):
|
||||||
"""Error to indicate we cannot connect."""
|
"""Error to indicate we cannot connect."""
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
"path": "Application Path",
|
"path": "Application Path",
|
||||||
"port": "Port Number",
|
"port": "Port Number",
|
||||||
"ssl": "Use SSL",
|
"ssl": "Use SSL",
|
||||||
|
"verify_ssl": "[%key:common::config_flow::data::verify_ssl%]",
|
||||||
"username": "[%key:common::config_flow::data::username%]"
|
"username": "[%key:common::config_flow::data::username%]"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,8 @@
|
|||||||
"path": "Application Path",
|
"path": "Application Path",
|
||||||
"port": "Port Number",
|
"port": "Port Number",
|
||||||
"ssl": "Use SSL",
|
"ssl": "Use SSL",
|
||||||
"username": "Username"
|
"username": "Username",
|
||||||
|
"verify_ssl": "Verify SSL certificate"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -63,6 +63,7 @@ async def test_form(hass):
|
|||||||
"port": 81,
|
"port": 81,
|
||||||
"ssl": True,
|
"ssl": True,
|
||||||
"path": "/",
|
"path": "/",
|
||||||
|
"verify_ssl": True,
|
||||||
}
|
}
|
||||||
assert len(mock_setup.mock_calls) == 1
|
assert len(mock_setup.mock_calls) == 1
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
assert len(mock_setup_entry.mock_calls) == 1
|
||||||
@ -107,6 +108,7 @@ async def test_form_cannot_connect(hass):
|
|||||||
"name": "Printer",
|
"name": "Printer",
|
||||||
"port": 81,
|
"port": 81,
|
||||||
"ssl": True,
|
"ssl": True,
|
||||||
|
"verify_ssl": True,
|
||||||
"path": "/",
|
"path": "/",
|
||||||
"api_key": "test-key",
|
"api_key": "test-key",
|
||||||
},
|
},
|
||||||
@ -157,6 +159,7 @@ async def test_form_unknown_exception(hass):
|
|||||||
"ssl": True,
|
"ssl": True,
|
||||||
"path": "/",
|
"path": "/",
|
||||||
"api_key": "test-key",
|
"api_key": "test-key",
|
||||||
|
"verify_ssl": True,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user