Skip to content

Commit 161567e

Browse files
committed
documentation updates
1 parent 3eca7c0 commit 161567e

File tree

4 files changed

+50
-13
lines changed

4 files changed

+50
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,5 @@ Helping other users is another great way to contribute to pyscript!
9494

9595
## Copyright
9696

97-
Copyright (c) 2020-2022 Craig Barratt. May be freely used and copied according to the terms of the
97+
Copyright (c) 2020-2023 Craig Barratt. May be freely used and copied according to the terms of the
9898
[Apache 2.0 License](LICENSE).

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# -- Project information -----------------------------------------------------
2020

2121
project = 'hacs-pyscript'
22-
copyright = '2020-2022, Craig Barratt'
22+
copyright = '2020-2023, Craig Barratt'
2323
author = 'Craig Barratt'
2424

2525
# The full version, including alpha/beta/rc tags

docs/overview.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ to states and functions to services. Pyscript supports imports, although
3636
by default the valid import list is restricted for security reasons
3737
(there is a configuration option ``allow_all_imports`` to allow all
3838
imports). Pyscript supports almost all Python language features except
39-
generators, ``yield`` and user-defined function decorators
39+
generators, ``yield``, and defining special class methods.
4040
(see `language limitations <reference.html#language-limitations>`__).
4141
Pyscript provides a handful of additional built-in functions that connect
4242
to HASS features, like logging, accessing state variables as strings
43-
(if you need to compute their names dynamically), sleeping and waiting
44-
for triggers.
43+
(if you need to compute their names dynamically), running and managing
44+
tasks, sleeping and waiting for triggers.
4545

4646
Pyscript also provides a kernel that interfaces with the Jupyter
47-
front-ends (eg, notebook, console and lab). That allows you to develop
47+
front-ends (eg, notebook, console, lab and VSC). That allows you to develop
4848
and test pyscript code interactively. Plus you can interact with much of
4949
HASS by looking at state variables, calling services etc, in a similar
5050
way to `HASS
@@ -55,7 +55,7 @@ For more information about the Jupyter kernel, see the
5555
`README <https://github.com/craigbarratt/hass-pyscript-jupyter/blob/master/README.md>`__.
5656
There is also a `Jupyter notebook
5757
tutorial <https://nbviewer.jupyter.org/github/craigbarratt/hass-pyscript-jupyter/blob/master/pyscript_tutorial.ipynb>`__,
58-
which can be downloaded and run interactively in Jupyter notebook
58+
which can be downloaded and run interactively in Jupyter notebook or VSC
5959
connected to your live HASS with pyscript.
6060

6161
Pyscript provides functionality that complements the existing

docs/reference.rst

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,13 +1092,13 @@ level will not appear in the log. Each log message function uses a log name of t
10921092

10931093
.. code:: yaml
10941094
1095-
custom_components.pyscript.file.FILENAME.FUNCNAME
1095+
custom_components.pyscript.file.SCRIPTNAME.FUNCNAME
10961096
10971097
where ``FUNCNAME`` is the name of the top-level Python function (e.g., the one called by a trigger
1098-
or service), defined in the script file ``FILENAME.py``. See the `Global Context <#global-context>`__
1098+
or service), defined in the script file ``SCRIPTNAME.py``. See the `Global Context <#global-context>`__
10991099
section for the logging paths for other cases.
11001100

1101-
That allows you to set the log level for each Python top-level function separately if necessary.
1101+
That allows you to set the log level for each Python top-level script or function separately if necessary.
11021102
That setting also applies to any other Python functions that the top-level Python function calls.
11031103
For example, these settings:
11041104

@@ -1108,15 +1108,35 @@ For example, these settings:
11081108
default: info
11091109
logs:
11101110
custom_components.pyscript.file: info
1111-
custom_components.pyscript.file.my_scripts.my_function: debug
1111+
custom_components.pyscript.file.my_script.my_function: debug
11121112
11131113
will log all messages at ``info`` or higher (ie: ``log.info()``, ``log.warning()`` and
1114-
``log.error()``), and inside ``my_function`` defined in the script file ``my_scripts.py`` (and any
1114+
``log.error()``), and inside ``my_function`` defined in the script file ``my_script.py`` (and any
11151115
other functions it calls) will log all messages at ``debug`` or higher.
11161116

