From 64a8c2f5eba93d3de3ad29a34f2f3d22b0122827 Mon Sep 17 00:00:00 2001 From: Piotr Machowski Date: Sat, 30 Sep 2023 04:37:31 +0200 Subject: [PATCH 1/3] Add dedicated jinja functions --- README.md | 102 +++++++++++++------------- custom_components/saver/__init__.py | 101 ++++++++++++++++++++++--- custom_components/saver/manifest.json | 2 +- custom_components/saver/services.yaml | 43 +++++++++-- 4 files changed, 181 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index 43abcb6..d2333db 100644 --- a/README.md +++ b/README.md @@ -22,18 +22,18 @@ [community_forum]: https://community.home-assistant.io/t/custom-component-saver/204249 -[![Open your Home Assistant instance and start setting up a new integration.](https://my.home-assistant.io/badges/config_flow_start.svg)](https://my.home-assistant.io/redirect/config_flow_start/?domain=saver) - # Saver This custom component allows you to save current state of any entity and use its data later to restore it. -Additionally you can create simple variables and use their values in scripts. +Additionally, you can create simple variables and use their values in scripts. ## Installation ### Using [HACS](https://hacs.xyz/) (recommended) +[![Open your Home Assistant instance and open a repository inside the Home Assistant Community Store.](https://my.home-assistant.io/badges/hacs_repository.svg)](https://my.home-assistant.io/redirect/hacs_repository/?owner=PiotrMachowski&repository=Home-Assistant-custom-components-Saver&category=Integration) + This integration can be installed using HACS. To do it search for `Saver` in *Integrations* section. @@ -50,15 +50,19 @@ rm saver.zip ## Configuration -### GUI +### Using UI + +[![Open your Home Assistant instance and start setting up a new integration.](https://my.home-assistant.io/badges/config_flow_start.svg)](https://my.home-assistant.io/redirect/config_flow_start/?domain=saver) + From the Home Assistant front page go to **Configuration** and then select **Integrations** from the list. -Use the plus button in the bottom right to add a new integration called **Saver**. +Use the "plus" button in the bottom right to add a new integration called **Saver**. The success dialog will appear or an error will be displayed in the popup. ### YAML -In configuration.yaml: + +Add following code in configuration.yaml: ```yaml saver: ``` @@ -73,28 +77,6 @@ data: entity_id: cover.living_room ``` -### Restore state -Executes the script using saved state of the entity. - -To use state of an entity you have to use a following value in a template: `state`. - -To use state attribute (in this case `current_position`) of an entity you have to use a following value in a template: `attr_current_position`. - -```yaml -service: saver.restore_state -data: - entity_id: cover.living_room - restore_script: - - service: cover.set_cover_position - data_template: - entity_id: cover.living_room - position: "{{ '{{ attr_current_position | int }}' }}" - - service: input_text.set_value - data_template: - entity_id: input_text.cover_description - value: "Cover is now {{ '{{ state }}' }}" -``` - ### Delete saved state Deletes a saved state for an entity. ```yaml @@ -120,7 +102,41 @@ data: name: counter ``` -### Execute script +### Clear +Deletes all saved data. +```yaml +service: saver.clear +``` + +### Deprecated services + +
+Show + + +#### Restore state +Executes the script using saved state of the entity. + +To use state of an entity you have to use a following value in a template: `state`. + +To use state attribute (in this case `current_position`) of an entity you have to use a following value in a template: `attr_current_position`. + +```yaml +service: saver.restore_state +data: + entity_id: cover.living_room + restore_script: + - service: cover.set_cover_position + data_template: + entity_id: cover.living_room + position: "{{ '{{ attr_current_position | int }}' }}" + - service: input_text.set_value + data_template: + entity_id: input_text.cover_description + value: "Cover is now {{ '{{ state }}' }}" +``` + +#### Execute script Executes a script using all saved entities and variables. To use state of an entity (in this case `cover.living_room`) you have to use a following value in a template: `cover_living_room_state`. @@ -145,28 +161,16 @@ data: value: "Counter has value {{ '{{ counter }}' }}" ``` -### Clear -Deletes all saved data. -```yaml -service: saver.clear -``` -## Using in templates -It is possible to use saved data in templates via `saver.saver` entity: +
+ + +## Using saved values in templates +It is possible to use saved data in templates using `saver_entity` and `saver_variable` functions: ```yaml -script: - - service: cover.set_cover_position - data_template: - entity_id: cover.living_room - position: "{{ state_attr('saver.saver', 'entities')['cover.living_room'].attributes.current_position | int }}" - - service: input_text.set_value - data_template: - entity_id: input_text.cover_description - value: "Cover is now {{ state_attr('saver.saver', 'entities')['cover.living_room'].state }}" - - service: input_text.set_value - data_template: - entity_id: input_text.counter_description - value: "Counter has value {{ state_attr('saver.saver', 'variables')['counter'] }}" +{{ saver_entity("sun.sun") }} # returns saved state of "sun.sun" entity +{{ saver_entity("sun.sun", "azimuth") }} # returns "azimuth" attribute of saved "sun.sun" entity +{{ saver_variable("counter") }} # returns saved variable "counter" ``` ## Events diff --git a/custom_components/saver/__init__.py b/custom_components/saver/__init__.py index 9beb7de..4fa85aa 100644 --- a/custom_components/saver/__init__.py +++ b/custom_components/saver/__init__.py @@ -1,9 +1,11 @@ import logging +from typing import Any, Callable -from homeassistant.core import Context +from homeassistant.core import Context, HomeAssistant from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.script import Script, SCRIPT_MODE_PARALLEL from homeassistant.helpers.entity_component import EntityComponent +from homeassistant.helpers.template import _get_state_if_valid, Template, TemplateEnvironment from .const import * @@ -21,10 +23,89 @@ async def async_setup_entry(hass, config_entry): return result +class SaverVariableTemplate: + def __init__(self, hass: HomeAssistant, entity_id: str) -> None: + self._hass = hass + self._entity_id = entity_id + + def __call__(self, variable: str) -> Any: + saver_state = _get_state_if_valid(self._hass, self._entity_id) + if saver_state is None: + return None + variables = saver_state.attributes["variables"] + if variable in variables: + return variables[variable] + return None + + def __repr__(self): + return "