Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions custom_components/pyscript/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def write_stubs(path: str) -> dict[str, Any]:
DOMAIN, SERVICE_GENERATE_STUBS, generate_stubs_service, supports_response=SupportsResponse.ONLY
)

async def jupyter_kernel_start(call: ServiceCall) -> None:
async def jupyter_kernel_start(call: ServiceCall) -> dict | None:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only returns a dict now, so remove None.

"""Handle Jupyter kernel start call."""
_LOGGER.debug("service call to jupyter_kernel_start: %s", call.data)

Expand All @@ -369,14 +369,25 @@ async def jupyter_kernel_start(call: ServiceCall) -> None:
Function.install_ast_funcs(ast_ctx)
kernel = Kernel(call.data, ast_ctx, global_ctx, global_ctx_name)
await kernel.session_start()
hass.states.async_set(call.data["state_var"], json.dumps(kernel.get_ports()))

def state_var_remove():
hass.states.async_remove(call.data["state_var"])
# older kernels look for this state var
if "state_var" in call.data:
hass.states.async_set(call.data["state_var"], json.dumps(kernel.get_ports()))

kernel.set_session_cleanup_callback(state_var_remove)
def state_var_remove():
hass.states.async_remove(call.data["state_var"])

hass.services.async_register(DOMAIN, SERVICE_JUPYTER_KERNEL_START, jupyter_kernel_start)
kernel.set_session_cleanup_callback(state_var_remove)

# newer kernels use the service response
return {'ports': kernel.get_ports()}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use double-quotes on "ports"


hass.services.async_register(
DOMAIN,
SERVICE_JUPYTER_KERNEL_START,
jupyter_kernel_start,
supports_response=SupportsResponse.OPTIONAL,
)

async def state_changed(event: HAEvent) -> None:
var_name = event.data["entity_id"]
Expand Down
Loading