Skip to content

state.persist with defined default_value does not initialize pyscript.<attribute> #440

@maciejmatczak

Description

@maciejmatczak

Hello, I have a trouble with using a state.persist with a default value.

I expected, that whenever I register new state to be persistent, the attribute of pyscript module will be available. It looks like the attribute of the pyscript module does not get initialize if I do provide a default value, though.

import pyscript


DEVICE_ID = "xyz"


state.persist('pyscript.something_state', default_value=0)


@event_trigger("zha_event", f"device_id == '{DEVICE_ID}' and command == 'single'")
def switch_single(**kwargs):
    pyscript.something_state += 1
    log.info(f"Got clicked {pyscript.something_state} times")

Yields:

pyscript.something_state += 1
^
AttributeError: module 'pyscript' has no attribute 'something_state'

I suppose I might use try/except to initialize it on my own (as mentioned in #96 (comment) by @dlashua), but shouldn't it be done by state.persist(..., default_value=<value>)?

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