Skip to content

Connection problems

Andreas Cambitsis edited this page Jul 24, 2022 · 9 revisions

If Jupyter is unable to connect to pyscript in HASS, here are a few debugging suggestions:

  • To confirm that pyscript is installed and running, look in the Services tab in the Developer Tools and confirm you can see a service pyscript.jupyter_kernel_start.
  • Does your HASS host have a firewall that prevents inbound tcp connections? For each Jupyter session, pyscript listens for connections on 5 tcp ports starting at 50321 on the HASS host. If you are running a firewall you should open up at least 20 ports starting at 50321 (to allow a couple of concurrent sessions). If your firewall allows, you could limit inbound connections to only be permitted from the client running Jupyter.
  • Assuming you have installed hass_pyscript_kernel version 1.0.0 or later, run jupyter pyscript info and check the vales for hass_host, hass_url, hass_token and hass_proxy.
  • In kernel.json (in the directory listed by jupyter pyscript info), add a -vvv argument after hass_pyscript_kernel to turn on debugging, eg:
{
 "argv": [
  "python",
  "-m",
  "hass_pyscript_kernel",
  "-vvv",
  "-f",
  "{connection_file}"
 ],
 "display_name": "hass pyscript",
 "language": "python"
}
  • Turn on debugging for pyscript, eg in the HASS configuration file:
logger:
  logs:
    custom_components.pyscript: debug
  • Run Jupyter to see if the debug output gives a clue. With Juptyer console, here's an example of the expected debug messages in the HASS log:
2020-08-28 22:20:21 DEBUG (MainThread) [custom_components.pyscript] service call to jupyter_kernel_start: {'ip': '127.0.0.1', 'key': '2ef959d7-d48314ef7a93ff9557e334f6', 'transport': 'tcp', 'signature_scheme': 'hmac-sha256', 'kernel_name': 'pyscript', 'state_var': 'pyscript.jupyter_ports_f4b39c0745'}
2020-08-28 22:20:21 INFO (MainThread) [custom_components.pyscript.jupyter_kernel] Starting session jupyter_0
2020-08-28 22:20:21 DEBUG (MainThread) [custom_components.pyscript.jupyter_kernel] iopub_listen connected
2020-08-28 22:20:21 DEBUG (MainThread) [custom_components.pyscript.jupyter_kernel] control_listen connected
2020-08-28 22:20:21 DEBUG (MainThread) [custom_components.pyscript.jupyter_kernel] control_listen connected
2020-08-28 22:20:21 DEBUG (MainThread) [custom_components.pyscript.jupyter_kernel] stdin_listen connected
2020-08-28 22:20:21 DEBUG (MainThread) [custom_components.pyscript.jupyter_kernel] shell_listen connected
2020-08-28 22:20:21 DEBUG (MainThread) [custom_components.pyscript.jupyter_kernel] heartbeat_listen connected

If you don't see the service call message that means hass_pyscript_kernel.py was not successful in making the service call via the httpd request. With the -vvv option you should see output from hass_pyscript_kernel.py (where you started Jupyter) that shows the URL it is attempting and the result.

Here's an example of output from hass_pyscript_kernel with the -vvv options when running Jupyter console:

hass_pyscript_kernel: got jupyter client config={'shell_port': 51052, 'iopub_port': 51053, 'stdin_port': 51054, 'control_port': 51055, 'hb_port': 51056, 'ip': '127.0.0.1', 'key': '2ef959d7-d48314ef7a93ff9557e334f6', 'transport': 'tcp', 'signature_scheme': 'hmac-sha256', 'kernel_name': 'pyscript'}
hass_pyscript_kernel: about to do service call post http://localhost:8123/api/services/pyscript/jupyter_kernel_start
hass_pyscript_kernel: service call put http://localhost:8123/api/services/pyscript/jupyter_kernel_start returned 200
hass_pyscript_kernel: about to do state get http://localhost:8123/api/states/pyscript.jupyter_ports_f4b39c0745
hass_pyscript_kernel: state variable get http://localhost:8123/api/states/pyscript.jupyter_ports_f4b39c0745 returned {'iopub_port': 43504, 'hb_port': 43505, 'control_port': 43506, 'stdin_port': 43507, 'shell_port': 43508}
hass_pyscript_kernel: hb_port listening for jupyter client at 127.0.0.1:51056
hass_pyscript_kernel: stdin_port listening for jupyter client at 127.0.0.1:51054
hass_pyscript_kernel: shell_port listening for jupyter client at 127.0.0.1:51052
hass_pyscript_kernel: iopub_port listening for jupyter client at 127.0.0.1:51053
hass_pyscript_kernel: control_port listening for jupyter client at 127.0.0.1:51055
hass_pyscript_kernel: iopub_port connected to jupyter client; now trying pyscript kernel at 127.0.0.1:43504
hass_pyscript_kernel: iopub_port pyscript kernel connected at 127.0.0.1:43504
hass_pyscript_kernel: control_port connected to jupyter client; now trying pyscript kernel at 127.0.0.1:43506
hass_pyscript_kernel: control_port pyscript kernel connected at 127.0.0.1:43506
hass_pyscript_kernel: control_port connected to jupyter client; now trying pyscript kernel at 127.0.0.1:43506
hass_pyscript_kernel: control_port pyscript kernel connected at 127.0.0.1:43506
hass_pyscript_kernel: stdin_port connected to jupyter client; now trying pyscript kernel at 127.0.0.1:43507
hass_pyscript_kernel: stdin_port pyscript kernel connected at 127.0.0.1:43507
hass_pyscript_kernel: shell_port connected to jupyter client; now trying pyscript kernel at 127.0.0.1:43508
hass_pyscript_kernel: shell_port pyscript kernel connected at 127.0.0.1:43508
hass_pyscript_kernel: hb_port connected to jupyter client; now trying pyscript kernel at 127.0.0.1:43505
hass_pyscript_kernel: hb_port pyscript kernel connected at 127.0.0.1:43505

and here's the output when Jupyter exits:

hass_pyscript_kernel: shell_port shutting down connections
hass_pyscript_kernel: iopub_port shutting down connections
hass_pyscript_kernel: hb_port shutting down connections
hass_pyscript_kernel: stdin_port shutting down connections
hass_pyscript_kernel: control_port shutting down connections
hass_pyscript_kernel: control_port shutting down connections

Note: refer to this issue if you are using https and having issues connecting.

Clone this wiki locally