I have many real world devices which are all similar, and talked to over mqtt. All have exactly the same interface to them.
With my cpp background I would typically make an abstract base class for the commonality, so this is my mindset.
The current code I have for a sonoff device that I will use for this example in my mqtt.things is:
Bridge mqtt:broker:mosquitto "Mosquitto" [ host="localhost", port=1883, secure=false, username="openhab", password="password", clientID="openHAB2server" ]
{
/////////////
// Sonoffs //
/////////////
Thing topic lounge_airwick2 "Airwick" @ "Lounge"
{
Channels:
Type string : reachable "Reachable" [ stateTopic="sonoff-airwick-lounge/tele/LWT" ]
Type switch : power "Power" [ stateTopic="sonoff-airwick-lounge/stat/POWER", commandTopic="sonoff-airwick-lounge/cmnd/POWER" ]
Type number : rssi "WiFi Signal Strength" [ stateTopic="sonoff-airwick-lounge/tele/STATE", transformationPattern="JSONPATH:$.Wifi.RSSI"]
Type string : ota "OTA URL" [ stateTopic="sonoff-airwick-lounge/tele/otaurl", commandTopic="sonoff-airwick-lounge/cmnd/otaurl"]
Type string : fw "FW Version" [ stateTopic="sonoff-airwick-lounge/tele/status2", commandTopic="sonoff-airwick-lounge/cmnd/status"]
Type switch : upgrade "Firmware Upgrade" [ stateTopic="sonoff-airwick-lounge/tele/upgrade", commandTopic="sonoff-airwick-lounge/cmnd/upgrade"]
Type switch : restart "Restart" [ stateTopic="sonoff-airwick-lounge/tele/restart", commandTopic="sonoff-airwick-lounge/cmnd/restart"]
}
}
Extending this to many other similar devices means copy and pasting the Thing, over and over again and changing the topic prefix (sonoff-xxxx-xxxx).
Is there a way I can abstract this definition, or perhaps create a custom Thing that just requires this prefix, and the rest of the definition is populated as per the template I define?
Even something like this would be better for me:
Bridge mqtt:broker:mosquitto "Mosquitto" [ host="localhost", port=1883, secure=false, username="openhab", password="password", clientID="openHAB2server" ]
{
/////////////
// Sonoffs //
/////////////
Thing topic lounge_airwick2 "Airwick" @ "Lounge"
{
Channels:
Type string : hostname "sonoff-foo-bar"
Type string : reachable "Reachable" [ stateTopic=hostname+"/tele/LWT" ]
Type switch : power "Power" [ stateTopic=hostname+"/stat/POWER", commandTopic=hostname+"/cmnd/POWER" ]
Type number : rssi "WiFi Signal Strength" [ stateTopic=hostname+"/tele/STATE", transformationPattern="JSONPATH:$.Wifi.RSSI"]
Type string : ota "OTA URL" [ stateTopic=hostname+"/tele/otaurl", commandTopic=hostname+"/cmnd/otaurl"]
Type string : fw "FW Version" [ stateTopic=hostname+"/tele/status2", commandTopic=hostname+"/cmnd/status"]
Type switch : upgrade "Firmware Upgrade" [ stateTopic=hostname+"/tele/upgrade", commandTopic=hostname+"/cmnd/upgrade"]
Type switch : restart "Restart" [ stateTopic=hostname+"/tele/restart", commandTopic=hostname+"/cmnd/restart"]
}
}
Open to other suggestions if there is a better way of course.
Thanks