8
8
from homeassistant .helpers .service import async_get_all_descriptions
9
9
10
10
from .const import LOGGER_PATH
11
+ from .entity import PyscriptEntity
11
12
from .function import Function
12
13
13
14
_LOGGER = logging .getLogger (LOGGER_PATH + ".state" )
@@ -56,7 +57,7 @@ class State:
56
57
#
57
58
# pyscript vars which have already been registered as persisted
58
59
#
59
- persisted_vars = set ()
60
+ persisted_vars = {}
60
61
61
62
#
62
63
# other parameters of all services that have "entity_id" as a parameter
@@ -198,6 +199,10 @@ def set(cls, var_name, value=None, new_attributes=None, **kwargs):
198
199
#
199
200
cls .notify_var_last [var_name ] = StateVal (cls .hass .states .get (var_name ))
200
201
202
+ if var_name in cls .persisted_vars :
203
+ cls .persisted_vars [var_name ].set_state (value )
204
+ cls .persisted_vars [var_name ].set_attributes (new_attributes )
205
+
201
206
@classmethod
202
207
def setattr (cls , var_attr_name , value ):
203
208
"""Set a state variable's attribute in hass."""
@@ -213,8 +218,13 @@ async def register_persist(cls, var_name):
213
218
"""Register pyscript state variable to be persisted with RestoreState."""
214
219
if var_name .startswith ("pyscript." ) and var_name not in cls .persisted_vars :
215
220
restore_data = await RestoreStateData .async_get_instance (cls .hass )
216
- restore_data .async_restore_entity_added (var_name )
217
- cls .persisted_vars .add (var_name )
221
+ this_entity = PyscriptEntity ()
222
+ this_entity .entity_id = var_name
223
+ cls .persisted_vars [var_name ] = this_entity
224
+ try :
225
+ restore_data .async_restore_entity_added (this_entity )
226
+ except TypeError :
227
+ restore_data .async_restore_entity_added (var_name )
218
228
219
229
@classmethod
220
230
async def persist (cls , var_name , default_value = None , default_attributes = None ):
0 commit comments