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 4 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.

Removal of entity_settings

The entity_settings and ignore_entity_settings options have been removed.

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") ]]]'

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.

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 ]]]'

Note: as part of this pull request, the Docker Cache action has been removed, it is slower decompresing the images than download the images from the Docker Hub.

@elchininet elchininet added the enhancement New feature or request label Sep 14, 2024
@elchininet elchininet force-pushed the support_for_jinja_templates branch 5 times, most recently from 08331ef to 3dda4ea Compare September 14, 2024 22:51
@elchininet elchininet force-pushed the support_for_jinja_templates branch 4 times, most recently from ce03393 to bfe6809 Compare September 15, 2024 14:55
@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
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.

1 participant