2022-04-21 10:47:05 +10:00
|
|
|
package mmHa
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-12-13 22:25:28 +11:00
|
|
|
"github.com/MickMake/GoUnify/Only"
|
2022-11-16 22:16:01 +11:00
|
|
|
"strings"
|
2022-04-21 10:47:05 +10:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func (m *Mqtt) SensorPublishConfig(config EntityConfig) error {
|
|
|
|
|
|
|
|
for range Only.Once {
|
2022-04-21 16:24:40 +10:00
|
|
|
config.FixConfig()
|
|
|
|
if !config.IsSensor() {
|
2022-04-21 10:47:05 +10:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-10-11 17:26:26 +11:00
|
|
|
ok, newDevice := m.NewDevice(config)
|
|
|
|
if !ok {
|
|
|
|
break
|
2022-04-21 10:47:05 +10:00
|
|
|
}
|
2022-10-11 17:26:26 +11:00
|
|
|
|
|
|
|
id := JoinStringsForId(m.DeviceName, config.FullId)
|
|
|
|
// id := JoinStringsForId(m.Device.Name, config.ParentName, config.Name, config.UniqueId),
|
2022-04-21 10:47:05 +10:00
|
|
|
|
|
|
|
payload := Sensor {
|
2022-10-11 17:26:26 +11:00
|
|
|
Device: newDevice,
|
2022-10-12 22:01:47 +11:00
|
|
|
Name: JoinStrings(m.DeviceName, config.Name),
|
2022-10-11 17:26:26 +11:00
|
|
|
StateTopic: JoinStringsForTopic(m.sensorPrefix, id, "state"),
|
2022-04-29 16:36:18 +10:00
|
|
|
StateClass: config.StateClass,
|
2022-12-13 22:25:28 +11:00
|
|
|
ObjectId: id,
|
2022-10-11 17:26:26 +11:00
|
|
|
UniqueId: id,
|
2022-04-21 10:47:05 +10:00
|
|
|
UnitOfMeasurement: config.Units,
|
2022-04-21 16:24:40 +10:00
|
|
|
DeviceClass: config.DeviceClass,
|
2022-04-21 10:47:05 +10:00
|
|
|
Qos: 0,
|
|
|
|
ForceUpdate: true,
|
|
|
|
ExpireAfter: 0,
|
|
|
|
Encoding: "utf-8",
|
|
|
|
EnabledByDefault: true,
|
2022-04-21 16:24:40 +10:00
|
|
|
LastResetValueTemplate: config.LastResetValueTemplate,
|
|
|
|
LastReset: config.LastReset,
|
|
|
|
ValueTemplate: config.ValueTemplate,
|
|
|
|
Icon: config.Icon,
|
|
|
|
|
|
|
|
// Availability: &Availability {
|
|
|
|
// PayloadAvailable: "",
|
|
|
|
// PayloadNotAvailable: "",
|
|
|
|
// Topic: "",
|
|
|
|
// ValueTemplate: "",
|
|
|
|
// },
|
|
|
|
// AvailabilityMode: "",
|
|
|
|
// AvailabilityTemplate: "",
|
|
|
|
// AvailabilityTopic: "",
|
|
|
|
// EntityCategory: "",
|
|
|
|
// JsonAttributesTemplate: "",
|
|
|
|
// JsonAttributesTopic: "",
|
|
|
|
// ObjectId: "",
|
|
|
|
// PayloadAvailable: "",
|
|
|
|
// PayloadNotAvailable: "",
|
2022-04-21 10:47:05 +10:00
|
|
|
}
|
|
|
|
|
2022-10-11 17:26:26 +11:00
|
|
|
tag := JoinStringsForTopic(m.sensorPrefix, id, "config")
|
|
|
|
t := m.client.Publish(tag, 0, true, payload.Json())
|
2022-04-21 10:47:05 +10:00
|
|
|
if !t.WaitTimeout(m.Timeout) {
|
|
|
|
m.err = t.Error()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Mqtt) SensorPublishValue(config EntityConfig) error {
|
|
|
|
|
|
|
|
for range Only.Once {
|
2022-10-11 17:26:26 +11:00
|
|
|
// if config.Units == LabelBinarySensor {
|
|
|
|
if !config.IsSensor() {
|
2022-04-21 10:47:05 +10:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-10-11 17:26:26 +11:00
|
|
|
id := JoinStringsForId(m.DeviceName, config.FullId)
|
2022-11-16 22:16:01 +11:00
|
|
|
tag := JoinStringsForTopic(m.sensorPrefix, id, "state")
|
|
|
|
|
|
|
|
// @TODO - Real hack here. Need to properly check for JSON.
|
|
|
|
if strings.Contains(config.Value, `{`) || strings.Contains(config.Value, `":`) {
|
|
|
|
t := m.client.Publish(tag, 0, true, config.Value)
|
|
|
|
if !t.WaitTimeout(m.Timeout) {
|
|
|
|
m.err = t.Error()
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-12-13 22:25:28 +11:00
|
|
|
payload := MqttState {
|
|
|
|
LastReset: config.LastReset, // m.GetLastReset(config.FullId),
|
2022-04-21 10:47:05 +10:00
|
|
|
Value: config.Value,
|
|
|
|
}
|
2022-12-13 22:25:28 +11:00
|
|
|
if config.StateClass != "total" {
|
|
|
|
payload = MqttState {
|
|
|
|
Value: config.Value,
|
|
|
|
}
|
|
|
|
}
|
2022-10-11 17:26:26 +11:00
|
|
|
t := m.client.Publish(tag, 0, true, payload.Json())
|
2022-04-21 10:47:05 +10:00
|
|
|
if !t.WaitTimeout(m.Timeout) {
|
|
|
|
m.err = t.Error()
|
|
|
|
}
|
|
|
|
}
|
2022-04-21 16:24:40 +10:00
|
|
|
|
2022-04-21 10:47:05 +10:00
|
|
|
return m.err
|
|
|
|
}
|
|
|
|
|
2022-04-21 16:24:40 +10:00
|
|
|
|
2022-04-21 10:47:05 +10:00
|
|
|
type Sensor struct {
|
|
|
|
Availability *Availability `json:"availability,omitempty" required:"false"`
|
|
|
|
AvailabilityMode string `json:"availability_mode,omitempty" required:"false"`
|
|
|
|
AvailabilityTemplate string `json:"availability_template,omitempty" required:"false"`
|
|
|
|
AvailabilityTopic string `json:"availability_topic,omitempty" required:"false"`
|
|
|
|
Device Device `json:"device,omitempty" required:"false"`
|
|
|
|
DeviceClass string `json:"device_class,omitempty" required:"false"`
|
|
|
|
EnabledByDefault bool `json:"enabled_by_default,omitempty" required:"false"`
|
|
|
|
Encoding string `json:"encoding,omitempty" required:"false"`
|
|
|
|
EntityCategory string `json:"entity_category,omitempty" required:"false"`
|
|
|
|
ExpireAfter int `json:"expire_after,omitempty" required:"false"`
|
|
|
|
ForceUpdate bool `json:"force_update,omitempty" required:"false"`
|
|
|
|
Icon string `json:"icon,omitempty" required:"false"`
|
|
|
|
JsonAttributesTemplate string `json:"json_attributes_template,omitempty" required:"false"`
|
|
|
|
JsonAttributesTopic string `json:"json_attributes_topic,omitempty" required:"false"`
|
|
|
|
LastResetValueTemplate string `json:"last_reset_value_template,omitempty" required:"false"`
|
|
|
|
Name string `json:"name,omitempty" required:"false"`
|
|
|
|
ObjectId string `json:"object_id,omitempty" required:"false"`
|
|
|
|
PayloadAvailable string `json:"payload_available,omitempty" required:"false"`
|
|
|
|
PayloadNotAvailable string `json:"payload_not_available,omitempty" required:"false"`
|
|
|
|
Qos int `json:"qos,omitempty" required:"false"`
|
|
|
|
StateClass string `json:"state_class,omitempty" required:"false"`
|
|
|
|
StateTopic string `json:"state_topic" required:"true"`
|
|
|
|
UniqueId string `json:"unique_id,omitempty" required:"false"`
|
|
|
|
UnitOfMeasurement string `json:"unit_of_measurement,omitempty" required:"false"`
|
|
|
|
ValueTemplate string `json:"value_template,omitempty" required:"false"`
|
|
|
|
LastReset string `json:"last_reset,omitempty" required:"false"`
|
|
|
|
|
|
|
|
// StateFunc func() string `json:"-"`
|
|
|
|
//
|
|
|
|
// UpdateInterval float64 `json:"-"`
|
|
|
|
// ForceUpdateMQTT bool `json:"-"`
|
|
|
|
}
|
|
|
|
func (c *Sensor) Json() string {
|
|
|
|
j, _ := json.Marshal(*c)
|
|
|
|
return string(j)
|
|
|
|
}
|
2022-04-21 16:24:40 +10:00
|
|
|
|
|
|
|
|
|
|
|
type Fields map[string]string
|
|
|
|
|
|
|
|
func (m *Mqtt) PublishSensorValues(configs []EntityConfig) error {
|
|
|
|
for range Only.Once {
|
|
|
|
cs := make(map[string]Fields)
|
|
|
|
topic := ""
|
|
|
|
for _, oid := range configs {
|
|
|
|
if topic == "" {
|
2022-10-11 17:26:26 +11:00
|
|
|
topic = JoinStringsForId(m.DeviceName, oid.ParentName, oid.Name)
|
2022-04-21 16:24:40 +10:00
|
|
|
}
|
2022-04-29 16:36:18 +10:00
|
|
|
if _, ok := cs[oid.StateClass]; !ok {
|
|
|
|
cs[oid.StateClass] = make(Fields)
|
2022-04-21 16:24:40 +10:00
|
|
|
}
|
2022-04-29 16:36:18 +10:00
|
|
|
cs[oid.StateClass][oid.ValueName] = oid.Value
|
2022-04-21 16:24:40 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
for n, c := range cs {
|
|
|
|
j, _ := json.Marshal(c)
|
|
|
|
fmt.Printf("%s (%s) -> %s\n", topic, n, string(j))
|
|
|
|
|
|
|
|
m.err = m.PublishValue(n, topic, string(j))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Mqtt) GetSensorStateTopic(config EntityConfig) string {
|
2022-10-11 17:26:26 +11:00
|
|
|
st := JoinStringsForId(m.DeviceName, config.FullId)
|
2022-04-21 16:24:40 +10:00
|
|
|
st = JoinStringsForTopic(m.sensorPrefix, st, "state") // m.GetSensorStateTopic(name, config.SubName),m.EntityPrefix, m.Device.FullName, config.SubName
|
|
|
|
return st
|
|
|
|
}
|