Add new Remote Python Debugger integration (#36960)

This commit is contained in:
Franck Nijhof
2020-06-22 15:17:59 +02:00
committed by GitHub
parent 9d40ae96b5
commit b47be05efc
9 changed files with 160 additions and 1 deletions

View File

@@ -0,0 +1,79 @@
"""The Remote Python Debugger integration."""
from asyncio import Event
import logging
from threading import Thread
from typing import Optional
import debugpy
import voluptuous as vol
from homeassistant.const import CONF_HOST, CONF_PORT
from homeassistant.core import HomeAssistant, ServiceCall
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.service import async_register_admin_service
from homeassistant.helpers.typing import ConfigType
DOMAIN = "debugpy"
CONF_WAIT = "wait"
CONF_START = "start"
SERVICE_START = "start"
CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
vol.Optional(CONF_HOST, default="0.0.0.0"): cv.string,
vol.Optional(CONF_PORT, default=5678): cv.port,
vol.Optional(CONF_START, default=True): cv.boolean,
vol.Optional(CONF_WAIT, default=False): cv.boolean,
}
)
},
extra=vol.ALLOW_EXTRA,
)
_LOGGER = logging.getLogger(__name__)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Remote Python Debugger component."""
conf = config[DOMAIN]
async def debug_start(
call: Optional[ServiceCall] = None, *, wait: bool = True
) -> None:
"""Start the debugger."""
debugpy.listen((conf[CONF_HOST], conf[CONF_PORT]))
wait = conf[CONF_WAIT]
if wait:
_LOGGER.warning(
"Waiting for remote debug connection on %s:%s",
conf[CONF_HOST],
conf[CONF_PORT],
)
ready = Event()
def waitfor():
debugpy.wait_for_client()
hass.loop.call_soon_threadsafe(ready.set)
Thread(target=waitfor).start()
await ready.wait()
else:
_LOGGER.warning(
"Listening for remote debug connection on %s:%s",
conf[CONF_HOST],
conf[CONF_PORT],
)
async_register_admin_service(
hass, DOMAIN, SERVICE_START, debug_start, schema=vol.Schema({})
)
# If set to start the debugger on startup, do so
if conf[CONF_START]:
await debug_start(wait=conf[CONF_WAIT])
return True

View File

@@ -0,0 +1,8 @@
{
"domain": "debugpy",
"name": "Remote Python Debugger",
"documentation": "https://www.home-assistant.io/integrations/debugpy",
"requirements": ["debugpy==1.0.0b11"],
"codeowners": ["@frenck"],
"quality_scale": "internal"
}

View File

@@ -0,0 +1,3 @@
# Describes the format for available Remote Python Debugger services
start:
description: Start the Remote Python Debugger.