Skip to content

Commit

Permalink
Merge pull request #31 from gaetancollaud/feature/topic-prefix-instea…
Browse files Browse the repository at this point in the history
…d-of-topic-format

use prefix instead of format for topic
  • Loading branch information
gaetancollaud authored Mar 27, 2022
2 parents d09bde7 + 8fb56e1 commit 619534a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ variables.
| * | MQTT_URL | MQTT url | | tcp://192.168.1.20:1883 |
| | MQTT_USERNAME | MQTT username | | myUser |
| | MQTT_PASSWORD | MQTT password | | 9TyVg74e5S |
| | MQTT_TOPIC_FORMAT | Topic format | digitalstrom/{deviceType}/{deviceName}/{channel}/{commandState} | |
| | MQTT_TOPIC_PREFIX | Topic prefix | digitalstrom | |
| | MQTT_NORMALIZE_DEVICE_NAME | Remove special chars from device name | true | |
| | MQTT_RETAIN | Retain MQTT messages | false | |
| | REFRESH_AT_START | should the states be refreshed at start | true | |
Expand All @@ -73,17 +73,33 @@ DIGITALSTROM_PASSWORD: XXX
MQTT_URL: tcp://192.168.1.X:1883
```
### MQTT topic format variable
### MQTT topic format
Those are the variable available for the configuration `MQTT_TOPIC_FORMAT`.
The topic format is as follows for the devices:
`{prefix}/devices/{deviceName}/{channel}/{commandState}`

The topic format is as follows for the circuits:

`{prefix}/circuits/{deviceName}/{channel}/state`

The topic format is as follows for the scenes:

`{prefix}/scenes/{zoneName}/{sceneName}/event`

The server status topic is

`{prefix}/server/state`

| variable | description | example |
| --- | --- | --- |
| {prefix} | | Defined by `MQTT_TOPIC_PREFIX` |
| {deviceType} | | `circuit` or `device` |
| {deviceName} | Device or Circuit name | `light_bathroom` |
| {deviceId} | Dsid of the device | `302de89f43f00e40000120b3` |
| {channel} | DS channel | |
| {commandState} | | `command` or `state` |
| {zoneName} | Zone name | `bathroom` |
| {sceneName} | Scene name | `double_press` |

## How to run

Expand Down
6 changes: 5 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type ConfigMqtt struct {
Username string
Password string
TopicFormat string
TopicPrefix string
NormalizeDeviceName bool
Retain bool
}
Expand Down Expand Up @@ -51,6 +52,7 @@ const (
envKeyMqttUsername string = "MQTT_USERNAME"
envKeyMqttPassword string = "MQTT_PASSWORD"
envKeyMqttTopicFormat string = "MQTT_TOPIC_FORMAT"
envKeyMqttTopicPrefix string = "MQTT_TOPIC_PREFIX"
envKeyMqttNormalizeTopicName string = "MQTT_NORMALIZE_DEVICE_NAME"
envKeyMqttRetain string = "MQTT_RETAIN"
envKeyInvertBlindsPosition string = "INVERT_BLINDS_POSITION"
Expand Down Expand Up @@ -94,7 +96,8 @@ func FromEnv() *Config {
envKeyMqttUrl: Undefined,
envKeyMqttUsername: Undefined,
envKeyMqttPassword: Undefined,
envKeyMqttTopicFormat: "digitalstrom/{deviceType}/{deviceName}/{channel}/{commandState}",
envKeyMqttTopicPrefix: "digitalstrom",
envKeyMqttTopicFormat: "deprecated",
envKeyMqttNormalizeTopicName: true,
envKeyMqttRetain: false,
envKeyRefreshAtStart: true,
Expand All @@ -118,6 +121,7 @@ func FromEnv() *Config {
Username: v.GetString(envKeyMqttUsername),
Password: v.GetString(envKeyMqttPassword),
TopicFormat: v.GetString(envKeyMqttTopicFormat),
TopicPrefix: v.GetString(envKeyMqttTopicPrefix),
NormalizeDeviceName: v.GetBool(envKeyMqttNormalizeTopicName),
Retain: v.GetBool(envKeyMqttRetain),
},
Expand Down
15 changes: 6 additions & 9 deletions digitalstrom_mqtt/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,26 +210,23 @@ func (dm *DigitalstromMqtt) subscribeToAllDevicesCommands() {
}

func (dm *DigitalstromMqtt) getTopic(deviceType string, deviceId string, deviceName string, channel string, commandState string) string {
topic := dm.config.TopicFormat
topic := dm.config.TopicPrefix

if dm.config.NormalizeDeviceName {
deviceName = normalizeForTopicName(deviceName)
}

topic = strings.ReplaceAll(topic, "{deviceType}", deviceType)
topic = strings.ReplaceAll(topic, "{deviceId}", deviceId)
topic = strings.ReplaceAll(topic, "{deviceName}", deviceName)
topic = strings.ReplaceAll(topic, "{channel}", channel)
topic = strings.ReplaceAll(topic, "{commandState}", commandState)
topic += "/"+deviceType
topic += "/"+deviceName
topic += "/"+channel
topic += "/"+commandState

return topic
}

// Returns MQTT topic to publish the Server status.
func (dm *DigitalstromMqtt) getStatusTopic() string {
// FIXME: use a topic prefix for all digitalstrom-mqtt messages.
root_topic := strings.Split(dm.config.TopicFormat, "/")[0]
return root_topic + "/server/state"
return dm.config.TopicPrefix + "/server/state"
}

func normalizeForTopicName(item string) string {
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func main() {
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
}

if config.Mqtt.TopicFormat != "deprecated" {
log.Fatal().Msg("MQTT_TOPIC_FORMAT is deprecated and cannot be used anymore, please use MQTT_TOPIC_PREFIX instead")
os.Exit(1)
}

log.Info().Msg("String digitalstrom MQTT!")

ds := digitalstrom.New(config)
Expand Down

0 comments on commit 619534a

Please sign in to comment.