YAML documentation formatting and standards #495
-
ContextAcross the documentation, there is a wild different use of standards when writing YAML. Especially newer users might get confused by "older" methods (e.g., things we needed templates for back in the day). But also different ways to write YAML in the same way. ProposalI want to make an effort into making it look consistent and "best" practice for the end-users. Some goals:
YAMLBasic YAML usage; no specific rules for our features. This is fully compatible with the styling Prettier applies.
Home Assistant specific YAMLWithin Home Assistant, we also have some things that can be done in different ways, while still adhering to the above set styling. This part is here to take care of that. Basics
Service targetsIf you want to fire a service call towards an entity ID (for example, to turn on a light), you can do so in three different ways. The entity ID can be specified as a property of the service level, part of the data that is sent towards service call or as an entity in a service target. This is confusing for some and had lead to different topics on the forum, issues, and completely different usages across the documentation. Service targets have been introduced recently, as part of Blueprints and allow one to target a service call towards an entity, device or area. And is, therefore, the most flexible of the options available. The suggestion is to adjust all documentation to use this consistently. # Good
action:
- service: light.turn_on
target:
entity_id: light.living_room
- service: light.turn_on
target:
area_id: light.living_room
- service: light.turn_on
target:
area_id: living_room
entity_id: light.office_desk
device_id: 21349287492398472398
# Bad
action:
- service: light.turn_on
entity_id: light.living_room
- service: light.turn_on
data:
entity_id: light.living_room Properties that accept a scalar or a list of scalarsWe have a lot of places that access both a scalar value or a list of scalar values, additionally, sometimes, we even accept a comma-separated string value as a list.
# Good
entity_id: light.living_room
entity_id:
- light.living_room
- light.office
# Bad
entity_id: light.living_room, light.office
entity_id: [light.living_room, light.office]
entity_id:
- light.living_room Properties that accept a mapping or a list of mappingsAdditional to the lists above, we also have properties that accept both a mapping or a list of mappings. Well known examples are: In case a property accepts a single mapping or a list of mappings, a list of mapping must be used. Even when a single mapping is passed in. This makes it easier to understand one can add more items to it and easier to copy and paste a single item into your own code. # Good
action:
- service: light.turn_on
target:
entity_id: light.living_room
# Bad
action:
service: light.turn_on
target:
entity_id: light.living_room TemplatesThe use of templates should be avoided. If there is a pure YAML version available, this should be used. It is easier to follow for the less technical user. It also removes the need for escaping it from the liquid template engine our website uses. # Good
condition:
- condition: numeric_state
entity_id: sun.sun
attribute: elevation
below: 4
# Bad
condition:
- condition: template
value_template: "{{ state_attr('sun.sun', 'elevation') < 4 }}" Additionally, long lines in templates should be avoided and split across multiple lines to make more clear what happens. # Good
value_template: >-
{{
is_state('sensor.bedroom_co_status', 'Ok')
and is_state('sensor.kitchen_co_status', 'Ok')
and is_state('sensor.wardrobe_co_status', 'Ok')
}}
# Bad
value_template: "{{ is_state('sensor.bedroom_co_status', 'Ok') and is_state('sensor.kitchen_co_status', 'Ok') and is_state('sensor.wardrobe_co_status', 'Ok') }}" Finally, prefer shorthand style templates over-expressive format, as they # Good
condition:
- "{{ some_value == some_other_value }}"
# Bad
condition:
- condition: template
value_template: "{{ some_value == some_other_value }}" We do not allow the use of the states object directly if a helper method is available. For example now allowing This applies to Spacing around the filter pipe marker # Good
condition:
- "{{ some_value | float }}"
- "{{ some_value == (some_other_value | some_filter) }}"
# Bad
condition:
- "{{ some_value == some_other_value|some_filter }}" Finally, templates are strings, and thus are double-quoted. As a result of that, single quotes should be used inside the template. # Good
condition:
- "{{ 'some_value' == some_other_value }}"
# Bad
condition:
- '{{ "some_value" == some_other_value }}' ConsequencesTons of examples to adjust, but at least we have something to hold on to. |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 19 replies
-
Ok, so the above might be missing things and I might be a bit opinionated (in general I tried to stick with what Prettier does for general formatting). For Home Assistant it would be nice to know if this covers it all, or if one thinks it should be documented differently. Mainly... well... there are so many ways to do things 🤷 |
Beta Was this translation helpful? Give feedback.
-
Good stuff! The null value marker is a unique and confusing syntax element. If it's recommended, I suggest to use E.g. going from frontend: ~ to frontend:
themes: !include_dir_merge_named my_themes The risk is that the removal of |
Beta Was this translation helpful? Give feedback.
-
A consideration about the service targets. I've taken to give all my examples in discord with I worry that this may cause more confusion than it cures - with the way dev-service works today. |
Beta Was this translation helpful? Give feedback.
-
Does this extend to using the Style suggestions I have read elsewhere: No spacing around the filter pipe marker
Single quotes inside double quotes:
|
Beta Was this translation helpful? Give feedback.
-
OK, so I'm left with the So let`s do this poll style. React with 👀 for: sensors: React with 🚀 for sensors: ~ React with 🎉 for sensors: null For some reasoning/views/perspectives on this; read this thread: |
Beta Was this translation helpful? Give feedback.
-
PR finalizing this architectural discussion: |
Beta Was this translation helpful? Give feedback.
-
One thing that has been mentioned in the forums a couple of times this week is that So this wont work:
It has to be:
I this a documentation issue or should I raise a core issue for templating support in |
Beta Was this translation helpful? Give feedback.
PR finalizing this architectural discussion:
home-assistant/developers.home-assistant#802