homeassistant support
This commit is contained in:
parent
0270cc3557
commit
6645ea6f5e
@ -7,6 +7,8 @@ import (
|
|||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const version = "0.1"
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
viper.SetDefault("system.hostname", "openpdu")
|
viper.SetDefault("system.hostname", "openpdu")
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ func init() {
|
|||||||
viper.SetDefault("Mqtt.Port", "1883")
|
viper.SetDefault("Mqtt.Port", "1883")
|
||||||
viper.SetDefault("Mqtt.Username", "")
|
viper.SetDefault("Mqtt.Username", "")
|
||||||
viper.SetDefault("Mqtt.Password", "")
|
viper.SetDefault("Mqtt.Password", "")
|
||||||
|
viper.SetDefault("Mqtt.HomeAssistant", false)
|
||||||
|
|
||||||
// MQTT.ERROR = log.New(os.Stdout, "[ERROR] ", 0)
|
// MQTT.ERROR = log.New(os.Stdout, "[ERROR] ", 0)
|
||||||
// MQTT.CRITICAL = log.New(os.Stdout, "[CRIT] ", 0)
|
// MQTT.CRITICAL = log.New(os.Stdout, "[CRIT] ", 0)
|
||||||
@ -73,7 +74,7 @@ func Setup() {
|
|||||||
|
|
||||||
func Subscribe(topic string, handler func(MQTT.Client, MQTT.Message)) {
|
func Subscribe(topic string, handler func(MQTT.Client, MQTT.Message)) {
|
||||||
// MQTTHandler(MQTT.Client, MQTT.Message)
|
// MQTTHandler(MQTT.Client, MQTT.Message)
|
||||||
MQTTclient.Subscribe(viper.GetString("Mqtt.Prefix")+"/"+topic, 1, handler)
|
MQTTclient.Subscribe(viper.GetString("Mqtt.Prefix")+"/switch/"+topic, 1, handler)
|
||||||
syslog.Debug(fmt.Sprintf("MQTT subscribed to topic: %s", topic))
|
syslog.Debug(fmt.Sprintf("MQTT subscribed to topic: %s", topic))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,6 +91,12 @@ func Disconnect() {
|
|||||||
|
|
||||||
func Publish(topic string, value string) {
|
func Publish(topic string, value string) {
|
||||||
if MQTTclient.IsConnected() {
|
if MQTTclient.IsConnected() {
|
||||||
MQTTclient.Publish(viper.GetString("Mqtt.Prefix")+"/"+topic, 0, false, value)
|
MQTTclient.Publish(viper.GetString("Mqtt.Prefix")+"/switch/"+topic, 0, false, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func PublishRoot(topic string, value string) {
|
||||||
|
if MQTTclient.IsConnected() {
|
||||||
|
MQTTclient.Publish(topic, 0, false, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -49,12 +51,69 @@ func init() {
|
|||||||
func MQTTSetup() {
|
func MQTTSetup() {
|
||||||
mqtt.Setup()
|
mqtt.Setup()
|
||||||
for o := range outlet.Outlets {
|
for o := range outlet.Outlets {
|
||||||
topic := outlet.Outlets[o].Channel.MQTTCommandTopic()
|
c := outlet.Outlets[o].Channel
|
||||||
|
topic := c.MQTTCommandTopic()
|
||||||
if topic == "" {
|
if topic == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
mqtt.Subscribe(topic, outlet.Outlets[o].Channel.MQTTHandler)
|
mqtt.Subscribe(topic, c.MQTTHandler)
|
||||||
|
hAssTopic := "homeassistant/switch/" + viper.GetString("Mqtt.Prefix") + "/" + fmt.Sprint(o) + "/config"
|
||||||
|
v := ""
|
||||||
|
if viper.GetBool("Mqtt.HomeAssistant") {
|
||||||
|
cfg := map[string]interface{}{
|
||||||
|
"name": outlet.Outlets[o].Description,
|
||||||
|
"state_topic": viper.GetString("Mqtt.Prefix") + "/switch/" + c.MQTTStateTopic(),
|
||||||
|
"command_topic": viper.GetString("Mqtt.Prefix") + "/switch/" + c.MQTTCommandTopic(),
|
||||||
|
"payload_off": "off",
|
||||||
|
"payload_on": "on",
|
||||||
|
"unique_id": viper.GetString("Mqtt.Prefix") + "_" + fmt.Sprint(o),
|
||||||
|
"device": map[string]interface{}{
|
||||||
|
"identifiers": []string{viper.GetString("Mqtt.Prefix")},
|
||||||
|
// "name": viper.GetString("system.hostname"),
|
||||||
|
// "model": "OpenPDU",
|
||||||
|
// "sw_version": version,
|
||||||
|
// "manufacturer": "OpenPDU",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s, err := json.Marshal(cfg)
|
||||||
|
if err != nil {
|
||||||
|
v = ""
|
||||||
|
} else {
|
||||||
|
v = string(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mqtt.PublishRoot(hAssTopic, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
hAssTopic := "homeassistant/switch/" + viper.GetString("Mqtt.Prefix") + "/config"
|
||||||
|
v := ""
|
||||||
|
if viper.GetBool("Mqtt.HomeAssistant") {
|
||||||
|
cfg := map[string]interface{}{
|
||||||
|
"name": viper.GetString("system.hostname"),
|
||||||
|
// "state_topic": viper.GetString("Mqtt.Prefix") + "/switch/" + c.MQTTStateTopic(),
|
||||||
|
// "command_topic": viper.GetString("Mqtt.Prefix") + "/switch/" + c.MQTTCommandTopic(),
|
||||||
|
"payload_off": "off",
|
||||||
|
"payload_on": "on",
|
||||||
|
"unique_id": viper.GetString("Mqtt.Prefix"),
|
||||||
|
"device": map[string]interface{}{
|
||||||
|
"identifiers": []string{viper.GetString("Mqtt.Prefix")},
|
||||||
|
"name": viper.GetString("system.hostname"),
|
||||||
|
"model": "OpenPDU",
|
||||||
|
"sw_version": version,
|
||||||
|
"manufacturer": "OpenPDU",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
s, err := json.Marshal(cfg)
|
||||||
|
if err != nil {
|
||||||
|
v = ""
|
||||||
|
} else {
|
||||||
|
v = string(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mqtt.PublishRoot(hAssTopic, v)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func MQTTreconfigure() {
|
func MQTTreconfigure() {
|
||||||
|
@ -23,6 +23,7 @@ func mqttPage(ctx *macaron.Context) {
|
|||||||
ctx.Data["prefix"] = viper.GetString("Mqtt.Prefix")
|
ctx.Data["prefix"] = viper.GetString("Mqtt.Prefix")
|
||||||
ctx.Data["username"] = viper.GetString("Mqtt.Username")
|
ctx.Data["username"] = viper.GetString("Mqtt.Username")
|
||||||
ctx.Data["password"] = viper.GetString("Mqtt.Password")
|
ctx.Data["password"] = viper.GetString("Mqtt.Password")
|
||||||
|
ctx.Data["homeassistant"] = viper.GetString("Mqtt.homeassistant")
|
||||||
|
|
||||||
ctx.Data["schemas"] = []string{"tcp", "ssl", "ws"}
|
ctx.Data["schemas"] = []string{"tcp", "ssl", "ws"}
|
||||||
ctx.HTML(200, "mqtt")
|
ctx.HTML(200, "mqtt")
|
||||||
@ -36,6 +37,7 @@ type MQTTPostForm struct {
|
|||||||
ClientID string `form:"clientid" binding:"Required"`
|
ClientID string `form:"clientid" binding:"Required"`
|
||||||
Username string `form:"username"`
|
Username string `form:"username"`
|
||||||
Password string `form:"password"`
|
Password string `form:"password"`
|
||||||
|
HomeAssistant bool `form:"homeassistant"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func mqttPost(ctx *macaron.Context, f MQTTPostForm) {
|
func mqttPost(ctx *macaron.Context, f MQTTPostForm) {
|
||||||
@ -57,6 +59,7 @@ func mqttPost(ctx *macaron.Context, f MQTTPostForm) {
|
|||||||
viper.Set("Mqtt.Prefix", strings.TrimSpace(f.Prefix))
|
viper.Set("Mqtt.Prefix", strings.TrimSpace(f.Prefix))
|
||||||
viper.Set("Mqtt.Username", strings.TrimSpace(f.Username))
|
viper.Set("Mqtt.Username", strings.TrimSpace(f.Username))
|
||||||
viper.Set("Mqtt.Password", f.Password)
|
viper.Set("Mqtt.Password", f.Password)
|
||||||
|
viper.Set("Mqtt.HomeAssistant", f.HomeAssistant)
|
||||||
|
|
||||||
viper.WriteConfig()
|
viper.WriteConfig()
|
||||||
|
|
||||||
|
@ -92,6 +92,20 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="homeassistant" class="col-sm-2 control-label">Home Assistant Integration</label>
|
||||||
|
<div class="col-sm-10">
|
||||||
|
<div class="checkbox">
|
||||||
|
<label>
|
||||||
|
{% if homeassistant == "true" %}
|
||||||
|
<input type="checkbox" id="homeassistant" name="homeassistant" checked="">
|
||||||
|
{% else %}
|
||||||
|
<input type="checkbox" id="homeassistant" name="homeassistant">
|
||||||
|
{% endif %}
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user