mirror of
https://github.com/home-assistant/core.git
synced 2026-08-24 10:13:52 -05:00
Move service registration to async_setup in octoprint (#174635)
This commit is contained in:
@@ -1,38 +1,35 @@
|
||||
"""Support for monitoring OctoPrint 3D printers."""
|
||||
|
||||
import logging
|
||||
from typing import cast
|
||||
|
||||
import aiohttp
|
||||
from pyoctoprintapi import OctoprintClient
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntryState
|
||||
from homeassistant.config_entries import SOURCE_IMPORT
|
||||
from homeassistant.const import (
|
||||
CONF_API_KEY,
|
||||
CONF_BINARY_SENSORS,
|
||||
CONF_DEVICE_ID,
|
||||
CONF_HOST,
|
||||
CONF_MONITORED_CONDITIONS,
|
||||
CONF_NAME,
|
||||
CONF_PATH,
|
||||
CONF_PORT,
|
||||
CONF_PROFILE_NAME,
|
||||
CONF_SENSORS,
|
||||
CONF_SSL,
|
||||
CONF_VERIFY_SSL,
|
||||
EVENT_HOMEASSISTANT_STOP,
|
||||
Platform,
|
||||
)
|
||||
from homeassistant.core import Event, HomeAssistant, ServiceCall, callback
|
||||
from homeassistant.exceptions import ServiceValidationError
|
||||
from homeassistant.helpers import config_validation as cv, device_registry as dr
|
||||
from homeassistant.core import Event, HomeAssistant, callback
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
from homeassistant.util import slugify as util_slugify
|
||||
from homeassistant.util.ssl import get_default_context, get_default_no_verify_context
|
||||
|
||||
from .const import CONF_BAUDRATE, DOMAIN, SERVICE_CONNECT
|
||||
from .const import DOMAIN
|
||||
from .coordinator import OctoprintConfigEntry, OctoprintDataUpdateCoordinator
|
||||
from .services import async_setup_services
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -131,18 +128,10 @@ CONFIG_SCHEMA = vol.Schema(
|
||||
extra=vol.ALLOW_EXTRA,
|
||||
)
|
||||
|
||||
SERVICE_CONNECT_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_DEVICE_ID): cv.string,
|
||||
vol.Optional(CONF_PROFILE_NAME): cv.string,
|
||||
vol.Optional(CONF_PORT): cv.string,
|
||||
vol.Optional(CONF_BAUDRATE): cv.positive_int,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
"""Set up the OctoPrint component."""
|
||||
async_setup_services(hass)
|
||||
if DOMAIN not in config:
|
||||
return True
|
||||
|
||||
@@ -208,49 +197,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: OctoprintConfigEntry) ->
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
async def async_printer_connect(call: ServiceCall) -> None:
|
||||
"""Connect to a printer."""
|
||||
client = async_get_client_for_service_call(hass, call)
|
||||
await client.connect(
|
||||
printer_profile=call.data.get(CONF_PROFILE_NAME),
|
||||
port=call.data.get(CONF_PORT),
|
||||
baud_rate=call.data.get(CONF_BAUDRATE),
|
||||
)
|
||||
|
||||
if not hass.services.has_service(DOMAIN, SERVICE_CONNECT):
|
||||
# pylint: disable-next=home-assistant-service-registered-in-setup-entry
|
||||
hass.services.async_register(
|
||||
DOMAIN,
|
||||
SERVICE_CONNECT,
|
||||
async_printer_connect,
|
||||
schema=SERVICE_CONNECT_SCHEMA,
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: OctoprintConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
||||
|
||||
def async_get_client_for_service_call(
|
||||
hass: HomeAssistant, call: ServiceCall
|
||||
) -> OctoprintClient:
|
||||
"""Get the client related to a service call (by device ID)."""
|
||||
device_id = call.data[CONF_DEVICE_ID]
|
||||
device_registry = dr.async_get(hass)
|
||||
|
||||
if device_entry := device_registry.async_get(device_id):
|
||||
for entry_id in device_entry.config_entries:
|
||||
if entry := hass.config_entries.async_get_entry(entry_id):
|
||||
if entry.domain == DOMAIN and entry.state is ConfigEntryState.LOADED:
|
||||
return cast(OctoprintConfigEntry, entry).runtime_data.octoprint
|
||||
|
||||
raise ServiceValidationError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="missing_client",
|
||||
translation_placeholders={
|
||||
"device_id": device_id,
|
||||
},
|
||||
)
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
"""Services for octoprint."""
|
||||
|
||||
from typing import cast
|
||||
|
||||
from pyoctoprintapi import OctoprintClient
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import CONF_DEVICE_ID, CONF_PORT, CONF_PROFILE_NAME
|
||||
from homeassistant.core import HomeAssistant, ServiceCall, callback
|
||||
from homeassistant.exceptions import ServiceValidationError
|
||||
from homeassistant.helpers import config_validation as cv, device_registry as dr
|
||||
|
||||
from .const import CONF_BAUDRATE, DOMAIN, SERVICE_CONNECT
|
||||
from .coordinator import OctoprintConfigEntry
|
||||
|
||||
SERVICE_CONNECT_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_DEVICE_ID): cv.string,
|
||||
vol.Optional(CONF_PROFILE_NAME): cv.string,
|
||||
vol.Optional(CONF_PORT): cv.string,
|
||||
vol.Optional(CONF_BAUDRATE): cv.positive_int,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def async_get_client_for_service_call(
|
||||
hass: HomeAssistant, call: ServiceCall
|
||||
) -> OctoprintClient:
|
||||
"""Get the client related to a service call (by device ID)."""
|
||||
device_id = call.data[CONF_DEVICE_ID]
|
||||
device_registry = dr.async_get(hass)
|
||||
|
||||
if device_entry := device_registry.async_get(device_id):
|
||||
for entry_id in device_entry.config_entries:
|
||||
if entry := hass.config_entries.async_get_entry(entry_id):
|
||||
if entry.domain == DOMAIN and entry.state is ConfigEntryState.LOADED:
|
||||
return cast(OctoprintConfigEntry, entry).runtime_data.octoprint
|
||||
|
||||
raise ServiceValidationError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="missing_client",
|
||||
translation_placeholders={
|
||||
"device_id": device_id,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@callback
|
||||
def async_setup_services(hass: HomeAssistant) -> None:
|
||||
"""Register services."""
|
||||
|
||||
async def async_printer_connect(call: ServiceCall) -> None:
|
||||
"""Connect to a printer."""
|
||||
client = async_get_client_for_service_call(hass, call)
|
||||
await client.connect(
|
||||
printer_profile=call.data.get(CONF_PROFILE_NAME),
|
||||
port=call.data.get(CONF_PORT),
|
||||
baud_rate=call.data.get(CONF_BAUDRATE),
|
||||
)
|
||||
|
||||
hass.services.async_register(
|
||||
DOMAIN,
|
||||
SERVICE_CONNECT,
|
||||
async_printer_connect,
|
||||
schema=SERVICE_CONNECT_SCHEMA,
|
||||
)
|
||||
Reference in New Issue
Block a user