11171117
Note that in Jupyter, all the ``log`` functions will display output in your session, independent of
11181118
the ``logger`` configuration settings.
11191119

1120+
Changing the log level via the main configuration yaml file will typically require a restart of HASS.
1121+
To avoid that, you can set the log level for any specific logging path by calling the ``logger.set_level``
1122+
service from Jupyter, VSC, or in a ``pyscript`` script; for example:
1123+
1124+
.. code:: python
1125+
1126+
logger.set_level(**{"custom_components.pyscript.file.my_script": "debug"})
1127+
1128+
will change the log level for the ``my_script.py`` script to ``debug``, while:
1129+
1130+
.. code:: python
1131+
1132+
logger.set_level(**{"custom_components.pyscript.file.my_script.func1": "debug"})
1133+
1134+
will change the log level for the ``func1()`` function in ``my_script.py`` script to ``debug``.
1135+
You can change multiple components in a single call just by adding them to the ``**kwargs`` dict.
1136+
1137+
Alternatively, you can use the `Developer Tools <https://www.home-assistant.io/docs/tools/dev-tools/>`__
1138+
to call the ``logger.set_level`` service with the logging path and desired level as parameters.
1139+
11201140
Tasks
11211141
^^^^^
11221142

@@ -1409,12 +1429,21 @@ global context, to the granularity of specific functions eg:
14091429
default: info
14101430
logs:
14111431
custom_components.pyscript.file: info
1412-
custom_components.pyscript.file.my_scripts.my_function: debug
1432+
custom_components.pyscript.file.my_script.my_function: debug
14131433
custom_components.pyscript.apps.my_app: debug
14141434
custom_components.pyscript.apps.my_app.my_function: debug
14151435
14161436
Each Jupyter global context name is ``jupyter_NNN`` where ``NNN`` is a unique integer starting at 0.
14171437

1438+
You can set the log level for one or more logging paths by calling the ``logger.set_level`` service
1439+
from Jupyter, VSC, or in a ``pyscript`` script; for example:
1440+
1441+
.. code:: python
1442+
1443+
logger.set_level(**{"custom_components.pyscript.file.my_script": "debug"})
1444+
1445+
will change the log level for the ``my_script.py`` script to ``debug``.
1446+
14181447
When a script file has changed (or an app's configuration has changed, provided the ``yaml`` file is
14191448
below the ``pyscript`` directory), a reload is triggered, and the corresponding global context whose
14201449
names starts with ``file.``, ``modules.``, ``apps.`` or ``scripts.`` is removed. As each file is
@@ -1980,6 +2009,14 @@ Here are some areas where pyscript differs from real Python:
19802009
be declared ``async``. Unless the Python module is designed to support async callbacks, it is not
19812010
currently possible to have Python modules and packages call pyscript functions. The workaround is
19822011
to move your callbacks from pyscript and make them native Python functions; see `Importing <#importing>`__.
2012+
- Continuing that point, special methods (eg, `__eq__`) in a class created in `pyscript` will not work since
2013+
they are async functions and Python will not be able to call them. The two workarounds are to
2014+
use the ``@pyscript_compile`` decorator so the method is compiled to a native (non-ascync) Python
2015+
function, or write your class in native Python and import it into ``pyscript``; see `Importing <#importing>`__.
2016+
- The ``import`` function in ``pyscript`` fails to import certain complex packages. This is an open bug and
2017+
it would be great if someone with some Python expertise could help fix it. In the meantime, the workaround
2018+
is to import the module in a native Python file, and then import that shim module into pyscript.
2019+
See `Importing <#importing>`__.
19832020
- pyscript and the HASS primitives that it uses are not thread safe - the code should only be executed
19842021
in the main event loop. The ``task.executor()`` function is one way that regular Python code
19852022
(not pyscript code) can safely be executed in a separate thread. The ``threading`` package can

0 commit comments

Comments
 (0)