arachnetech / homebridge-mqttthing

A plugin for Homebridge allowing the integration of many different accessory types using MQTT.
Apache License 2.0
462 stars 104 forks source link

Better apply function support #658

Closed isalkind closed 6 months ago

isalkind commented 6 months ago

Apply functions could only be updated via the config file and these custom entries where overwritten if changes where made to the accessory through the UI.

The changes in this PR were made to allow a separate entry for the topic apply function (still only visible in the config file), but these changes are not overwritten by changes made through the UI.

Transform topics of this form in the config file:

config = {
    topics: {
        topicName = 'some/topic/name',
        topicName_apply = 'return message * 2;'
    }
}

Into topics of this form at runtime ==>

config = {
    topics: {
        topicName: {
            topic: 'some/topic/name',
            apply: 'return message * 2;'
        }
    }
}

IFF the 'topicName_apply' property exists AND 'topicName'
is a string, otherwise 'topicName' is unchanged.

This is a workaround for apply functions not being supported by config-ui-x (see: https://github.com/arachnetech/homebridge-mqttthing/blob/master/docs/Configuration.md#apply-functions). This non-support currently causes topics to be overwritten if a manual update is applied to the config file and then an update is made via config-ui-x. With the updates in this PR, apply functions can be written directly to the configuration file and will not be lost if config-ui-x is used at some later point to make changes. The apply function is still not visible in config-ui-x and can only be updated through directly editing the config file.

To maintain these changes, for every topic entry that is created in config.schema.json a corresponding topic_apply entry must be created.

The referenced documentation would need to be updated if these changes are accepted.

arachnetech commented 6 months ago

This isn't working for me in the settings editor - I think because you haven't added all the _apply parameters to the layout, e.g. getOn and setOn are here:

        {
            "type": "fieldset",
            "expandable": true,
            "title": "MQTT Topics",
            "condition": {
                "functionBody": "return model.type && model.type !== 'null';"
            },
            "items": [
                "topics.setOn",
                "topics.getOn",

You also haven't specified conditions - so if they were added they would be present for every accessory type:

                    "getWhite": {
                        "type": "string",
                        "description": "Topic used to notify mqttthing of white level (0-255) - can be used with getRGB for RGBW with separately-published white level",
                        "condition": {
                            "functionBody": "return ['lightbulb','lightbulb-White','lightbulb-RGB'].includes(model.type);"
                        }
                    },
                    "getWhite_apply": {
                        "type": "string"
                    },

They also need descriptions.

Thanks,

David

isalkind commented 6 months ago

These changes have no impact in the setting editor. They simply allow an apply function to exist in the settings file and not be overwritten if the settings editor is used.

arachnetech commented 6 months ago

Ah, I understand - thanks.