Allow specifying template entities based on triggers#48169
Conversation
| 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, |
There was a problem hiding this comment.
did we not fade out *_template ?
There was a problem hiding this comment.
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.
|
@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 |
|
Updated the PR and made it now a new top-level template integration configuration option. |
|
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 configurationtemplate:
- 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 }}"DifferencesAll keys to define a sensor in both cases are exactly the same. The differences are:
|
|
Alright, went ahead and aligned the integration top-level with the existing platform integration. I am unable to add |
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
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 ofsensor, the configuration of this integration mimics the exact same configuration (minusplatform: template, plustrigger:) as is used for platforms.Screenshot.2021-03-21.at.20.59.29.mp4
Ideas for follow-up work that I won't include in this PR:
Type of change
Example entry for
configuration.yaml:# Example configuration.yamlAdditional information
Checklist
black --fast homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all..coveragerc.The integration reached or maintains the following Integration Quality Scale:
To help with the load of incoming pull requests: