Skip to content

Commit 8daa4b8

Browse files
committed
Persist pyscript.* states between home assistant restarts
With this change any states set by pyscript in the pyscript domain will be persisted when home assistant next reboots. For example when a script runs `pyscript.abc = 'foo'` then this value will be persisted. Fixes #47
1 parent b7bb235 commit 8daa4b8

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

custom_components/pyscript/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
)
1818
from homeassistant.exceptions import HomeAssistantError
1919
import homeassistant.helpers.config_validation as cv
20+
from homeassistant.helpers.restore_state import RestoreStateData
2021
from homeassistant.loader import bind_hass
2122

2223
from .const import CONF_ALLOW_ALL_IMPORTS, DOMAIN, FOLDER, LOGGER_PATH, SERVICE_JUPYTER_KERNEL_START
@@ -39,6 +40,7 @@
3940

4041
async def async_setup(hass, config):
4142
"""Component setup, run import config flow for each entry in config."""
43+
await restore_state(hass)
4244
if DOMAIN in config:
4345
hass.async_create_task(
4446
hass.config_entries.flow.async_init(
@@ -49,6 +51,14 @@ async def async_setup(hass, config):
4951
return True
5052

5153

54+
async def restore_state(hass):
55+
restore_data = await RestoreStateData.async_get_instance(hass)
56+
for entity_id, value in restore_data.last_states.items():
57+
if entity_id.startswith("pyscript."):
58+
last_state = value.state
59+
hass.states.async_set(entity_id, last_state.state, last_state.attributes)
60+
61+
5262
async def async_setup_entry(hass, config_entry):
5363
"""Initialize the pyscript config entry."""
5464
Function.init(hass)

custom_components/pyscript/eval.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,7 @@ async def recurse_assign(self, lhs, val):
11681168
if not isinstance(var_name, str):
11691169
raise NotImplementedError(f"unknown lhs type {lhs} (got {var_name}) in assign")
11701170
if var_name.find(".") >= 0:
1171-
State.set(var_name, val)
1171+
await State.set(var_name, val)
11721172
return
11731173
if self.curr_func and var_name in self.curr_func.global_names:
11741174
self.global_sym_table[var_name] = val

custom_components/pyscript/state.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import logging
44

5+
from homeassistant.helpers.restore_state import RestoreStateData
6+
57
from .const import LOGGER_PATH
68
from .function import Function
79

@@ -98,7 +100,7 @@ def notify_var_get(cls, var_names, new_vars):
98100
return notify_vars
99101

100102
@classmethod
101-
def set(cls, var_name, value, new_attributes=None, **kwargs):
103+
async def set(cls, var_name, value, new_attributes=None, **kwargs):
102104
"""Set a state variable and optional attributes in hass."""
103105
if var_name.count(".") != 1:
104106
raise NameError(f"invalid name {var_name} (should be 'domain.entity')")
@@ -113,6 +115,12 @@ def set(cls, var_name, value, new_attributes=None, **kwargs):
113115
new_attributes.update(kwargs)
114116
_LOGGER.debug("setting %s = %s, attr = %s", var_name, value, new_attributes)
115117
cls.notify_var_last[var_name] = str(value)
118+
119+
if var_name.startswith("pyscript."):
120+
# have this var tracked for restore
121+
restore_data = await RestoreStateData.async_get_instance(cls.hass)
122+
restore_data.async_restore_entity_added(var_name)
123+
116124
cls.hass.states.async_set(var_name, value, new_attributes)
117125

118126
@classmethod

0 commit comments

Comments
 (0)