Wouldn’t it be neat to add string concatenation to YAML in homeassistant to simplify configuration writing? Thus:
user_directory: /home/user/me
user_pictures: /home/user/me/pictures
user_videos: /home/user/me/videos
would be simplified in:
user_directory: &DIR /home/user/me
user_pictures: !join [*DIR, /pictures]
user_videos: !join [*DIR, /videos]
Obviously, the more a constant string gets reused, the more advantageous this writing gets to be!
The possibility to do so and the way to implement it is taken from Chris Johnson discussion on StackOverflow:
Fortunately there’s a simple way to add string concatenation to YAML via user-defined tags
User-defined tags is a standard YAML capability - the YAML 1.2 spec says YAML schemas allow the “use of arbitrary explicit tags”. Handlers for those custom tags need to be implemented in a custom way in each language you’re targeting. Doing that in Python looks like this:
## in your python code
import yaml
## define custom tag handler
def join(loader, node):
seq = loader.construct_sequence(node)
return ''.join([str(i) for i in seq])
## register the tag handler
yaml.add_constructor('!join', join)
In home assistant core, only file /homeassistant/util/yaml/loader.py would have to be modified to implement such a feature. Would this feature be acceptable?