From 1c1032daffca877829d693afc5234826d03520b7 Mon Sep 17 00:00:00 2001 From: Daniel Lashua Date: Fri, 4 Mar 2022 00:21:51 +0000 Subject: [PATCH 01/10] temporary fix for state.persist --- custom_components/pyscript/state.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/custom_components/pyscript/state.py b/custom_components/pyscript/state.py index 312ef95..9be4b97 100644 --- a/custom_components/pyscript/state.py +++ b/custom_components/pyscript/state.py @@ -4,8 +4,9 @@ import logging from homeassistant.core import Context -from homeassistant.helpers.restore_state import RestoreStateData +from homeassistant.helpers.restore_state import RestoreStateData, RestoreEntity from homeassistant.helpers.service import async_get_all_descriptions +from homeassistant.helpers.entity import Entity from .const import LOGGER_PATH from .function import Function @@ -15,6 +16,10 @@ STATE_VIRTUAL_ATTRS = {"last_changed", "last_updated"} +class PyscriptEntity(RestoreEntity): + pass + + class StateVal(str): """Class for representing the value and attributes of a state variable.""" @@ -56,7 +61,7 @@ class State: # # pyscript vars which have already been registered as persisted # - persisted_vars = set() + persisted_vars = {} # # other parameters of all services that have "entity_id" as a parameter @@ -198,6 +203,10 @@ def set(cls, var_name, value=None, new_attributes=None, **kwargs): # cls.notify_var_last[var_name] = StateVal(cls.hass.states.get(var_name)) + if var_name in cls.persisted_vars: + cls.persisted_vars[var_name]._attr_state = value + cls.persisted_vars[var_name]._attr_extra_state_attributes = new_attributes + @classmethod def setattr(cls, var_attr_name, value): """Set a state variable's attribute in hass.""" @@ -213,8 +222,10 @@ 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) - restore_data.async_restore_entity_added(var_name) - cls.persisted_vars.add(var_name) + x = PyscriptEntity() + x.entity_id = var_name + restore_data.async_restore_entity_added(x) + cls.persisted_vars[var_name] = x @classmethod async def persist(cls, var_name, default_value=None, default_attributes=None): From 51a940ae778ba6fcdc1d316ac0c9d5aeee1c7a97 Mon Sep 17 00:00:00 2001 From: Daniel Lashua Date: Fri, 4 Mar 2022 00:30:00 +0000 Subject: [PATCH 02/10] remove unused import --- custom_components/pyscript/state.py | 1 - 1 file changed, 1 deletion(-) diff --git a/custom_components/pyscript/state.py b/custom_components/pyscript/state.py index 9be4b97..17c4594 100644 --- a/custom_components/pyscript/state.py +++ b/custom_components/pyscript/state.py @@ -6,7 +6,6 @@ from homeassistant.core import Context from homeassistant.helpers.restore_state import RestoreStateData, RestoreEntity from homeassistant.helpers.service import async_get_all_descriptions -from homeassistant.helpers.entity import Entity from .const import LOGGER_PATH from .function import Function From af363ca7768de94b3101a6ec0d8cdbfe3fbf870d Mon Sep 17 00:00:00 2001 From: Daniel Lashua Date: Fri, 4 Mar 2022 00:33:47 +0000 Subject: [PATCH 03/10] clean up --- custom_components/pyscript/state.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/custom_components/pyscript/state.py b/custom_components/pyscript/state.py index 17c4594..7edf52f 100644 --- a/custom_components/pyscript/state.py +++ b/custom_components/pyscript/state.py @@ -15,10 +15,6 @@ STATE_VIRTUAL_ATTRS = {"last_changed", "last_updated"} -class PyscriptEntity(RestoreEntity): - pass - - class StateVal(str): """Class for representing the value and attributes of a state variable.""" @@ -221,10 +217,10 @@ 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) - x = PyscriptEntity() - x.entity_id = var_name - restore_data.async_restore_entity_added(x) - cls.persisted_vars[var_name] = x + this_entity = RestoreEntity() + this_entity.entity_id = var_name + restore_data.async_restore_entity_added(this_entity) + cls.persisted_vars[var_name] = this_entity @classmethod async def persist(cls, var_name, default_value=None, default_attributes=None): From 8fac6e83dc342ebe8015a718ceeebd4f6b538328 Mon Sep 17 00:00:00 2001 From: Daniel Lashua Date: Fri, 4 Mar 2022 23:16:58 +0000 Subject: [PATCH 04/10] more proper fix --- custom_components/pyscript/entity.py | 9 +++++++++ custom_components/pyscript/state.py | 9 +++++---- 2 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 custom_components/pyscript/entity.py diff --git a/custom_components/pyscript/entity.py b/custom_components/pyscript/entity.py new file mode 100644 index 0000000..531b070 --- /dev/null +++ b/custom_components/pyscript/entity.py @@ -0,0 +1,9 @@ +from homeassistant.helpers.restore_state import RestoreEntity + + +class PyscriptEntity(RestoreEntity): + def set_state(self, state): + self._attr_state = state + + def set_attributes(self, attributes): + self._attr_extra_state_attributes = attributes diff --git a/custom_components/pyscript/state.py b/custom_components/pyscript/state.py index 7edf52f..415fe40 100644 --- a/custom_components/pyscript/state.py +++ b/custom_components/pyscript/state.py @@ -4,10 +4,11 @@ import logging from homeassistant.core import Context -from homeassistant.helpers.restore_state import RestoreStateData, RestoreEntity +from homeassistant.helpers.restore_state import RestoreStateData from homeassistant.helpers.service import async_get_all_descriptions from .const import LOGGER_PATH +from .entity import PyscriptEntity from .function import Function _LOGGER = logging.getLogger(LOGGER_PATH + ".state") @@ -199,8 +200,8 @@ def set(cls, var_name, value=None, new_attributes=None, **kwargs): cls.notify_var_last[var_name] = StateVal(cls.hass.states.get(var_name)) if var_name in cls.persisted_vars: - cls.persisted_vars[var_name]._attr_state = value - cls.persisted_vars[var_name]._attr_extra_state_attributes = new_attributes + cls.persisted_vars[var_name].set_state(value) + cls.persisted_vars[var_name].set_attributes(new_attributes) @classmethod def setattr(cls, var_attr_name, value): @@ -217,7 +218,7 @@ 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_entity = RestoreEntity() + this_entity = PyscriptEntity() this_entity.entity_id = var_name restore_data.async_restore_entity_added(this_entity) cls.persisted_vars[var_name] = this_entity From 12e3e3f5fa29316eaf935685a913d4617833f4e8 Mon Sep 17 00:00:00 2001 From: Daniel Lashua Date: Fri, 4 Mar 2022 23:39:18 +0000 Subject: [PATCH 05/10] happy tests --- custom_components/pyscript/entity.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/custom_components/pyscript/entity.py b/custom_components/pyscript/entity.py index 531b070..8355892 100644 --- a/custom_components/pyscript/entity.py +++ b/custom_components/pyscript/entity.py @@ -1,9 +1,14 @@ +"""Entity Classes""" from homeassistant.helpers.restore_state import RestoreEntity class PyscriptEntity(RestoreEntity): + """Generic Pyscript Entity""" + def set_state(self, state): + """Set the state""" self._attr_state = state def set_attributes(self, attributes): + """Set Attributes""" self._attr_extra_state_attributes = attributes From f0845befa7c69ce9473bc023a5a46504ff1d565b Mon Sep 17 00:00:00 2001 From: Daniel Lashua Date: Fri, 4 Mar 2022 23:45:10 +0000 Subject: [PATCH 06/10] even happier tests --- custom_components/pyscript/entity.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/custom_components/pyscript/entity.py b/custom_components/pyscript/entity.py index 8355892..58e1aba 100644 --- a/custom_components/pyscript/entity.py +++ b/custom_components/pyscript/entity.py @@ -1,10 +1,17 @@ """Entity Classes""" from homeassistant.helpers.restore_state import RestoreEntity +from collections.abc import MutableMapping +from typing import Any +from homeassistant.helpers.typing import StateType +from homeassistant.const import STATE_UNKNOWN class PyscriptEntity(RestoreEntity): """Generic Pyscript Entity""" + _attr_extra_state_attributes: MutableMapping[str, Any] + _attr_state: StateType = STATE_UNKNOWN + def set_state(self, state): """Set the state""" self._attr_state = state From 291936f69507dde42c1ba0c7ce01d9e07b67d5eb Mon Sep 17 00:00:00 2001 From: Daniel Lashua Date: Fri, 4 Mar 2022 23:59:22 +0000 Subject: [PATCH 07/10] even more happiness --- custom_components/pyscript/entity.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/custom_components/pyscript/entity.py b/custom_components/pyscript/entity.py index 58e1aba..7326ee1 100644 --- a/custom_components/pyscript/entity.py +++ b/custom_components/pyscript/entity.py @@ -1,6 +1,5 @@ """Entity Classes""" from homeassistant.helpers.restore_state import RestoreEntity -from collections.abc import MutableMapping from typing import Any from homeassistant.helpers.typing import StateType from homeassistant.const import STATE_UNKNOWN @@ -9,7 +8,7 @@ class PyscriptEntity(RestoreEntity): """Generic Pyscript Entity""" - _attr_extra_state_attributes: MutableMapping[str, Any] + _attr_extra_state_attributes: dict _attr_state: StateType = STATE_UNKNOWN def set_state(self, state): From 53e6de2a1ebb4efa7fced8453464937d0c15b5f4 Mon Sep 17 00:00:00 2001 From: Daniel Lashua Date: Sat, 5 Mar 2022 00:07:45 +0000 Subject: [PATCH 08/10] and more happiness still --- custom_components/pyscript/entity.py | 1 - 1 file changed, 1 deletion(-) diff --git a/custom_components/pyscript/entity.py b/custom_components/pyscript/entity.py index 7326ee1..7b00e06 100644 --- a/custom_components/pyscript/entity.py +++ b/custom_components/pyscript/entity.py @@ -1,6 +1,5 @@ """Entity Classes""" from homeassistant.helpers.restore_state import RestoreEntity -from typing import Any from homeassistant.helpers.typing import StateType from homeassistant.const import STATE_UNKNOWN From 358963a525dfdf41052f1bc97eadcfda62996753 Mon Sep 17 00:00:00 2001 From: Daniel Lashua Date: Sat, 5 Mar 2022 12:34:05 +0000 Subject: [PATCH 09/10] add compatibility with previous HASS versions --- custom_components/pyscript/state.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/custom_components/pyscript/state.py b/custom_components/pyscript/state.py index 415fe40..d8b515b 100644 --- a/custom_components/pyscript/state.py +++ b/custom_components/pyscript/state.py @@ -220,8 +220,11 @@ async def register_persist(cls, var_name): restore_data = await RestoreStateData.async_get_instance(cls.hass) this_entity = PyscriptEntity() this_entity.entity_id = var_name - restore_data.async_restore_entity_added(this_entity) cls.persisted_vars[var_name] = this_entity + try: + restore_data.async_restore_entity_added(this_entity) + except TypeError as e: + restore_data.async_restore_entity_added(var_name) @classmethod async def persist(cls, var_name, default_value=None, default_attributes=None): From 88d3c9cdbe25de5eabb99883c97400eacd533078 Mon Sep 17 00:00:00 2001 From: Daniel Lashua Date: Sat, 5 Mar 2022 12:39:31 +0000 Subject: [PATCH 10/10] happy tests --- custom_components/pyscript/state.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/pyscript/state.py b/custom_components/pyscript/state.py index d8b515b..12e2231 100644 --- a/custom_components/pyscript/state.py +++ b/custom_components/pyscript/state.py @@ -223,7 +223,7 @@ async def register_persist(cls, var_name): cls.persisted_vars[var_name] = this_entity try: restore_data.async_restore_entity_added(this_entity) - except TypeError as e: + except TypeError: restore_data.async_restore_entity_added(var_name) @classmethod