diff --git a/README.md b/README.md index 9270ec8..3eb305f 100644 --- a/README.md +++ b/README.md @@ -250,6 +250,9 @@ elements: ![lovelace_gen](https://user-images.githubusercontent.com/1299821/62565373-ecd4ac80-b886-11e9-9dcb-c41b43027b2b.png) +## Hidden bonus +With lovelace_gen installed, you'll be able to redefine node anchors in the same file. A feature in the YAML specification, but an error in the engine Home Assistant normally uses... + # FAQ ### How can I do global variables? diff --git a/custom_components/lovelace_gen/__init__.py b/custom_components/lovelace_gen/__init__.py index 94edcb2..6bcc742 100644 --- a/custom_components/lovelace_gen/__init__.py +++ b/custom_components/lovelace_gen/__init__.py @@ -65,3 +65,28 @@ loader.yaml.SafeLoader.add_constructor("!file", _uncache_file) async def async_setup(hass, config): llgen_config.update(config.get("lovelace_gen")); return True + +# Allow redefinition of node anchors +import yaml + +def compose_node(self, parent, index): + if self.check_event(yaml.events.AliasEvent): + event = self.get_event() + anchor = event.anchor + if anchor not in self.anchors: + raise yaml.composer.ComposerError(None, None, "found undefined alias %r" + % anchor, event.start_mark) + return self.anchors[anchor] + event = self.peek_event() + anchor = event.anchor + self.descend_resolver(parent, index) + if self.check_event(yaml.events.ScalarEvent): + node = self.compose_scalar_node(anchor) + elif self.check_event(yaml.events.SequenceStartEvent): + node = self.compose_sequence_node(anchor) + elif self.check_event(yaml.events.MappingStartEvent): + node = self.compose_mapping_node(anchor) + self.ascend_resolver() + return node + +yaml.composer.Composer.compose_node = compose_node \ No newline at end of file