Thanks for cleaning this up Felipe. As previously mentioned i think you need to move this code out of the config-changed hooks though since it depends on ha relations being extant and that might not be the case in the config-changed hook if it runs prior to the ha relation being joined. I think a more sensible approach would be to move this piece of code into its own function and call it from the config-changed and ha-relation-changed hooks that way you cover the case of (a) config changes after ha rels are joined and (b) config applied prior to ha-rels joined.
Thanks for cleaning this up Felipe. As previously mentioned i think you need to move this code out of the config-changed hooks though since it depends on ha relations being extant and that might not be the case in the config-changed hook if it runs prior to the ha relation being joined. I think a more sensible approach would be to move this piece of code into its own function and call it from the config-changed and ha-relation-changed hooks that way you cover the case of (a) config changes after ha rels are joined and (b) config applied prior to ha-rels joined.