Skip to content

modules and decorators #126

Closed
Closed
@dlashua

Description

@dlashua

Trigger registers twice when using decorator in a module.

Test code

modules/dec_test.py:

def startup_with_note(note):

    def inner_dec(func):
        nonlocal note

        @time_trigger('startup')
        def inner(*args, **kwargs):
            nonlocal note
            log.info(f"NOTE: {note}")
            return func(*args, **kwargs)

        return inner

    return inner_dec

script file:

import dec_test

@dec_test.startup_with_note('this is the note 1')
def at_startup():
    log.info('startup 1')

logs:

2020-12-26 07:51:48 INFO (MainThread) [custom_components.pyscript.global_ctx] Reloaded /config/pyscript/scripts/test/dec_test_script.py
2020-12-26 07:51:48 INFO (MainThread) [custom_components.pyscript.modules.dec_test.inner] NOTE: this is the note 1
2020-12-26 07:51:48 INFO (MainThread) [custom_components.pyscript.modules.dec_test.inner] startup 1
2020-12-26 07:51:48 INFO (MainThread) [custom_components.pyscript.modules.dec_test.inner] NOTE: this is the note 1
2020-12-26 07:51:48 INFO (MainThread) [custom_components.pyscript.modules.dec_test.inner] startup 1

Moving the function out of the module and into the script file makes the log lines only show once.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions