Skip to content

Commit

Permalink
directly access hass.data[DATA_RESTORE_STATE] to avoid deprecated api
Browse files Browse the repository at this point in the history
see #473.  This is a hack; should re-implement using RestoreEntity.
  • Loading branch information
craigbarratt committed Jun 18, 2023
1 parent 3225f41 commit 716ffd7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions custom_components/pyscript/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from homeassistant.exceptions import HomeAssistantError
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.event import Event as HAEvent
from homeassistant.helpers.restore_state import RestoreStateData
from homeassistant.helpers.restore_state import DATA_RESTORE_STATE
from homeassistant.loader import bind_hass

from .const import (
Expand Down Expand Up @@ -80,7 +80,8 @@ async def async_setup(hass: HomeAssistant, config: Config) -> bool:

async def restore_state(hass: HomeAssistant) -> None:
"""Restores the persisted pyscript state."""
restore_data = await RestoreStateData.async_get_instance(hass)
# this is a hack accessing hass internals; should re-implement using RestoreEntity
restore_data = hass.data[DATA_RESTORE_STATE]
for entity_id, value in restore_data.last_states.items():
if entity_id.startswith("pyscript."):
last_state = value.state
Expand Down
5 changes: 3 additions & 2 deletions custom_components/pyscript/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging

from homeassistant.core import Context
from homeassistant.helpers.restore_state import RestoreStateData
from homeassistant.helpers.restore_state import DATA_RESTORE_STATE
from homeassistant.helpers.service import async_get_all_descriptions

from .const import LOGGER_PATH
Expand Down Expand Up @@ -217,7 +217,8 @@ def setattr(cls, var_attr_name, value):
async def register_persist(cls, var_name):
"""Register pyscript state variable to be persisted with RestoreState."""
if var_name.startswith("pyscript.") and var_name not in cls.persisted_vars:
restore_data = await RestoreStateData.async_get_instance(cls.hass)
# this is a hack accessing hass internals; should re-implement using RestoreEntity
restore_data = cls.hass.data[DATA_RESTORE_STATE]
this_entity = PyscriptEntity()
this_entity.entity_id = var_name
cls.persisted_vars[var_name] = this_entity
Expand Down

0 comments on commit 716ffd7

Please sign in to comment.