Skip to content

Allow specifying template entities based on triggers#48169

Merged
balloob merged 18 commits into
devfrom
trigger-entity
Mar 29, 2021
Merged

Allow specifying template entities based on triggers#48169
balloob merged 18 commits into
devfrom
trigger-entity

Conversation

@balloob
Copy link
Copy Markdown
Member

@balloob balloob commented Mar 20, 2021

Breaking change

Proposed change

Foundation for a trigger entity.

Allows you to specify any number of triggers as sources. Trigger data is forwarded to the entities to render their value off.

Use cases: webhook entities, MQTT entities etc, listen more efficiently to state changes etc

Placed under template: instead of sensor, the configuration of this integration mimics the exact same configuration (minus platform: template, plus trigger:) as is used for platforms.

template:
- trigger:
    platform: event
    event_type: test_event
  sensors:
    hello:
      friendly_name: Hello
      value_template: "{{ trigger.event.data.beer }}"

- trigger:
    platform: webhook
    webhook_id: my-super-secret-webhook-id
  sensors:
    temperature:
      friendly_name: "Webhook Temperature"
      value_template: "{{ trigger.json.temperature }}"
Screenshot.2021-03-21.at.20.59.29.mp4

Ideas for follow-up work that I won't include in this PR:

  • support for trigger-based binary sensors
  • Allow defining state-based template entities at top level (so no trigger)
  • support trigger entities in reload service
  • support trigger_variables and variables (processed post-trigger)

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example entry for configuration.yaml:

# Example configuration.yaml

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

To help with the load of incoming pull requests:

Comment thread homeassistant/components/trigger/__init__.py Outdated
@balloob balloob marked this pull request as ready for review March 22, 2021 03:40
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
vol.Optional(CONF_DEVICE_CLASS): SENSOR_DEVICE_CLASSES_SCHEMA,
vol.Required(CONF_VALUE_TEMPLATE): cv.template,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did we not fade out *_template ?

Copy link
Copy Markdown
Contributor

@amelchio amelchio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would fit better as a trigger: section of existing template entities. That would save you from having to grow things like icon_template. I think it would also make it easier to learn the feature and it could decrease the amount of documentation needed.

That is, something like this:

template:
  - trigger:
      platform: event
      event_type: test_event
    sensor:
      - name: Hello
        value_template: "{{ trigger.event.data.beer }}"

  - trigger:
      platform: webhook
      webhook_id: my-super-secret-webhook-id
    sensor:
      name: "Webhook Temperature"
      value_template: "{{ trigger.json.temperature }}"

Edit: the above goes with the new config layout, it can of course also be added to the existing for a smaller change:

sensor:
  - platform: template
    trigger:
      platform: event
      event_type: test_event
    sensors:
      beer:
        name: Hello
        value_template: "{{ trigger.event.data.beer }}"
  - platform: template
    trigger:
      platform: webhook
      webhook_id: my-super-secret-webhook-id
    sensors:
      temperature:
        name: "Webhook Temperature"
        value_template: "{{ trigger.json.temperature }}"

On a meta note, this is another example where an architectural discussion is missing. Now we already have an implementation that people are attached to and discussion is very hard.

@balloob
Copy link
Copy Markdown
Member Author

balloob commented Mar 22, 2021

@amelchio the problem is that template entities have their own way of deciding when to update: when any depend state has changed. So would we disable that then when a trigger is defined? Seems like a confusing user experience

@balloob balloob changed the title Add trigger entity groundwork Allow specifying template entities based on triggers Mar 24, 2021
@balloob
Copy link
Copy Markdown
Member Author

balloob commented Mar 24, 2021

Updated the PR and made it now a new top-level template integration configuration option.

@balloob
Copy link
Copy Markdown
Member Author

balloob commented Mar 24, 2021

I think a remaining open question is how much we should align top-level configuration with the platform configuration.

Current platform configuration

# Example configuration.yaml entry
sensor:
  - platform: template
    sensors:
      solar_angle:
        friendly_name: "Sun angle"
        unit_of_measurement: "degrees"
        value_template: "{{ state_attr('sun.sun', 'elevation') }}"

      sunrise:
        value_template: "{{ state_attr('sun.sun', 'next_rising') }}"

Proposed integration configuration

template:
- trigger:
    platform: event
    event_type: test_event
  sensor:
    - friendly_name: Hello
      value_template: "{{ trigger.event.data.beer }}"

- trigger:
    platform: webhook
    webhook_id: my-super-secret-webhook-id
  sensor:
    friendly_name: "Webhook Temperature"
    value_template: "{{ trigger.json.temperature }}"

Differences

All keys to define a sensor in both cases are exactly the same. The differences are:

  1. Platform uses plural version sensors, integration config uses platform domain/singular sensor
  2. Platform requires to define a slug for each entity and uses this for entity ID (but does not use this as unique id 🤔)

@balloob
Copy link
Copy Markdown
Member Author

balloob commented Mar 24, 2021

Alright, went ahead and aligned the integration top-level with the existing platform integration.

I am unable to add trigger support to existing platform configuration because triggers require an async validator config.py (because of device triggers). This is not supported for platforms.

Comment thread homeassistant/components/template/config.py Outdated
Comment thread homeassistant/components/template/sensor.py Outdated
Comment thread homeassistant/components/template/trigger_entity.py Outdated
Comment thread tests/components/template/test_sensor.py Outdated
Comment thread tests/components/template/test_sensor.py Outdated
Comment thread homeassistant/components/template/trigger_entity.py Outdated
balloob and others added 3 commits March 27, 2021 00:00
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
@balloob balloob merged commit 022f56f into dev Mar 29, 2021
@balloob balloob deleted the trigger-entity branch March 29, 2021 16:57
@github-actions github-actions Bot locked and limited conversation to collaborators Mar 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants