Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for JavaScript / Jinja templates in the kiosk-mode options #274

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

elchininet
Copy link
Collaborator

@elchininet elchininet commented Sep 14, 2024

The next gist will be updated with the changes of this pull request to be able to test it by others without compiling the project.

This pull request is a major refactoring of the plugin and it changes radically the way of setting kiosk-mode options through entities state changes.

1. Removal of entity_settings

The entity_settings and ignore_entity_settings options have been removed.

2. All the options can be booleans or JavaScript / Jinja templates

Excluding ignore_mobile_settings and ignore_disable_km, all the options can be set as a JavaScript or a Jinja template that returns a boolean. If you set the option as a string but it is not a valid template, the library will throw an error. If you set a template and it doesn't return a boolean, the option will be set as false and a warning will be thrown.

JavaScript template example:

kiosk_mode:
  hide_header: '[[[ user_name === "ElChiniNet" ]]]'
  hide_sidebar: '[[[ is_state("input_boolean.hide_sidebar", "on") ]]]'

Jinja template example:

kiosk_mode:
  hide_header: '{{ user_name == "ElChiniNet" }}'
  hide_sidebar: '{{ is_state("input_boolean.hide_sidebar", "on") }}'

This solves one of the biggest issues that the plugin has: If one wants to configure entity changes for different users or for admins and non-admins the current configuration is not enough because there is a single place to configure entitites changes and it doesn't take into account users or admin privileges. With this new change it is possible to make something like this:

kiosk_mode:
  ## By default the sidebar will be hidden 
  hide_sidebar: true
  admin_settings:
    ## If the input_boolean.hide_sidebar is off the sidebar will be visible for admins
    hide_sidebar: '[[[ is_state("input_boolean.hide_sidebar", "on") ]]]'
  user_settings:
    - users:
        - "ryan meek"
        - "maykar"
      ## If the input_boolean.hide_sidebar_for_friends is off the sidebar will be visible for ryan meek and maykar
      hide_sidebar: '[[[ is_state("input_boolean.hide_sidebar_for_friends", "on") ]]]'

3. JavaScript templates use Home Assistant Javascript Templates behind the scenes

JavaScript templates use Home Assistant Javascript Templates for the JavaScript templating system. To know all the objects, variables and methods available in the JavaScript templates, consult the proper section in the repository.

4. Jinja templates will have access to client side variables

Inside the Jinja templates you can use some client variables that are very useful:

  • user_name: String with the logged user's name
  • user_is_admin: Bolean value than indicates if the logged user is admin or not
  • user_is_owner: Bolean value than indicates if the logged user is the owner or not
  • user_agent: User agent of the browser in which Home Assistant is being executed

Advantages of using templates for the options

Using templates for the options gives a lot of flexibility and depending on your objectives you can omit the usage of the conditional configs, so when you start to create conditional configurations, ask yourself if it can be achieved with templates. For example:

kiosk_mode:
  hide_sidebar: false
  hide_header: false
  user_settings:
    - users:
      - "ryan meek"
      - "maykar"
      hide_header: true
  non_admin_settings:
    hide_sidebar: true

Can be transformed into a simpler version:

kiosk_mode:
  hide_header: '{{ user_name in ("maykar", "ryan meek") }}'
  hide_sidebar: '[[[ !user_is_admin ]]]'

5. New debug options

This pull request implements two new options:

Config Option Description
debug Prints useful information in the console. The raw config loaded from the Lovelace panel, the resulting final config with all the options, and if a template is rendered, it will print the option that trigerred the template, the template code and the evaluated result of it.
debug_template Useful to debug the result of a single template without activating the debug mode.

debug

Example with a valid template

image

Example with an invalid template

image

debug_template

image

@elchininet elchininet added the enhancement New feature or request label Sep 14, 2024
@elchininet elchininet force-pushed the support_for_jinja_templates branch 11 times, most recently from 4a8ba0f to e6ce660 Compare September 20, 2024 21:17
@elchininet elchininet changed the title Add support for Jinja templates in the kiosk-mode options Add support for JavaScript and Jinja templates in the kiosk-mode options Sep 21, 2024
@elchininet elchininet changed the title Add support for JavaScript and Jinja templates in the kiosk-mode options Add support for JavaScript / Jinja templates in the kiosk-mode options Sep 21, 2024
@elchininet elchininet force-pushed the support_for_jinja_templates branch 2 times, most recently from 8e299a3 to 5b4664a Compare September 22, 2024 10:51
@elchininet elchininet force-pushed the support_for_jinja_templates branch from 5b4664a to ca2e950 Compare September 29, 2024 22:30
@elchininet elchininet force-pushed the support_for_jinja_templates branch from 77a6568 to d6cc9cb Compare September 30, 2024 22:11
@elchininet elchininet force-pushed the support_for_jinja_templates branch 3 times, most recently from 13177b6 to d2d19f7 Compare October 14, 2024 00:18
@elchininet elchininet force-pushed the support_for_jinja_templates branch from d2d19f7 to b823278 Compare October 21, 2024 21:48
@elchininet elchininet force-pushed the support_for_jinja_templates branch 5 times, most recently from 2c39d47 to 7e0aecd Compare November 10, 2024 14:12
@elchininet elchininet force-pushed the support_for_jinja_templates branch from 7e0aecd to 5512e68 Compare November 12, 2024 11:01
@coveralls
Copy link

coveralls commented Nov 12, 2024

Coverage Status

coverage: 92.857% (+0.8%) from 92.025%
when pulling a0aa630 on support_for_jinja_templates
into da8a422 on master.

@elchininet elchininet force-pushed the support_for_jinja_templates branch from 5512e68 to 11837b5 Compare November 19, 2024 17:35
@elchininet elchininet force-pushed the support_for_jinja_templates branch 2 times, most recently from d61a056 to fc93c9d Compare November 27, 2024 21:37
@elchininet elchininet force-pushed the support_for_jinja_templates branch 2 times, most recently from dca2c7b to 6fa2035 Compare December 14, 2024 21:10
@elchininet elchininet force-pushed the support_for_jinja_templates branch from 6fa2035 to 698f173 Compare December 25, 2024 00:06
@elchininet elchininet force-pushed the support_for_jinja_templates branch from 698f173 to a0aa630 Compare January 9, 2025 23:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants