mirror of
https://github.com/home-assistant/core.git
synced 2025-06-25 01:21:51 +02:00
Fix octoprint down every two minutes (#90001)
This commit is contained in:
@ -5,6 +5,7 @@ from datetime import timedelta
|
||||
import logging
|
||||
from typing import cast
|
||||
|
||||
import aiohttp
|
||||
from pyoctoprintapi import ApiError, OctoprintClient, PrinterOffline
|
||||
from pyoctoprintapi.exceptions import UnauthorizedException
|
||||
import voluptuous as vol
|
||||
@ -22,11 +23,11 @@ from homeassistant.const import (
|
||||
CONF_SENSORS,
|
||||
CONF_SSL,
|
||||
CONF_VERIFY_SSL,
|
||||
EVENT_HOMEASSISTANT_STOP,
|
||||
Platform,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.core import Event, HomeAssistant, callback
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
@ -163,14 +164,25 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
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)
|
||||
connector = aiohttp.TCPConnector(
|
||||
force_close=True,
|
||||
ssl=False if not entry.data[CONF_VERIFY_SSL] else None,
|
||||
)
|
||||
session = aiohttp.ClientSession(connector=connector)
|
||||
|
||||
@callback
|
||||
def _async_close_websession(event: Event) -> None:
|
||||
"""Close websession."""
|
||||
session.detach()
|
||||
|
||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_close_websession)
|
||||
|
||||
client = OctoprintClient(
|
||||
entry.data[CONF_HOST],
|
||||
websession,
|
||||
entry.data[CONF_PORT],
|
||||
entry.data[CONF_SSL],
|
||||
entry.data[CONF_PATH],
|
||||
host=entry.data[CONF_HOST],
|
||||
session=session,
|
||||
port=entry.data[CONF_PORT],
|
||||
ssl=entry.data[CONF_SSL],
|
||||
path=entry.data[CONF_PATH],
|
||||
)
|
||||
|
||||
client.set_api_key(entry.data[CONF_API_KEY])
|
||||
|
@ -6,6 +6,7 @@ from collections.abc import Mapping
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
import aiohttp
|
||||
from pyoctoprintapi import ApiError, OctoprintClient, OctoprintException
|
||||
import voluptuous as vol
|
||||
from yarl import URL
|
||||
@ -22,7 +23,6 @@ from homeassistant.const import (
|
||||
CONF_VERIFY_SSL,
|
||||
)
|
||||
from homeassistant.data_entry_flow import FlowResult
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
from .const import DOMAIN
|
||||
@ -58,6 +58,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
"""Handle a config flow for OctoPrint."""
|
||||
self.discovery_schema = None
|
||||
self._user_input = None
|
||||
self._sessions: list[aiohttp.ClientSession] = []
|
||||
|
||||
async def async_step_user(self, user_input=None):
|
||||
"""Handle the initial step."""
|
||||
@ -260,14 +261,26 @@ 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],
|
||||
|
||||
connector = aiohttp.TCPConnector(
|
||||
force_close=True,
|
||||
ssl=False if not verify_ssl else None,
|
||||
)
|
||||
session = aiohttp.ClientSession(connector=connector)
|
||||
self._sessions.append(session)
|
||||
|
||||
return OctoprintClient(
|
||||
host=user_input[CONF_HOST],
|
||||
session=session,
|
||||
port=user_input[CONF_PORT],
|
||||
ssl=user_input[CONF_SSL],
|
||||
path=user_input[CONF_PATH],
|
||||
)
|
||||
|
||||
def async_remove(self):
|
||||
"""Detach the session."""
|
||||
for session in self._sessions:
|
||||
session.detach()
|
||||
|
||||
|
||||
class CannotConnect(exceptions.HomeAssistantError):
|
||||
|
@ -6,7 +6,7 @@
|
||||
"documentation": "https://www.home-assistant.io/integrations/octoprint",
|
||||
"iot_class": "local_polling",
|
||||
"loggers": ["pyoctoprintapi"],
|
||||
"requirements": ["pyoctoprintapi==0.1.11"],
|
||||
"requirements": ["pyoctoprintapi==0.1.12"],
|
||||
"ssdp": [
|
||||
{
|
||||
"manufacturer": "The OctoPrint Project",
|
||||
|
@ -1887,7 +1887,7 @@ pynzbgetapi==0.2.0
|
||||
pyobihai==1.4.2
|
||||
|
||||
# homeassistant.components.octoprint
|
||||
pyoctoprintapi==0.1.11
|
||||
pyoctoprintapi==0.1.12
|
||||
|
||||
# homeassistant.components.ombi
|
||||
pyombi==0.1.10
|
||||
|
@ -1400,7 +1400,7 @@ pynzbgetapi==0.2.0
|
||||
pyobihai==1.4.2
|
||||
|
||||
# homeassistant.components.octoprint
|
||||
pyoctoprintapi==0.1.11
|
||||
pyoctoprintapi==0.1.12
|
||||
|
||||
# homeassistant.components.openuv
|
||||
pyopenuv==2023.02.0
|
||||
|
Reference in New Issue
Block a user