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