mirror of
https://github.com/MickMake/GoSungrow.git
synced 2025-03-26 17:41:42 +01:00
v3.0.1 - Additional MQTT types.
This commit is contained in:
parent
91cfecb620
commit
e76f44356c
231
mmHa/alarm_control_panel.go
Normal file
231
mmHa/alarm_control_panel.go
Normal file
@ -0,0 +1,231 @@
|
||||
package mmHa
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/MickMake/GoUnify/Only"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const LabelAlarmControlPanel = "alarm_control_panel"
|
||||
|
||||
|
||||
func (m *Mqtt) AlarmControlPanelPublishConfig(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
config.FixConfig()
|
||||
if !config.IsAlarmControlPanel() {
|
||||
break
|
||||
}
|
||||
|
||||
ok, newDevice := m.NewDevice(config)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
|
||||
payload := AlarmControlPanel {
|
||||
Device: newDevice,
|
||||
Name: String(JoinStrings(m.DeviceName, config.Name)),
|
||||
// StateTopic: JoinStringsForTopic(m.switchPrefix, id, "state"),
|
||||
CommandTopic: String(JoinStringsForTopic(m.Prefix, LabelAlarmControlPanel, m.ClientId, id, "cmd")),
|
||||
ObjectId: String(id),
|
||||
UniqueId: String(id),
|
||||
Qos: 0,
|
||||
Retain: true,
|
||||
|
||||
// PayloadOn: "true",
|
||||
// PayloadOff: "false",
|
||||
// StateOn: "true",
|
||||
// StateOff: "false",
|
||||
// ValueTemplate: config.ValueTemplate,
|
||||
Icon: Icon(config.Icon),
|
||||
|
||||
// Availability: &Availability {
|
||||
// PayloadAvailable: "",
|
||||
// PayloadNotAvailable: "",
|
||||
// Topic: "",
|
||||
// ValueTemplate: "",
|
||||
// },
|
||||
// AvailabilityMode: "",
|
||||
// AvailabilityTemplate: "",
|
||||
// AvailabilityTopic: "",
|
||||
// CommandTopic: "",
|
||||
// JSONAttributesTemplate: "",
|
||||
// JSONAttributesTopic: "",
|
||||
// Optimistic: false,
|
||||
// PayloadAvailable: "",
|
||||
// PayloadNotAvailable: "",
|
||||
}
|
||||
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelAlarmControlPanel, m.ClientId, id, "config")
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
func (m *Mqtt) AlarmControlPanelPublishValue(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
if !config.IsAlarmControlPanel() {
|
||||
break
|
||||
}
|
||||
|
||||
if config.IgnoreUpdate {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelAlarmControlPanel, m.ClientId, id, "state")
|
||||
|
||||
value := config.Value.String()
|
||||
if value == "--" {
|
||||
value = ""
|
||||
}
|
||||
|
||||
// @TODO - Real hack here. Need to properly check for JSON.
|
||||
if strings.Contains(config.Value.String(), `{`) || strings.Contains(config.Value.String(), `":`) {
|
||||
t := m.client.Publish(tag, 0, true, config.Value.String())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
payload := MqttState{
|
||||
LastReset: config.LastReset, // m.GetLastReset(config.FullId),
|
||||
Value: value,
|
||||
}
|
||||
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
type AlarmControlPanel struct {
|
||||
// A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
|
||||
Availability *Availability `json:"availability,omitempty"`
|
||||
|
||||
// When availability is configured, this controls the conditions needed to set the entity to available. Valid entries are all, any, and latest. If set to all, payload_available must be received on all configured availability topics before the entity is marked as online. If set to any, payload_available must be received on at least one configured availability topic before the entity is marked as online. If set to latest, the last payload_available or payload_not_available received on any configured availability topic controls the availability.
|
||||
AvailabilityMode String `json:"availability_mode,omitempty" default:"latest"`
|
||||
|
||||
// Defines a template to extract device’s availability from the availability_topic. To determine the devices’s availability result of this template will be compared to payload_available and payload_not_available.
|
||||
AvailabilityTemplate Template `json:"availability_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive availability (online/offline) updates. Must not be used together with availability.
|
||||
AvailabilityTopic String `json:"availability_topic,omitempty"`
|
||||
|
||||
// If defined, specifies a code to enable or disable the alarm in the frontend. Note that the code is validated locally and blocks sending MQTT messages to the remote device. For remote code validation, the code can be configured to either of the special values REMOTE_CODE (numeric code) or REMOTE_CODE_TEXT (text code). In this case, local code validation is bypassed but the frontend will still show a numeric or text code dialog. Use command_template to send the code to the remote device. Example configurations for remote code validation can be found here.
|
||||
Code String `json:"code,omitempty"`
|
||||
|
||||
// If true the code is required to arm the alarm. If false the code is not validated.
|
||||
CodeArmRequired Boolean `json:"code_arm_required,omitempty" default:"true"`
|
||||
|
||||
// If true the code is required to disarm the alarm. If false the code is not validated.
|
||||
CodeDisarmRequired Boolean `json:"code_disarm_required,omitempty" default:"true"`
|
||||
|
||||
// If true the code is required to trigger the alarm. If false the code is not validated.
|
||||
CodeTriggerRequired Boolean `json:"code_trigger_required,omitempty" default:"true"`
|
||||
|
||||
// The template used for the command payload. Available variables: action and code.
|
||||
CommandTemplate String `json:"command_template,omitempty" default:"action"`
|
||||
|
||||
// The MQTT topic to publish commands to change the alarm state.
|
||||
CommandTopic String `json:"command_topic,omitempty" required:"true"`
|
||||
|
||||
// Information about the device this alarm panel is a part of to tie it into the device registry. Only works through MQTT discovery and when unique_id is set. At least one of identifiers or connections must be present to identify the device.
|
||||
Device Device `json:"device,omitempty"`
|
||||
|
||||
// Flag which defines if the entity should be enabled when first added.
|
||||
EnabledByDefault Boolean `json:"enabled_by_default,omitempty" default:"true"`
|
||||
|
||||
// The encoding of the payloads received and published messages. Set to "" to disable decoding of incoming payload.
|
||||
Encoding String `json:"encoding,omitempty" default:"utf"`
|
||||
|
||||
// The category of the entity.
|
||||
EntityCategory String `json:"entity_category,omitempty" default:"None"`
|
||||
|
||||
// Icon for the entity.
|
||||
Icon Icon `json:"icon,omitempty"`
|
||||
|
||||
// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTemplate Template `json:"json_attributes_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive a JSON dictionary payload and then set as sensor attributes. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTopic String `json:"json_attributes_topic,omitempty"`
|
||||
|
||||
// The name of the alarm.
|
||||
Name String `json:"name,omitempty" default:"MQTT"`
|
||||
|
||||
// Used instead of name for automatic generation of entity_id
|
||||
ObjectId String `json:"object_id,omitempty"`
|
||||
|
||||
// The payload to set armed-away mode on your Alarm Panel.
|
||||
PayloadArmAway String `json:"payload_arm_away,omitempty" default:"ARM_AWAY"`
|
||||
|
||||
// The payload to set armed-home mode on your Alarm Panel.
|
||||
PayloadArmHome String `json:"payload_arm_home,omitempty" default:"ARM_HOME"`
|
||||
|
||||
// The payload to set armed-night mode on your Alarm Panel.
|
||||
PayloadArmNight String `json:"payload_arm_night,omitempty" default:"ARM_NIGHT"`
|
||||
|
||||
// The payload to set armed-vacation mode on your Alarm Panel.
|
||||
PayloadArmVacation String `json:"payload_arm_vacation,omitempty" default:"ARM_VACATION"`
|
||||
|
||||
// The payload to set armed-custom-bypass mode on your Alarm Panel.
|
||||
PayloadArmCustomBypass String `json:"payload_arm_custom_bypass,omitempty" default:"ARM_CUSTOM_BYPASS"`
|
||||
|
||||
// The payload that represents the available state.
|
||||
PayloadAvailable String `json:"payload_available,omitempty" default:"online"`
|
||||
|
||||
// The payload to disarm your Alarm Panel.
|
||||
PayloadDisarm String `json:"payload_disarm,omitempty" default:"DISARM"`
|
||||
|
||||
// The payload that represents the unavailable state.
|
||||
PayloadNotAvailable String `json:"payload_not_available,omitempty" default:"offline"`
|
||||
|
||||
// The payload to trigger the alarm on your Alarm Panel.
|
||||
PayloadTrigger String `json:"payload_trigger,omitempty" default:"TRIGGER"`
|
||||
|
||||
// The maximum QoS level of the state topic.
|
||||
Qos Integer `json:"qos,omitempty" default:"0"`
|
||||
|
||||
// If the published message should have the retain flag on or not.
|
||||
Retain Boolean `json:"retain,omitempty" default:"false"`
|
||||
|
||||
// The MQTT topic subscribed to receive state updates.
|
||||
StateTopic String `json:"state_topic,omitempty" required:"true"`
|
||||
|
||||
// An ID that uniquely identifies this alarm panel. If two alarm panels have the same unique ID, Home Assistant will raise an exception.
|
||||
UniqueId String `json:"unique_id,omitempty"`
|
||||
|
||||
// Defines a template to extract the value.
|
||||
ValueTemplate Template `json:"value_template,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
func (c *AlarmControlPanel) Json() string {
|
||||
j, _ := json.Marshal(*c)
|
||||
return string(j)
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsAlarmControlPanel() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.Units == LabelAlarmControlPanel {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
@ -6,6 +6,8 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const LabelBinarySensor = "binary_sensor"
|
||||
|
||||
|
||||
func (m *Mqtt) BinarySensorPublishConfig(config EntityConfig) error {
|
||||
|
||||
@ -21,17 +23,37 @@ func (m *Mqtt) BinarySensorPublishConfig(config EntityConfig) error {
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
// id := JoinStringsForId(m.Device.Name, config.ParentName, config.Name, config.UniqueId),
|
||||
|
||||
// StateClass := config.Point.UpdateFreq
|
||||
// switch {
|
||||
// case config.Point.IsTotal():
|
||||
// StateClass = "total"
|
||||
// default:
|
||||
// StateClass = "measurement"
|
||||
// }
|
||||
// ValueName := config.Point.Description
|
||||
// UniqueId := config.Point.Id
|
||||
// Units := config.Point.Unit
|
||||
// LastReset := config.Point.WhenReset()
|
||||
//
|
||||
// re := mmHa.EntityConfig {
|
||||
// Name: name, // mmHa.JoinStringsForName(" - ", id), // r.Point.Name, // PointName,
|
||||
// SubName: "",
|
||||
// ParentId: r.EndPoint,
|
||||
// ParentName: r.Parent.Key,
|
||||
// FullId: id, // string(r.FullId), // WAS r.Point.FullId
|
||||
// DeviceClass: "",
|
||||
// }
|
||||
|
||||
payload := BinarySensor {
|
||||
Device: newDevice,
|
||||
Name: JoinStrings(m.DeviceName, config.Name),
|
||||
StateTopic: JoinStringsForTopic(m.binarySensorPrefix, id, "state"),
|
||||
StateClass: config.StateClass,
|
||||
ObjectId: id,
|
||||
UniqueId: id,
|
||||
UnitOfMeasurement: config.Units,
|
||||
DeviceClass: config.DeviceClass,
|
||||
Name: String(JoinStrings(m.DeviceName, config.Name)),
|
||||
StateTopic: String(JoinStringsForTopic(m.Prefix, LabelBinarySensor, m.ClientId, id, "state")),
|
||||
// StateClass: config.StateClass,
|
||||
ObjectId: String(id),
|
||||
UniqueId: String(id),
|
||||
// UnitOfMeasurement: config.Units,
|
||||
DeviceClass: DeviceClass(config.DeviceClass),
|
||||
Qos: 0,
|
||||
ForceUpdate: true,
|
||||
ExpireAfter: 0,
|
||||
@ -39,9 +61,9 @@ func (m *Mqtt) BinarySensorPublishConfig(config EntityConfig) error {
|
||||
EnabledByDefault: true,
|
||||
PayloadOn: "true",
|
||||
PayloadOff: "false",
|
||||
LastResetValueTemplate: config.LastResetValueTemplate,
|
||||
ValueTemplate: config.ValueTemplate,
|
||||
Icon: config.Icon,
|
||||
// LastResetValueTemplate: config.LastResetValueTemplate,
|
||||
ValueTemplate: String(config.ValueTemplate),
|
||||
Icon: Icon(config.Icon),
|
||||
|
||||
// Availability: &Availability {
|
||||
// PayloadAvailable: "",
|
||||
@ -61,7 +83,7 @@ func (m *Mqtt) BinarySensorPublishConfig(config EntityConfig) error {
|
||||
// OffDelay: 0,
|
||||
}
|
||||
|
||||
tag := JoinStringsForTopic(m.binarySensorPrefix, id, "config")
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelBinarySensor, m.ClientId, id, "config")
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
@ -74,18 +96,26 @@ func (m *Mqtt) BinarySensorPublishConfig(config EntityConfig) error {
|
||||
func (m *Mqtt) BinarySensorPublishValue(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
// if config.Units != LabelBinarySensor {
|
||||
if !config.IsBinarySensor() {
|
||||
break
|
||||
}
|
||||
|
||||
if config.IgnoreUpdate {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
// tagId := JoinStringsForId(m.Device.Name, config.ParentName, config.Name, config.UniqueId),
|
||||
tag := JoinStringsForTopic(m.binarySensorPrefix, id, "state")
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelBinarySensor, m.ClientId, id, "state")
|
||||
|
||||
value := config.Value.String()
|
||||
if value == "--" {
|
||||
value = ""
|
||||
}
|
||||
|
||||
// @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 strings.Contains(config.Value.String(), `{`) || strings.Contains(config.Value.String(), `":`) {
|
||||
t := m.client.Publish(tag, 0, true, config.Value.String())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
@ -93,14 +123,15 @@ func (m *Mqtt) BinarySensorPublishValue(config EntityConfig) error {
|
||||
}
|
||||
|
||||
payload := MqttState {
|
||||
LastReset: m.GetLastReset(config.UniqueId),
|
||||
Value: config.Value,
|
||||
LastReset: config.LastReset, // m.GetLastReset(config.FullId),
|
||||
Value: value,
|
||||
}
|
||||
if config.Units == LabelBinarySensor {
|
||||
payload = MqttState {
|
||||
Value: config.Value,
|
||||
Value: value,
|
||||
}
|
||||
}
|
||||
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
@ -110,39 +141,171 @@ func (m *Mqtt) BinarySensorPublishValue(config EntityConfig) error {
|
||||
return m.err
|
||||
}
|
||||
|
||||
|
||||
type BinarySensor 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"`
|
||||
// A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
|
||||
Availability *Availability `json:"availability,omitempty"`
|
||||
|
||||
OffDelay int `json:"off_delay,omitempty" required:"false"`
|
||||
PayloadOff string `json:"pl_off,omitempty" required:"false"`
|
||||
PayloadOn string `json:"pl_on,omitempty" required:"false"`
|
||||
// When availability is configured, this controls the conditions needed to set the entity to available. Valid entries are all, any, and latest. If set to all, payload_available must be received on all configured availability topics before the entity is marked as online. If set to any, payload_available must be received on at least one configured availability topic before the entity is marked as online. If set to latest, the last payload_available or payload_not_available received on any configured availability topic controls the availability.
|
||||
AvailabilityMode String `json:"availability_mode,omitempty" default:"latest"`
|
||||
|
||||
// Defines a template to extract device’s availability from the availability_topic. To determine the devices’s availability result of this template will be compared to payload_available and payload_not_available.
|
||||
AvailabilityTemplate Template `json:"availability_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive birth and LWT messages from the MQTT device. If availability is not defined, the binary sensor will always be considered available and its state will be on, off or unknown. If availability is defined, the binary sensor will be considered as unavailable by default and the sensor’s initial state will be unavailable. Must not be used together with availability.
|
||||
AvailabilityTopic String `json:"availability_topic,omitempty"`
|
||||
|
||||
// Information about the device this binary sensor is a part of to tie it into the device registry. Only works through MQTT discovery and when unique_id is set. At least one of identifiers or connections must be present to identify the device.
|
||||
Device Device `json:"device,omitempty"`
|
||||
|
||||
// Sets the class of the device, changing the device state and icon that is displayed on the frontend.
|
||||
DeviceClass DeviceClass `json:"device_class,omitempty"`
|
||||
|
||||
// Flag which defines if the entity should be enabled when first added.
|
||||
EnabledByDefault Boolean `json:"enabled_by_default,omitempty" default:"true"`
|
||||
|
||||
// The encoding of the payloads received. Set to "" to disable decoding of incoming payload.
|
||||
Encoding String `json:"encoding,omitempty" default:"utf"`
|
||||
|
||||
// The category of the entity.
|
||||
EntityCategory String `json:"entity_category,omitempty" default:"None"`
|
||||
|
||||
// If set, it defines the number of seconds after the sensor’s state expires, if it’s not updated. After expiry, the sensor’s state becomes unavailable. Default the sensors state never expires.
|
||||
ExpireAfter Integer `json:"expire_after,omitempty"`
|
||||
|
||||
// Sends update events (which results in update of state object’s last_changed) even if the sensor’s state hasn’t changed. Useful if you want to have meaningful value graphs in history or want to create an automation that triggers on every incoming state message (not only when the sensor’s new state is different to the current one).
|
||||
ForceUpdate Boolean `json:"force_update,omitempty" default:"false"`
|
||||
|
||||
// Icon for the entity.
|
||||
Icon Icon `json:"icon,omitempty"`
|
||||
|
||||
// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTemplate Template `json:"json_attributes_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive a JSON dictionary payload and then set as sensor attributes. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTopic String `json:"json_attributes_topic,omitempty"`
|
||||
|
||||
// The name of the binary sensor.
|
||||
Name String `json:"name,omitempty" default:"MQTT"`
|
||||
|
||||
// Used instead of name for automatic generation of entity_id
|
||||
ObjectId String `json:"object_id,omitempty"`
|
||||
|
||||
// For sensors that only send on state updates (like PIRs), this variable sets a delay in seconds after which the sensor’s state will be updated back to off.
|
||||
OffDelay Integer `json:"off_delay,omitempty"`
|
||||
|
||||
// The string that represents the online state.
|
||||
PayloadAvailable String `json:"payload_available,omitempty" default:"online"`
|
||||
|
||||
// The string that represents the offline state.
|
||||
PayloadNotAvailable String `json:"payload_not_available,omitempty" default:"offline"`
|
||||
|
||||
// The string that represents the off state. It will be compared to the message in the state_topic (see value_template for details)
|
||||
PayloadOff String `json:"payload_off,omitempty" default:"OFF"`
|
||||
|
||||
// The string that represents the on state. It will be compared to the message in the state_topic (see value_template for details)
|
||||
PayloadOn String `json:"payload_on,omitempty" default:"ON"`
|
||||
|
||||
// The maximum QoS level to be used when receiving messages.
|
||||
Qos Integer `json:"qos,omitempty" default:"0"`
|
||||
|
||||
// The MQTT topic subscribed to receive sensor’s state.
|
||||
StateTopic String `json:"state_topic,omitempty" required:"true"`
|
||||
|
||||
// An ID that uniquely identifies this sensor. If two sensors have the same unique ID, Home Assistant will raise an exception.
|
||||
UniqueId String `json:"unique_id,omitempty"`
|
||||
|
||||
// Defines a template that returns a string to be compared to payload_on/payload_off or an empty string, in which case the MQTT message will be removed. Remove this option when payload_on and payload_off are sufficient to match your payloads (i.e no pre-processing of original message is required).
|
||||
ValueTemplate String `json:"value_template,omitempty"`
|
||||
}
|
||||
|
||||
// type BinarySensor struct {
|
||||
// Availability *Availability `json:"availability,omitempty"`
|
||||
//
|
||||
// AvailabilityTopic string `json:"availability_topic,omitempty"`
|
||||
//
|
||||
// AvailabilityMode string `json:"availability_mode,omitempty"`
|
||||
//
|
||||
// AvailabilityTemplate string `json:"availability_template,omitempty"`
|
||||
//
|
||||
// Device Device `json:"device,omitempty"`
|
||||
//
|
||||
// DeviceClass string `json:"device_class,omitempty"`
|
||||
//
|
||||
// EnabledByDefault bool `json:"enabled_by_default,omitempty"`
|
||||
//
|
||||
// Encoding string `json:"encoding,omitempty"`
|
||||
//
|
||||
// EntityCategory string `json:"entity_category,omitempty"`
|
||||
//
|
||||
// ExpireAfter int `json:"expire_after,omitempty"`
|
||||
//
|
||||
// ForceUpdate bool `json:"force_update,omitempty"`
|
||||
//
|
||||
// Icon string `json:"icon,omitempty"`
|
||||
//
|
||||
// JsonAttributesTemplate string `json:"json_attributes_template,omitempty"`
|
||||
//
|
||||
// JsonAttributesTopic string `json:"json_attributes_topic,omitempty"`
|
||||
//
|
||||
// LastResetValueTemplate string `json:"last_reset_value_template,omitempty"`
|
||||
//
|
||||
// Name string `json:"name,omitempty"`
|
||||
//
|
||||
// ObjectId string `json:"object_id,omitempty"`
|
||||
//
|
||||
// PayloadAvailable string `json:"payload_available,omitempty"`
|
||||
//
|
||||
// PayloadNotAvailable string `json:"payload_not_available,omitempty"`
|
||||
//
|
||||
// Qos int `json:"qos,omitempty"`
|
||||
//
|
||||
// Retain bool `json:"ret,omitempty"`
|
||||
//
|
||||
// StateClass string `json:"state_class,omitempty"`
|
||||
//
|
||||
// StateTopic string `json:"state_topic,omitempty" required:"true"`
|
||||
//
|
||||
// UniqueId string `json:"unique_id,omitempty"`
|
||||
//
|
||||
// UnitOfMeasurement string `json:"unit_of_measurement,omitempty"`
|
||||
//
|
||||
// Topic string `json:"topic,omitempty" required:"true"`
|
||||
//
|
||||
// ValueTemplate string `json:"value_template,omitempty"`
|
||||
//
|
||||
// OffDelay int `json:"off_delay,omitempty"`
|
||||
//
|
||||
// PayloadOff string `json:"pl_off,omitempty"`
|
||||
//
|
||||
// PayloadOn string `json:"pl_on,omitempty"`
|
||||
// }
|
||||
|
||||
func (c *BinarySensor) Json() string {
|
||||
j, _ := json.Marshal(*c)
|
||||
return string(j)
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsBinarySensor() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.Units == LabelBinarySensor {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
if config.Units == "binary" {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
if config.Units == "Bool" {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
if config.Units == "valueTypes.Bool" {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
||||
|
233
mmHa/button.go
Normal file
233
mmHa/button.go
Normal file
@ -0,0 +1,233 @@
|
||||
package mmHa
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/MickMake/GoUnify/Only"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const LabelButton = "button"
|
||||
|
||||
|
||||
func (m *Mqtt) ButtonPublishConfig(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
config.FixConfig()
|
||||
if !config.IsButton() {
|
||||
break
|
||||
}
|
||||
|
||||
ok, newDevice := m.NewDevice(config)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
|
||||
payload := Button {
|
||||
Device: newDevice,
|
||||
Name: String(JoinStrings(m.DeviceName, config.Name)),
|
||||
// StateTopic: JoinStringsForTopic(m.switchPrefix, id, "state"),
|
||||
CommandTopic: String(JoinStringsForTopic(m.Prefix, LabelButton, m.ClientId, id, "cmd")),
|
||||
ObjectId: String(id),
|
||||
UniqueId: String(id),
|
||||
Qos: 0,
|
||||
Retain: true,
|
||||
Icon: Icon(config.Icon),
|
||||
}
|
||||
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelButton, m.ClientId, id, "config")
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
func (m *Mqtt) ButtonPublishValue(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
if !config.IsButton() {
|
||||
break
|
||||
}
|
||||
|
||||
if config.IgnoreUpdate {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelButton, m.ClientId, id, "state")
|
||||
|
||||
value := config.Value.String()
|
||||
if value == "--" {
|
||||
value = ""
|
||||
}
|
||||
|
||||
// @TODO - Real hack here. Need to properly check for JSON.
|
||||
if strings.Contains(config.Value.String(), `{`) || strings.Contains(config.Value.String(), `":`) {
|
||||
t := m.client.Publish(tag, 0, true, config.Value.String())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
payload := MqttState{
|
||||
LastReset: config.LastReset, // m.GetLastReset(config.FullId),
|
||||
Value: value,
|
||||
}
|
||||
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
type Button struct {
|
||||
// A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
|
||||
Availability *Availability `json:"availability,omitempty"`
|
||||
|
||||
// When availability is configured, this controls the conditions needed to set the entity to available. Valid entries are all, any, and latest. If set to all, payload_available must be received on all configured availability topics before the entity is marked as online. If set to any, payload_available must be received on at least one configured availability topic before the entity is marked as online. If set to latest, the last payload_available or payload_not_available received on any configured availability topic controls the availability.
|
||||
AvailabilityMode String `json:"availability_mode,omitempty" default:"latest"`
|
||||
|
||||
// Defines a template to extract device’s availability from the availability_topic. To determine the devices’s availability result of this template will be compared to payload_available and payload_not_available.
|
||||
AvailabilityTemplate Template `json:"availability_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive availability (online/offline) updates. Must not be used together with availability.
|
||||
AvailabilityTopic String `json:"availability_topic,omitempty"`
|
||||
|
||||
// Defines a template to generate the payload to send to command_topic.
|
||||
CommandTemplate Template `json:"command_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to trigger the button.
|
||||
CommandTopic String `json:"command_topic,omitempty"`
|
||||
|
||||
// Information about the device this button is a part of to tie it into the device registry. Only works through MQTT discovery and when unique_id is set. At least one of identifiers or connections must be present to identify the device.
|
||||
Device Device `json:"device,omitempty"`
|
||||
|
||||
// The type/class of the button to set the icon in the frontend.
|
||||
DeviceClass DeviceClass `json:"device_class,omitempty" default:"None"`
|
||||
|
||||
// Flag which defines if the entity should be enabled when first added.
|
||||
EnabledByDefault Boolean `json:"enabled_by_default,omitempty" default:"true"`
|
||||
|
||||
// The encoding of the published messages.
|
||||
Encoding String `json:"encoding,omitempty" default:"utf"`
|
||||
|
||||
// The category of the entity.
|
||||
EntityCategory String `json:"entity_category,omitempty" default:"None"`
|
||||
|
||||
// Icon for the entity.
|
||||
Icon Icon `json:"icon,omitempty"`
|
||||
|
||||
// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTemplate Template `json:"json_attributes_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive a JSON dictionary payload and then set as sensor attributes. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTopic String `json:"json_attributes_topic,omitempty"`
|
||||
|
||||
// The name to use when displaying this button.
|
||||
Name String `json:"name,omitempty" default:"MQTT"`
|
||||
|
||||
// Used instead of name for automatic generation of entity_id
|
||||
ObjectId String `json:"object_id,omitempty"`
|
||||
|
||||
// The payload that represents the available state.
|
||||
PayloadAvailable String `json:"payload_available,omitempty" default:"online"`
|
||||
|
||||
// The payload that represents the unavailable state.
|
||||
PayloadNotAvailable String `json:"payload_not_available,omitempty" default:"offline"`
|
||||
|
||||
// The payload To send to trigger the button.
|
||||
PayloadPress String `json:"payload_press,omitempty" default:"PRESS"`
|
||||
|
||||
// The maximum QoS level of the state topic. Default is 0 and will also be used to publishing messages.
|
||||
Qos Integer `json:"qos,omitempty" default:"0"`
|
||||
|
||||
// If the published message should have the retain flag on or not.
|
||||
Retain Boolean `json:"retain,omitempty" default:"false"`
|
||||
|
||||
// An ID that uniquely identifies this button entity. If two buttons have the same unique ID, Home Assistant will raise an exception.
|
||||
UniqueId String `json:"unique_id,omitempty"`
|
||||
}
|
||||
|
||||
// type Button struct {
|
||||
// Availability *Availability `json:"availability,omitempty" required:"false"`
|
||||
//
|
||||
// AvailabilityTopic string `json:"availability_topic,omitempty" required:"false"`
|
||||
//
|
||||
// AvailabilityMode string `json:"availability_mode,omitempty" required:"false"`
|
||||
//
|
||||
// AvailabilityTemplate string `json:"availability_template,omitempty" required:"false"`
|
||||
//
|
||||
// CommandTemplate string `json:"command_template" required:"false"`
|
||||
//
|
||||
// CommandTopic string `json:"command_topic" required:"true"`
|
||||
//
|
||||
// Device Device `json:"device,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"`
|
||||
//
|
||||
// Name string `json:"name,omitempty" required:"false"`
|
||||
//
|
||||
// ObjectId string `json:"object_id,omitempty" required:"false"`
|
||||
//
|
||||
// Optimistic bool `json:"optimistic,omitempty" required:"false"`
|
||||
//
|
||||
// PayloadAvailable string `json:"pl_avail,omitempty"`
|
||||
//
|
||||
// PayloadNotAvailable string `json:"pl_not_avail,omitempty"`
|
||||
//
|
||||
// PayloadOff string `json:"pl_off,omitempty"`
|
||||
//
|
||||
// PayloadOn string `json:"pl_on,omitempty"`
|
||||
//
|
||||
// Qos int `json:"qos,omitempty" required:"false"`
|
||||
//
|
||||
// Retain bool `json:"ret,omitempty" required:"false"`
|
||||
//
|
||||
// StateOff string `json:"stat_off,omitempty"`
|
||||
//
|
||||
// StateOn string `json:"stat_on,omitempty"`
|
||||
//
|
||||
// StateTopic string `json:"state_topic,omitempty" required:"false"`
|
||||
//
|
||||
// UniqueId string `json:"unique_id,omitempty" required:"false"`
|
||||
//
|
||||
// ValueTemplate string `json:"value_template,omitempty" required:"false"`
|
||||
//
|
||||
// // CommandFunc func(mqtt.Message, mqtt.Client) `json:"-"`
|
||||
// // StateFunc func() string `json:"-"`
|
||||
// //
|
||||
// // UpdateInterval float64 `json:"-"`
|
||||
// // ForceUpdateMQTT bool `json:"-"`
|
||||
// //
|
||||
// // messageHandler mqtt.MessageHandler
|
||||
// }
|
||||
|
||||
func (c *Button) Json() string {
|
||||
j, _ := json.Marshal(*c)
|
||||
return string(j)
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsButton() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.Units == LabelButton {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
164
mmHa/camera.go
Normal file
164
mmHa/camera.go
Normal file
@ -0,0 +1,164 @@
|
||||
package mmHa
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/MickMake/GoUnify/Only"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const LabelCamera = "camera"
|
||||
|
||||
|
||||
func (m *Mqtt) CameraPublishConfig(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
config.FixConfig()
|
||||
if !config.IsCamera() {
|
||||
break
|
||||
}
|
||||
|
||||
ok, newDevice := m.NewDevice(config)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
|
||||
payload := Camera {
|
||||
Device: newDevice,
|
||||
Name: String(JoinStrings(m.DeviceName, config.Name)),
|
||||
// StateTopic: JoinStringsForTopic(m.switchPrefix, id, "state"),
|
||||
// CommandTopic: String(JoinStringsForTopic(m.switchPrefix, id, "cmd")),
|
||||
ObjectId: String(id),
|
||||
UniqueId: String(id),
|
||||
// Qos: 0,
|
||||
// Retain: true,
|
||||
|
||||
// PayloadOn: "true",
|
||||
// PayloadOff: "false",
|
||||
// StateOn: "true",
|
||||
// StateOff: "false",
|
||||
// ValueTemplate: config.ValueTemplate,
|
||||
Icon: Icon(config.Icon),
|
||||
}
|
||||
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelCamera, m.ClientId, id, "config")
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
func (m *Mqtt) CameraPublishValue(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
if !config.IsCamera() {
|
||||
break
|
||||
}
|
||||
|
||||
if config.IgnoreUpdate {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelCamera, m.ClientId, id, "state")
|
||||
|
||||
value := config.Value.String()
|
||||
if value == "--" {
|
||||
value = ""
|
||||
}
|
||||
|
||||
// @TODO - Real hack here. Need to properly check for JSON.
|
||||
if strings.Contains(config.Value.String(), `{`) || strings.Contains(config.Value.String(), `":`) {
|
||||
t := m.client.Publish(tag, 0, true, config.Value.String())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
payload := MqttState{
|
||||
LastReset: config.LastReset, // m.GetLastReset(config.FullId),
|
||||
Value: value,
|
||||
}
|
||||
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
type Camera struct {
|
||||
// A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
|
||||
Availability *Availability `json:"availability,omitempty"`
|
||||
|
||||
// When availability is configured, this controls the conditions needed to set the entity to available. Valid entries are all, any, and latest. If set to all, payload_available must be received on all configured availability topics before the entity is marked as online. If set to any, payload_available must be received on at least one configured availability topic before the entity is marked as online. If set to latest, the last payload_available or payload_not_available received on any configured availability topic controls the availability.
|
||||
AvailabilityMode String `json:"availability_mode,omitempty" default:"latest"`
|
||||
|
||||
// Defines a template to extract device’s availability from the availability_topic. To determine the devices’s availability result of this template will be compared to payload_available and payload_not_available.
|
||||
AvailabilityTemplate Template `json:"availability_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive availability (online/offline) updates. Must not be used together with availability.
|
||||
AvailabilityTopic String `json:"availability_topic,omitempty"`
|
||||
|
||||
// Information about the device this camera is a part of to tie it into the device registry. Only works through MQTT discovery and when unique_id is set. At least one of identifiers or connections must be present to identify the device.
|
||||
Device Device `json:"device,omitempty"`
|
||||
|
||||
// Flag which defines if the entity should be enabled when first added.
|
||||
EnabledByDefault Boolean `json:"enabled_by_default,omitempty" default:"true"`
|
||||
|
||||
// The encoding of the payloads received. Set to "" to disable decoding of incoming payload. Use image_encoding to enable Base64 decoding on topic.
|
||||
Encoding String `json:"encoding,omitempty" default:"utf"`
|
||||
|
||||
// The category of the entity.
|
||||
EntityCategory String `json:"entity_category,omitempty" default:"None"`
|
||||
|
||||
// Icon for the entity.
|
||||
Icon Icon `json:"icon,omitempty"`
|
||||
|
||||
// The encoding of the image payloads received. Set to "b64" to enable base64 decoding of image payload. If not set, the image payload must be raw binary data.
|
||||
ImageEncoding String `json:"image_encoding,omitempty" default:"None"`
|
||||
|
||||
// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic.
|
||||
JsonAttributesTemplate Template `json:"json_attributes_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive a JSON dictionary payload and then set as sensor attributes. Implies force_update of the current sensor state when a message is received on this topic.
|
||||
JsonAttributesTopic String `json:"json_attributes_topic,omitempty"`
|
||||
|
||||
// The name of the camera.
|
||||
Name String `json:"name,omitempty"`
|
||||
|
||||
// Used instead of name for automatic generation of entity_id
|
||||
ObjectId String `json:"object_id,omitempty"`
|
||||
|
||||
// The MQTT topic to subscribe to.
|
||||
Topic String `json:"topic,omitempty" required:"true"`
|
||||
|
||||
// An ID that uniquely identifies this camera. If two cameras have the same unique ID Home Assistant will raise an exception.
|
||||
UniqueId String `json:"unique_id,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
func (c *Camera) Json() string {
|
||||
j, _ := json.Marshal(*c)
|
||||
return string(j)
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsCamera() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.Units == LabelCamera {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
320
mmHa/climate.go
Normal file
320
mmHa/climate.go
Normal file
@ -0,0 +1,320 @@
|
||||
package mmHa
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/MickMake/GoUnify/Only"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const LabelClimate = "climate"
|
||||
|
||||
|
||||
func (m *Mqtt) ClimatePublishConfig(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
config.FixConfig()
|
||||
if !config.IsClimate() {
|
||||
break
|
||||
}
|
||||
|
||||
ok, newDevice := m.NewDevice(config)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
|
||||
payload := Climate {
|
||||
Device: newDevice,
|
||||
Name: String(JoinStrings(m.DeviceName, config.Name)),
|
||||
// StateTopic: JoinStringsForTopic(m.switchPrefix, id, "state"),
|
||||
// CommandTopic: String(JoinStringsForTopic(m.switchPrefix, id, "cmd")),
|
||||
ObjectId: String(id),
|
||||
UniqueId: String(id),
|
||||
Qos: 0,
|
||||
Retain: true,
|
||||
|
||||
// PayloadOn: "true",
|
||||
// PayloadOff: "false",
|
||||
// StateOn: "true",
|
||||
// StateOff: "false",
|
||||
// ValueTemplate: config.ValueTemplate,
|
||||
Icon: Icon(config.Icon),
|
||||
}
|
||||
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelClimate, m.ClientId, id, "config")
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
func (m *Mqtt) ClimatePublishValue(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
if !config.IsClimate() {
|
||||
break
|
||||
}
|
||||
|
||||
if config.IgnoreUpdate {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelClimate, m.ClientId, id, "state")
|
||||
|
||||
value := config.Value.String()
|
||||
if value == "--" {
|
||||
value = ""
|
||||
}
|
||||
|
||||
// @TODO - Real hack here. Need to properly check for JSON.
|
||||
if strings.Contains(config.Value.String(), `{`) || strings.Contains(config.Value.String(), `":`) {
|
||||
t := m.client.Publish(tag, 0, true, config.Value.String())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
payload := MqttState{
|
||||
LastReset: config.LastReset, // m.GetLastReset(config.FullId),
|
||||
Value: value,
|
||||
}
|
||||
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
type Climate struct {
|
||||
// A template to render the value received on the action_topic with.
|
||||
ActionTemplate Template `json:"action_template,omitempty"`
|
||||
|
||||
// The MQTT topic to subscribe for changes of the current action. If this is set, the climate graph uses the value received as data source. Valid values: off, heating, cooling, drying, idle, fan.
|
||||
ActionTopic String `json:"action_topic,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to switch auxiliary heat.
|
||||
AuxCommandTopic String `json:"aux_command_topic,omitempty"`
|
||||
|
||||
// A template to render the value received on the aux_state_topic with.
|
||||
AuxStateTemplate Template `json:"aux_state_template,omitempty"`
|
||||
|
||||
// The MQTT topic to subscribe for changes of the auxiliary heat mode. If this is not set, the auxiliary heat mode works in optimistic mode (see below).
|
||||
AuxStateTopic String `json:"aux_state_topic,omitempty"`
|
||||
|
||||
// A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
|
||||
Availability *Availability `json:"availability,omitempty"`
|
||||
|
||||
// When availability is configured, this controls the conditions needed to set the entity to available. Valid entries are all, any, and latest. If set to all, payload_available must be received on all configured availability topics before the entity is marked as online. If set to any, payload_available must be received on at least one configured availability topic before the entity is marked as online. If set to latest, the last payload_available or payload_not_available received on any configured availability topic controls the availability.
|
||||
AvailabilityMode String `json:"availability_mode,omitempty" default:"latest"`
|
||||
|
||||
// Defines a template to extract device’s availability from the availability_topic. To determine the devices’s availability result of this template will be compared to payload_available and payload_not_available.
|
||||
AvailabilityTemplate Template `json:"availability_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive availability (online/offline) updates. Must not be used together with availability.
|
||||
AvailabilityTopic String `json:"availability_topic,omitempty"`
|
||||
|
||||
// A template with which the value received on current_temperature_topic will be rendered.
|
||||
CurrentTemperatureTemplate Template `json:"current_temperature_template,omitempty"`
|
||||
|
||||
// The MQTT topic on which to listen for the current temperature.
|
||||
CurrentTemperatureTopic String `json:"current_temperature_topic,omitempty"`
|
||||
|
||||
// Information about the device this HVAC device is a part of to tie it into the device registry. Only works through MQTT discovery and when unique_id is set. At least one of identifiers or connections must be present to identify the device.
|
||||
Device Device `json:"device,omitempty"`
|
||||
|
||||
// Flag which defines if the entity should be enabled when first added.
|
||||
EnabledByDefault Boolean `json:"enabled_by_default,omitempty" default:"true"`
|
||||
|
||||
// The encoding of the payloads received and published messages. Set to "" to disable decoding of incoming payload.
|
||||
Encoding String `json:"encoding,omitempty" default:"utf"`
|
||||
|
||||
// The category of the entity.
|
||||
EntityCategory String `json:"entity_category,omitempty" default:"None"`
|
||||
|
||||
// A template to render the value sent to the fan_mode_command_topic with.
|
||||
FanModeCommandTemplate Template `json:"fan_mode_command_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the fan mode.
|
||||
FanModeCommandTopic String `json:"fan_mode_command_topic,omitempty"`
|
||||
|
||||
// A template to render the value received on the fan_mode_state_topic with.
|
||||
FanModeStateTemplate Template `json:"fan_mode_state_template,omitempty"`
|
||||
|
||||
// The MQTT topic to subscribe for changes of the HVAC fan mode. If this is not set, the fan mode works in optimistic mode (see below).
|
||||
FanModeStateTopic String `json:"fan_mode_state_topic,omitempty"`
|
||||
|
||||
// A list of supported fan modes.
|
||||
FanModes List `json:"fan_modes,omitempty"`
|
||||
|
||||
// Default: [“auto”, “low”, “medium”, “high”]
|
||||
// Set the initial target temperature.
|
||||
Initial Integer `json:"initial,omitempty" default:"21"`
|
||||
|
||||
// Icon for the entity.
|
||||
Icon Icon `json:"icon,omitempty"`
|
||||
|
||||
// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTemplate Template `json:"json_attributes_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive a JSON dictionary payload and then set as sensor attributes. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTopic String `json:"json_attributes_topic,omitempty"`
|
||||
|
||||
// Maximum set point available.
|
||||
MaxTemp Float `json:"max_temp,omitempty"`
|
||||
|
||||
// Minimum set point available.
|
||||
MinTemp Float `json:"min_temp,omitempty"`
|
||||
|
||||
// A template to render the value sent to the mode_command_topic with.
|
||||
ModeCommandTemplate Template `json:"mode_command_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the HVAC operation mode.
|
||||
ModeCommandTopic String `json:"mode_command_topic,omitempty"`
|
||||
|
||||
// A template to render the value received on the mode_state_topic with.
|
||||
ModeStateTemplate Template `json:"mode_state_template,omitempty"`
|
||||
|
||||
// The MQTT topic to subscribe for changes of the HVAC operation mode. If this is not set, the operation mode works in optimistic mode (see below).
|
||||
ModeStateTopic String `json:"mode_state_topic,omitempty"`
|
||||
|
||||
// A list of supported modes. Needs to be a subset of the default values.
|
||||
Modes List `json:"modes,omitempty"`
|
||||
|
||||
// Default: [“auto”, “off”, “cool”, “heat”, “dry”, “fan_only”]
|
||||
// The name of the HVAC.
|
||||
Name String `json:"name,omitempty" default:"MQTT"`
|
||||
|
||||
// Used instead of name for automatic generation of entity_id
|
||||
ObjectId String `json:"object_id,omitempty"`
|
||||
|
||||
// The payload that represents the available state.
|
||||
PayloadAvailable String `json:"payload_available,omitempty" default:"online"`
|
||||
|
||||
// The payload that represents the unavailable state.
|
||||
PayloadNotAvailable String `json:"payload_not_available,omitempty" default:"offline"`
|
||||
|
||||
// The payload that represents disabled state.
|
||||
PayloadOff String `json:"payload_off,omitempty" default:"OFF"`
|
||||
|
||||
// The payload that represents enabled state.
|
||||
PayloadOn String `json:"payload_on,omitempty" default:"ON"`
|
||||
|
||||
// The MQTT topic to publish commands to change the power state. This is useful if your device has a separate power toggle in addition to mode.
|
||||
PowerCommandTopic String `json:"power_command_topic,omitempty"`
|
||||
|
||||
// The desired precision for this device. Can be used to match your actual thermostat’s precision. Supported values are 0.1, 0.5 and 1.0.
|
||||
Precision Float `json:"precision,omitempty"`
|
||||
|
||||
// Default: 0.1 for Celsius and 1.0 for Fahrenheit.
|
||||
// Defines a template to generate the payload to send to preset_mode_command_topic.
|
||||
PresetModeCommandTemplate Template `json:"preset_mode_command_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the preset mode.
|
||||
PresetModeCommandTopic String `json:"preset_mode_command_topic,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive climate speed based on presets. When preset ‘none’ is received or None the preset_mode will be reset.
|
||||
PresetModeStateTopic String `json:"preset_mode_state_topic,omitempty"`
|
||||
|
||||
// Defines a template to extract the preset_mode value from the payload received on preset_mode_state_topic.
|
||||
PresetModeValueTemplate String `json:"preset_mode_value_template,omitempty"`
|
||||
|
||||
// List of preset modes this climate is supporting. Common examples include eco, away, boost, comfort, home, sleep and activity.
|
||||
PresetModes List `json:"preset_modes,omitempty"`
|
||||
|
||||
// The maximum QoS level to be used when receiving and publishing messages.
|
||||
Qos Integer `json:"qos,omitempty" default:"0"`
|
||||
|
||||
// Defines if published messages should have the retain flag set.
|
||||
Retain Boolean `json:"retain,omitempty" default:"false"`
|
||||
|
||||
// A template to render the value sent to the swing_mode_command_topic with.
|
||||
SwingModeCommandTemplate Template `json:"swing_mode_command_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the swing mode.
|
||||
SwingModeCommandTopic String `json:"swing_mode_command_topic,omitempty"`
|
||||
|
||||
// A template to render the value received on the swing_mode_state_topic with.
|
||||
SwingModeStateTemplate Template `json:"swing_mode_state_template,omitempty"`
|
||||
|
||||
// The MQTT topic to subscribe for changes of the HVAC swing mode. If this is not set, the swing mode works in optimistic mode (see below).
|
||||
SwingModeStateTopic String `json:"swing_mode_state_topic,omitempty"`
|
||||
|
||||
// A list of supported swing modes - (optional, default: [“on”, “off”])
|
||||
SwingModes List `json:"swing_modes,omitempty"`
|
||||
|
||||
// A template to render the value sent to the temperature_command_topic with.
|
||||
TemperatureCommandTemplate Template `json:"temperature_command_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the target temperature.
|
||||
TemperatureCommandTopic String `json:"temperature_command_topic,omitempty"`
|
||||
|
||||
// A template to render the value sent to the temperature_high_command_topic with.
|
||||
TemperatureHighCommandTemplate Template `json:"temperature_high_command_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the high target temperature.
|
||||
TemperatureHighCommandTopic String `json:"temperature_high_command_topic,omitempty"`
|
||||
|
||||
// A template to render the value received on the temperature_high_state_topic with.
|
||||
TemperatureHighStateTemplate Template `json:"temperature_high_state_template,omitempty"`
|
||||
|
||||
// The MQTT topic to subscribe for changes in the target high temperature. If this is not set, the target high temperature works in optimistic mode (see below).
|
||||
TemperatureHighStateTopic String `json:"temperature_high_state_topic,omitempty"`
|
||||
|
||||
// A template to render the value sent to the temperature_low_command_topic with.
|
||||
TemperatureLowCommandTemplate Template `json:"temperature_low_command_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the target low temperature.
|
||||
TemperatureLowCommandTopic String `json:"temperature_low_command_topic,omitempty"`
|
||||
|
||||
// A template to render the value received on the temperature_low_state_topic with.
|
||||
TemperatureLowStateTemplate Template `json:"temperature_low_state_template,omitempty"`
|
||||
|
||||
// The MQTT topic to subscribe for changes in the target low temperature. If this is not set, the target low temperature works in optimistic mode (see below).
|
||||
TemperatureLowStateTopic String `json:"temperature_low_state_topic,omitempty"`
|
||||
|
||||
// A template to render the value received on the temperature_state_topic with.
|
||||
TemperatureStateTemplate Template `json:"temperature_state_template,omitempty"`
|
||||
|
||||
// The MQTT topic to subscribe for changes in the target temperature. If this is not set, the target temperature works in optimistic mode (see below).
|
||||
TemperatureStateTopic String `json:"temperature_state_topic,omitempty"`
|
||||
|
||||
// Defines the temperature unit of the device, C or F. If this is not set, the temperature unit is set to the system temperature unit.
|
||||
TemperatureUnit String `json:"temperature_unit,omitempty"`
|
||||
|
||||
// Step size for temperature set point.
|
||||
TempStep Float `json:"temp_step,omitempty" default:"1"`
|
||||
|
||||
// An ID that uniquely identifies this HVAC device. If two HVAC devices have the same unique ID, Home Assistant will raise an exception.
|
||||
UniqueId String `json:"unique_id,omitempty"`
|
||||
|
||||
// Default template to render the payloads on all *_state_topics with.
|
||||
ValueTemplate Template `json:"value_template,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
func (c *Climate) Json() string {
|
||||
j, _ := json.Marshal(*c)
|
||||
return string(j)
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsClimate() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.Units == LabelClimate {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
@ -27,19 +27,6 @@ func (c *Config) Json() string {
|
||||
return string(j)
|
||||
}
|
||||
|
||||
|
||||
type Device struct {
|
||||
ConfigurationUrl string `json:"configuration_url,omitempty" required:"false"`
|
||||
Connections [][]string `json:"connections,omitempty" required:"false"`
|
||||
Identifiers []string `json:"identifiers,omitempty" required:"false"`
|
||||
Manufacturer string `json:"manufacturer,omitempty" required:"false"`
|
||||
Model string `json:"model,omitempty" required:"false"`
|
||||
Name string `json:"name,omitempty" required:"false"`
|
||||
SuggestedArea string `json:"suggested_area,omitempty" required:"false"`
|
||||
SwVersion string `json:"sw_version,omitempty" required:"false"`
|
||||
ViaDevice string `json:"via_device,omitempty" required:"false"`
|
||||
}
|
||||
|
||||
func (m *Mqtt) NewDevice(config EntityConfig) (bool, Device) {
|
||||
var ok bool
|
||||
var ret Device
|
||||
@ -80,52 +67,3 @@ func (m *Mqtt) NewDevice(config EntityConfig) (bool, Device) {
|
||||
return ok, ret
|
||||
|
||||
}
|
||||
|
||||
// func (d *Device) NewDevice(config EntityConfig) Device {
|
||||
// var ret Device
|
||||
//
|
||||
// for range Only.Once {
|
||||
// ret = Device{
|
||||
// ConfigurationUrl: d.ConfigurationUrl,
|
||||
// Connections: [][]string {
|
||||
// { d.Name, JoinStringsForId(d.Name, config.ParentName) },
|
||||
// { JoinStringsForId(d.Name, config.ParentName), JoinStringsForId(d.Name, config.ParentId) },
|
||||
// },
|
||||
// Identifiers: []string{ JoinStringsForId(d.Name, config.ParentId) },
|
||||
// Manufacturer: d.Manufacturer,
|
||||
// Model: d.Model,
|
||||
// Name: JoinStrings(d.Name, config.ParentName),
|
||||
// SuggestedArea: d.SuggestedArea,
|
||||
// SwVersion: d.SwVersion,
|
||||
// ViaDevice: d.ViaDevice,
|
||||
// }
|
||||
//
|
||||
// // // device.Name = JoinStrings(m.Device.Name, config.ParentId)
|
||||
// // device.Name = JoinStrings(m.Device.Name, config.ParentName) // , config.ValueName)
|
||||
// // device.Connections = [][]string {
|
||||
// // { m.Device.Name, JoinStringsForId(m.Device.Name, config.ParentName) },
|
||||
// // { JoinStringsForId(m.Device.Name, config.ParentName), JoinStringsForId(m.Device.Name, config.ParentId) },
|
||||
// // // { JoinStringsForId(m.Device.Name, config.ParentId), JoinStringsForId(m.Device.Name, config.ParentId, config.Name) },
|
||||
// // }
|
||||
// // // device.Identifiers = []string{ JoinStringsForId(m.Device.Name, config.ParentId, config.Name) }
|
||||
// // device.Identifiers = []string{ JoinStringsForId(m.Device.Name, config.ParentId) }
|
||||
// }
|
||||
//
|
||||
// return ret
|
||||
// }
|
||||
|
||||
// {
|
||||
// "device": {
|
||||
// "identifiers": [
|
||||
// "sungrow"
|
||||
// ],
|
||||
// "manufacturer": "MickMake",
|
||||
// "model": "GoLang",
|
||||
// "name": "sungrow",
|
||||
// "sw_version": "sungrow https://github.com/MickMake/GoSungrow"
|
||||
// },
|
||||
// "name": "sungrow",
|
||||
// "stat_t": "~/state",
|
||||
// "unique_id": "sungrow",
|
||||
// "~": "homeassistant/binary_sensor/SunGrow"
|
||||
// }
|
||||
|
@ -1,7 +1 @@
|
||||
package mmHa
|
||||
|
||||
const (
|
||||
LabelSwitch = "switch"
|
||||
LabelBinarySensor = "binary"
|
||||
LabelLight = "light"
|
||||
)
|
||||
|
256
mmHa/cover.go
Normal file
256
mmHa/cover.go
Normal file
@ -0,0 +1,256 @@
|
||||
package mmHa
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/MickMake/GoUnify/Only"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
||||
const LabelCover = "cover"
|
||||
|
||||
|
||||
func (m *Mqtt) CoverPublishConfig(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
config.FixConfig()
|
||||
if !config.IsCover() {
|
||||
break
|
||||
}
|
||||
|
||||
ok, newDevice := m.NewDevice(config)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
|
||||
payload := Cover {
|
||||
Device: newDevice,
|
||||
Name: String(JoinStrings(m.DeviceName, config.Name)),
|
||||
// StateTopic: JoinStringsForTopic(m.switchPrefix, id, "state"),
|
||||
CommandTopic: String(JoinStringsForTopic(m.Prefix, LabelCover, m.ClientId, id, "cmd")),
|
||||
ObjectId: String(id),
|
||||
UniqueId: String(id),
|
||||
Qos: 0,
|
||||
Retain: true,
|
||||
|
||||
// PayloadOn: "true",
|
||||
// PayloadOff: "false",
|
||||
// StateOn: "true",
|
||||
// StateOff: "false",
|
||||
// ValueTemplate: config.ValueTemplate,
|
||||
Icon: Icon(config.Icon),
|
||||
}
|
||||
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelCover, m.ClientId, id, "config")
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
func (m *Mqtt) CoverPublishValue(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
if !config.IsCover() {
|
||||
break
|
||||
}
|
||||
|
||||
if config.IgnoreUpdate {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelCover, m.ClientId, id, "state")
|
||||
|
||||
value := config.Value.String()
|
||||
if value == "--" {
|
||||
value = ""
|
||||
}
|
||||
|
||||
// @TODO - Real hack here. Need to properly check for JSON.
|
||||
if strings.Contains(config.Value.String(), `{`) || strings.Contains(config.Value.String(), `":`) {
|
||||
t := m.client.Publish(tag, 0, true, config.Value.String())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
payload := MqttState{
|
||||
LastReset: config.LastReset, // m.GetLastReset(config.FullId),
|
||||
Value: value,
|
||||
}
|
||||
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
type Cover struct {
|
||||
// A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
|
||||
Availability *Availability `json:"availability,omitempty"`
|
||||
|
||||
// When availability is configured, this controls the conditions needed to set the entity to available. Valid entries are all, any, and latest. If set to all, payload_available must be received on all configured availability topics before the entity is marked as online. If set to any, payload_available must be received on at least one configured availability topic before the entity is marked as online. If set to latest, the last payload_available or payload_not_available received on any configured availability topic controls the availability.
|
||||
AvailabilityMode String `json:"availability_mode,omitempty" default:"latest"`
|
||||
|
||||
// Defines a template to extract device’s availability from the availability_topic. To determine the devices’s availability result of this template will be compared to payload_available and payload_not_available.
|
||||
AvailabilityTemplate Template `json:"availability_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive birth and LWT messages from the MQTT cover device. If an availability topic is not defined, the cover availability state will always be available. If an availability topic is defined, the cover availability state will be unavailable by default. Must not be used together with availability.
|
||||
AvailabilityTopic String `json:"availability_topic,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to control the cover.
|
||||
CommandTopic String `json:"command_topic,omitempty"`
|
||||
|
||||
// Information about the device this cover is a part of to tie it into the device registry. Only works through MQTT discovery and when unique_id is set. At least one of identifiers or connections must be present to identify the device.
|
||||
Device Device `json:"device,omitempty"`
|
||||
|
||||
// Sets the class of the device, changing the device state and icon that is displayed on the frontend.
|
||||
DeviceClass String `json:"device_class,omitempty"`
|
||||
|
||||
// Flag which defines if the entity should be enabled when first added.
|
||||
EnabledByDefault Boolean `json:"enabled_by_default,omitempty" default:"true"`
|
||||
|
||||
// The encoding of the payloads received and published messages. Set to "" to disable decoding of incoming payload.
|
||||
Encoding String `json:"encoding,omitempty" default:"utf"`
|
||||
|
||||
// The category of the entity.
|
||||
EntityCategory String `json:"entity_category,omitempty" default:"None"`
|
||||
|
||||
// Icon for the entity.
|
||||
Icon Icon `json:"icon,omitempty"`
|
||||
|
||||
// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTemplate Template `json:"json_attributes_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive a JSON dictionary payload and then set as sensor attributes. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTopic String `json:"json_attributes_topic,omitempty"`
|
||||
|
||||
// The name of the cover.
|
||||
Name String `json:"name,omitempty" default:"MQTT"`
|
||||
|
||||
// Used instead of name for automatic generation of entity_id
|
||||
ObjectId String `json:"object_id,omitempty"`
|
||||
|
||||
// Flag that defines if switch works in optimistic mode.
|
||||
Optimistic Boolean `json:"optimistic,omitempty"`
|
||||
|
||||
// Default: false if state or position topic defined, else true.
|
||||
// The payload that represents the online state.
|
||||
PayloadAvailable String `json:"payload_available,omitempty" default:"online"`
|
||||
|
||||
// The command payload that closes the cover.
|
||||
PayloadClose String `json:"payload_close,omitempty" default:"CLOSE"`
|
||||
|
||||
// The payload that represents the offline state.
|
||||
PayloadNotAvailable String `json:"payload_not_available,omitempty" default:"offline"`
|
||||
|
||||
// The command payload that opens the cover.
|
||||
PayloadOpen String `json:"payload_open,omitempty" default:"OPEN"`
|
||||
|
||||
// The command payload that stops the cover.
|
||||
PayloadStop String `json:"payload_stop,omitempty" default:"STOP"`
|
||||
|
||||
// Number which represents closed position.
|
||||
PositionClosed Integer `json:"position_closed,omitempty" default:"0"`
|
||||
|
||||
// Number which represents open position.
|
||||
PositionOpen Integer `json:"position_open,omitempty" default:"100"`
|
||||
|
||||
// Defines a template that can be used to extract the payload for the position_topic topic. Within the template the following variables are available: entity_id, position_open; position_closed; tilt_min; tilt_max. The entity_id can be used to reference the entity’s attributes with help of the states template function;
|
||||
PositionTemplate String `json:"position_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive cover position messages.
|
||||
PositionTopic String `json:"position_topic,omitempty"`
|
||||
|
||||
// The maximum QoS level to be used when receiving and publishing messages.
|
||||
Qos Integer `json:"qos,omitempty" default:"0"`
|
||||
|
||||
// Defines if published messages should have the retain flag set.
|
||||
Retain Boolean `json:"retain,omitempty" default:"false"`
|
||||
|
||||
// Defines a template to define the position to be sent to the set_position_topic topic. Incoming position value is available for use in the template {{ position }}. Within the template the following variables are available: entity_id, position, the target position in percent; position_open; position_closed; tilt_min; tilt_max. The entity_id can be used to reference the entity’s attributes with help of the states template function;
|
||||
SetPositionTemplate String `json:"set_position_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish position commands to. You need to set position_topic as well if you want to use position topic. Use template if position topic wants different values than within range position_closed - position_open. If template is not defined and position_closed != 100 and position_open != 0 then proper position value is calculated from percentage position.
|
||||
SetPositionTopic String `json:"set_position_topic,omitempty"`
|
||||
|
||||
// The payload that represents the closed state.
|
||||
StateClosed String `json:"state_closed,omitempty" default:"closed"`
|
||||
|
||||
// The payload that represents the closing state.
|
||||
StateClosing String `json:"state_closing,omitempty" default:"closing"`
|
||||
|
||||
// The payload that represents the open state.
|
||||
StateOpen String `json:"state_open,omitempty" default:"open"`
|
||||
|
||||
// The payload that represents the opening state.
|
||||
StateOpening String `json:"state_opening,omitempty" default:"opening"`
|
||||
|
||||
// The payload that represents the stopped state (for covers that do not report open/closed state).
|
||||
StateStopped String `json:"state_stopped,omitempty" default:"stopped"`
|
||||
|
||||
// The MQTT topic subscribed to receive cover state messages. State topic can only read (open, opening, closed, closing or stopped) state.
|
||||
StateTopic String `json:"state_topic,omitempty"`
|
||||
|
||||
// The value that will be sent on a close_cover_tilt command.
|
||||
TiltClosedValue Integer `json:"tilt_closed_value,omitempty" default:"0"`
|
||||
|
||||
// Defines a template that can be used to extract the payload for the tilt_command_topic topic. Within the template the following variables are available: entity_id, tilt_position, the target tilt position in percent; position_open; position_closed; tilt_min; tilt_max. The entity_id can be used to reference the entity’s attributes with help of the states template function;
|
||||
TiltCommandTemplate String `json:"tilt_command_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to control the cover tilt.
|
||||
TiltCommandTopic String `json:"tilt_command_topic,omitempty"`
|
||||
|
||||
// The maximum tilt value.
|
||||
TiltMax Integer `json:"tilt_max,omitempty" default:"100"`
|
||||
|
||||
// The minimum tilt value.
|
||||
TiltMin Integer `json:"tilt_min,omitempty" default:"0"`
|
||||
|
||||
// The value that will be sent on an open_cover_tilt command.
|
||||
TiltOpenedValue Integer `json:"tilt_opened_value,omitempty" default:"100"`
|
||||
|
||||
// Flag that determines if tilt works in optimistic mode.
|
||||
TiltOptimistic Boolean `json:"tilt_optimistic,omitempty"`
|
||||
|
||||
// Default: true if tilt_status_topic is not defined, else false
|
||||
// Defines a template that can be used to extract the payload for the tilt_status_topic topic. Within the template the following variables are available: entity_id, position_open; position_closed; tilt_min; tilt_max. The entity_id can be used to reference the entity’s attributes with help of the states template function;
|
||||
TiltStatusTemplate String `json:"tilt_status_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive tilt status update values.
|
||||
TiltStatusTopic String `json:"tilt_status_topic,omitempty"`
|
||||
|
||||
// An ID that uniquely identifies this cover. If two covers have the same unique ID, Home Assistant will raise an exception.
|
||||
UniqueId String `json:"unique_id,omitempty"`
|
||||
|
||||
// Defines a template that can be used to extract the payload for the state_topic topic.
|
||||
ValueTemplate String `json:"value_template,omitempty"`
|
||||
}
|
||||
|
||||
func (c *Cover) Json() string {
|
||||
j, _ := json.Marshal(*c)
|
||||
return string(j)
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsCover() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.Units == LabelCover {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
173
mmHa/device_tracker.go
Normal file
173
mmHa/device_tracker.go
Normal file
@ -0,0 +1,173 @@
|
||||
package mmHa
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/MickMake/GoUnify/Only"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const LabelDeviceTracker = "device_tracker"
|
||||
|
||||
|
||||
func (m *Mqtt) DeviceTrackerPublishConfig(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
config.FixConfig()
|
||||
if !config.IsDeviceTracker() {
|
||||
break
|
||||
}
|
||||
|
||||
ok, newDevice := m.NewDevice(config)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
|
||||
payload := DeviceTracker {
|
||||
Device: newDevice,
|
||||
Name: String(JoinStrings(m.DeviceName, config.Name)),
|
||||
// StateTopic: JoinStringsForTopic(m.switchPrefix, id, "state"),
|
||||
// CommandTopic: String(JoinStringsForTopic(m.switchPrefix, id, "cmd")),
|
||||
ObjectId: String(id),
|
||||
UniqueId: String(id),
|
||||
Qos: 0,
|
||||
// Retain: true,
|
||||
|
||||
// PayloadOn: "true",
|
||||
// PayloadOff: "false",
|
||||
// StateOn: "true",
|
||||
// StateOff: "false",
|
||||
// ValueTemplate: config.ValueTemplate,
|
||||
Icon: Icon(config.Icon),
|
||||
}
|
||||
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelDeviceTracker, m.ClientId, id, "config")
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
func (m *Mqtt) DeviceTrackerPublishValue(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
if !config.IsDeviceTracker() {
|
||||
break
|
||||
}
|
||||
|
||||
if config.IgnoreUpdate {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelDeviceTracker, m.ClientId, id, "state")
|
||||
|
||||
value := config.Value.String()
|
||||
if value == "--" {
|
||||
value = ""
|
||||
}
|
||||
|
||||
// @TODO - Real hack here. Need to properly check for JSON.
|
||||
if strings.Contains(config.Value.String(), `{`) || strings.Contains(config.Value.String(), `":`) {
|
||||
t := m.client.Publish(tag, 0, true, config.Value.String())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
payload := MqttState{
|
||||
LastReset: config.LastReset, // m.GetLastReset(config.FullId),
|
||||
Value: value,
|
||||
}
|
||||
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
type DeviceTracker struct {
|
||||
// A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
|
||||
Availability *Availability `json:"availability,omitempty"`
|
||||
|
||||
// When availability is configured, this controls the conditions needed to set the entity to available. Valid entries are all, any, and latest. If set to all, payload_available must be received on all configured availability topics before the entity is marked as online. If set to any, payload_available must be received on at least one configured availability topic before the entity is marked as online. If set to latest, the last payload_available or payload_not_available received on any configured availability topic controls the availability.
|
||||
AvailabilityMode String `json:"availability_mode,omitempty" default:"latest"`
|
||||
|
||||
// Defines a template to extract device’s availability from the availability_topic. To determine the devices’s availability result of this template will be compared to payload_available and payload_not_available.
|
||||
AvailabilityTemplate Template `json:"availability_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive availability (online/offline) updates. Must not be used together with availability.
|
||||
AvailabilityTopic String `json:"availability_topic,omitempty"`
|
||||
|
||||
// Information about the device this device tracker is a part of that ties it into the device registry. At least one of identifiers or connections must be present to identify the device.
|
||||
Device Device `json:"device,omitempty"`
|
||||
|
||||
// Icon for the entity.
|
||||
Icon Icon `json:"icon,omitempty"`
|
||||
|
||||
// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTemplate Template `json:"json_attributes_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive a JSON dictionary payload and then set as device_tracker attributes. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTopic String `json:"json_attributes_topic,omitempty"`
|
||||
|
||||
// The name of the MQTT device_tracker.
|
||||
Name String `json:"name,omitempty"`
|
||||
|
||||
// Used instead of name for automatic generation of entity_id
|
||||
ObjectId String `json:"object_id,omitempty"`
|
||||
|
||||
// The payload that represents the available state.
|
||||
PayloadAvailable String `json:"payload_available,omitempty" default:"online"`
|
||||
|
||||
// The payload value that represents the ‘home’ state for the device.
|
||||
PayloadHome String `json:"payload_home,omitempty" default:"home"`
|
||||
|
||||
// The payload that represents the unavailable state.
|
||||
PayloadNotAvailable String `json:"payload_not_available,omitempty" default:"offline"`
|
||||
|
||||
// The payload value that represents the ‘not_home’ state for the device.
|
||||
PayloadNotHome String `json:"payload_not_home,omitempty" default:"not_home"`
|
||||
|
||||
// The maximum QoS level of the state topic.
|
||||
Qos Integer `json:"qos,omitempty" default:"0"`
|
||||
|
||||
// Attribute of a device tracker that affects state when being used to track a person. Valid options are gps, router, bluetooth, or bluetooth_le.
|
||||
SourceType String `json:"source_type,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive device tracker state changes.
|
||||
StateTopic String `json:"state_topic,omitempty" required:"true"`
|
||||
|
||||
// An ID that uniquely identifies this device_tracker. If two device_trackers have the same unique ID, Home Assistant will raise an exception.
|
||||
UniqueId String `json:"unique_id,omitempty"`
|
||||
|
||||
// Defines a template that returns a device tracker state.
|
||||
ValueTemplate Template `json:"value_template,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
func (c *DeviceTracker) Json() string {
|
||||
j, _ := json.Marshal(*c)
|
||||
return string(j)
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsDeviceTracker() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.Units == LabelDeviceTracker {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
140
mmHa/device_trigger.go
Normal file
140
mmHa/device_trigger.go
Normal file
@ -0,0 +1,140 @@
|
||||
package mmHa
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/MickMake/GoUnify/Only"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const LabelDeviceTrigger = "device_trigger"
|
||||
|
||||
|
||||
func (m *Mqtt) DeviceTriggerPublishConfig(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
config.FixConfig()
|
||||
if !config.IsDeviceTrigger() {
|
||||
break
|
||||
}
|
||||
|
||||
ok, newDevice := m.NewDevice(config)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
|
||||
payload := DeviceTrigger {
|
||||
Device: newDevice,
|
||||
// Name: String(JoinStrings(m.DeviceName, config.Name)),
|
||||
// StateTopic: JoinStringsForTopic(m.switchPrefix, id, "state"),
|
||||
// CommandTopic: String(JoinStringsForTopic(m.switchPrefix, id, "cmd")),
|
||||
// ObjectId: String(id),
|
||||
// UniqueId: String(id),
|
||||
Qos: 0,
|
||||
// Retain: true,
|
||||
|
||||
// PayloadOn: "true",
|
||||
// PayloadOff: "false",
|
||||
// StateOn: "true",
|
||||
// StateOff: "false",
|
||||
// ValueTemplate: config.ValueTemplate,
|
||||
// Icon: Icon(config.Icon),
|
||||
}
|
||||
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelDeviceTrigger, m.ClientId, id, "config")
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
func (m *Mqtt) DeviceTriggerPublishValue(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
if !config.IsDeviceTrigger() {
|
||||
break
|
||||
}
|
||||
|
||||
if config.IgnoreUpdate {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelDeviceTrigger, m.ClientId, id, "state")
|
||||
|
||||
value := config.Value.String()
|
||||
if value == "--" {
|
||||
value = ""
|
||||
}
|
||||
|
||||
// @TODO - Real hack here. Need to properly check for JSON.
|
||||
if strings.Contains(config.Value.String(), `{`) || strings.Contains(config.Value.String(), `":`) {
|
||||
t := m.client.Publish(tag, 0, true, config.Value.String())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
payload := MqttState{
|
||||
LastReset: config.LastReset, // m.GetLastReset(config.FullId),
|
||||
Value: value,
|
||||
}
|
||||
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
type DeviceTrigger struct {
|
||||
// The type of automation, must be ‘trigger’.
|
||||
AutomationType String `json:"automation_type,omitempty" required:"true"`
|
||||
|
||||
// Optional payload to match the payload being sent over the topic.
|
||||
Payload String `json:"payload,omitempty"`
|
||||
|
||||
// The maximum QoS level to be used when receiving messages.
|
||||
Qos Integer `json:"qos,omitempty" default:"0"`
|
||||
|
||||
// The MQTT topic subscribed to receive trigger events.
|
||||
Topic String `json:"topic,omitempty" required:"true"`
|
||||
|
||||
// The type of the trigger, e.g. button_short_press. Entries supported by the frontend: button_short_press, button_short_release, button_long_press, button_long_release, button_double_press, button_triple_press, button_quadruple_press, button_quintuple_press. If set to an unsupported value, will render as subtype type, e.g. button_1 spammed with type set to spammed and subtype set to button_1
|
||||
Type String `json:"type,omitempty" required:"true"`
|
||||
|
||||
// The subtype of the trigger, e.g. button_1. Entries supported by the frontend: turn_on, turn_off, button_1, button_2, button_3, button_4, button_5, button_6. If set to an unsupported value, will render as subtype type, e.g. left_button pressed with type set to button_short_press and subtype set to left_button
|
||||
Subtype String `json:"subtype,omitempty" required:"true"`
|
||||
|
||||
// Information about the device this device trigger is a part of to tie it into the device registry. At least one of identifiers or connections must be present to identify the device.
|
||||
Device Device `json:"device,omitempty" required:"true"`
|
||||
|
||||
// Defines a template to extract the value.
|
||||
ValueTemplate Template `json:"value_template,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
func (c *DeviceTrigger) Json() string {
|
||||
j, _ := json.Marshal(*c)
|
||||
return string(j)
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsDeviceTrigger() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.Units == LabelDeviceTrigger {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
249
mmHa/fan.go
Normal file
249
mmHa/fan.go
Normal file
@ -0,0 +1,249 @@
|
||||
package mmHa
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/MickMake/GoUnify/Only"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const LabelFan = "fan"
|
||||
|
||||
|
||||
func (m *Mqtt) FanPublishConfig(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
config.FixConfig()
|
||||
if !config.IsFan() {
|
||||
break
|
||||
}
|
||||
|
||||
ok, newDevice := m.NewDevice(config)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
|
||||
payload := Fan {
|
||||
Device: newDevice,
|
||||
Name: String(JoinStrings(m.DeviceName, config.Name)),
|
||||
// StateTopic: JoinStringsForTopic(m.switchPrefix, id, "state"),
|
||||
CommandTopic: String(JoinStringsForTopic(m.Prefix, LabelFan, m.ClientId, id, "cmd")),
|
||||
ObjectId: String(id),
|
||||
UniqueId: String(id),
|
||||
Qos: 0,
|
||||
Retain: true,
|
||||
|
||||
// PayloadOn: "true",
|
||||
// PayloadOff: "false",
|
||||
// StateOn: "true",
|
||||
// StateOff: "false",
|
||||
// ValueTemplate: config.ValueTemplate,
|
||||
Icon: Icon(config.Icon),
|
||||
}
|
||||
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelFan, m.ClientId, id, "config")
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
func (m *Mqtt) FanPublishValue(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
if !config.IsFan() {
|
||||
break
|
||||
}
|
||||
|
||||
if config.IgnoreUpdate {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelFan, m.ClientId, id, "state")
|
||||
|
||||
value := config.Value.String()
|
||||
if value == "--" {
|
||||
value = ""
|
||||
}
|
||||
|
||||
// @TODO - Real hack here. Need to properly check for JSON.
|
||||
if strings.Contains(config.Value.String(), `{`) || strings.Contains(config.Value.String(), `":`) {
|
||||
t := m.client.Publish(tag, 0, true, config.Value.String())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
payload := MqttState{
|
||||
LastReset: config.LastReset, // m.GetLastReset(config.FullId),
|
||||
Value: value,
|
||||
}
|
||||
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
type Fan struct {
|
||||
// A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
|
||||
Availability *Availability `json:"availability,omitempty"`
|
||||
|
||||
// When availability is configured, this controls the conditions needed to set the entity to available. Valid entries are all, any, and latest. If set to all, payload_available must be received on all configured availability topics before the entity is marked as online. If set to any, payload_available must be received on at least one configured availability topic before the entity is marked as online. If set to latest, the last payload_available or payload_not_available received on any configured availability topic controls the availability.
|
||||
AvailabilityMode String `json:"availability_mode,omitempty" default:"latest"`
|
||||
|
||||
// Defines a template to extract device’s availability from the availability_topic. To determine the devices’s availability result of this template will be compared to payload_available and payload_not_available.
|
||||
AvailabilityTemplate Template `json:"availability_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive availability (online/offline) updates. Must not be used together with availability.
|
||||
AvailabilityTopic String `json:"availability_topic,omitempty"`
|
||||
|
||||
// Defines a template to generate the payload to send to command_topic.
|
||||
CommandTemplate Template `json:"command_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the fan state.
|
||||
CommandTopic String `json:"command_topic,omitempty" required:"true"`
|
||||
|
||||
// Information about the device this fan is a part of to tie it into the device registry. Only works through MQTT discovery and when unique_id is set. At least one of identifiers or connections must be present to identify the device.
|
||||
Device Device `json:"device,omitempty"`
|
||||
|
||||
// Flag which defines if the entity should be enabled when first added.
|
||||
EnabledByDefault Boolean `json:"enabled_by_default,omitempty" default:"true"`
|
||||
|
||||
// The encoding of the payloads received and published messages. Set to "" to disable decoding of incoming payload.
|
||||
Encoding String `json:"encoding,omitempty" default:"utf"`
|
||||
|
||||
// The category of the entity.
|
||||
EntityCategory String `json:"entity_category,omitempty" default:"None"`
|
||||
|
||||
// Icon for the entity.
|
||||
Icon Icon `json:"icon,omitempty"`
|
||||
|
||||
// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTemplate Template `json:"json_attributes_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive a JSON dictionary payload and then set as sensor attributes. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTopic String `json:"json_attributes_topic,omitempty"`
|
||||
|
||||
// The name of the fan.
|
||||
Name String `json:"name,omitempty" default:"MQTT"`
|
||||
|
||||
// Used instead of name for automatic generation of entity_id
|
||||
ObjectId String `json:"object_id,omitempty"`
|
||||
|
||||
// Flag that defines if fan works in optimistic mode
|
||||
Optimistic Boolean `json:"optimistic,omitempty"`
|
||||
|
||||
// Default: true if no state topic defined, else false.
|
||||
// Defines a template to generate the payload to send to oscillation_command_topic.
|
||||
OscillationCommandTemplate Template `json:"oscillation_command_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the oscillation state.
|
||||
OscillationCommandTopic String `json:"oscillation_command_topic,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive oscillation state updates.
|
||||
OscillationStateTopic String `json:"oscillation_state_topic,omitempty"`
|
||||
|
||||
// Defines a template to extract a value from the oscillation.
|
||||
OscillationValueTemplate String `json:"oscillation_value_template,omitempty"`
|
||||
|
||||
// The payload that represents the available state.
|
||||
PayloadAvailable String `json:"payload_available,omitempty" default:"online"`
|
||||
|
||||
// The payload that represents the unavailable state.
|
||||
PayloadNotAvailable String `json:"payload_not_available,omitempty" default:"offline"`
|
||||
|
||||
// The payload that represents the stop state.
|
||||
PayloadOff String `json:"payload_off,omitempty" default:"OFF"`
|
||||
|
||||
// The payload that represents the running state.
|
||||
PayloadOn String `json:"payload_on,omitempty" default:"ON"`
|
||||
|
||||
// The payload that represents the oscillation off state.
|
||||
PayloadOscillationOff String `json:"payload_oscillation_off,omitempty" default:"oscillate_off"`
|
||||
|
||||
// The payload that represents the oscillation on state.
|
||||
PayloadOscillationOn String `json:"payload_oscillation_on,omitempty" default:"oscillate_on"`
|
||||
|
||||
// A special payload that resets the percentage state attribute to None when received at the percentage_state_topic.
|
||||
PayloadResetPercentage String `json:"payload_reset_percentage,omitempty" default:"None"`
|
||||
|
||||
// A special payload that resets the preset_mode state attribute to None when received at the preset_mode_state_topic.
|
||||
PayloadResetPresetMode String `json:"payload_reset_preset_mode,omitempty" default:"None"`
|
||||
|
||||
// Defines a template to generate the payload to send to percentage_command_topic.
|
||||
PercentageCommandTemplate Template `json:"percentage_command_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the fan speed state based on a percentage.
|
||||
PercentageCommandTopic String `json:"percentage_command_topic,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive fan speed based on percentage.
|
||||
PercentageStateTopic String `json:"percentage_state_topic,omitempty"`
|
||||
|
||||
// Defines a template to extract the percentage value from the payload received on percentage_state_topic.
|
||||
PercentageValueTemplate String `json:"percentage_value_template,omitempty"`
|
||||
|
||||
// Defines a template to generate the payload to send to preset_mode_command_topic.
|
||||
PresetModeCommandTemplate Template `json:"preset_mode_command_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the preset mode.
|
||||
PresetModeCommandTopic String `json:"preset_mode_command_topic,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive fan speed based on presets.
|
||||
PresetModeStateTopic String `json:"preset_mode_state_topic,omitempty"`
|
||||
|
||||
// Defines a template to extract the preset_mode value from the payload received on preset_mode_state_topic.
|
||||
PresetModeValueTemplate String `json:"preset_mode_value_template,omitempty"`
|
||||
|
||||
// List of preset modes this fan is capable of running at. Common examples include auto, smart, whoosh, eco and breeze.
|
||||
PresetModes List `json:"preset_modes,omitempty"(optional, default: [])`
|
||||
|
||||
// The maximum QoS level of the state topic.
|
||||
Qos Integer `json:"qos,omitempty" default:"0"`
|
||||
|
||||
// If the published message should have the retain flag on or not.
|
||||
Retain Boolean `json:"retain,omitempty" default:"true"`
|
||||
|
||||
// The maximum of numeric output range (representing 100 %). The number of speeds within the speed_range / 100 will determine the percentage_step.
|
||||
SpeedRangeMax Integer `json:"speed_range_max,omitempty" default:"100"`
|
||||
|
||||
// The minimum of numeric output range (off not included, so speed_range_min - 1 represents 0 %). The number of speeds within the speed_range / 100 will determine the percentage_step.
|
||||
SpeedRangeMin Integer `json:"speed_range_min,omitempty" default:"1"`
|
||||
|
||||
// The MQTT topic subscribed to receive state updates.
|
||||
StateTopic String `json:"state_topic,omitempty"`
|
||||
|
||||
// Defines a template to extract a value from the state.
|
||||
StateValueTemplate String `json:"state_value_template,omitempty"`
|
||||
|
||||
// An ID that uniquely identifies this fan. If two fans have the same unique ID, Home Assistant will raise an exception.
|
||||
UniqueId String `json:"unique_id,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
func (c *Fan) Json() string {
|
||||
j, _ := json.Marshal(*c)
|
||||
return string(j)
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsFan() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.Units == LabelFan {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
234
mmHa/humidifier.go
Normal file
234
mmHa/humidifier.go
Normal file
@ -0,0 +1,234 @@
|
||||
package mmHa
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/MickMake/GoUnify/Only"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const LabelHumidifier = "humidifier"
|
||||
|
||||
|
||||
func (m *Mqtt) HumidifierPublishConfig(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
config.FixConfig()
|
||||
if !config.IsHumidifier() {
|
||||
break
|
||||
}
|
||||
|
||||
ok, newDevice := m.NewDevice(config)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
|
||||
payload := Humidifier {
|
||||
Device: newDevice,
|
||||
Name: String(JoinStrings(m.DeviceName, config.Name)),
|
||||
// StateTopic: JoinStringsForTopic(m.switchPrefix, id, "state"),
|
||||
CommandTopic: String(JoinStringsForTopic(m.Prefix, LabelHumidifier, m.ClientId, id, "cmd")),
|
||||
ObjectId: String(id),
|
||||
UniqueId: String(id),
|
||||
Qos: 0,
|
||||
Retain: true,
|
||||
|
||||
// PayloadOn: "true",
|
||||
// PayloadOff: "false",
|
||||
// StateOn: "true",
|
||||
// StateOff: "false",
|
||||
// ValueTemplate: config.ValueTemplate,
|
||||
Icon: Icon(config.Icon),
|
||||
}
|
||||
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelHumidifier, m.ClientId, id, "config")
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
func (m *Mqtt) HumidifierPublishValue(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
if !config.IsHumidifier() {
|
||||
break
|
||||
}
|
||||
|
||||
if config.IgnoreUpdate {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelHumidifier, m.ClientId, id, "state")
|
||||
|
||||
value := config.Value.String()
|
||||
if value == "--" {
|
||||
value = ""
|
||||
}
|
||||
|
||||
// @TODO - Real hack here. Need to properly check for JSON.
|
||||
if strings.Contains(config.Value.String(), `{`) || strings.Contains(config.Value.String(), `":`) {
|
||||
t := m.client.Publish(tag, 0, true, config.Value.String())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
payload := MqttState{
|
||||
LastReset: config.LastReset, // m.GetLastReset(config.FullId),
|
||||
Value: value,
|
||||
}
|
||||
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
type Humidifier struct {
|
||||
// A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
|
||||
Availability *Availability `json:"availability,omitempty"`
|
||||
|
||||
// When availability is configured, this controls the conditions needed to set the entity to available. Valid entries are all, any, and latest. If set to all, payload_available must be received on all configured availability topics before the entity is marked as online. If set to any, payload_available must be received on at least one configured availability topic before the entity is marked as online. If set to latest, the last payload_available or payload_not_available received on any configured availability topic controls the availability.
|
||||
AvailabilityMode String `json:"availability_mode,omitempty" default:"latest"`
|
||||
|
||||
// Defines a template to extract device’s availability from the availability_topic. To determine the devices’s availability result of this template will be compared to payload_available and payload_not_available.
|
||||
AvailabilityTemplate Template `json:"availability_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive availability (online/offline) updates. Must not be used together with availability.
|
||||
AvailabilityTopic String `json:"availability_topic,omitempty"`
|
||||
|
||||
// Defines a template to generate the payload to send to command_topic.
|
||||
CommandTemplate Template `json:"command_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the humidifier state.
|
||||
CommandTopic String `json:"command_topic,omitempty" required:"true"`
|
||||
|
||||
// Information about the device this humidifier is a part of to tie it into the device registry. Only works through MQTT discovery and when unique_id is set. At least one of identifiers or connections must be present to identify the device.
|
||||
Device Device `json:"device,omitempty"`
|
||||
|
||||
// The device class of the MQTT device. Must be either humidifier or dehumidifier.
|
||||
DeviceClass String `json:"device_class,omitempty" default:"humidifier"`
|
||||
|
||||
// Flag which defines if the entity should be enabled when first added.
|
||||
EnabledByDefault Boolean `json:"enabled_by_default,omitempty" default:"true"`
|
||||
|
||||
// The encoding of the payloads received and published messages. Set to "" to disable decoding of incoming payload.
|
||||
Encoding String `json:"encoding,omitempty" default:"utf"`
|
||||
|
||||
// The category of the entity.
|
||||
EntityCategory String `json:"entity_category,omitempty" default:"None"`
|
||||
|
||||
// Icon for the entity.
|
||||
Icon Icon `json:"icon,omitempty"`
|
||||
|
||||
// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTemplate Template `json:"json_attributes_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive a JSON dictionary payload and then set as sensor attributes. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTopic String `json:"json_attributes_topic,omitempty"`
|
||||
|
||||
// The minimum target humidity percentage that can be set.
|
||||
MaxHumidity Integer `json:"max_humidity,omitempty" default:"100"`
|
||||
|
||||
// The maximum target humidity percentage that can be set.
|
||||
MinHumidity Integer `json:"min_humidity,omitempty" default:"0"`
|
||||
|
||||
// The name of the humidifier.
|
||||
Name String `json:"name,omitempty" default:"MQTT"`
|
||||
|
||||
// Used instead of name for automatic generation of entity_id
|
||||
ObjectId String `json:"object_id,omitempty"`
|
||||
|
||||
// Flag that defines if humidifier works in optimistic mode
|
||||
Optimistic Boolean `json:"optimistic,omitempty"`
|
||||
|
||||
// Default: true if no state topic defined, else false.
|
||||
// The payload that represents the available state.
|
||||
PayloadAvailable String `json:"payload_available,omitempty" default:"online"`
|
||||
|
||||
// The payload that represents the unavailable state.
|
||||
PayloadNotAvailable String `json:"payload_not_available,omitempty" default:"offline"`
|
||||
|
||||
// The payload that represents the stop state.
|
||||
PayloadOff String `json:"payload_off,omitempty" default:"OFF"`
|
||||
|
||||
// The payload that represents the running state.
|
||||
PayloadOn String `json:"payload_on,omitempty" default:"ON"`
|
||||
|
||||
// A special payload that resets the target_humidity state attribute to None when received at the target_humidity_state_topic.
|
||||
PayloadResetHumidity String `json:"payload_reset_humidity,omitempty" default:"None"`
|
||||
|
||||
// A special payload that resets the mode state attribute to None when received at the mode_state_topic.
|
||||
PayloadResetMode String `json:"payload_reset_mode,omitempty" default:"None"`
|
||||
|
||||
// Defines a template to generate the payload to send to target_humidity_command_topic.
|
||||
TargetHumidityCommandTemplate Template `json:"target_humidity_command_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the humidifier target humidity state based on a percentage.
|
||||
TargetHumidityCommandTopic String `json:"target_humidity_command_topic,omitempty" required:"true"`
|
||||
|
||||
// The MQTT topic subscribed to receive humidifier target humidity.
|
||||
TargetHumidityStateTopic String `json:"target_humidity_state_topic,omitempty"`
|
||||
|
||||
// Defines a template to extract a value for the humidifier target_humidity state.
|
||||
TargetHumidityStateTemplate String `json:"target_humidity_state_template,omitempty"`
|
||||
|
||||
// Defines a template to generate the payload to send to mode_command_topic.
|
||||
ModeCommandTemplate Template `json:"mode_command_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the mode on the humidifier. This attribute ust be configured together with the modes attribute.
|
||||
ModeCommandTopic String `json:"mode_command_topic,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive the humidifier mode.
|
||||
ModeStateTopic String `json:"mode_state_topic,omitempty"`
|
||||
|
||||
// Defines a template to extract a value for the humidifier mode state.
|
||||
ModeStateTemplate String `json:"mode_state_template,omitempty"`
|
||||
|
||||
// List of available modes this humidifier is capable of running at. Common examples include normal, eco, away, boost, comfort, home, sleep, auto and baby. These examples offer built-in translations but other custom modes are allowed as well. This attribute ust be configured together with the mode_command_topic attribute.
|
||||
Modes List `json:"modes,omitempty"(optional, default: [])`
|
||||
|
||||
// The maximum QoS level of the state topic.
|
||||
Qos Integer `json:"qos,omitempty" default:"0"`
|
||||
|
||||
// If the published message should have the retain flag on or not.
|
||||
Retain Boolean `json:"retain,omitempty" default:"true"`
|
||||
|
||||
// The MQTT topic subscribed to receive state updates.
|
||||
StateTopic String `json:"state_topic,omitempty"`
|
||||
|
||||
// Defines a template to extract a value from the state.
|
||||
StateValueTemplate String `json:"state_value_template,omitempty"`
|
||||
|
||||
// An ID that uniquely identifies this humidifier. If two humidifiers have the same unique ID, Home Assistant will raise an exception.
|
||||
UniqueId String `json:"unique_id,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
func (c *Humidifier) Json() string {
|
||||
j, _ := json.Marshal(*c)
|
||||
return string(j)
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsHumidifier() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.Units == LabelHumidifier {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
645
mmHa/lights.go
645
mmHa/lights.go
@ -3,8 +3,11 @@ package mmHa
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/MickMake/GoUnify/Only"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const LabelLight = "light"
|
||||
|
||||
|
||||
func (m *Mqtt) PublishLightConfig(config EntityConfig) error {
|
||||
// func (m *Mqtt) PublishLightConfig(id string, name string, subName string, units string, valueName string, class string) error {
|
||||
@ -23,10 +26,10 @@ func (m *Mqtt) PublishLightConfig(config EntityConfig) error {
|
||||
|
||||
payload := Light {
|
||||
Device: newDevice,
|
||||
Name: JoinStrings(m.DeviceName, config.Name),
|
||||
StateTopic: JoinStringsForTopic(m.switchPrefix, id, "state"),
|
||||
UniqueId: id,
|
||||
ObjectId: id,
|
||||
Name: String(JoinStrings(m.DeviceName, config.Name)),
|
||||
StateTopic: String(JoinStringsForTopic(m.Prefix, LabelLight, m.ClientId, id, "state")),
|
||||
UniqueId: String(id),
|
||||
ObjectId: String(id),
|
||||
|
||||
// StateClass: config.StateClass,
|
||||
// UniqueId: id,
|
||||
@ -40,27 +43,12 @@ func (m *Mqtt) PublishLightConfig(config EntityConfig) error {
|
||||
PayloadOn: "true",
|
||||
PayloadOff: "false",
|
||||
// LastResetValueTemplate: config.LastResetValueTemplate,
|
||||
ValueTemplate: config.ValueTemplate,
|
||||
// Icon: config.Icon,
|
||||
|
||||
// StateClass: "measurement",
|
||||
// UnitOfMeasurement: units,
|
||||
// DeviceClass: class,
|
||||
// Qos: 0,
|
||||
// ForceUpdate: true,
|
||||
// ExpireAfter: 0,
|
||||
// Encoding: "utf-8",
|
||||
// EnabledByDefault: true,
|
||||
// LastResetValueTemplate: LastResetValueTemplate,
|
||||
// LastReset: LastReset,
|
||||
// ValueTemplate: "{{ value_json.value | float }}",
|
||||
// LastReset: time.Now().Format("2006-01-02T00:00:00+00:00"),
|
||||
// LastResetValueTemplate: "{{entity_id}}",
|
||||
// LastResetValueTemplate: "{{ (as_datetime((value_json.last_reset | int | timestamp_utc)|string+'Z')).isoformat() }}",
|
||||
StateValueTemplate: String(config.ValueTemplate),
|
||||
Icon: Icon(config.Icon),
|
||||
}
|
||||
|
||||
// m.client.Publish(JoinStringsForTopic(m.lightPrefix, id, "config"), 0, true, payload.Json())
|
||||
tag := JoinStringsForTopic(m.lightPrefix, id, "config")
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelLight, m.ClientId, id, "config")
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
@ -72,19 +60,36 @@ func (m *Mqtt) PublishLightConfig(config EntityConfig) error {
|
||||
func (m *Mqtt) LightPublishValue(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
// if config.Units != LabelBinarySensor {
|
||||
if !config.IsLight() {
|
||||
break
|
||||
}
|
||||
|
||||
if config.IgnoreUpdate {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
// tagId := JoinStringsForId(m.Device.Name, config.ParentName, config.Name, config.UniqueId),
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelLight, m.ClientId, id, "state")
|
||||
|
||||
value := config.Value.String()
|
||||
if value == "--" {
|
||||
value = ""
|
||||
}
|
||||
|
||||
// @TODO - Real hack here. Need to properly check for JSON.
|
||||
if strings.Contains(config.Value.String(), `{`) || strings.Contains(config.Value.String(), `":`) {
|
||||
t := m.client.Publish(tag, 0, true, config.Value.String())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
payload := MqttState {
|
||||
LastReset: m.GetLastReset(config.UniqueId),
|
||||
Value: config.Value,
|
||||
LastReset: config.LastReset, // m.GetLastReset(config.FullId),
|
||||
Value: value,
|
||||
}
|
||||
tag := JoinStringsForTopic(m.lightPrefix, id, "state")
|
||||
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
@ -94,106 +99,508 @@ func (m *Mqtt) LightPublishValue(config EntityConfig) error {
|
||||
return m.err
|
||||
}
|
||||
|
||||
// func (m *Mqtt) PublishLight(subtopic string, payload interface{}) error {
|
||||
// for range Only.Once {
|
||||
// t := m.client.Publish(JoinStringsForTopic(m.lightPrefix, subtopic), 0, true, payload)
|
||||
// if !t.WaitTimeout(m.Timeout) {
|
||||
// m.err = t.Error()
|
||||
// }
|
||||
// }
|
||||
// return m.err
|
||||
// }
|
||||
//
|
||||
// func (m *Mqtt) PublishLightState(topic string, payload interface{}) error {
|
||||
// for range Only.Once {
|
||||
// topic = JoinStringsForId(m.EntityPrefix, m.DeviceName, topic)
|
||||
// t := m.client.Publish(JoinStringsForTopic(m.lightPrefix, topic, "state"), 0, true, payload)
|
||||
// if !t.WaitTimeout(m.Timeout) {
|
||||
// m.err = t.Error()
|
||||
// }
|
||||
// }
|
||||
// return m.err
|
||||
// }
|
||||
|
||||
|
||||
type Light struct {
|
||||
AvailabilityTopic string `json:"availability_topic,omitempty"`
|
||||
BrightnessCommandTopic string `json:"brightness_command_topic,omitempty"`
|
||||
BrightnessScale int `json:"brightness_scale,omitempty"`
|
||||
BrightnessStateTopic string `json:"brightness_state_topic,omitempty"`
|
||||
BrightnessValueTemplate string `json:"brightness_value_template,omitempty"`
|
||||
ColorTempCommandTemplate string `json:"color_temp_command_template,omitempty"`
|
||||
ColorTempCommandTopic string `json:"color_temp_command_topic,omitempty"`
|
||||
ColorTempStateTopic string `json:"color_temp_state_topic,omitempty"`
|
||||
ColorTempValueTemplate string `json:"color_temp_value_template,omitempty"`
|
||||
CommandTopic string `json:"command_topic"`
|
||||
Device Device `json:"device,omitempty"`
|
||||
EffectCommandTopic string `json:"effect_command_topic,omitempty"`
|
||||
EffectList []string `json:"effect_list,omitempty"`
|
||||
EffectStateTopic string `json:"effect_state_topic,omitempty"`
|
||||
EffectValueTemplate string `json:"effect_value_template,omitempty"`
|
||||
HsCommandTopic string `json:"hs_command_topic,omitempty"`
|
||||
HsStateTopic string `json:"hs_state_topic,omitempty"`
|
||||
HsValueTemplate string `json:"hs_value_template,omitempty"`
|
||||
JSONAttributesTemplate string `json:"json_attributes_template,omitempty"`
|
||||
JSONAttributesTopic string `json:"json_attributes_topic,omitempty"`
|
||||
MaxMireds int `json:"max_mireds,omitempty"`
|
||||
MinMireds int `json:"min_mireds,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
ObjectId string `json:"object_id,omitempty" required:"false"`
|
||||
OnCommandType string `json:"on_command_type,omitempty"`
|
||||
Optimistic bool `json:"opt,omitempty"`
|
||||
PayloadAvailable string `json:"payload_available,omitempty"`
|
||||
PayloadNotAvailable string `json:"payload_not_available,omitempty"`
|
||||
PayloadOff string `json:"pl_off,omitempty"`
|
||||
PayloadOn string `json:"pl_on,omitempty"`
|
||||
QOS int `json:"qos,omitempty"`
|
||||
Retain bool `json:"ret,omitempty"`
|
||||
RgbCommandTemplate string `json:"rgb_command_template,omitempty"`
|
||||
RgbCommandTopic string `json:"rgb_command_topic,omitempty"`
|
||||
RgbStateTopic string `json:"rgb_state_topic,omitempty"`
|
||||
RgbValueTemplate string `json:"rgb_value_template,omitempty"`
|
||||
Schema string `json:"schema,omitempty"`
|
||||
StateTopic string `json:"state_topic,omitempty"`
|
||||
StateValueTemplate string `json:"state_value_template,omitempty"`
|
||||
UniqueId string `json:"unique_id,omitempty" required:"false"`
|
||||
WhiteValueCommandTopic string `json:"white_value_command_topic,omitempty"`
|
||||
WhiteValueScale int `json:"white_value_scale,omitempty"`
|
||||
WhiteValueStateTopic string `json:"white_value_state_topic,omitempty"`
|
||||
WhiteValueTemplate string `json:"white_value_template,omitempty"`
|
||||
XyCommandTopic string `json:"xy_command_topic,omitempty"`
|
||||
XyStateTopic string `json:"xy_state_topic,omitempty"`
|
||||
XyValueTemplate string `json:"xy_value_template,omitempty"`
|
||||
ValueTemplate string `json:"value_template,omitempty"`
|
||||
// A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
|
||||
Availability *Availability `json:"availability,omitempty"`
|
||||
|
||||
// BrightnessStateFunc func() string `json:"-"`
|
||||
// ColorTempStateFunc func() string `json:"-"`
|
||||
// EffectStateFunc func() string `json:"-"`
|
||||
// HsStateFunc func() string `json:"-"`
|
||||
// RgbStateFunc func() string `json:"-"`
|
||||
// StateFunc func() string `json:"-"`
|
||||
// WhiteValueStateFunc func() string `json:"-"`
|
||||
// XyStateFunc func() string `json:"-"`
|
||||
//
|
||||
// BrightnessCommandFunc func(mqtt.Message, mqtt.Client) `json:"-"`
|
||||
// ColorTempCommandFunc func(mqtt.Message, mqtt.Client) `json:"-"`
|
||||
// CommandFunc func(mqtt.Message, mqtt.Client) `json:"-"`
|
||||
// EffectCommandFunc func(mqtt.Message, mqtt.Client) `json:"-"`
|
||||
// HsCommandFunc func(mqtt.Message, mqtt.Client) `json:"-"`
|
||||
// RgbCommandFunc func(mqtt.Message, mqtt.Client) `json:"-"`
|
||||
// WhiteValueCommandFunc func(mqtt.Message, mqtt.Client) `json:"-"`
|
||||
// XyCommandFunc func(mqtt.Message, mqtt.Client) `json:"-"`
|
||||
//
|
||||
// UpdateInterval float64 `json:"-"`
|
||||
// ForceUpdateMQTT bool `json:"-"`
|
||||
//
|
||||
// messageHandler mqtt.MessageHandler
|
||||
// When availability is configured, this controls the conditions needed to set the entity to available. Valid entries are all, any, and latest. If set to all, payload_available must be received on all configured availability topics before the entity is marked as online. If set to any, payload_available must be received on at least one configured availability topic before the entity is marked as online. If set to latest, the last payload_available or payload_not_available received on any configured availability topic controls the availability.
|
||||
AvailabilityMode String `json:"availability_mode,omitempty" default:"latest"`
|
||||
|
||||
// Defines a template to extract device’s availability from the availability_topic. To determine the devices’s availability result of this template will be compared to payload_available and payload_not_available.
|
||||
AvailabilityTemplate Template `json:"availability_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive availability (online/offline) updates. Must not be used together with availability.
|
||||
AvailabilityTopic String `json:"availability_topic,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the light’s brightness.
|
||||
BrightnessCommandTopic String `json:"brightness_command_topic,omitempty"`
|
||||
|
||||
// Defines a template to compose message which will be sent to brightness_command_topic. Available variables: value.
|
||||
BrightnessCommandTemplate String `json:"brightness_command_template,omitempty"`
|
||||
|
||||
// Defines the maximum brightness value (i.e., 100%) of the MQTT device.
|
||||
BrightnessScale Integer `json:"brightness_scale,omitempty" default:"255"`
|
||||
|
||||
// The MQTT topic subscribed to receive brightness state updates.
|
||||
BrightnessStateTopic String `json:"brightness_state_topic,omitempty"`
|
||||
|
||||
// Defines a template to extract the brightness value.
|
||||
BrightnessValueTemplate String `json:"brightness_value_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive color mode updates. If this is not configured, color_mode will be automatically set according to the last received valid color or color temperature
|
||||
ColorModeStateTopic String `json:"color_mode_state_topic,omitempty"`
|
||||
|
||||
// Defines a template to extract the color mode.
|
||||
ColorModeValueTemplate String `json:"color_mode_value_template,omitempty"`
|
||||
|
||||
// Defines a template to compose message which will be sent to color_temp_command_topic. Available variables: value.
|
||||
ColorTempCommandTemplate String `json:"color_temp_command_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the light’s color temperature state. The color temperature command slider has a range of 153 to 500 mireds (micro reciprocal degrees).
|
||||
ColorTempCommandTopic String `json:"color_temp_command_topic,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive color temperature state updates.
|
||||
ColorTempStateTopic String `json:"color_temp_state_topic,omitempty"`
|
||||
|
||||
// Defines a template to extract the color temperature value.
|
||||
ColorTempValueTemplate String `json:"color_temp_value_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the switch state.
|
||||
CommandTopic String `json:"command_topic,omitempty" required:"true"`
|
||||
|
||||
// Information about the device this light is a part of to tie it into the device registry. Only works through MQTT discovery and when unique_id is set. At least one of identifiers or connections must be present to identify the device.
|
||||
Device Device `json:"device,omitempty"`
|
||||
|
||||
// Flag which defines if the entity should be enabled when first added.
|
||||
EnabledByDefault Boolean `json:"enabled_by_default,omitempty" default:"true"`
|
||||
|
||||
// The encoding of the payloads received and published messages. Set to "" to disable decoding of incoming payload.
|
||||
Encoding String `json:"encoding,omitempty" default:"utf"`
|
||||
|
||||
// The category of the entity.
|
||||
EntityCategory String `json:"entity_category,omitempty" default:"None"`
|
||||
|
||||
// The MQTT topic to publish commands to change the light’s effect state.
|
||||
EffectCommandTopic String `json:"effect_command_topic,omitempty"`
|
||||
|
||||
// Defines a template to compose message which will be sent to effect_command_topic. Available variables: value.
|
||||
EffectCommandTemplate String `json:"effect_command_template,omitempty"`
|
||||
|
||||
// effect_list string | list (optional)
|
||||
// The list of effects the light supports.
|
||||
|
||||
// The MQTT topic subscribed to receive effect state updates.
|
||||
EffectStateTopic String `json:"effect_state_topic,omitempty"`
|
||||
|
||||
// Defines a template to extract the effect value.
|
||||
EffectValueTemplate String `json:"effect_value_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the light’s color state in HS format (Hue Saturation). Range for Hue: 0° .. 360°, Range of Saturation: 0..100. Note: Brightness is sent separately in the brightness_command_topic.
|
||||
HsCommandTopic String `json:"hs_command_topic,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive color state updates in HS format. Note: Brightness is received separately in the brightness_state_topic.
|
||||
HsStateTopic String `json:"hs_state_topic,omitempty"`
|
||||
|
||||
// Defines a template to extract the HS value.
|
||||
HsValueTemplate String `json:"hs_value_template,omitempty"`
|
||||
|
||||
// Icon for the entity.
|
||||
Icon Icon `json:"icon,omitempty"`
|
||||
|
||||
// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTemplate Template `json:"json_attributes_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive a JSON dictionary payload and then set as sensor attributes. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTopic String `json:"json_attributes_topic,omitempty"`
|
||||
|
||||
// The maximum color temperature in mireds.
|
||||
MaxMireds Integer `json:"max_mireds,omitempty"`
|
||||
|
||||
// The minimum color temperature in mireds.
|
||||
MinMireds Integer `json:"min_mireds,omitempty"`
|
||||
|
||||
// The name of the light.
|
||||
Name String `json:"name,omitempty" default:"MQTT"`
|
||||
|
||||
// Used instead of name for automatic generation of entity_id
|
||||
ObjectId String `json:"object_id,omitempty"`
|
||||
|
||||
// Defines when on the payload_on is sent. Using last (the default) will send any style (brightness, color, etc) topics first and then a payload_on to the command_topic. Using first will send the payload_on and then any style topics. Using brightness will only send brightness commands instead of the payload_on to turn the light on.
|
||||
OnCommandType String `json:"on_command_type,omitempty"`
|
||||
|
||||
// Flag that defines if switch works in optimistic mode.
|
||||
Optimistic Boolean `json:"optimistic,omitempty"`
|
||||
|
||||
// Default: true if no state topic defined, else false.
|
||||
// The payload that represents the available state.
|
||||
PayloadAvailable String `json:"payload_available,omitempty" default:"online"`
|
||||
|
||||
// The payload that represents the unavailable state.
|
||||
PayloadNotAvailable String `json:"payload_not_available,omitempty" default:"offline"`
|
||||
|
||||
// The payload that represents disabled state.
|
||||
PayloadOff String `json:"payload_off,omitempty" default:"OFF"`
|
||||
|
||||
// The payload that represents enabled state.
|
||||
PayloadOn String `json:"payload_on,omitempty" default:"ON"`
|
||||
|
||||
// The maximum QoS level of the state topic.
|
||||
Qos Integer `json:"qos,omitempty" default:"0"`
|
||||
|
||||
// If the published message should have the retain flag on or not.
|
||||
Retain Boolean `json:"retain,omitempty" default:"false"`
|
||||
|
||||
// Defines a template to compose message which will be sent to rgb_command_topic. Available variables: red, green and blue.
|
||||
RgbCommandTemplate String `json:"rgb_command_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the light’s RGB state.
|
||||
RgbCommandTopic String `json:"rgb_command_topic,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive RGB state updates. The expected payload is the RGB values separated by commas, for example, 255,0,127.
|
||||
RgbStateTopic String `json:"rgb_state_topic,omitempty"`
|
||||
|
||||
// Defines a template to extract the RGB value.
|
||||
RgbValueTemplate String `json:"rgb_value_template,omitempty"`
|
||||
|
||||
// Defines a template to compose message which will be sent to rgbw_command_topic. Available variables: red, green, blue and white.
|
||||
RgbwCommandTemplate String `json:"rgbw_command_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the light’s RGBW state.
|
||||
RgbwCommandTopic String `json:"rgbw_command_topic,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive RGBW state updates. The expected payload is the RGBW values separated by commas, for example, 255,0,127,64.
|
||||
RgbwStateTopic String `json:"rgbw_state_topic,omitempty"`
|
||||
|
||||
// Defines a template to extract the RGBW value.
|
||||
RgbwValueTemplate String `json:"rgbw_value_template,omitempty"`
|
||||
|
||||
// Defines a template to compose message which will be sent to rgbww_command_topic. Available variables: red, green, blue, cold_white and warm_white.
|
||||
RgbwwCommandTemplate String `json:"rgbww_command_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the light’s RGBWW state.
|
||||
RgbwwCommandTopic String `json:"rgbww_command_topic,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive RGBWW state updates. The expected payload is the RGBWW values separated by commas, for example, 255,0,127,64,32.
|
||||
RgbwwStateTopic String `json:"rgbww_state_topic,omitempty"`
|
||||
|
||||
// Defines a template to extract the RGBWW value.
|
||||
RgbwwValueTemplate String `json:"rgbww_value_template,omitempty"`
|
||||
|
||||
// The schema to use. Must be default or omitted to select the default schema.
|
||||
Schema String `json:"schema,omitempty" default:"default"`
|
||||
|
||||
// The MQTT topic subscribed to receive state updates.
|
||||
StateTopic String `json:"state_topic,omitempty"`
|
||||
|
||||
// Defines a template to extract the state value. The template should match the payload on and off values, so if your light uses power on to turn on, your state_value_template string should return power on when the switch is on. For example if the message is just on, your state_value_template should be power .
|
||||
StateValueTemplate String `json:"state_value_template,omitempty"`
|
||||
|
||||
// An ID that uniquely identifies this light. If two lights have the same unique ID, Home Assistant will raise an exception.
|
||||
UniqueId String `json:"unique_id,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the light to white mode with a given brightness.
|
||||
WhiteCommandTopic String `json:"white_command_topic,omitempty"`
|
||||
|
||||
// Defines the maximum white level (i.e., 100%) of the MQTT device.
|
||||
WhiteScale Integer `json:"white_scale,omitempty" default:"255"`
|
||||
|
||||
// The MQTT topic to publish commands to change the light’s XY state.
|
||||
XyCommandTopic String `json:"xy_command_topic,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive XY state updates.
|
||||
XyStateTopic String `json:"xy_state_topic,omitempty"`
|
||||
|
||||
// Defines a template to extract the XY value.
|
||||
XyValueTemplate String `json:"xy_value_template,omitempty"`
|
||||
}
|
||||
|
||||
// type Light struct {
|
||||
// // availability list (optional)
|
||||
// // A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
|
||||
// Availability *Availability `json:"availability,omitempty" required:"false"`
|
||||
//
|
||||
// // availability_mode string (optional, default: latest)
|
||||
// // When availability is configured, this controls the conditions needed to set the entity to available. Valid entries are all, any, and latest. If set to all, payload_available must be received on all configured availability topics before the entity is marked as online. If set to any, payload_available must be received on at least one configured availability topic before the entity is marked as online. If set to latest, the last payload_available or payload_not_available received on any configured availability topic controls the availability.
|
||||
// AvailabilityMode string `json:"availability_mode,omitempty" required:"false"`
|
||||
//
|
||||
// // availability_template template (optional)
|
||||
// // Defines a template to extract device’s availability from the availability_topic. To determine the devices’s availability result of this template will be compared to payload_available and payload_not_available.
|
||||
// AvailabilityTemplate string `json:"availability_template,omitempty" required:"false"`
|
||||
//
|
||||
// // availability_topic string (optional)
|
||||
// // The MQTT topic subscribed to receive availability (online/offline) updates. Must not be used together with availability.
|
||||
// AvailabilityTopic string `json:"availability_topic,omitempty" required:"false"`
|
||||
//
|
||||
// // brightness_command_topic string (optional)
|
||||
// // The MQTT topic to publish commands to change the light’s brightness.
|
||||
// BrightnessCommandTopic string `json:"brightness_command_topic,omitempty"`
|
||||
//
|
||||
// // brightness_command_template string (optional)
|
||||
// // Defines a template to compose message which will be sent to brightness_command_topic. Available variables: value.
|
||||
// BrightnessCommandTemplate string `json:"brightness_command_template,omitempty"`
|
||||
//
|
||||
// // brightness_scale integer (optional, default: 255)
|
||||
// // Defines the maximum brightness value (i.e., 100%) of the MQTT device.
|
||||
// BrightnessScale int `json:"brightness_scale,omitempty"`
|
||||
//
|
||||
// // brightness_state_topic string (optional)
|
||||
// // The MQTT topic subscribed to receive brightness state updates.
|
||||
// BrightnessStateTopic string `json:"brightness_state_topic,omitempty"`
|
||||
//
|
||||
// // brightness_value_template string (optional)
|
||||
// // Defines a template to extract the brightness value.
|
||||
// BrightnessValueTemplate string `json:"brightness_value_template,omitempty"`
|
||||
//
|
||||
// // color_mode_state_topic string (optional)
|
||||
// // The MQTT topic subscribed to receive color mode updates. If this is not configured, color_mode will be automatically set according to the last received valid color or color temperature
|
||||
// ColorModeStateTopic string `json:"color_mode_state_topic,omitempty"`
|
||||
//
|
||||
// // color_mode_value_template string (optional)
|
||||
// // Defines a template to extract the color mode.
|
||||
// ColorModeValueTemplate string `json:"color_mode_value_template,omitempty"`
|
||||
//
|
||||
// // color_temp_command_template string (optional)
|
||||
// // Defines a template to compose message which will be sent to color_temp_command_topic. Available variables: value.
|
||||
// ColorTempCommandTemplate string `json:"color_temp_command_template,omitempty"`
|
||||
//
|
||||
// // color_temp_command_topic string (optional)
|
||||
// // The MQTT topic to publish commands to change the light’s color temperature state. The color temperature command slider has a range of 153 to 500 mireds (micro reciprocal degrees).
|
||||
// ColorTempCommandTopic string `json:"color_temp_command_topic,omitempty"`
|
||||
//
|
||||
// // color_temp_state_topic string (optional)
|
||||
// // The MQTT topic subscribed to receive color temperature state updates.
|
||||
// ColorTempStateTopic string `json:"color_temp_state_topic,omitempty"`
|
||||
//
|
||||
// // color_temp_value_template string (optional)
|
||||
// // Defines a template to extract the color temperature value.
|
||||
// ColorTempValueTemplate string `json:"color_temp_value_template,omitempty"`
|
||||
// // CommandTemplate string `json:"command_template" required:"false"`
|
||||
//
|
||||
// // command_topic string REQUIRED
|
||||
// // The MQTT topic to publish commands to change the switch state.
|
||||
// CommandTopic string `json:"command_topic" required:"true"`
|
||||
//
|
||||
// // device map (optional)
|
||||
// // Information about the device this light is a part of to tie it into the device registry. Only works through MQTT discovery and when unique_id is set. At least one of identifiers or connections must be present to identify the device.
|
||||
// Device Device `json:"device,omitempty" required:"false"`
|
||||
//
|
||||
// // enabled_by_default boolean (optional, default: true)
|
||||
// // Flag which defines if the entity should be enabled when first added.
|
||||
// EnabledByDefault bool `json:"enabled_by_default,omitempty" required:"false"`
|
||||
//
|
||||
// // encoding string (optional, default: utf-8)
|
||||
// // The encoding of the payloads received and published messages. Set to "" to disable decoding of incoming payload.
|
||||
// Encoding string `json:"encoding,omitempty" required:"false"`
|
||||
//
|
||||
// // entity_category string (optional, default: None)
|
||||
// // The category of the entity.
|
||||
// EntityCategory string `json:"entity_category,omitempty" required:"false"`
|
||||
//
|
||||
// // effect_command_template string (optional)
|
||||
// // Defines a template to compose message which will be sent to effect_command_topic. Available variables: value.
|
||||
// EffectCommandTemplate string `json:"effect_command_template" required:"true"`
|
||||
//
|
||||
// // effect_command_topic string (optional)
|
||||
// // The MQTT topic to publish commands to change the light’s effect state.
|
||||
// EffectCommandTopic string `json:"effect_command_topic,omitempty"`
|
||||
//
|
||||
// // effect_list string | list (optional)
|
||||
// // The list of effects the light supports.
|
||||
// EffectList []string `json:"effect_list,omitempty"`
|
||||
//
|
||||
// // effect_state_topic string (optional)
|
||||
// // The MQTT topic subscribed to receive effect state updates.
|
||||
// EffectStateTopic string `json:"effect_state_topic,omitempty"`
|
||||
//
|
||||
// // effect_value_template string (optional)
|
||||
// // Defines a template to extract the effect value.
|
||||
// EffectValueTemplate string `json:"effect_value_template,omitempty"`
|
||||
//
|
||||
// // hs_command_topic string (optional)
|
||||
// // The MQTT topic to publish commands to change the light’s color state in HS format (Hue Saturation). Range for Hue: 0° .. 360°, Range of Saturation: 0..100. Note: Brightness is sent separately in the brightness_command_topic.
|
||||
// HsCommandTopic string `json:"hs_command_topic,omitempty"`
|
||||
//
|
||||
// // hs_state_topic string (optional)
|
||||
// // The MQTT topic subscribed to receive color state updates in HS format. Note: Brightness is received separately in the brightness_state_topic.
|
||||
// HsStateTopic string `json:"hs_state_topic,omitempty"`
|
||||
//
|
||||
// // hs_value_template string (optional)
|
||||
// // Defines a template to extract the HS value.
|
||||
// HsValueTemplate string `json:"hs_value_template,omitempty"`
|
||||
//
|
||||
// // icon string (optional)
|
||||
// // Icon for the entity.
|
||||
// Icon string `json:"icon,omitempty" required:"false"`
|
||||
//
|
||||
// // json_attributes_template template (optional)
|
||||
// // Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic. Usage example can be found in MQTT sensor documentation.
|
||||
// JsonAttributesTemplate string `json:"json_attributes_template,omitempty" required:"false"`
|
||||
//
|
||||
// // json_attributes_topic string (optional)
|
||||
// // The MQTT topic subscribed to receive a JSON dictionary payload and then set as sensor attributes. Usage example can be found in MQTT sensor documentation.
|
||||
// JsonAttributesTopic string `json:"json_attributes_topic,omitempty" required:"false"`
|
||||
//
|
||||
// // max_mireds integer (optional)
|
||||
// // The maximum color temperature in mireds.
|
||||
// MaxMireds int `json:"max_mireds,omitempty"`
|
||||
//
|
||||
// // min_mireds integer (optional)
|
||||
// // The minimum color temperature in mireds.
|
||||
// MinMireds int `json:"min_mireds,omitempty"`
|
||||
//
|
||||
// // name string (optional, default: MQTT Light)
|
||||
// // The name of the light.
|
||||
// Name string `json:"name,omitempty" required:"false"`
|
||||
//
|
||||
// // object_id string (optional)
|
||||
// // Used instead of name for automatic generation of entity_id
|
||||
// ObjectId string `json:"object_id,omitempty" required:"false"`
|
||||
//
|
||||
// // on_command_type string (optional)
|
||||
// // Defines when on the payload_on is sent. Using last (the default) will send any style (brightness, color, etc) topics first and then a payload_on to the command_topic. Using first will send the payload_on and then any style topics. Using brightness will only send brightness commands instead of the payload_on to turn the light on.
|
||||
// OnCommandType string `json:"on_command_type,omitempty"`
|
||||
//
|
||||
// // optimistic boolean (optional)
|
||||
// // Flag that defines if switch works in optimistic mode.
|
||||
// // Default: true if no state topic defined, else false.
|
||||
// Optimistic bool `json:"optimistic,omitempty" required:"false"`
|
||||
//
|
||||
// // payload_available string (optional, default: online)
|
||||
// // The payload that represents the available state.
|
||||
// PayloadAvailable string `json:"payload_available,omitempty"`
|
||||
//
|
||||
// // payload_not_available string (optional, default: offline)
|
||||
// // The payload that represents the unavailable state.
|
||||
// PayloadNotAvailable string `json:"payload_not_available,omitempty"`
|
||||
//
|
||||
// // payload_off string (optional, default: OFF)
|
||||
// // The payload that represents disabled state.
|
||||
// PayloadOff string `json:"payload_off,omitempty"`
|
||||
//
|
||||
// // payload_on string (optional, default: ON)
|
||||
// // The payload that represents enabled state.
|
||||
// PayloadOn string `json:"payload_on,omitempty"`
|
||||
//
|
||||
// // qos integer (optional, default: 0)
|
||||
// // The maximum QoS level of the state topic.
|
||||
// Qos int `json:"qos,omitempty" required:"false"`
|
||||
//
|
||||
// // retain boolean (optional, default: false)
|
||||
// // If the published message should have the retain flag on or not.
|
||||
// Retain bool `json:"retain,omitempty" required:"false"`
|
||||
//
|
||||
// // rgb_command_template string (optional)
|
||||
// // Defines a template to compose message which will be sent to rgb_command_topic. Available variables: red, green and blue.
|
||||
// RgbCommandTemplate string `json:"rgb_command_template,omitempty"`
|
||||
//
|
||||
// // rgb_command_topic string (optional)
|
||||
// // The MQTT topic to publish commands to change the light’s RGB state.
|
||||
// RgbCommandTopic string `json:"rgb_command_topic,omitempty"`
|
||||
//
|
||||
// // rgb_state_topic string (optional)
|
||||
// // The MQTT topic subscribed to receive RGB state updates. The expected payload is the RGB values separated by commas, for example, 255,0,127.
|
||||
// RgbStateTopic string `json:"rgb_state_topic,omitempty"`
|
||||
//
|
||||
// // rgb_value_template string (optional)
|
||||
// // Defines a template to extract the RGB value.
|
||||
// RgbValueTemplate string `json:"rgb_value_template,omitempty"`
|
||||
//
|
||||
// // rgbw_command_template string (optional)
|
||||
// // Defines a template to compose message which will be sent to rgbw_command_topic. Available variables: red, green, blue and white.
|
||||
// RgbwCommandTemplate string `json:"rgbw_command_template,omitempty"`
|
||||
//
|
||||
// // rgbw_command_topic string (optional)
|
||||
// // The MQTT topic to publish commands to change the light’s RGBW state.
|
||||
// RgbwCommandTopic string `json:"rgbw_command_topic,omitempty"`
|
||||
//
|
||||
// // rgbw_state_topic string (optional)
|
||||
// // The MQTT topic subscribed to receive RGBW state updates. The expected payload is the RGBW values separated by commas, for example, 255,0,127,64.
|
||||
// RgbwStateTopic string `json:"rgbw_state_topic,omitempty"`
|
||||
//
|
||||
// // rgbw_value_template string (optional)
|
||||
// // Defines a template to extract the RGBW value.
|
||||
// RgbwValueTemplate string `json:"rgbw_value_template,omitempty"`
|
||||
//
|
||||
// // rgbww_command_template string (optional)
|
||||
// // Defines a template to compose message which will be sent to rgbww_command_topic. Available variables: red, green, blue, cold_white and warm_white.
|
||||
// RgbwwCommandTemplate string `json:"rgbww_command_template,omitempty"`
|
||||
//
|
||||
// // rgbww_command_topic string (optional)
|
||||
// // The MQTT topic to publish commands to change the light’s RGBWW state.
|
||||
// RgbwwCommandTopic string `json:"rgbww_command_topic,omitempty"`
|
||||
//
|
||||
// // rgbww_state_topic string (optional)
|
||||
// // The MQTT topic subscribed to receive RGBWW state updates. The expected payload is the RGBWW values separated by commas, for example, 255,0,127,64,32.
|
||||
// RgbwwStateTopic string `json:"rgbww_state_topic,omitempty"`
|
||||
//
|
||||
// // rgbww_value_template string (optional)
|
||||
// // Defines a template to extract the RGBWW value.
|
||||
// RgbwwValueTemplate string `json:"rgbww_value_template,omitempty"`
|
||||
//
|
||||
// // schema string (optional, default: default)
|
||||
// // The schema to use. Must be default or omitted to select the default schema.
|
||||
// Schema string `json:"schema,omitempty"`
|
||||
//
|
||||
// // state_topic string (optional)
|
||||
// // The MQTT topic subscribed to receive state updates.
|
||||
// StateTopic string `json:"state_topic,omitempty" required:"false"`
|
||||
//
|
||||
// // state_value_template string (optional)
|
||||
// // Defines a template to extract the state value. The template should match the payload on and off values, so if your light uses power on to turn on, your state_value_template string should return power on when the switch is on. For example if the message is just on, your state_value_template should be power .
|
||||
// StateValueTemplate string `json:"state_value_template,omitempty"`
|
||||
//
|
||||
// // unique_id string (optional)
|
||||
// // An ID that uniquely identifies this light. If two lights have the same unique ID, Home Assistant will raise an exception.
|
||||
// UniqueId string `json:"unique_id,omitempty" required:"false"`
|
||||
//
|
||||
// // white_command_topic string (optional)
|
||||
// // The MQTT topic to publish commands to change the light to white mode with a given brightness.
|
||||
// WhiteCommandTopic string `json:"white_command_topic,omitempty"`
|
||||
//
|
||||
// // white_scale integer (optional, default: 255)
|
||||
// // Defines the maximum white level (i.e., 100%) of the MQTT device.
|
||||
// WhiteScale int `json:"white_scale,omitempty"`
|
||||
//
|
||||
// // xy_command_topic string (optional)
|
||||
// // The MQTT topic to publish commands to change the light’s XY state.
|
||||
// XyCommandTopic string `json:"xy_command_topic,omitempty"`
|
||||
//
|
||||
// // xy_state_topic string (optional)
|
||||
// // The MQTT topic subscribed to receive XY state updates.
|
||||
// XyStateTopic string `json:"xy_state_topic,omitempty"`
|
||||
//
|
||||
// // xy_value_template string (optional)
|
||||
// // Defines a template to extract the XY value.
|
||||
// XyValueTemplate string `json:"xy_value_template,omitempty"`
|
||||
//
|
||||
// // WhiteValueCommandTopic string `json:"white_value_command_topic,omitempty"`
|
||||
// // WhiteValueScale int `json:"white_value_scale,omitempty"`
|
||||
// // WhiteValueStateTopic string `json:"white_value_state_topic,omitempty"`
|
||||
// // WhiteValueTemplate string `json:"white_value_template,omitempty"`
|
||||
// // ValueTemplate string `json:"value_template,omitempty" required:"false"`
|
||||
//
|
||||
// // BrightnessStateFunc func() string `json:"-"`
|
||||
// // ColorTempStateFunc func() string `json:"-"`
|
||||
// // EffectStateFunc func() string `json:"-"`
|
||||
// // HsStateFunc func() string `json:"-"`
|
||||
// // RgbStateFunc func() string `json:"-"`
|
||||
// // StateFunc func() string `json:"-"`
|
||||
// // WhiteValueStateFunc func() string `json:"-"`
|
||||
// // XyStateFunc func() string `json:"-"`
|
||||
// //
|
||||
// // BrightnessCommandFunc func(mqtt.Message, mqtt.Client) `json:"-"`
|
||||
// // ColorTempCommandFunc func(mqtt.Message, mqtt.Client) `json:"-"`
|
||||
// // CommandFunc func(mqtt.Message, mqtt.Client) `json:"-"`
|
||||
// // EffectCommandFunc func(mqtt.Message, mqtt.Client) `json:"-"`
|
||||
// // HsCommandFunc func(mqtt.Message, mqtt.Client) `json:"-"`
|
||||
// // RgbCommandFunc func(mqtt.Message, mqtt.Client) `json:"-"`
|
||||
// // WhiteValueCommandFunc func(mqtt.Message, mqtt.Client) `json:"-"`
|
||||
// // XyCommandFunc func(mqtt.Message, mqtt.Client) `json:"-"`
|
||||
// //
|
||||
// // UpdateInterval float64 `json:"-"`
|
||||
// // ForceUpdateMQTT bool `json:"-"`
|
||||
// //
|
||||
// // messageHandler mqtt.MessageHandler
|
||||
// }
|
||||
|
||||
func (c *Light) Json() string {
|
||||
j, _ := json.Marshal(*c)
|
||||
return string(j)
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsLight() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.Units == LabelLight {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
||||
|
||||
|
||||
// {
|
||||
// "brightness": true,
|
||||
|
198
mmHa/lock.go
Normal file
198
mmHa/lock.go
Normal file
@ -0,0 +1,198 @@
|
||||
package mmHa
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/MickMake/GoUnify/Only"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const LabelLock = "lock"
|
||||
|
||||
|
||||
func (m *Mqtt) LockPublishConfig(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
config.FixConfig()
|
||||
if !config.IsLock() {
|
||||
break
|
||||
}
|
||||
|
||||
ok, newDevice := m.NewDevice(config)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
|
||||
payload := Lock {
|
||||
Device: newDevice,
|
||||
Name: String(JoinStrings(m.DeviceName, config.Name)),
|
||||
// StateTopic: JoinStringsForTopic(m.switchPrefix, id, "state"),
|
||||
CommandTopic: String(JoinStringsForTopic(m.Prefix, LabelLock, m.ClientId, id, "cmd")),
|
||||
ObjectId: String(id),
|
||||
UniqueId: String(id),
|
||||
Qos: 0,
|
||||
Retain: true,
|
||||
|
||||
// PayloadOn: "true",
|
||||
// PayloadOff: "false",
|
||||
// StateOn: "true",
|
||||
// StateOff: "false",
|
||||
// ValueTemplate: config.ValueTemplate,
|
||||
Icon: Icon(config.Icon),
|
||||
}
|
||||
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelLock, m.ClientId, id, "config")
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
func (m *Mqtt) LockPublishValue(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
if !config.IsLock() {
|
||||
break
|
||||
}
|
||||
|
||||
if config.IgnoreUpdate {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelLock, m.ClientId, id, "state")
|
||||
|
||||
value := config.Value.String()
|
||||
if value == "--" {
|
||||
value = ""
|
||||
}
|
||||
|
||||
// @TODO - Real hack here. Need to properly check for JSON.
|
||||
if strings.Contains(config.Value.String(), `{`) || strings.Contains(config.Value.String(), `":`) {
|
||||
t := m.client.Publish(tag, 0, true, config.Value.String())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
payload := MqttState{
|
||||
LastReset: config.LastReset, // m.GetLastReset(config.FullId),
|
||||
Value: value,
|
||||
}
|
||||
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
type Lock struct {
|
||||
// A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
|
||||
Availability *Availability `json:"availability,omitempty"`
|
||||
|
||||
// When availability is configured, this controls the conditions needed to set the entity to available. Valid entries are all, any, and latest. If set to all, payload_available must be received on all configured availability topics before the entity is marked as online. If set to any, payload_available must be received on at least one configured availability topic before the entity is marked as online. If set to latest, the last payload_available or payload_not_available received on any configured availability topic controls the availability.
|
||||
AvailabilityMode String `json:"availability_mode,omitempty" default:"latest"`
|
||||
|
||||
// Defines a template to extract device’s availability from the availability_topic. To determine the devices’s availability result of this template will be compared to payload_available and payload_not_available.
|
||||
AvailabilityTemplate Template `json:"availability_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive availability (online/offline) updates. Must not be used together with availability.
|
||||
AvailabilityTopic String `json:"availability_topic,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the lock state.
|
||||
CommandTopic String `json:"command_topic,omitempty" required:"true"`
|
||||
|
||||
// Information about the device this lock is a part of to tie it into the device registry. Only works through MQTT discovery and when unique_id is set. At least one of identifiers or connections must be present to identify the device.
|
||||
Device Device `json:"device,omitempty"`
|
||||
|
||||
// Flag which defines if the entity should be enabled when first added.
|
||||
EnabledByDefault Boolean `json:"enabled_by_default,omitempty" default:"true"`
|
||||
|
||||
// The encoding of the payloads received and published messages. Set to "" to disable decoding of incoming payload.
|
||||
Encoding String `json:"encoding,omitempty" default:"utf"`
|
||||
|
||||
// The category of the entity.
|
||||
EntityCategory String `json:"entity_category,omitempty" default:"None"`
|
||||
|
||||
// Icon for the entity.
|
||||
Icon Icon `json:"icon,omitempty"`
|
||||
|
||||
// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTemplate Template `json:"json_attributes_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive a JSON dictionary payload and then set as sensor attributes. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTopic String `json:"json_attributes_topic,omitempty"`
|
||||
|
||||
// The name of the lock.
|
||||
Name String `json:"name,omitempty" default:"MQTT"`
|
||||
|
||||
// Used instead of name for automatic generation of entity_id
|
||||
ObjectId String `json:"object_id,omitempty"`
|
||||
|
||||
// Flag that defines if lock works in optimistic mode.
|
||||
Optimistic Boolean `json:"optimistic,omitempty"`
|
||||
|
||||
// Default: true if no state_topic defined, else false.
|
||||
// The payload that represents the available state.
|
||||
PayloadAvailable String `json:"payload_available,omitempty" default:"online"`
|
||||
|
||||
// The payload sent to the lock to lock it.
|
||||
PayloadLock String `json:"payload_lock,omitempty" default:"LOCK"`
|
||||
|
||||
// The payload that represents the unavailable state.
|
||||
PayloadNotAvailable String `json:"payload_not_available,omitempty" default:"offline"`
|
||||
|
||||
// The payload sent to the lock to unlock it.
|
||||
PayloadUnlock String `json:"payload_unlock,omitempty" default:"UNLOCK"`
|
||||
|
||||
// The payload sent to the lock to open it.
|
||||
PayloadOpen String `json:"payload_open,omitempty" default:"OPEN"`
|
||||
|
||||
// The maximum QoS level of the state topic.
|
||||
Qos Integer `json:"qos,omitempty" default:"0"`
|
||||
|
||||
// If the published message should have the retain flag on or not.
|
||||
Retain Boolean `json:"retain,omitempty" default:"false"`
|
||||
|
||||
// The payload sent to by the lock when it’s locked.
|
||||
StateLocked String `json:"state_locked,omitempty" default:"LOCKED"`
|
||||
|
||||
// The MQTT topic subscribed to receive state updates.
|
||||
StateTopic String `json:"state_topic,omitempty"`
|
||||
|
||||
// The payload sent to by the lock when it’s unlocked.
|
||||
StateUnlocked String `json:"state_unlocked,omitempty" default:"UNLOCKED"`
|
||||
|
||||
// An ID that uniquely identifies this lock. If two locks have the same unique ID, Home Assistant will raise an exception.
|
||||
UniqueId String `json:"unique_id,omitempty"`
|
||||
|
||||
// Defines a template to extract a value from the payload.
|
||||
ValueTemplate String `json:"value_template,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
func (c *Lock) Json() string {
|
||||
j, _ := json.Marshal(*c)
|
||||
return string(j)
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsLock() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.Units == LabelLock {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
198
mmHa/number.go
Normal file
198
mmHa/number.go
Normal file
@ -0,0 +1,198 @@
|
||||
package mmHa
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/MickMake/GoUnify/Only"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const LabelNumber = "number"
|
||||
|
||||
|
||||
func (m *Mqtt) NumberPublishConfig(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
config.FixConfig()
|
||||
if !config.IsNumber() {
|
||||
break
|
||||
}
|
||||
|
||||
ok, newDevice := m.NewDevice(config)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
|
||||
payload := Number {
|
||||
Device: newDevice,
|
||||
Name: String(JoinStrings(m.DeviceName, config.Name)),
|
||||
// StateTopic: JoinStringsForTopic(m.switchPrefix, id, "state"),
|
||||
CommandTopic: String(JoinStringsForTopic(m.Prefix, LabelNumber, m.ClientId, id, "cmd")),
|
||||
ObjectId: String(id),
|
||||
UniqueId: String(id),
|
||||
Qos: 0,
|
||||
Retain: true,
|
||||
|
||||
// PayloadOn: "true",
|
||||
// PayloadOff: "false",
|
||||
// StateOn: "true",
|
||||
// StateOff: "false",
|
||||
// ValueTemplate: config.ValueTemplate,
|
||||
Icon: Icon(config.Icon),
|
||||
}
|
||||
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelNumber, m.ClientId, id, "config")
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
func (m *Mqtt) NumberPublishValue(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
if !config.IsNumber() {
|
||||
break
|
||||
}
|
||||
|
||||
if config.IgnoreUpdate {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelNumber, m.ClientId, id, "state")
|
||||
|
||||
value := config.Value.String()
|
||||
if value == "--" {
|
||||
value = ""
|
||||
}
|
||||
|
||||
// @TODO - Real hack here. Need to properly check for JSON.
|
||||
if strings.Contains(config.Value.String(), `{`) || strings.Contains(config.Value.String(), `":`) {
|
||||
t := m.client.Publish(tag, 0, true, config.Value.String())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
payload := MqttState{
|
||||
LastReset: config.LastReset, // m.GetLastReset(config.FullId),
|
||||
Value: value,
|
||||
}
|
||||
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
type Number struct {
|
||||
// A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
|
||||
Availability *Availability `json:"availability,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive availability (online/offline) updates. Must not be used together with availability.
|
||||
AvailabilityTopic String `json:"availability_topic,omitempty"`
|
||||
|
||||
// When availability is configured, this controls the conditions needed to set the entity to available. Valid entries are all, any, and latest. If set to all, payload_available must be received on all configured availability topics before the entity is marked as online. If set to any, payload_available must be received on at least one configured availability topic before the entity is marked as online. If set to latest, the last payload_available or payload_not_available received on any configured availability topic controls the availability.
|
||||
AvailabilityMode String `json:"availability_mode,omitempty" default:"latest"`
|
||||
|
||||
// Defines a template to generate the payload to send to command_topic.
|
||||
CommandTemplate Template `json:"command_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the number.
|
||||
CommandTopic String `json:"command_topic,omitempty" required:"true"`
|
||||
|
||||
// Information about the device this Number is a part of to tie it into the device registry. Only works through MQTT discovery and when unique_id is set. At least one of identifiers or connections must be present to identify the device.
|
||||
Device Device `json:"device,omitempty"`
|
||||
|
||||
// The type/class of the number.
|
||||
DeviceClass DeviceClass `json:"device_class,omitempty" default:"None"`
|
||||
|
||||
// Flag which defines if the entity should be enabled when first added.
|
||||
EnabledByDefault Boolean `json:"enabled_by_default,omitempty" default:"true"`
|
||||
|
||||
// The encoding of the payloads received and published messages. Set to "" to disable decoding of incoming payload.
|
||||
Encoding String `json:"encoding,omitempty" default:"utf"`
|
||||
|
||||
// The category of the entity.
|
||||
EntityCategory String `json:"entity_category,omitempty" default:"None"`
|
||||
|
||||
// Icon for the entity.
|
||||
Icon Icon `json:"icon,omitempty"`
|
||||
|
||||
// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic.
|
||||
JsonAttributesTemplate Template `json:"json_attributes_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive a JSON dictionary payload and then set as number attributes. Implies force_update of the current number state when a message is received on this topic.
|
||||
JsonAttributesTopic String `json:"json_attributes_topic,omitempty"`
|
||||
|
||||
// Minimum value.
|
||||
Min Float `json:"min,omitempty" default:"1"`
|
||||
|
||||
// Maximum value.
|
||||
Max Float `json:"max,omitempty" default:"100"`
|
||||
|
||||
// Control how the number should be displayed in the UI. Can be set to box or slider to force a display mode.
|
||||
Mode String `json:"mode,omitempty"(optional, default: “auto”)`
|
||||
|
||||
// The name of the Number.
|
||||
Name String `json:"name,omitempty"`
|
||||
|
||||
// Used instead of name for automatic generation of entity_id
|
||||
ObjectId String `json:"object_id,omitempty"`
|
||||
|
||||
// Flag that defines if number works in optimistic mode.
|
||||
Optimistic Boolean `json:"optimistic,omitempty"`
|
||||
|
||||
// Default: true if no state_topic defined, else false.
|
||||
// A special payload that resets the state to None when received on the state_topic.
|
||||
PayloadReset String `json:"payload_reset,omitempty"(optional, default: “None”)`
|
||||
|
||||
// The maximum QoS level of the state topic. Default is 0 and will also be used to publishing messages.
|
||||
Qos Integer `json:"qos,omitempty" default:"0"`
|
||||
|
||||
// If the published message should have the retain flag on or not.
|
||||
Retain Boolean `json:"retain,omitempty" default:"false"`
|
||||
|
||||
// The MQTT topic subscribed to receive number values.
|
||||
StateTopic String `json:"state_topic,omitempty"`
|
||||
|
||||
// Step value. Smallest value 0.001.
|
||||
Step Float `json:"step,omitempty" default:"1"`
|
||||
|
||||
// An ID that uniquely identifies this Number. If two Numbers have the same unique ID Home Assistant will raise an exception.
|
||||
UniqueId String `json:"unique_id,omitempty"`
|
||||
|
||||
// Defines the unit of measurement of the sensor, if any.
|
||||
UnitOfMeasurement String `json:"unit_of_measurement,omitempty"`
|
||||
|
||||
// Defines a template to extract the value.
|
||||
ValueTemplate Template `json:"value_template,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
func (c *Number) Json() string {
|
||||
j, _ := json.Marshal(*c)
|
||||
return string(j)
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsNumber() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.Units == LabelNumber {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
164
mmHa/scene.go
Normal file
164
mmHa/scene.go
Normal file
@ -0,0 +1,164 @@
|
||||
package mmHa
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/MickMake/GoUnify/Only"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const LabelScene = "scene"
|
||||
|
||||
|
||||
func (m *Mqtt) ScenePublishConfig(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
config.FixConfig()
|
||||
if !config.IsScene() {
|
||||
break
|
||||
}
|
||||
|
||||
// ok, newDevice := m.NewDevice(config)
|
||||
// if !ok {
|
||||
// break
|
||||
// }
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
|
||||
payload := Scene {
|
||||
// Device: newDevice,
|
||||
Name: String(JoinStrings(m.DeviceName, config.Name)),
|
||||
// StateTopic: JoinStringsForTopic(m.switchPrefix, id, "state"),
|
||||
CommandTopic: String(JoinStringsForTopic(m.Prefix, LabelScene, m.ClientId, id, "cmd")),
|
||||
ObjectId: String(id),
|
||||
UniqueId: String(id),
|
||||
Qos: 0,
|
||||
Retain: true,
|
||||
|
||||
// PayloadOn: "true",
|
||||
// PayloadOff: "false",
|
||||
// StateOn: "true",
|
||||
// StateOff: "false",
|
||||
// ValueTemplate: config.ValueTemplate,
|
||||
Icon: Icon(config.Icon),
|
||||
}
|
||||
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelScene, m.ClientId, id, "config")
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
func (m *Mqtt) ScenePublishValue(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
if !config.IsScene() {
|
||||
break
|
||||
}
|
||||
|
||||
if config.IgnoreUpdate {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelScene, m.ClientId, id, "state")
|
||||
|
||||
value := config.Value.String()
|
||||
if value == "--" {
|
||||
value = ""
|
||||
}
|
||||
|
||||
// @TODO - Real hack here. Need to properly check for JSON.
|
||||
if strings.Contains(config.Value.String(), `{`) || strings.Contains(config.Value.String(), `":`) {
|
||||
t := m.client.Publish(tag, 0, true, config.Value.String())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
payload := MqttState{
|
||||
LastReset: config.LastReset, // m.GetLastReset(config.FullId),
|
||||
Value: value,
|
||||
}
|
||||
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
type Scene struct {
|
||||
// A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
|
||||
Availability *Availability `json:"availability,omitempty"`
|
||||
|
||||
// When availability is configured, this controls the conditions needed to set the entity to available. Valid entries are all, any, and latest. If set to all, payload_available must be received on all configured availability topics before the entity is marked as online. If set to any, payload_available must be received on at least one configured availability topic before the entity is marked as online. If set to latest, the last payload_available or payload_not_available received on any configured availability topic controls the availability.
|
||||
AvailabilityMode String `json:"availability_mode,omitempty" default:"latest"`
|
||||
|
||||
// Defines a template to extract device’s availability from the availability_topic. To determine the devices’s availability result of this template will be compared to payload_available and payload_not_available.
|
||||
AvailabilityTemplate Template `json:"availability_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive availability (online/offline) updates. Must not be used together with availability.
|
||||
AvailabilityTopic String `json:"availability_topic,omitempty"`
|
||||
|
||||
// The MQTT topic to publish payload_on to activate the scene.
|
||||
CommandTopic String `json:"command_topic,omitempty"`
|
||||
|
||||
// Flag which defines if the entity should be enabled when first added.
|
||||
EnabledByDefault Boolean `json:"enabled_by_default,omitempty" default:"true"`
|
||||
|
||||
// The category of the entity.
|
||||
EntityCategory String `json:"entity_category,omitempty" default:"None"`
|
||||
|
||||
// Icon for the scene.
|
||||
Icon Icon `json:"icon,omitempty"`
|
||||
|
||||
// The name to use when displaying this scene.
|
||||
Name String `json:"name,omitempty" default:"MQTT"`
|
||||
|
||||
// Used instead of name for automatic generation of entity_id
|
||||
ObjectId String `json:"object_id,omitempty"`
|
||||
|
||||
// The payload that represents the available state.
|
||||
PayloadAvailable String `json:"payload_available,omitempty" default:"online"`
|
||||
|
||||
// The payload that represents the unavailable state.
|
||||
PayloadNotAvailable String `json:"payload_not_available,omitempty" default:"offline"`
|
||||
|
||||
// The payload that will be sent to command_topic when activating the MQTT scene.
|
||||
PayloadOn String `json:"payload_on,omitempty" default:"ON"`
|
||||
|
||||
// The maximum QoS level of the state topic. Default is 0 and will also be used to publishing messages.
|
||||
Qos Integer `json:"qos,omitempty" default:"0"`
|
||||
|
||||
// If the published message should have the retain flag on or not.
|
||||
Retain Boolean `json:"retain,omitempty" default:"false"`
|
||||
|
||||
// An ID that uniquely identifies this scene entity. If two scenes have the same unique ID, Home Assistant will raise an exception.
|
||||
UniqueId String `json:"unique_id,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
func (c *Scene) Json() string {
|
||||
j, _ := json.Marshal(*c)
|
||||
return string(j)
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsScene() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.Units == LabelScene {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
358
mmHa/select.go
Normal file
358
mmHa/select.go
Normal file
@ -0,0 +1,358 @@
|
||||
package mmHa
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/MickMake/GoUnify/Only"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const LabelSelect = "select"
|
||||
|
||||
|
||||
func (m *Mqtt) SelectPublishConfig(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
config.FixConfig()
|
||||
if !config.IsSelect() {
|
||||
break
|
||||
}
|
||||
|
||||
ok, newDevice := m.NewDevice(config)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
|
||||
// {
|
||||
// "device": {
|
||||
// "connections": [
|
||||
// [
|
||||
// "GoSungrow",
|
||||
// "GoSungrow-Control"
|
||||
// ]
|
||||
// ],
|
||||
// "identifiers": [
|
||||
// "GoSungrow-Control"
|
||||
// ],
|
||||
// "manufacturer": "MickMake",
|
||||
// "model": "GoSungrow",
|
||||
// "name": "GoSungrow Control",
|
||||
// "suggested_area": "Roof",
|
||||
// "sw_version": "GoSungrow https://github.com/MickMake/GoSungrow",
|
||||
// "via_device": "GoSungrow"
|
||||
// },
|
||||
// "object_id": "GoSungrow-select-endpoints",
|
||||
// "unique_id": "GoSungrow-select-endpoints",
|
||||
// "name": "GoSungrow Select EndPoints",
|
||||
// "command_topic": "homeassistant/select/GoSungrow/endpoint-select/set",
|
||||
// "state_topic": "homeassistant/select/GoSungrow/endpoint-select/state",
|
||||
// "command_template": "{\"select\": \"{{ value }}\" }",
|
||||
// "value_template": "{{ value_json.select }}",
|
||||
// "enabled_by_default": true,
|
||||
// "retain": true,
|
||||
// "encoding": "utf-8",
|
||||
// "icon": "mdi:format-list-group",
|
||||
// "options": [
|
||||
// "queryDeviceList",
|
||||
// "getPsList",
|
||||
// "getPsDetail"
|
||||
// ]
|
||||
// }
|
||||
|
||||
payload := Select {
|
||||
Device: newDevice,
|
||||
Name: String(JoinStrings(m.DeviceName, config.Name)),
|
||||
StateTopic: String(JoinStringsForTopic(m.Prefix, LabelSelect, m.ClientId, id, "state")),
|
||||
CommandTopic: String(JoinStringsForTopic(m.Prefix, LabelSelect, m.ClientId, id, "cmd")),
|
||||
ObjectId: String(id),
|
||||
UniqueId: String(id),
|
||||
Qos: 0,
|
||||
Retain: true,
|
||||
|
||||
ValueTemplate: Template(config.ValueTemplate),
|
||||
Icon: Icon(config.Icon),
|
||||
}
|
||||
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelSelect, m.ClientId, id, "config")
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
func (m *Mqtt) SelectPublishValue(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
if !config.IsSelect() {
|
||||
break
|
||||
}
|
||||
|
||||
if config.IgnoreUpdate {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelSelect, m.ClientId, id, "state")
|
||||
|
||||
value := config.Value.String()
|
||||
if value == "--" {
|
||||
value = ""
|
||||
}
|
||||
|
||||
// @TODO - Real hack here. Need to properly check for JSON.
|
||||
if strings.Contains(config.Value.String(), `{`) || strings.Contains(config.Value.String(), `":`) {
|
||||
t := m.client.Publish(tag, 0, true, config.Value.String())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
payload := MqttState{
|
||||
LastReset: config.LastReset, // m.GetLastReset(config.FullId),
|
||||
Value: value,
|
||||
}
|
||||
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
type Select struct {
|
||||
// A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
|
||||
Availability *Availability `json:"availability,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive availability (online/offline) updates. Must not be used together with availability.
|
||||
AvailabilityTopic String `json:"availability_topic,omitempty"`
|
||||
|
||||
// When availability is configured, this controls the conditions needed to set the entity to available. Valid entries are all, any, and latest. If set to all, payload_available must be received on all configured availability topics before the entity is marked as online. If set to any, payload_available must be received on at least one configured availability topic before the entity is marked as online. If set to latest, the last payload_available or payload_not_available received on any configured availability topic controls the availability.
|
||||
AvailabilityMode String `json:"availability_mode,omitempty" default:"latest"`
|
||||
|
||||
// Defines a template to extract device’s availability from the availability_topic. To determine the devices’s availability result of this template will be compared to payload_available and payload_not_available.
|
||||
AvailabilityTemplate Template `json:"availability_template,omitempty"`
|
||||
|
||||
// Defines a template to generate the payload to send to command_topic.
|
||||
CommandTemplate Template `json:"command_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the selected option.
|
||||
CommandTopic String `json:"command_topic,omitempty" required:"true"`
|
||||
|
||||
// Information about the device this Select is a part of to tie it into the device registry. Only works through MQTT discovery and when unique_id is set. At least one of identifiers or connections must be present to identify the device.
|
||||
Device Device `json:"device,omitempty"`
|
||||
|
||||
// Flag which defines if the entity should be enabled when first added.
|
||||
EnabledByDefault Boolean `json:"enabled_by_default,omitempty" default:"true"`
|
||||
|
||||
// The encoding of the payloads received and published messages. Set to "" to disable decoding of incoming payload.
|
||||
Encoding String `json:"encoding,omitempty" default:"utf"`
|
||||
|
||||
// The category of the entity.
|
||||
EntityCategory String `json:"entity_category,omitempty" default:"None"`
|
||||
|
||||
// Icon for the entity.
|
||||
Icon Icon `json:"icon,omitempty"`
|
||||
|
||||
// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic.
|
||||
JsonAttributesTemplate Template `json:"json_attributes_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive a JSON dictionary payload and then set as entity attributes. Implies force_update of the current select state when a message is received on this topic.
|
||||
JsonAttributesTopic String `json:"json_attributes_topic,omitempty"`
|
||||
|
||||
// The name of the Select.
|
||||
Name String `json:"name,omitempty"`
|
||||
|
||||
// Used instead of name for automatic generation of entity_id
|
||||
ObjectId String `json:"object_id,omitempty"`
|
||||
|
||||
// Flag that defines if the select works in optimistic mode.
|
||||
Optimistic Boolean `json:"optimistic,omitempty"`
|
||||
|
||||
// Default: true if no state_topic defined, else false.
|
||||
// List of options that can be selected. An empty list or a list with a single item is allowed.
|
||||
Options List `json:"options,omitempty" required:"true"`
|
||||
|
||||
// The maximum QoS level of the state topic. Default is 0 and will also be used to publishing messages.
|
||||
Qos Integer `json:"qos,omitempty" default:"0"`
|
||||
|
||||
// If the published message should have the retain flag on or not.
|
||||
Retain Boolean `json:"retain,omitempty" default:"false"`
|
||||
|
||||
// The MQTT topic subscribed to receive update of the selected option.
|
||||
StateTopic String `json:"state_topic,omitempty"`
|
||||
|
||||
// An ID that uniquely identifies this Select. If two Selects have the same unique ID Home Assistant will raise an exception.
|
||||
UniqueId String `json:"unique_id,omitempty"`
|
||||
|
||||
// Defines a template to extract the value.
|
||||
ValueTemplate Template `json:"value_template,omitempty"`
|
||||
}
|
||||
|
||||
// type Select struct {
|
||||
// // availability list (optional)
|
||||
// // A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
|
||||
// Availability *Availability `json:"availability,omitempty" required:"false"`
|
||||
//
|
||||
// // availability_topic string (optional)
|
||||
// // The MQTT topic subscribed to receive availability (online/offline) updates. Must not be used together with availability.
|
||||
// AvailabilityTopic string `json:"availability_topic,omitempty" required:"false"`
|
||||
//
|
||||
// // availability_mode string (optional, default: latest)
|
||||
// // When availability is configured, this controls the conditions needed to set the entity to available.
|
||||
// // Valid entries are all, any, and latest. If set to all, payload_available must be received on all configured availability
|
||||
// // topics before the entity is marked as online. If set to any, payload_available must be received on at least one configured
|
||||
// // availability topic before the entity is marked as online. If set to latest, the last payload_available or payload_not_available
|
||||
// // received on any configured availability topic controls the availability.
|
||||
// AvailabilityMode string `json:"availability_mode,omitempty" required:"false"`
|
||||
//
|
||||
// // availability_template template (optional)
|
||||
// // Defines a template to extract device’s availability from the availability_topic.
|
||||
// // To determine the devices’ availability result of this template will be compared to payload_available and payload_not_available.
|
||||
// AvailabilityTemplate string `json:"availability_template,omitempty" required:"false"`
|
||||
//
|
||||
// // command_template template (optional)
|
||||
// // Defines a template to generate the payload to send to command_topic.
|
||||
// CommandTemplate string `json:"command_template" required:"false"`
|
||||
//
|
||||
// // command_topic string REQUIRED
|
||||
// // The MQTT topic to publish commands to change the selected option.
|
||||
// CommandTopic string `json:"command_topic" required:"true"`
|
||||
//
|
||||
// // device map (optional)
|
||||
// // Information about the device this Select is a part of to tie it into the device registry.
|
||||
// // Only works through MQTT discovery and when unique_id is set.
|
||||
// // At least one of identifiers or connections must be present to identify the device.
|
||||
// Device Device `json:"device,omitempty" required:"false"`
|
||||
//
|
||||
// // enabled_by_default boolean (optional, default: true)
|
||||
// // Flag which defines if the entity should be enabled when first added.
|
||||
// EnabledByDefault bool `json:"enabled_by_default,omitempty" required:"false"`
|
||||
//
|
||||
// // encoding string (optional, default: utf-8)
|
||||
// // The encoding of the payloads received and published messages. Set to "" to disable decoding of incoming payload.
|
||||
// Encoding string `json:"encoding,omitempty" required:"false"`
|
||||
//
|
||||
// // entity_category string (optional, default: None)
|
||||
// // The category of the entity.
|
||||
// EntityCategory string `json:"entity_category,omitempty" required:"false"`
|
||||
//
|
||||
// // icon (optional)
|
||||
// // Icon for the entity.
|
||||
// Icon string `json:"icon,omitempty" required:"false"`
|
||||
//
|
||||
// // json_attributes_template template (optional)
|
||||
// // Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic.
|
||||
// JsonAttributesTemplate string `json:"json_attributes_template,omitempty" required:"false"`
|
||||
//
|
||||
// // json_attributes_topic string (optional)
|
||||
// // The MQTT topic subscribed to receive a JSON dictionary payload and then set as entity attributes.
|
||||
// // Implies force_update of the current select state when a message is received on this topic.
|
||||
// JsonAttributesTopic string `json:"json_attributes_topic,omitempty" required:"false"`
|
||||
//
|
||||
// // name string (optional)
|
||||
// // The name of the Select.
|
||||
// Name string `json:"name,omitempty" required:"false"`
|
||||
//
|
||||
// // object_id string (optional)
|
||||
// // Used instead of name for automatic generation of entity_id
|
||||
// ObjectId string `json:"object_id,omitempty" required:"false"`
|
||||
//
|
||||
// // optimistic boolean (optional)
|
||||
// // Flag that defines if the select works in optimistic mode.
|
||||
// // Default: true if no state_topic defined, else false.
|
||||
// Optimistic bool `json:"optimistic,omitempty" required:"false"`
|
||||
//
|
||||
// // options list REQUIRED
|
||||
// // List of options that can be selected. An empty list or a list with a single item is allowed.
|
||||
// Options string `json:"options,omitempty" required:"true"`
|
||||
//
|
||||
// // qos integer (optional, default: 0)
|
||||
// // The maximum QoS level of the state topic. Default is 0 and will also be used to publishing messages.
|
||||
// Qos int `json:"qos,omitempty" required:"false"`
|
||||
//
|
||||
// // retain boolean (optional, default: false)
|
||||
// // If the published message should have the retain flag on or not.
|
||||
// Retain bool `json:"ret,omitempty" required:"false"`
|
||||
//
|
||||
// // state_topic string (optional)
|
||||
// // The MQTT topic subscribed to receive update of the selected option.
|
||||
// StateTopic string `json:"state_topic,omitempty" required:"false"`
|
||||
//
|
||||
// // unique_id string (optional)
|
||||
// // An ID that uniquely identifies this Select. If two Selects have the same unique ID Home Assistant will raise an exception.
|
||||
// UniqueId string `json:"unique_id,omitempty" required:"false"`
|
||||
//
|
||||
// // value_template template (optional)
|
||||
// // Defines a template to extract the value.
|
||||
// ValueTemplate string `json:"value_template,omitempty" required:"false"`
|
||||
//
|
||||
// // CommandFunc func(mqtt.Message, mqtt.Client) `json:"-"`
|
||||
// // StateFunc func() string `json:"-"`
|
||||
// //
|
||||
// // UpdateInterval float64 `json:"-"`
|
||||
// // ForceUpdateMQTT bool `json:"-"`
|
||||
// //
|
||||
// // messageHandler mqtt.MessageHandler
|
||||
// }
|
||||
|
||||
func (c *Select) Json() string {
|
||||
j, _ := json.Marshal(*c)
|
||||
return string(j)
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsSelect() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.Units == LabelSelect {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
||||
|
||||
|
||||
// {
|
||||
// "device": {
|
||||
// "connections": [
|
||||
// [
|
||||
// "GoSungrow",
|
||||
// "GoSungrow-Control"
|
||||
// ]
|
||||
// ],
|
||||
// "identifiers": [
|
||||
// "GoSungrow-Control"
|
||||
// ],
|
||||
// "manufacturer": "MickMake",
|
||||
// "model": "GoSungrow",
|
||||
// "name": "GoSungrow Control",
|
||||
// "suggested_area": "Roof",
|
||||
// "sw_version": "GoSungrow https://github.com/MickMake/GoSungrow",
|
||||
// "via_device": "GoSungrow"
|
||||
// },
|
||||
// "object_id": "GoSungrow-select-endpoints",
|
||||
// "unique_id": "GoSungrow-select-endpoints",
|
||||
// "name": "GoSungrow Select EndPoints",
|
||||
// "command_topic": "homeassistant/select/GoSungrow/endpoint-select/set",
|
||||
// "state_topic": "homeassistant/select/GoSungrow/endpoint-select/state",
|
||||
// "command_template": "{\"select\": \"{{ value }}\" }",
|
||||
// "value_template": "{{ value_json.select }}",
|
||||
// "enabled_by_default": true,
|
||||
// "retain": true,
|
||||
// "encoding": "utf-8",
|
||||
// "icon": "mdi:format-list-group",
|
||||
// "options": [
|
||||
// "queryDeviceList",
|
||||
// "getPsList",
|
||||
// "getPsDetail"
|
||||
// ]
|
||||
// }
|
308
mmHa/sensors.go
308
mmHa/sensors.go
@ -8,6 +8,8 @@ import (
|
||||
)
|
||||
|
||||
|
||||
const LabelSensor = "sensor"
|
||||
|
||||
func (m *Mqtt) SensorPublishConfig(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
@ -22,45 +24,28 @@ func (m *Mqtt) SensorPublishConfig(config EntityConfig) error {
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
// id := JoinStringsForId(m.Device.Name, config.ParentName, config.Name, config.UniqueId),
|
||||
|
||||
payload := Sensor {
|
||||
Device: newDevice,
|
||||
Name: JoinStrings(m.DeviceName, config.Name),
|
||||
StateTopic: JoinStringsForTopic(m.sensorPrefix, id, "state"),
|
||||
StateClass: config.StateClass,
|
||||
ObjectId: id,
|
||||
UniqueId: id,
|
||||
UnitOfMeasurement: config.Units,
|
||||
DeviceClass: config.DeviceClass,
|
||||
Name: String(JoinStrings(m.DeviceName, config.Name)),
|
||||
StateTopic: String(JoinStringsForTopic(m.Prefix, LabelSensor, m.ClientId, id, "state")),
|
||||
StateClass: String(config.StateClass),
|
||||
ObjectId: String(id),
|
||||
UniqueId: String(id),
|
||||
UnitOfMeasurement: String(config.Units),
|
||||
DeviceClass: DeviceClass(config.DeviceClass),
|
||||
Qos: 0,
|
||||
ForceUpdate: true,
|
||||
ExpireAfter: 0,
|
||||
Encoding: "utf-8",
|
||||
EnabledByDefault: true,
|
||||
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: "",
|
||||
LastResetValueTemplate: String(config.LastResetValueTemplate),
|
||||
// LastReset: config.LastReset,
|
||||
ValueTemplate: Template(config.ValueTemplate),
|
||||
Icon: Icon(config.Icon),
|
||||
}
|
||||
|
||||
tag := JoinStringsForTopic(m.sensorPrefix, id, "config")
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelSensor, m.ClientId, id, "config")
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
@ -73,17 +58,25 @@ func (m *Mqtt) SensorPublishConfig(config EntityConfig) error {
|
||||
func (m *Mqtt) SensorPublishValue(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
// if config.Units == LabelBinarySensor {
|
||||
if !config.IsSensor() {
|
||||
break
|
||||
}
|
||||
|
||||
if config.IgnoreUpdate {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
tag := JoinStringsForTopic(m.sensorPrefix, id, "state")
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelSensor, m.ClientId, id, "state")
|
||||
|
||||
value := config.Value.String()
|
||||
if value == "--" {
|
||||
value = ""
|
||||
}
|
||||
|
||||
// @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 strings.Contains(config.Value.String(), `{`) || strings.Contains(config.Value.String(), `":`) {
|
||||
t := m.client.Publish(tag, 0, true, config.Value.String())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
@ -92,13 +85,14 @@ func (m *Mqtt) SensorPublishValue(config EntityConfig) error {
|
||||
|
||||
payload := MqttState {
|
||||
LastReset: config.LastReset, // m.GetLastReset(config.FullId),
|
||||
Value: config.Value,
|
||||
Value: value,
|
||||
}
|
||||
if config.StateClass != "total" {
|
||||
payload = MqttState {
|
||||
Value: config.Value,
|
||||
Value: value,
|
||||
}
|
||||
}
|
||||
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
@ -110,43 +104,223 @@ func (m *Mqtt) SensorPublishValue(config EntityConfig) error {
|
||||
|
||||
|
||||
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"`
|
||||
// A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
|
||||
Availability *Availability `json:"availability,omitempty"`
|
||||
|
||||
// StateFunc func() string `json:"-"`
|
||||
//
|
||||
// UpdateInterval float64 `json:"-"`
|
||||
// ForceUpdateMQTT bool `json:"-"`
|
||||
// When availability is configured, this controls the conditions needed to set the entity to available. Valid entries are all, any, and latest. If set to all, payload_available must be received on all configured availability topics before the entity is marked as online. If set to any, payload_available must be received on at least one configured availability topic before the entity is marked as online. If set to latest, the last payload_available or payload_not_available received on any configured availability topic controls the availability.
|
||||
AvailabilityMode String `json:"availability_mode,omitempty" Default:"latest"`
|
||||
|
||||
// Defines a template to extract device’s availability from the availability_topic. To determine the devices’ availability result of this template will be compared to payload_available and payload_not_available.
|
||||
AvailabilityTemplate Template `json:"availability_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive availability (online/offline) updates.
|
||||
AvailabilityTopic String `json:"availability_topic,omitempty"`
|
||||
|
||||
// Information about the device this sensor is a part of to tie it into the device registry. Only works through MQTT discovery and when unique_id is set. At least one of identifiers or connections must be present to identify the device.
|
||||
Device Device `json:"device,omitempty"`
|
||||
|
||||
// The type/class of the sensor to set the icon in the frontend.
|
||||
DeviceClass DeviceClass `json:"device_class,omitempty" Default:"None"`
|
||||
|
||||
// Flag which defines if the entity should be enabled when first added.
|
||||
EnabledByDefault Boolean `json:"enabled_by_default,omitempty" Default:"true"`
|
||||
|
||||
// The encoding of the payloads received. Set to "" to disable decoding of incoming payload.
|
||||
Encoding String `json:"encoding,omitempty" Default:"utf"`
|
||||
|
||||
// The category of the entity.
|
||||
EntityCategory String `json:"entity_category,omitempty" Default:"None"`
|
||||
|
||||
// If set, it defines the number of seconds after the sensor’s state expires, if it’s not updated. After expiry, the sensor’s state becomes unavailable. Default the sensors state never expires.
|
||||
ExpireAfter Integer `json:"expire_after,omitempty" Default:"0"`
|
||||
|
||||
// Sends update events even if the value hasn’t changed. Useful if you want to have meaningful value graphs in history.
|
||||
ForceUpdate Boolean `json:"force_update,omitempty" Default:"false"`
|
||||
|
||||
// Icon for the entity.
|
||||
Icon Icon `json:"icon,omitempty"`
|
||||
|
||||
// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic.
|
||||
JsonAttributesTemplate Template `json:"json_attributes_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive a JSON dictionary payload and then set as sensor attributes. Implies force_update of the current sensor state when a message is received on this topic.
|
||||
JsonAttributesTopic String `json:"json_attributes_topic,omitempty"`
|
||||
|
||||
// Defines a template to extract the last_reset. Available variables: entity_id. The entity_id can be used to reference the entity’s attributes.
|
||||
LastResetValueTemplate String `json:"last_reset_value_template,omitempty"`
|
||||
|
||||
// The name of the MQTT sensor.
|
||||
Name String `json:"name,omitempty" Default:"MQTT"`
|
||||
|
||||
// Used instead of name for automatic generation of entity_id
|
||||
ObjectId String `json:"object_id,omitempty"`
|
||||
|
||||
// The payload that represents the available state.
|
||||
PayloadAvailable String `json:"payload_available,omitempty" Default:"online"`
|
||||
|
||||
// The payload that represents the unavailable state.
|
||||
PayloadNotAvailable String `json:"payload_not_available,omitempty" Default:"offline"`
|
||||
|
||||
// The maximum QoS level of the state topic.
|
||||
Qos Integer `json:"qos,omitempty" Default:"0"`
|
||||
|
||||
// The state_class of the sensor.
|
||||
StateClass String `json:"state_class,omitempty" Default:"None"`
|
||||
|
||||
// The MQTT topic subscribed to receive sensor values.
|
||||
StateTopic String `json:"state_topic,omitempty" required:"true"`
|
||||
|
||||
// An ID that uniquely identifies this sensor. If two sensors have the same unique ID, Home Assistant will raise an exception.
|
||||
UniqueId String `json:"unique_id,omitempty"`
|
||||
|
||||
// Defines the units of measurement of the sensor, if any.
|
||||
UnitOfMeasurement String `json:"unit_of_measurement,omitempty"`
|
||||
|
||||
// Defines a template to extract the value. If the template throws an error, the current state will be used instead.
|
||||
ValueTemplate Template `json:"value_template,omitempty"`
|
||||
}
|
||||
|
||||
// type Sensor struct {
|
||||
// // availability list (optional)
|
||||
// // A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
|
||||
// Availability *Availability `json:"availability,omitempty" required:"false"`
|
||||
//
|
||||
// // availability_template template (optional)
|
||||
// // Defines a template to extract device’s availability from the availability_topic. To determine the devices’s availability result of this template will be compared to payload_available and payload_not_available.
|
||||
// AvailabilityTemplate string `json:"availability_template,omitempty" required:"false"`
|
||||
//
|
||||
// // availability_topic string (optional)
|
||||
// // The MQTT topic subscribed to receive availability (online/offline) updates.
|
||||
// AvailabilityTopic string `json:"availability_topic,omitempty" required:"false"`
|
||||
//
|
||||
// // availability_mode string (optional, default: latest)
|
||||
// // When availability is configured, this controls the conditions needed to set the entity to available. Valid entries are all, any, and latest. If set to all, payload_available must be received on all configured availability topics before the entity is marked as online. If set to any, payload_available must be received on at least one configured availability topic before the entity is marked as online. If set to latest, the last payload_available or payload_not_available received on any configured availability topic controls the availability.
|
||||
// AvailabilityMode string `json:"availability_mode,omitempty" required:"false"`
|
||||
//
|
||||
// // device map (optional)
|
||||
// // Information about the device this sensor is a part of to tie it into the device registry. Only works through MQTT discovery and when unique_id is set. At least one of identifiers or connections must be present to identify the device.
|
||||
// Device Device `json:"device,omitempty" required:"false"`
|
||||
//
|
||||
// // device_class device_class (optional, default: None)
|
||||
// // The type/class of the sensor to set the icon in the frontend.
|
||||
// DeviceClass string `json:"device_class,omitempty" required:"false"`
|
||||
//
|
||||
// // enabled_by_default boolean (optional, default: true)
|
||||
// // Flag which defines if the entity should be enabled when first added.
|
||||
// EnabledByDefault bool `json:"enabled_by_default,omitempty" required:"false"`
|
||||
//
|
||||
// // encoding string (optional, default: utf-8)
|
||||
// // The encoding of the payloads received. Set to "" to disable decoding of incoming payload.
|
||||
// Encoding string `json:"encoding,omitempty" required:"false"`
|
||||
//
|
||||
// // entity_category string (optional, default: None)
|
||||
// // The category of the entity.
|
||||
// EntityCategory string `json:"entity_category,omitempty" required:"false"`
|
||||
//
|
||||
// // expire_after integer (optional, default: 0)
|
||||
// // If set, it defines the number of seconds after the sensor’s state expires, if it’s not updated. After expiry, the sensor’s state becomes unavailable. Default the sensors state never expires.
|
||||
// ExpireAfter int `json:"expire_after,omitempty" required:"false"`
|
||||
//
|
||||
// // force_update boolean (optional, default: false)
|
||||
// // Sends update events even if the value hasn’t changed. Useful if you want to have meaningful value graphs in history.
|
||||
// ForceUpdate bool `json:"force_update,omitempty" required:"false"`
|
||||
//
|
||||
// // icon string (optional)
|
||||
// // Icon for the entity.
|
||||
// Icon string `json:"icon,omitempty" required:"false"`
|
||||
//
|
||||
// // json_attributes_template template (optional)
|
||||
// // Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic.
|
||||
// JsonAttributesTemplate string `json:"json_attributes_template,omitempty" required:"false"`
|
||||
//
|
||||
// // json_attributes_topic string (optional)
|
||||
// // The MQTT topic subscribed to receive a JSON dictionary payload and then set as sensor attributes. Implies force_update of the current sensor state when a message is received on this topic.
|
||||
// JsonAttributesTopic string `json:"json_attributes_topic,omitempty" required:"false"`
|
||||
//
|
||||
// // last_reset_value_template string (optional)
|
||||
// // Defines a template to extract the last_reset. Available variables: entity_id. The entity_id can be used to reference the entity’s attributes.
|
||||
// LastResetValueTemplate string `json:"last_reset_value_template,omitempty" required:"false"`
|
||||
//
|
||||
// // name string (optional, default: MQTT Sensor)
|
||||
// // The name of the MQTT sensor.
|
||||
// Name string `json:"name,omitempty" required:"false"`
|
||||
//
|
||||
// // object_id string (optional)
|
||||
// // Used instead of name for automatic generation of entity_id
|
||||
// ObjectId string `json:"object_id,omitempty" required:"false"`
|
||||
//
|
||||
// // payload_available string (optional, default: online)
|
||||
// // The payload that represents the available state.
|
||||
// PayloadAvailable string `json:"payload_available,omitempty" required:"false"`
|
||||
//
|
||||
// // payload_not_available string (optional, default: offline)
|
||||
// // The payload that represents the unavailable state.
|
||||
// PayloadNotAvailable string `json:"payload_not_available,omitempty" required:"false"`
|
||||
//
|
||||
// // qos integer (optional, default: 0)
|
||||
// // The maximum QoS level of the state topic.
|
||||
// Qos int `json:"qos,omitempty" required:"false"`
|
||||
//
|
||||
// // retain boolean (optional, default: false)
|
||||
// // If the published message should have the retain flag on or not.
|
||||
// Retain bool `json:"retain,omitempty" required:"false"`
|
||||
//
|
||||
// // state_class string (optional, default: None)
|
||||
// // The state_class of the sensor.
|
||||
// StateClass string `json:"state_class,omitempty" required:"false"`
|
||||
//
|
||||
// // state_topic string REQUIRED
|
||||
// // The MQTT topic subscribed to receive sensor values.
|
||||
// StateTopic string `json:"state_topic,omitempty" required:"false"`
|
||||
//
|
||||
// // unique_id string (optional)
|
||||
// // An ID that uniquely identifies this sensor. If two sensors have the same unique ID, Home Assistant will raise an exception.
|
||||
// UniqueId string `json:"unique_id,omitempty" required:"false"`
|
||||
//
|
||||
// // unit_of_measurement string (optional)
|
||||
// // Defines the units of measurement of the sensor, if any.
|
||||
// UnitOfMeasurement string `json:"unit_of_measurement,omitempty" required:"false"`
|
||||
//
|
||||
// // value_template template (optional)
|
||||
// // Defines a template to extract the value. If the template throws an error, the current state will be used instead.
|
||||
// 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)
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsSensor() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.IsBinarySensor() {
|
||||
break
|
||||
}
|
||||
if config.IsSwitch() {
|
||||
break
|
||||
}
|
||||
if config.IsLight() {
|
||||
break
|
||||
}
|
||||
if config.IsSelect() {
|
||||
break
|
||||
}
|
||||
|
||||
ok = true
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
||||
|
||||
|
||||
type Fields map[string]string
|
||||
|
||||
@ -161,7 +335,7 @@ func (m *Mqtt) PublishSensorValues(configs []EntityConfig) error {
|
||||
if _, ok := cs[oid.StateClass]; !ok {
|
||||
cs[oid.StateClass] = make(Fields)
|
||||
}
|
||||
cs[oid.StateClass][oid.ValueName] = oid.Value
|
||||
cs[oid.StateClass][oid.ValueName] = oid.Value.String()
|
||||
}
|
||||
|
||||
for n, c := range cs {
|
||||
@ -176,6 +350,6 @@ func (m *Mqtt) PublishSensorValues(configs []EntityConfig) error {
|
||||
|
||||
func (m *Mqtt) GetSensorStateTopic(config EntityConfig) string {
|
||||
st := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
st = JoinStringsForTopic(m.sensorPrefix, st, "state") // m.GetSensorStateTopic(name, config.SubName),m.EntityPrefix, m.Device.FullName, config.SubName
|
||||
st = JoinStringsForTopic(m.Prefix, LabelSensor, m.ClientId, st, "state") // m.GetSensorStateTopic(name, config.SubName),m.EntityPrefix, m.Device.FullName, config.SubName
|
||||
return st
|
||||
}
|
||||
|
212
mmHa/siren.go
Normal file
212
mmHa/siren.go
Normal file
@ -0,0 +1,212 @@
|
||||
package mmHa
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/MickMake/GoUnify/Only"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const LabelSiren = "siren"
|
||||
|
||||
|
||||
func (m *Mqtt) SirenPublishConfig(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
config.FixConfig()
|
||||
if !config.IsSiren() {
|
||||
break
|
||||
}
|
||||
|
||||
ok, newDevice := m.NewDevice(config)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
|
||||
payload := Siren {
|
||||
Device: newDevice,
|
||||
Name: String(JoinStrings(m.DeviceName, config.Name)),
|
||||
// StateTopic: JoinStringsForTopic(m.switchPrefix, id, "state"),
|
||||
CommandTopic: String(JoinStringsForTopic(m.Prefix, LabelSiren, m.ClientId, id, "cmd")),
|
||||
ObjectId: String(id),
|
||||
UniqueId: String(id),
|
||||
Qos: 0,
|
||||
Retain: true,
|
||||
|
||||
// PayloadOn: "true",
|
||||
// PayloadOff: "false",
|
||||
// StateOn: "true",
|
||||
// StateOff: "false",
|
||||
// ValueTemplate: config.ValueTemplate,
|
||||
Icon: Icon(config.Icon),
|
||||
}
|
||||
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelSiren, m.ClientId, id, "config")
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
func (m *Mqtt) SirenPublishValue(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
if !config.IsSiren() {
|
||||
break
|
||||
}
|
||||
|
||||
if config.IgnoreUpdate {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelSiren, m.ClientId, id, "state")
|
||||
|
||||
value := config.Value.String()
|
||||
if value == "--" {
|
||||
value = ""
|
||||
}
|
||||
|
||||
// @TODO - Real hack here. Need to properly check for JSON.
|
||||
if strings.Contains(config.Value.String(), `{`) || strings.Contains(config.Value.String(), `":`) {
|
||||
t := m.client.Publish(tag, 0, true, config.Value.String())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
payload := MqttState{
|
||||
LastReset: config.LastReset, // m.GetLastReset(config.FullId),
|
||||
Value: value,
|
||||
}
|
||||
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
type Siren struct {
|
||||
// A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
|
||||
Availability *Availability `json:"availability,omitempty"`
|
||||
|
||||
// When availability is configured, this controls the conditions needed to set the entity to available. Valid entries are all, any, and latest. If set to all, payload_available must be received on all configured availability topics before the entity is marked as online. If set to any, payload_available must be received on at least one configured availability topic before the entity is marked as online. If set to latest, the last payload_available or payload_not_available received on any configured availability topic controls the availability.
|
||||
AvailabilityMode String `json:"availability_mode,omitempty" default:"latest"`
|
||||
|
||||
// Defines a template to extract device’s availability from the availability_topic. To determine the devices’s availability result of this template will be compared to payload_available and payload_not_available.
|
||||
AvailabilityTemplate Template `json:"availability_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive availability (online/offline) updates. Must not be used together with availability.
|
||||
AvailabilityTopic String `json:"availability_topic,omitempty"`
|
||||
|
||||
// A list of available tones the siren supports. When configured, this enables the support for setting a tone and enables the tone state attribute.
|
||||
AvailableTones List `json:"available_tones,omitempty"`
|
||||
|
||||
// Defines a template to generate a custom payload to send to command_topic. The variable value will be assigned with the configured payload_on or payload_off setting. The siren turn on service parameters tone, volume_level or duration can be used as variables in the template. When operation in optimistic mode the corresponding state attributes will be set. Turn on parameters will be filtered if a device misses the support.
|
||||
CommandTemplate Template `json:"command_template,omitempty"`
|
||||
|
||||
// Defines a template to generate a custom payload to send to command_topic when the siren turn off service is called. By default command_template will be used as template for service turn off. The variable value will be assigned with the configured payload_off setting.
|
||||
CommandOffTemplate Template `json:"command_off_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the siren state. Without command templates, a default JSON payload like {"state":"ON", "tone": "bell", "duration": 10, "volume_level": 0.5 } is published. When the siren turn on service is called, the startup parameters will be added to the JSON payload. The state value of the JSON payload will be set to the the payload_on or payload_off configured payload.
|
||||
CommandTopic String `json:"command_topic,omitempty"`
|
||||
|
||||
// Information about the device this siren is a part of to tie it into the device registry. Only works through MQTT discovery and when unique_id is set. At least one of identifiers or connections must be present to identify the device.
|
||||
Device Device `json:"device,omitempty"`
|
||||
|
||||
// Flag which defines if the entity should be enabled when first added.
|
||||
EnabledByDefault Boolean `json:"enabled_by_default,omitempty" default:"true"`
|
||||
|
||||
// The encoding of the payloads received and published messages. Set to "" to disable decoding of incoming payload.
|
||||
Encoding String `json:"encoding,omitempty" default:"utf"`
|
||||
|
||||
// The category of the entity.
|
||||
EntityCategory String `json:"entity_category,omitempty" default:"None"`
|
||||
|
||||
// Icon for the entity.
|
||||
Icon Icon `json:"icon,omitempty"`
|
||||
|
||||
// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTemplate Template `json:"json_attributes_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive a JSON dictionary payload and then set as sensor attributes. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTopic String `json:"json_attributes_topic,omitempty"`
|
||||
|
||||
// The name to use when displaying this siren.
|
||||
Name String `json:"name,omitempty" default:"MQTT"`
|
||||
|
||||
// Used instead of name for automatic generation of entity_id
|
||||
ObjectId String `json:"object_id,omitempty"`
|
||||
|
||||
// Flag that defines if siren works in optimistic mode.
|
||||
Optimistic Boolean `json:"optimistic,omitempty"`
|
||||
|
||||
// Default: true if no state_topic defined, else false.
|
||||
// The payload that represents the available state.
|
||||
PayloadAvailable String `json:"payload_available,omitempty" default:"online"`
|
||||
|
||||
// The payload that represents the unavailable state.
|
||||
PayloadNotAvailable String `json:"payload_not_available,omitempty" default:"offline"`
|
||||
|
||||
// The payload that represents off state. If specified, will be used for both comparing to the value in the state_topic (see value_template and state_off for details) and sending as off command to the command_topic.
|
||||
PayloadOff String `json:"payload_off,omitempty" default:"OFF"`
|
||||
|
||||
// The payload that represents on state. If specified, will be used for both comparing to the value in the state_topic (see value_template and state_on for details) and sending as on command to the command_topic.
|
||||
PayloadOn String `json:"payload_on,omitempty" default:"ON"`
|
||||
|
||||
// The maximum QoS level of the state topic. Default is 0 and will also be used to publishing messages.
|
||||
Qos Integer `json:"qos,omitempty" default:"0"`
|
||||
|
||||
// If the published message should have the retain flag on or not.
|
||||
Retain Boolean `json:"retain,omitempty" default:"false"`
|
||||
|
||||
// The payload that represents the off state. Used when value that represents off state in the state_topic is different from value that should be sent to the command_topic to turn the device off.
|
||||
StateOff String `json:"state_off,omitempty"`
|
||||
|
||||
// Default: payload_off if defined, else 'OFF'
|
||||
// The payload that represents the on state. Used when value that represents on state in the state_topic is different from value that should be sent to the command_topic to turn the device on.
|
||||
StateOn String `json:"state_on,omitempty"`
|
||||
|
||||
// Default: payload_on if defined, else 'ON'
|
||||
// The MQTT topic subscribed to receive state updates. The state update may be either JSON or a simple string. When a JSON payload is detected, the state value of the JSON payload should supply the payload_on or payload_off defined payload to turn the siren on or off. Additionally, the state attributes duration, tone and volume_level can be updated. Use value_template to transform the received state udpate to a compliant JSON payload. Attributes will only be set if the function is supported by the device and a valid value is supplied. When a non JSON payload is detected, it should be either of the payload_on or payload_off defined payloads or None to reset the siren’s state to unknown. The initial state will be unknown. The state will be reset to unknown if a None payload or null JSON value is received as a state update.
|
||||
StateTopic String `json:"state_topic,omitempty"`
|
||||
|
||||
// Defines a template to extract device’s state from the state_topic. To determine the siren’s state result of this template will be compared to state_on and state_off. Alternatively value_template can be used to render to a valid JSON payload.
|
||||
StateValueTemplate String `json:"state_value_template,omitempty"`
|
||||
|
||||
// Set to true if the MQTT siren supports the duration service turn on parameter and enables the duration state attribute.
|
||||
SupportDuration Boolean `json:"support_duration,omitempty" default:"true"`
|
||||
|
||||
// Set to true if the MQTT siren supports the volume_set service turn on parameter and enables the volume_level state attribute.
|
||||
SupportVolumeSet Boolean `json:"support_volume_set,omitempty" default:"true"`
|
||||
|
||||
// An ID that uniquely identifies this siren device. If two sirens have the same unique ID, Home Assistant will raise an exception.
|
||||
UniqueId String `json:"unique_id,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
func (c *Siren) Json() string {
|
||||
j, _ := json.Marshal(*c)
|
||||
return string(j)
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsSiren() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.Units == LabelSiren {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
317
mmHa/struct.go
317
mmHa/struct.go
@ -37,11 +37,13 @@ type Mqtt struct {
|
||||
DeviceName string
|
||||
MqttDevices map[string]Device
|
||||
|
||||
servicePrefix string
|
||||
sensorPrefix string
|
||||
lightPrefix string
|
||||
switchPrefix string
|
||||
binarySensorPrefix string
|
||||
Prefix string
|
||||
// selectPrefix string
|
||||
// servicePrefix string
|
||||
// sensorPrefix string
|
||||
// lightPrefix string
|
||||
// switchPrefix string
|
||||
// binarySensorPrefix string
|
||||
|
||||
token mqtt.Token
|
||||
firstRun bool
|
||||
@ -59,12 +61,14 @@ func New(req Mqtt) *Mqtt {
|
||||
}
|
||||
ret.firstRun = true
|
||||
ret.EntityPrefix = req.EntityPrefix
|
||||
ret.Prefix = "homeassistant"
|
||||
|
||||
ret.servicePrefix = "homeassistant/sensor/" + req.ClientId
|
||||
ret.sensorPrefix = "homeassistant/sensor/" + req.ClientId
|
||||
ret.lightPrefix = "homeassistant/light/" + req.ClientId
|
||||
ret.switchPrefix = "homeassistant/switch/" + req.ClientId
|
||||
ret.binarySensorPrefix = "homeassistant/binary_sensor/" + req.ClientId
|
||||
// ret.selectPrefix = fmt.Sprintf("homeassistant/%s/%s", LabelSelect, req.ClientId)
|
||||
// ret.servicePrefix = fmt.Sprintf("homeassistant/%s/%s", LabelSensor, req.ClientId)
|
||||
// ret.sensorPrefix = fmt.Sprintf("homeassistant/%s/%s", LabelSensor, req.ClientId)
|
||||
// ret.lightPrefix = fmt.Sprintf("homeassistant/%s/%s", LabelLight, req.ClientId)
|
||||
// ret.switchPrefix = fmt.Sprintf("homeassistant/%s/%s", LabelSwitch, req.ClientId)
|
||||
// ret.binarySensorPrefix = fmt.Sprintf("homeassistant/%s/%s", LabelBinarySensor, req.ClientId)
|
||||
|
||||
ret.MqttDevices = make(map[string]Device)
|
||||
ret.SungrowPsIds = make(map[valueTypes.PsId]bool)
|
||||
@ -186,7 +190,7 @@ func (m *Mqtt) Connect() error {
|
||||
}
|
||||
|
||||
device := Config {
|
||||
Entry: m.servicePrefix,
|
||||
Entry: JoinStringsForTopic(m.Prefix, LabelSensor, m.ClientId), // m.servicePrefix
|
||||
Name: m.ClientId,
|
||||
UniqueId: m.ClientId, // + "_Service",
|
||||
StateTopic: "~/state",
|
||||
@ -199,21 +203,51 @@ func (m *Mqtt) Connect() error {
|
||||
},
|
||||
}
|
||||
|
||||
m.err = m.Publish(JoinStringsForTopic(m.servicePrefix, "config"), 0, true, device.Json())
|
||||
m.err = m.Publish(JoinStringsForTopic(m.Prefix, LabelSensor, m.ClientId, "config"), 0, true, device.Json())
|
||||
if m.err != nil {
|
||||
break
|
||||
}
|
||||
m.err = m.Publish(JoinStringsForTopic(m.Prefix, LabelSensor, m.ClientId, "state"), 0, true, "ON")
|
||||
if m.err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
m.err = m.Publish(JoinStringsForTopic(m.servicePrefix, "state"), 0, true, "ON")
|
||||
// Device: newDevice,
|
||||
// Name: String(JoinStrings(m.DeviceName, config.Name)),
|
||||
// StateTopic: String(JoinStringsForTopic(m.switchPrefix, id, "state")),
|
||||
// CommandTopic: String(JoinStringsForTopic(m.switchPrefix, id, "cmd")),
|
||||
// ObjectId: String(id),
|
||||
// UniqueId: String(id),
|
||||
// Qos: 0,
|
||||
// Retain: true,
|
||||
//
|
||||
// ValueTemplate: Template(config.ValueTemplate),
|
||||
// Icon: Icon(config.Icon),
|
||||
m.err = m.SelectPublishConfig(EntityConfig {
|
||||
Name: "Debug Level",
|
||||
FullId: "GoSungrow.Control.DebugLevel",
|
||||
Icon: "",
|
||||
ValueTemplate: `{\"select\": \"{{ value }}\"}`,
|
||||
})
|
||||
if m.err != nil {
|
||||
break
|
||||
}
|
||||
m.err = m.Subscribe(JoinStringsForTopic(m.Prefix, LabelSelect, m.ClientId, "state"), m.Fart)
|
||||
if m.err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
func (m *Mqtt) Fart(client mqtt.Client, msg mqtt.Message) {
|
||||
for range Only.Once {
|
||||
fmt.Printf("DONE\n")
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Mqtt) Disconnect() error {
|
||||
for range Only.Once {
|
||||
m.client.Disconnect(250)
|
||||
@ -231,7 +265,7 @@ func (m *Mqtt) createClientOptions() error {
|
||||
m.clientOptions.SetPassword(password)
|
||||
m.clientOptions.SetClientID(m.ClientId)
|
||||
|
||||
m.clientOptions.WillTopic = JoinStringsForTopic(m.servicePrefix, "state")
|
||||
m.clientOptions.WillTopic = JoinStringsForTopic(m.Prefix, LabelSensor, m.ClientId, "state")
|
||||
m.clientOptions.WillPayload = []byte("OFF")
|
||||
m.clientOptions.WillQos = 0
|
||||
m.clientOptions.WillRetained = true
|
||||
@ -240,11 +274,17 @@ func (m *Mqtt) createClientOptions() error {
|
||||
return m.err
|
||||
}
|
||||
|
||||
func (m *Mqtt) Subscribe(topic string) error {
|
||||
// type SubscribeFunc func(client mqtt.Client, msg mqtt.Message)
|
||||
func (m *Mqtt) subscribeDefault(client mqtt.Client, msg mqtt.Message) {
|
||||
fmt.Printf("* [%s] %s\n", msg.Topic(), string(msg.Payload()))
|
||||
}
|
||||
|
||||
func (m *Mqtt) Subscribe(topic string, fn mqtt.MessageHandler) error {
|
||||
for range Only.Once {
|
||||
t := m.client.Subscribe(topic, 0, func(client mqtt.Client, msg mqtt.Message) {
|
||||
fmt.Printf("* [%s] %s\n", msg.Topic(), string(msg.Payload()))
|
||||
})
|
||||
if fn == nil {
|
||||
fn = m.subscribeDefault
|
||||
}
|
||||
t := m.client.Subscribe(topic, 0, fn)
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
// m.err = errors.New("mqtt subscribe timeout")
|
||||
@ -258,7 +298,6 @@ func (m *Mqtt) Publish(topic string, qos byte, retained bool, payload interface{
|
||||
t := m.client.Publish(topic, qos, retained, payload)
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
// m.err = errors.New("mqtt publish timeout")
|
||||
}
|
||||
}
|
||||
return m.err
|
||||
@ -271,16 +310,16 @@ func (m *Mqtt) PublishState(Type string, subtopic string, payload interface{}) e
|
||||
// st := JoinStringsForTopic(m.sensorPrefix, JoinStringsForId(m.EntityPrefix, m.Device.FullName, strings.ReplaceAll(subName, "/", ".")), "state")
|
||||
topic := ""
|
||||
switch Type {
|
||||
case "sensor":
|
||||
topic = JoinStringsForTopic(m.sensorPrefix, subtopic, "state")
|
||||
case "binary_sensor":
|
||||
topic = JoinStringsForTopic(m.binarySensorPrefix, subtopic, "state")
|
||||
case "lights":
|
||||
topic = JoinStringsForTopic(m.lightPrefix, subtopic, "state")
|
||||
case "switch":
|
||||
topic = JoinStringsForTopic(m.switchPrefix, subtopic, "state")
|
||||
case LabelSensor:
|
||||
topic = JoinStringsForTopic(m.Prefix, LabelSensor, m.ClientId, subtopic, "state")
|
||||
case LabelBinarySensor:
|
||||
topic = JoinStringsForTopic(m.Prefix, LabelBinarySensor, m.ClientId, subtopic, "state")
|
||||
case LabelLight:
|
||||
topic = JoinStringsForTopic(m.Prefix, LabelLight, m.ClientId, subtopic, "state")
|
||||
case LabelSwitch:
|
||||
topic = JoinStringsForTopic(m.Prefix, LabelSwitch, m.ClientId, subtopic, "state")
|
||||
default:
|
||||
topic = JoinStringsForTopic(m.sensorPrefix, subtopic, "state")
|
||||
topic = JoinStringsForTopic(m.Prefix, LabelSensor, m.ClientId, subtopic, "state")
|
||||
}
|
||||
|
||||
t := m.client.Publish(topic, 0, true, payload)
|
||||
@ -295,40 +334,40 @@ func (m *Mqtt) PublishValue(Type string, subtopic string, value string) error {
|
||||
for range Only.Once {
|
||||
topic := ""
|
||||
switch Type {
|
||||
case "sensor":
|
||||
topic = JoinStringsForTopic(m.sensorPrefix, subtopic, "state")
|
||||
// state := MqttState {
|
||||
// LastReset: "", // m.GetLastReset(point.PointId),
|
||||
// Value: value,
|
||||
// }
|
||||
// value = state.Json()
|
||||
case LabelSensor:
|
||||
topic = JoinStringsForTopic(m.Prefix, LabelSensor, m.ClientId, subtopic, "state")
|
||||
// state := MqttState {
|
||||
// LastReset: "", // m.GetLastReset(point.PointId),
|
||||
// Value: value,
|
||||
// }
|
||||
// value = state.Json()
|
||||
|
||||
case "binary_sensor":
|
||||
topic = JoinStringsForTopic(m.binarySensorPrefix, subtopic, "state")
|
||||
// state := MqttState {
|
||||
// LastReset: "", // m.GetLastReset(point.PointId),
|
||||
// Value: value,
|
||||
// }
|
||||
// value = state.Json()
|
||||
case "binary_sensor":
|
||||
topic = JoinStringsForTopic(m.Prefix, LabelBinarySensor, m.ClientId, subtopic, "state")
|
||||
// state := MqttState {
|
||||
// LastReset: "", // m.GetLastReset(point.PointId),
|
||||
// Value: value,
|
||||
// }
|
||||
// value = state.Json()
|
||||
|
||||
case "lights":
|
||||
topic = JoinStringsForTopic(m.lightPrefix, subtopic, "state")
|
||||
// state := MqttState {
|
||||
// LastReset: "", // m.GetLastReset(point.PointId),
|
||||
// Value: value,
|
||||
// }
|
||||
// value = state.Json()
|
||||
case "lights":
|
||||
topic = JoinStringsForTopic(m.Prefix, LabelLight, m.ClientId, subtopic, "state")
|
||||
// state := MqttState {
|
||||
// LastReset: "", // m.GetLastReset(point.PointId),
|
||||
// Value: value,
|
||||
// }
|
||||
// value = state.Json()
|
||||
|
||||
case "switch":
|
||||
topic = JoinStringsForTopic(m.switchPrefix, subtopic, "state")
|
||||
// state := MqttState {
|
||||
// LastReset: "", // m.GetLastReset(point.PointId),
|
||||
// Value: value,
|
||||
// }
|
||||
// value = state.Json()
|
||||
case LabelSwitch:
|
||||
topic = JoinStringsForTopic(m.Prefix, LabelSwitch, m.ClientId, subtopic, "state")
|
||||
// state := MqttState {
|
||||
// LastReset: "", // m.GetLastReset(point.PointId),
|
||||
// Value: value,
|
||||
// }
|
||||
// value = state.Json()
|
||||
|
||||
default:
|
||||
topic = JoinStringsForTopic(m.sensorPrefix, subtopic, "state")
|
||||
default:
|
||||
topic = JoinStringsForTopic(m.Prefix, LabelSensor, m.ClientId, subtopic, "state")
|
||||
}
|
||||
|
||||
// t = JoinStringsForId(m.EntityPrefix, m.Device.Name, t)
|
||||
@ -411,16 +450,8 @@ func (mq *MqttState) Json() string {
|
||||
return ret
|
||||
}
|
||||
|
||||
|
||||
type Availability struct {
|
||||
PayloadAvailable string `json:"payload_available,omitempty" required:"false"`
|
||||
PayloadNotAvailable string `json:"payload_not_available,omitempty" required:"false"`
|
||||
Topic string `json:"topic,omitempty" required:"true"`
|
||||
ValueTemplate string `json:"value_template,omitempty" required:"false"`
|
||||
}
|
||||
type SensorState string
|
||||
|
||||
|
||||
type EntityConfig struct {
|
||||
// Type string
|
||||
Name string
|
||||
@ -437,86 +468,19 @@ type EntityConfig struct {
|
||||
StateClass string
|
||||
Icon string
|
||||
|
||||
Value string
|
||||
Value valueTypes.UnitValue
|
||||
Point *api.Point
|
||||
ValueTemplate string
|
||||
|
||||
UpdateFreq string
|
||||
LastReset string
|
||||
LastResetValueTemplate string
|
||||
|
||||
IgnoreUpdate bool
|
||||
|
||||
haType string
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsSensor() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.IsBinarySensor() {
|
||||
break
|
||||
}
|
||||
if config.IsSwitch() {
|
||||
break
|
||||
}
|
||||
if config.IsLight() {
|
||||
break
|
||||
}
|
||||
// if config.Value != "" {
|
||||
// ok = true
|
||||
// break
|
||||
// }
|
||||
// if config.Units == "" {
|
||||
// break
|
||||
// }
|
||||
|
||||
ok = true
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsBinarySensor() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.Units == LabelBinarySensor {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
if config.Units == "Bool" {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsSwitch() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.Units == LabelSwitch {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsLight() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.Units == LabelLight {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
||||
|
||||
func (config *EntityConfig) FixConfig() {
|
||||
|
||||
for range Only.Once {
|
||||
@ -538,6 +502,9 @@ func (config *EntityConfig) FixConfig() {
|
||||
config.DeviceClass = SetDefault(config.DeviceClass, "power")
|
||||
config.Icon = SetDefault(config.Icon, "mdi:check-circle-outline")
|
||||
config.ValueTemplate = SetDefault(config.ValueTemplate, "{{ value_json.value }}")
|
||||
// if !config.Value.Valid {
|
||||
// config.Value = "false"
|
||||
// }
|
||||
|
||||
case "MW":
|
||||
fallthrough
|
||||
@ -546,9 +513,14 @@ func (config *EntityConfig) FixConfig() {
|
||||
case "W":
|
||||
config.DeviceClass = SetDefault(config.DeviceClass, "power")
|
||||
config.Icon = SetDefault(config.Icon, "mdi:lightning-bolt")
|
||||
config.ValueTemplate = SetDefault(config.ValueTemplate, "{{ value_json.value | float }}")
|
||||
// config.ValueTemplate = SetDefault(config.ValueTemplate, fmt.Sprintf("{{ value_json.%s | float }}", config.ValueName))
|
||||
// - Used with merged values.
|
||||
if config.ValueName == "" {
|
||||
config.ValueTemplate = SetDefault(config.ValueTemplate, "{{ value_json.value | float }}")
|
||||
} else {
|
||||
config.ValueTemplate = SetDefault(config.ValueTemplate, fmt.Sprintf("{{ value_json.%s | float }}", config.ValueName))
|
||||
}
|
||||
if !config.Value.Valid {
|
||||
config.IgnoreUpdate = true
|
||||
}
|
||||
|
||||
case "MWh":
|
||||
fallthrough
|
||||
@ -557,27 +529,62 @@ func (config *EntityConfig) FixConfig() {
|
||||
case "Wh":
|
||||
config.DeviceClass = SetDefault(config.DeviceClass, "energy")
|
||||
config.Icon = SetDefault(config.Icon, "mdi:lightning-bolt")
|
||||
config.ValueTemplate = SetDefault(config.ValueTemplate, "{{ value_json.value | float }}")
|
||||
if config.ValueName == "" {
|
||||
config.ValueTemplate = SetDefault(config.ValueTemplate, "{{ value_json.value | float }}")
|
||||
} else {
|
||||
config.ValueTemplate = SetDefault(config.ValueTemplate, fmt.Sprintf("{{ value_json.%s | float }}", config.ValueName))
|
||||
}
|
||||
if !config.Value.Valid {
|
||||
config.IgnoreUpdate = true
|
||||
}
|
||||
|
||||
case "kvar":
|
||||
config.DeviceClass = SetDefault(config.DeviceClass, "reactive_power")
|
||||
config.Icon = SetDefault(config.Icon, "mdi:lightning-bolt")
|
||||
config.ValueTemplate = SetDefault(config.ValueTemplate, "{{ value_json.value | float }}")
|
||||
if config.ValueName == "" {
|
||||
config.ValueTemplate = SetDefault(config.ValueTemplate, "{{ value_json.value | float }}")
|
||||
} else {
|
||||
config.ValueTemplate = SetDefault(config.ValueTemplate, fmt.Sprintf("{{ value_json.%s | float }}", config.ValueName))
|
||||
}
|
||||
if !config.Value.Valid {
|
||||
config.IgnoreUpdate = true
|
||||
}
|
||||
|
||||
case "Hz":
|
||||
config.DeviceClass = SetDefault(config.DeviceClass, "frequency")
|
||||
config.Icon = SetDefault(config.Icon, "mdi:sine-wave")
|
||||
config.ValueTemplate = SetDefault(config.ValueTemplate, "{{ value_json.value | float }}")
|
||||
if config.ValueName == "" {
|
||||
config.ValueTemplate = SetDefault(config.ValueTemplate, "{{ value_json.value | float }}")
|
||||
} else {
|
||||
config.ValueTemplate = SetDefault(config.ValueTemplate, fmt.Sprintf("{{ value_json.%s | float }}", config.ValueName))
|
||||
}
|
||||
if !config.Value.Valid {
|
||||
config.IgnoreUpdate = true
|
||||
}
|
||||
|
||||
case "V":
|
||||
config.DeviceClass = SetDefault(config.DeviceClass, "voltage")
|
||||
config.Icon = SetDefault(config.Icon, "mdi:current-dc")
|
||||
config.ValueTemplate = SetDefault(config.ValueTemplate, "{{ value_json.value | float }}")
|
||||
if config.ValueName == "" {
|
||||
config.ValueTemplate = SetDefault(config.ValueTemplate, "{{ value_json.value | float }}")
|
||||
} else {
|
||||
config.ValueTemplate = SetDefault(config.ValueTemplate, fmt.Sprintf("{{ value_json.%s | float }}", config.ValueName))
|
||||
}
|
||||
if !config.Value.Valid {
|
||||
config.IgnoreUpdate = true
|
||||
}
|
||||
|
||||
case "A":
|
||||
config.DeviceClass = SetDefault(config.DeviceClass, "current")
|
||||
config.Icon = SetDefault(config.Icon, "mdi:current-ac")
|
||||
config.ValueTemplate = SetDefault(config.ValueTemplate, "{{ value_json.value | float }}")
|
||||
if config.ValueName == "" {
|
||||
config.ValueTemplate = SetDefault(config.ValueTemplate, "{{ value_json.value | float }}")
|
||||
} else {
|
||||
config.ValueTemplate = SetDefault(config.ValueTemplate, fmt.Sprintf("{{ value_json.%s | float }}", config.ValueName))
|
||||
}
|
||||
if !config.Value.Valid {
|
||||
config.IgnoreUpdate = true
|
||||
}
|
||||
|
||||
case "°F":
|
||||
fallthrough
|
||||
@ -593,12 +600,26 @@ func (config *EntityConfig) FixConfig() {
|
||||
config.DeviceClass = SetDefault(config.DeviceClass, "temperature")
|
||||
config.Units = "°C"
|
||||
config.Icon = SetDefault(config.Icon, "mdi:thermometer")
|
||||
config.ValueTemplate = SetDefault(config.ValueTemplate, "{{ value_json.value | float }}")
|
||||
if config.ValueName == "" {
|
||||
config.ValueTemplate = SetDefault(config.ValueTemplate, "{{ value_json.value | float }}")
|
||||
} else {
|
||||
config.ValueTemplate = SetDefault(config.ValueTemplate, fmt.Sprintf("{{ value_json.%s | float }}", config.ValueName))
|
||||
}
|
||||
if !config.Value.Valid {
|
||||
config.IgnoreUpdate = true
|
||||
}
|
||||
|
||||
case "%":
|
||||
config.DeviceClass = SetDefault(config.DeviceClass, "battery")
|
||||
config.Icon = SetDefault(config.Icon, "mdi:home-battery-outline")
|
||||
config.ValueTemplate = SetDefault(config.ValueTemplate, "{{ value_json.value | float }}")
|
||||
if config.ValueName == "" {
|
||||
config.ValueTemplate = SetDefault(config.ValueTemplate, "{{ value_json.value | float }}")
|
||||
} else {
|
||||
config.ValueTemplate = SetDefault(config.ValueTemplate, fmt.Sprintf("{{ value_json.%s | float }}", config.ValueName))
|
||||
}
|
||||
if !config.Value.Valid {
|
||||
config.IgnoreUpdate = true
|
||||
}
|
||||
|
||||
default:
|
||||
config.DeviceClass = SetDefault(config.DeviceClass, "")
|
||||
|
377
mmHa/switch.go
377
mmHa/switch.go
@ -3,8 +3,11 @@ package mmHa
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/MickMake/GoUnify/Only"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const LabelSwitch = "switch"
|
||||
|
||||
|
||||
func (m *Mqtt) SwitchPublishConfig(config EntityConfig) error {
|
||||
|
||||
@ -31,40 +34,24 @@ func (m *Mqtt) SwitchPublishConfig(config EntityConfig) error {
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
|
||||
payload := Switch {
|
||||
Device: newDevice,
|
||||
Name: JoinStrings(m.DeviceName, config.Name),
|
||||
StateTopic: JoinStringsForTopic(m.switchPrefix, id, "state"),
|
||||
CommandTopic: JoinStringsForTopic(m.switchPrefix, id, "cmd"),
|
||||
ObjectId: id,
|
||||
UniqueId: id,
|
||||
Qos: 0,
|
||||
Retain: true,
|
||||
Device: newDevice,
|
||||
Name: String(JoinStrings(m.DeviceName, config.Name)),
|
||||
StateTopic: String(JoinStringsForTopic(m.Prefix, LabelSwitch, m.ClientId, id, "state")),
|
||||
CommandTopic: String(JoinStringsForTopic(m.Prefix, LabelSwitch, m.ClientId, id, "cmd")),
|
||||
ObjectId: String(id),
|
||||
UniqueId: String(id),
|
||||
Qos: 0,
|
||||
Retain: true,
|
||||
|
||||
PayloadOn: "true",
|
||||
PayloadOff: "false",
|
||||
StateOn: "true",
|
||||
StateOff: "false",
|
||||
ValueTemplate: config.ValueTemplate,
|
||||
Icon: config.Icon,
|
||||
|
||||
// Availability: &Availability {
|
||||
// PayloadAvailable: "",
|
||||
// PayloadNotAvailable: "",
|
||||
// Topic: "",
|
||||
// ValueTemplate: "",
|
||||
// },
|
||||
// AvailabilityMode: "",
|
||||
// AvailabilityTemplate: "",
|
||||
// AvailabilityTopic: "",
|
||||
// CommandTopic: "",
|
||||
// JSONAttributesTemplate: "",
|
||||
// JSONAttributesTopic: "",
|
||||
// Optimistic: false,
|
||||
// PayloadAvailable: "",
|
||||
// PayloadNotAvailable: "",
|
||||
PayloadOn: "true",
|
||||
PayloadOff: "false",
|
||||
StateOn: "true",
|
||||
StateOff: "false",
|
||||
ValueTemplate: String(config.ValueTemplate),
|
||||
Icon: Icon(config.Icon),
|
||||
}
|
||||
|
||||
tag := JoinStringsForTopic(m.switchPrefix, id, "config")
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelSwitch, m.ClientId, id, "config")
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
@ -77,17 +64,36 @@ func (m *Mqtt) SwitchPublishConfig(config EntityConfig) error {
|
||||
func (m *Mqtt) SwitchPublishValue(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
// if config.Units != LabelSwitch {
|
||||
if !config.IsSwitch() {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
payload := MqttState {
|
||||
LastReset: m.GetLastReset(config.UniqueId),
|
||||
Value: config.Value,
|
||||
if config.IgnoreUpdate {
|
||||
break
|
||||
}
|
||||
tag := JoinStringsForTopic(m.switchPrefix, id, "state")
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelSwitch, m.ClientId, id, "state")
|
||||
|
||||
value := config.Value.String()
|
||||
if value == "--" {
|
||||
value = ""
|
||||
}
|
||||
|
||||
// @TODO - Real hack here. Need to properly check for JSON.
|
||||
if strings.Contains(config.Value.String(), `{`) || strings.Contains(config.Value.String(), `":`) {
|
||||
t := m.client.Publish(tag, 0, true, config.Value.String())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
payload := MqttState{
|
||||
LastReset: config.LastReset, // m.GetLastReset(config.FullId),
|
||||
Value: value,
|
||||
}
|
||||
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
@ -98,90 +104,229 @@ func (m *Mqtt) SwitchPublishValue(config EntityConfig) error {
|
||||
}
|
||||
|
||||
|
||||
// func (m *Mqtt) PublishSwitchConfig(id string, name string, subName string, units string, valueName string, class string) error {
|
||||
// for range Only.Once {
|
||||
// id = JoinStringsForId(m.EntityPrefix, m.Device.Name, id)
|
||||
//
|
||||
// payload := Switch {
|
||||
// Device: m.Device,
|
||||
// Name: JoinStrings(m.Device.ViaDevice, name),
|
||||
// StateTopic: JoinStringsForTopic(m.switchPrefix, id, "state"),
|
||||
// // StateClass: "measurement",
|
||||
// // UniqueId: id,
|
||||
// // UnitOfMeasurement: units,
|
||||
// // DeviceClass: class,
|
||||
// // Qos: 0,
|
||||
// // ForceUpdate: true,
|
||||
// // ExpireAfter: 0,
|
||||
// // Encoding: "utf-8",
|
||||
// // EnabledByDefault: true,
|
||||
// // LastResetValueTemplate: LastResetValueTemplate,
|
||||
// // LastReset: LastReset,
|
||||
// // ValueTemplate: "{{ value_json.value | float }}",
|
||||
// // LastReset: time.Now().Format("2006-01-02T00:00:00+00:00"),
|
||||
// // LastResetValueTemplate: "{{entity_id}}",
|
||||
// // LastResetValueTemplate: "{{ (as_datetime((value_json.last_reset | int | timestamp_utc)|string+'Z')).isoformat() }}",
|
||||
// }
|
||||
//
|
||||
// m.client.Publish(JoinStringsForTopic(m.switchPrefix, id, "config"), 0, true, payload.Json())
|
||||
// }
|
||||
// return m.err
|
||||
// }
|
||||
//
|
||||
// func (m *Mqtt) PublishSwitch(subtopic string, payload interface{}) error {
|
||||
// for range Only.Once {
|
||||
// t := m.client.Publish(JoinStringsForTopic(m.switchPrefix, subtopic), 0, true, payload)
|
||||
// if !t.WaitTimeout(m.Timeout) {
|
||||
// m.err = t.Error()
|
||||
// }
|
||||
// }
|
||||
// return m.err
|
||||
// }
|
||||
//
|
||||
// func (m *Mqtt) PublishSwitchState(topic string, payload interface{}) error {
|
||||
// for range Only.Once {
|
||||
// topic = JoinStringsForId(m.EntityPrefix, m.Device.Name, topic)
|
||||
// t := m.client.Publish(JoinStringsForTopic(m.switchPrefix, topic, "state"), 0, true, payload)
|
||||
// if !t.WaitTimeout(m.Timeout) {
|
||||
// m.err = t.Error()
|
||||
// }
|
||||
// }
|
||||
// return m.err
|
||||
// }
|
||||
|
||||
|
||||
type Switch struct {
|
||||
AvailabilityTopic string `json:"avty_t,omitempty"`
|
||||
CommandTopic string `json:"cmd_t"`
|
||||
Device Device `json:"dev,omitempty"`
|
||||
Icon string `json:"ic,omitempty"`
|
||||
JSONAttributesTemplate string `json:"json_attr_tpl,omitempty"`
|
||||
JSONAttributesTopic string `json:"json_attr_t,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
ObjectId string `json:"object_id,omitempty" required:"false"`
|
||||
Optimistic bool `json:"opt,omitempty"`
|
||||
PayloadAvailable string `json:"pl_avail,omitempty"`
|
||||
PayloadNotAvailable string `json:"pl_not_avail,omitempty"`
|
||||
PayloadOff string `json:"pl_off,omitempty"`
|
||||
PayloadOn string `json:"pl_on,omitempty"`
|
||||
Qos int `json:"qos,omitempty"`
|
||||
Retain bool `json:"ret,omitempty"`
|
||||
StateOff string `json:"stat_off,omitempty"`
|
||||
StateOn string `json:"stat_on,omitempty"`
|
||||
StateTopic string `json:"stat_t,omitempty"`
|
||||
UniqueId string `json:"uniq_id,omitempty"`
|
||||
ValueTemplate string `json:"val_tpl,omitempty"`
|
||||
// A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
|
||||
Availability *Availability `json:"availability,omitempty"`
|
||||
|
||||
// CommandFunc func(mqtt.Message, mqtt.Client) `json:"-"`
|
||||
// StateFunc func() string `json:"-"`
|
||||
//
|
||||
// UpdateInterval float64 `json:"-"`
|
||||
// ForceUpdateMQTT bool `json:"-"`
|
||||
//
|
||||
// messageHandler mqtt.MessageHandler
|
||||
// When availability is configured, this controls the conditions needed to set the entity to available. Valid entries are all, any, and latest. If set to all, payload_available must be received on all configured availability topics before the entity is marked as online. If set to any, payload_available must be received on at least one configured availability topic before the entity is marked as online. If set to latest, the last payload_available or payload_not_available received on any configured availability topic controls the availability.
|
||||
AvailabilityMode String `json:"availability_mode,omitempty" default:"latest"`
|
||||
|
||||
// Defines a template to extract device’s availability from the availability_topic. To determine the devices’s availability result of this template will be compared to payload_available and payload_not_available.
|
||||
AvailabilityTemplate Template `json:"availability_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive availability (online/offline) updates. Must not be used together with availability.
|
||||
AvailabilityTopic String `json:"availability_topic,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to change the switch state.
|
||||
CommandTopic String `json:"command_topic,omitempty"`
|
||||
|
||||
// Information about the device this switch is a part of to tie it into the device registry. Only works through MQTT discovery and when unique_id is set. At least one of identifiers or connections must be present to identify the device.
|
||||
Device Device `json:"device,omitempty"`
|
||||
|
||||
// The type/class of the switch to set the icon in the frontend.
|
||||
DeviceClass DeviceClass `json:"device_class,omitempty" default:"None"`
|
||||
|
||||
// Flag which defines if the entity should be enabled when first added.
|
||||
EnabledByDefault Boolean `json:"enabled_by_default,omitempty" default:"true"`
|
||||
|
||||
// The encoding of the payloads received and published messages. Set to "" to disable decoding of incoming payload.
|
||||
Encoding String `json:"encoding,omitempty" default:"utf"`
|
||||
|
||||
// The category of the entity.
|
||||
EntityCategory String `json:"entity_category,omitempty" default:"None"`
|
||||
|
||||
// Icon for the entity.
|
||||
Icon Icon `json:"icon,omitempty"`
|
||||
|
||||
// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTemplate Template `json:"json_attributes_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive a JSON dictionary payload and then set as sensor attributes. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTopic String `json:"json_attributes_topic,omitempty"`
|
||||
|
||||
// The name to use when displaying this switch.
|
||||
Name String `json:"name,omitempty" default:"MQTT"`
|
||||
|
||||
// Used instead of name for automatic generation of entity_id
|
||||
ObjectId String `json:"object_id,omitempty"`
|
||||
|
||||
// Flag that defines if switch works in optimistic mode.
|
||||
Optimistic Boolean `json:"optimistic,omitempty"`
|
||||
|
||||
// Default: true if no state_topic defined, else false.
|
||||
// The payload that represents the available state.
|
||||
PayloadAvailable String `json:"payload_available,omitempty" default:"online"`
|
||||
|
||||
// The payload that represents the unavailable state.
|
||||
PayloadNotAvailable String `json:"payload_not_available,omitempty" default:"offline"`
|
||||
|
||||
// The payload that represents off state. If specified, will be used for both comparing to the value in the state_topic (see value_template and state_off for details) and sending as off command to the command_topic.
|
||||
PayloadOff String `json:"payload_off,omitempty" default:"OFF"`
|
||||
|
||||
// The payload that represents on state. If specified, will be used for both comparing to the value in the state_topic (see value_template and state_on for details) and sending as on command to the command_topic.
|
||||
PayloadOn String `json:"payload_on,omitempty" default:"ON"`
|
||||
|
||||
// The maximum QoS level of the state topic. Default is 0 and will also be used to publishing messages.
|
||||
Qos Integer `json:"qos,omitempty" default:"0"`
|
||||
|
||||
// If the published message should have the retain flag on or not.
|
||||
Retain Boolean `json:"retain,omitempty" default:"false"`
|
||||
|
||||
// The payload that represents the off state. Used when value that represents off state in the state_topic is different from value that should be sent to the command_topic to turn the device off.
|
||||
StateOff String `json:"state_off,omitempty"`
|
||||
|
||||
// Default: payload_off if defined, else OFF
|
||||
// The payload that represents the on state. Used when value that represents on state in the state_topic is different from value that should be sent to the command_topic to turn the device on.
|
||||
StateOn String `json:"state_on,omitempty"`
|
||||
|
||||
// Default: payload_on if defined, else ON
|
||||
// The MQTT topic subscribed to receive state updates.
|
||||
StateTopic String `json:"state_topic,omitempty"`
|
||||
|
||||
// An ID that uniquely identifies this switch device. If two switches have the same unique ID, Home Assistant will raise an exception.
|
||||
UniqueId String `json:"unique_id,omitempty"`
|
||||
|
||||
// Defines a template to extract device’s state from the state_topic. To determine the switches’s state result of this template will be compared to state_on and state_off.
|
||||
ValueTemplate String `json:"value_template,omitempty"`
|
||||
}
|
||||
|
||||
// type Switch struct {
|
||||
// // availability list (optional)
|
||||
// // A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
|
||||
// Availability *Availability `json:"availability,omitempty" required:"false"`
|
||||
//
|
||||
// // availability_template template (optional)
|
||||
// // Defines a template to extract device’s availability from the availability_topic. To determine the devices’s availability result of this template will be compared to payload_available and payload_not_available.
|
||||
// AvailabilityTemplate string `json:"availability_template,omitempty" required:"false"`
|
||||
//
|
||||
// // availability_topic string (optional)
|
||||
// // The MQTT topic subscribed to receive availability (online/offline) updates. Must not be used together with availability.
|
||||
// AvailabilityTopic string `json:"availability_topic,omitempty" required:"false"`
|
||||
//
|
||||
// // availability_mode string (optional, default: latest)
|
||||
// // When availability is configured, this controls the conditions needed to set the entity to available. Valid entries are all, any, and latest. If set to all, payload_available must be received on all configured availability topics before the entity is marked as online. If set to any, payload_available must be received on at least one configured availability topic before the entity is marked as online. If set to latest, the last payload_available or payload_not_available received on any configured availability topic controls the availability.
|
||||
// AvailabilityMode string `json:"availability_mode,omitempty" required:"false"`
|
||||
//
|
||||
// // command_topic string (optional)
|
||||
// // The MQTT topic to publish commands to change the switch state.
|
||||
// CommandTopic string `json:"command_topic" required:"true"`
|
||||
//
|
||||
// // device map (optional)
|
||||
// // Information about the device this switch is a part of to tie it into the device registry. Only works through MQTT discovery and when unique_id is set. At least one of identifiers or connections must be present to identify the device.
|
||||
// Device Device `json:"device,omitempty" required:"false"`
|
||||
//
|
||||
// // device_class device_class (optional, default: None)
|
||||
// // The type/class of the switch to set the icon in the frontend.
|
||||
// DeviceClass string `json:"device_class,omitempty" required:"false"`
|
||||
//
|
||||
// // enabled_by_default boolean (optional, default: true)
|
||||
// // Flag which defines if the entity should be enabled when first added.
|
||||
// EnabledByDefault bool `json:"enabled_by_default,omitempty" required:"false"`
|
||||
//
|
||||
// // encoding string (optional, default: utf-8)
|
||||
// // The encoding of the payloads received and published messages. Set to "" to disable decoding of incoming payload.
|
||||
// Encoding string `json:"encoding,omitempty" required:"false"`
|
||||
//
|
||||
// // entity_category string (optional, default: None)
|
||||
// // The category of the entity.
|
||||
// EntityCategory string `json:"entity_category,omitempty" required:"false"`
|
||||
//
|
||||
// // icon string (optional)
|
||||
// // Icon for the entity.
|
||||
// Icon string `json:"icon,omitempty" required:"false"`
|
||||
//
|
||||
// // json_attributes_template template (optional)
|
||||
// // Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic. Usage example can be found in MQTT sensor documentation.
|
||||
// JsonAttributesTemplate string `json:"json_attributes_template,omitempty" required:"false"`
|
||||
//
|
||||
// // json_attributes_topic string (optional)
|
||||
// // The MQTT topic subscribed to receive a JSON dictionary payload and then set as sensor attributes. Usage example can be found in MQTT sensor documentation.
|
||||
// JsonAttributesTopic string `json:"json_attributes_topic,omitempty" required:"false"`
|
||||
//
|
||||
// // name string (optional, default: MQTT Switch)
|
||||
// // The name to use when displaying this switch.
|
||||
// Name string `json:"name,omitempty" required:"false"`
|
||||
//
|
||||
// // object_id string (optional)
|
||||
// // Used instead of name for automatic generation of entity_id
|
||||
// ObjectId string `json:"object_id,omitempty" required:"false"`
|
||||
//
|
||||
// // optimistic boolean (optional)
|
||||
// // Flag that defines if switch works in optimistic mode.
|
||||
// // Default: true if no state_topic defined, else false.
|
||||
// Optimistic bool `json:"optimistic,omitempty" required:"false"`
|
||||
//
|
||||
// // payload_available string (optional, default: online)
|
||||
// // The payload that represents the available state.
|
||||
// PayloadAvailable string `json:"payload_available,omitempty"`
|
||||
//
|
||||
// // payload_not_available string (optional, default: offline)
|
||||
// // The payload that represents the unavailable state.
|
||||
// PayloadNotAvailable string `json:"payload_not_available,omitempty"`
|
||||
//
|
||||
// // payload_off string (optional, default: OFF)
|
||||
// // The payload that represents off state. If specified, will be used for both comparing to the value in the state_topic (see value_template and state_off for details) and sending as off command to the command_topic.
|
||||
// PayloadOff string `json:"payload_off,omitempty"`
|
||||
//
|
||||
// // payload_on string (optional, default: ON)
|
||||
// // The payload that represents on state. If specified, will be used for both comparing to the value in the state_topic (see value_template and state_on for details) and sending as on command to the command_topic.
|
||||
// PayloadOn string `json:"payload_on,omitempty"`
|
||||
//
|
||||
// // qos integer (optional, default: 0)
|
||||
// // The maximum QoS level of the state topic. Default is 0 and will also be used to publishing messages.
|
||||
// Qos int `json:"qos,omitempty" required:"false"`
|
||||
//
|
||||
// // retain boolean (optional, default: false)
|
||||
// // If the published message should have the retain flag on or not.
|
||||
// Retain bool `json:"retain,omitempty" required:"false"`
|
||||
//
|
||||
// // state_off string (optional)
|
||||
// // The payload that represents the off state. Used when value that represents off state in the state_topic is different from value that should be sent to the command_topic to turn the device off.
|
||||
// // Default: payload_off if defined, else OFF
|
||||
// StateOff string `json:"state_off,omitempty"`
|
||||
//
|
||||
// // state_on string (optional)
|
||||
// // The payload that represents the on state. Used when value that represents on state in the state_topic is different from value that should be sent to the command_topic to turn the device on.
|
||||
// // Default: payload_on if defined, else ON
|
||||
// StateOn string `json:"state_on,omitempty"`
|
||||
//
|
||||
// // state_topic string (optional)
|
||||
// // The MQTT topic subscribed to receive state updates.
|
||||
// StateTopic string `json:"state_topic,omitempty" required:"false"`
|
||||
//
|
||||
// // unique_id string (optional)
|
||||
// // An ID that uniquely identifies this switch device. If two switches have the same unique ID, Home Assistant will raise an exception.
|
||||
// UniqueId string `json:"unique_id,omitempty" required:"false"`
|
||||
//
|
||||
// // value_template value_templatestring (optional)
|
||||
// // Defines a template to extract device’s state from the state_topic. To determine the switches’s state result of this template will be compared to state_on and state_off.
|
||||
// ValueTemplate string `json:"value_template,omitempty" required:"false"`
|
||||
//
|
||||
// // CommandTemplate string `json:"command_template" required:"false"`
|
||||
//
|
||||
// // CommandFunc func(mqtt.Message, mqtt.Client) `json:"-"`
|
||||
// // StateFunc func() string `json:"-"`
|
||||
// //
|
||||
// // UpdateInterval float64 `json:"-"`
|
||||
// // ForceUpdateMQTT bool `json:"-"`
|
||||
// //
|
||||
// // messageHandler mqtt.MessageHandler
|
||||
// }
|
||||
|
||||
func (c *Switch) Json() string {
|
||||
j, _ := json.Marshal(*c)
|
||||
return string(j)
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsSwitch() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.Units == LabelSwitch {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
||||
|
125
mmHa/tag.go
Normal file
125
mmHa/tag.go
Normal file
@ -0,0 +1,125 @@
|
||||
package mmHa
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/MickMake/GoUnify/Only"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const LabelTag = "tag"
|
||||
|
||||
|
||||
func (m *Mqtt) TagPublishConfig(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
config.FixConfig()
|
||||
if !config.IsTag() {
|
||||
break
|
||||
}
|
||||
|
||||
ok, newDevice := m.NewDevice(config)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
|
||||
payload := Tag {
|
||||
Device: newDevice,
|
||||
// Name: String(JoinStrings(m.DeviceName, config.Name)),
|
||||
// StateTopic: JoinStringsForTopic(m.switchPrefix, id, "state"),
|
||||
// CommandTopic: String(JoinStringsForTopic(m.switchPrefix, id, "cmd")),
|
||||
// ObjectId: String(id),
|
||||
// UniqueId: String(id),
|
||||
// Qos: 0,
|
||||
// Retain: true,
|
||||
|
||||
// PayloadOn: "true",
|
||||
// PayloadOff: "false",
|
||||
// StateOn: "true",
|
||||
// StateOff: "false",
|
||||
// ValueTemplate: config.ValueTemplate,
|
||||
// Icon: Icon(config.Icon),
|
||||
}
|
||||
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelTag, m.ClientId, id, "config")
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
func (m *Mqtt) TagPublishValue(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
if !config.IsTag() {
|
||||
break
|
||||
}
|
||||
|
||||
if config.IgnoreUpdate {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelTag, m.ClientId, id, "state")
|
||||
|
||||
value := config.Value.String()
|
||||
if value == "--" {
|
||||
value = ""
|
||||
}
|
||||
|
||||
// @TODO - Real hack here. Need to properly check for JSON.
|
||||
if strings.Contains(config.Value.String(), `{`) || strings.Contains(config.Value.String(), `":`) {
|
||||
t := m.client.Publish(tag, 0, true, config.Value.String())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
payload := MqttState{
|
||||
LastReset: config.LastReset, // m.GetLastReset(config.FullId),
|
||||
Value: value,
|
||||
}
|
||||
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
type Tag struct {
|
||||
// The MQTT topic subscribed to receive tag scanned events.
|
||||
Topic String `json:"topic,omitempty" required:"true"`
|
||||
|
||||
// Defines a template that returns a tag ID.
|
||||
ValueTemplate String `json:"value_template,omitempty"`
|
||||
|
||||
// Information about the device this device trigger is a part of to tie it into the device registry. At least one of identifiers or connections must be present to identify the device.
|
||||
Device Device `json:"device,omitempty" required:"true"`
|
||||
}
|
||||
|
||||
|
||||
func (c *Tag) Json() string {
|
||||
j, _ := json.Marshal(*c)
|
||||
return string(j)
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsTag() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.Units == LabelTag {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
186
mmHa/text.go
Normal file
186
mmHa/text.go
Normal file
@ -0,0 +1,186 @@
|
||||
package mmHa
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/MickMake/GoUnify/Only"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
||||
const LabelText = "text"
|
||||
|
||||
|
||||
func (m *Mqtt) TextPublishConfig(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
config.FixConfig()
|
||||
if !config.IsText() {
|
||||
break
|
||||
}
|
||||
|
||||
ok, newDevice := m.NewDevice(config)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
|
||||
payload := Text {
|
||||
Device: newDevice,
|
||||
Name: String(JoinStrings(m.DeviceName, config.Name)),
|
||||
// StateTopic: JoinStringsForTopic(m.switchPrefix, id, "state"),
|
||||
CommandTopic: String(JoinStringsForTopic(m.Prefix, LabelText, m.ClientId, id, "cmd")),
|
||||
ObjectId: String(id),
|
||||
UniqueId: String(id),
|
||||
Qos: 0,
|
||||
Retain: true,
|
||||
|
||||
// PayloadOn: "true",
|
||||
// PayloadOff: "false",
|
||||
// StateOn: "true",
|
||||
// StateOff: "false",
|
||||
// ValueTemplate: config.ValueTemplate,
|
||||
// Icon: Icon(config.Icon),
|
||||
}
|
||||
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelText, m.ClientId, id, "config")
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
func (m *Mqtt) TextPublishValue(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
if !config.IsText() {
|
||||
break
|
||||
}
|
||||
|
||||
if config.IgnoreUpdate {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelText, m.ClientId, id, "state")
|
||||
|
||||
value := config.Value.String()
|
||||
if value == "--" {
|
||||
value = ""
|
||||
}
|
||||
|
||||
// @TODO - Real hack here. Need to properly check for JSON.
|
||||
if strings.Contains(config.Value.String(), `{`) || strings.Contains(config.Value.String(), `":`) {
|
||||
t := m.client.Publish(tag, 0, true, config.Value.String())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
payload := MqttState{
|
||||
LastReset: config.LastReset, // m.GetLastReset(config.FullId),
|
||||
Value: value,
|
||||
}
|
||||
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
type Text struct {
|
||||
// A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
|
||||
Availability *Availability `json:"availability,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive availability (online/offline) updates. Must not be used together with availability.
|
||||
AvailabilityTopic String `json:"availability_topic,omitempty"`
|
||||
|
||||
// When availability is configured, this controls the conditions needed to set the entity to available. Valid entries are all, any, and latest. If set to all, payload_available must be received on all configured availability topics before the entity is marked as online. If set to any, payload_available must be received on at least one configured availability topic before the entity is marked as online. If set to latest, the last payload_available or payload_not_available received on any configured availability topic controls the availability.
|
||||
AvailabilityMode String `json:"availability_mode,omitempty" default:"latest"`
|
||||
|
||||
// Defines a template to extract device’s availability from the availability_topic. To determine the devices’s availability result of this template will be compared to payload_available and payload_not_available.
|
||||
AvailabilityTemplate Template `json:"availability_template,omitempty"`
|
||||
|
||||
// Defines a template to generate the payload to send to command_topic.
|
||||
CommandTemplate Template `json:"command_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish the text value that is set.
|
||||
CommandTopic String `json:"command_topic,omitempty" required:"true"`
|
||||
|
||||
// Information about the device this of text capability is a part of to tie it into the device registry. Only works through MQTT discovery and when unique_id is set. At least one of identifiers or connections must be present to identify the device.
|
||||
Device Device `json:"device,omitempty"`
|
||||
|
||||
// Flag which defines if the entity should be enabled when first added.
|
||||
EnabledByDefault Boolean `json:"enabled_by_default,omitempty" default:"true"`
|
||||
|
||||
// The encoding of the payloads received and published messages. Set to "" to disable decoding of incoming payload.
|
||||
Encoding String `json:"encoding,omitempty" default:"utf"`
|
||||
|
||||
// The category of the entity.
|
||||
EntityCategory String `json:"entity_category,omitempty" default:"None"`
|
||||
|
||||
// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic.
|
||||
JsonAttributesTemplate Template `json:"json_attributes_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive a JSON dictionary payload and then set as entity attributes. Implies force_update of the current select state when a message is received on this topic.
|
||||
JsonAttributesTopic String `json:"json_attributes_topic,omitempty"`
|
||||
|
||||
// The maximum size of a text being set or received (maximum is 255).
|
||||
Max Integer `json:"max,omitempty" default:"255"`
|
||||
|
||||
// The minimum size of a text being set or received.
|
||||
Min Integer `json:"min,omitempty" default:"0"`
|
||||
|
||||
// The mode off the text entity. Must be either text or password.
|
||||
Mode String `json:"mode,omitempty" default:"text"`
|
||||
|
||||
// The name of the text entity.
|
||||
Name String `json:"name,omitempty" default:"MQTT"`
|
||||
|
||||
// Used instead of name for automatic generation of entity_id
|
||||
ObjectId String `json:"object_id,omitempty"`
|
||||
|
||||
// A valid regular expression the text being set or received must match with.
|
||||
Pattern String `json:"pattern,omitempty"`
|
||||
|
||||
// The maximum QoS level of the state topic. Default is 0 and will also be used to publishing messages.
|
||||
Qos Integer `json:"qos,omitempty" default:"0"`
|
||||
|
||||
// If the published message should have the retain flag on or not.
|
||||
Retain Boolean `json:"retain,omitempty" default:"false"`
|
||||
|
||||
// The MQTT topic subscribed to receive text state updates. Text state updates should match the pattern (if set) and meet the size constraints min and max. Can be used with value_template to render the incoming payload to a text update.
|
||||
StateTopic String `json:"state_topic,omitempty"`
|
||||
|
||||
// An ID that uniquely identifies this Select. If two Selects have the same unique ID Home Assistant will raise an exception.
|
||||
UniqueId String `json:"unique_id,omitempty"`
|
||||
|
||||
// Defines a template to extract the text state value from the payload received on state_topic.
|
||||
ValueTemplate Template `json:"value_template,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
func (c *Text) Json() string {
|
||||
j, _ := json.Marshal(*c)
|
||||
return string(j)
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsText() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.Units == LabelText {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
81
mmHa/types.go
Normal file
81
mmHa/types.go
Normal file
@ -0,0 +1,81 @@
|
||||
package mmHa
|
||||
|
||||
|
||||
// Device - device map (optional)
|
||||
// Information about the device this Select is a part of to tie it into the device registry.
|
||||
// Only works through MQTT discovery and when unique_id is set.
|
||||
// At least one of identifiers or connections must be present to identify the device.
|
||||
type Device struct {
|
||||
// configuration_url string (optional)
|
||||
// A link to the webpage that can manage the configuration of this device. Can be either an HTTP or HTTPS link.
|
||||
ConfigurationUrl string `json:"configuration_url,omitempty" required:"false"`
|
||||
|
||||
// hw_version string (optional)
|
||||
// The hardware version of the device.
|
||||
HwVersion string `json:"hw_version,omitempty" required:"false"`
|
||||
|
||||
// connections list (optional)
|
||||
// A list of connections of the device to the outside world as a list of tuples [connection_type, connection_identifier].
|
||||
// For example the MAC address of a network interface: "connections": ["mac", "02:5b:26:a8:dc:12"].
|
||||
Connections [][]string `json:"connections,omitempty" required:"false"`
|
||||
|
||||
// identifiers list | string (optional)
|
||||
// A list of IDs that uniquely identify the device. For example a serial number.
|
||||
Identifiers []string `json:"identifiers,omitempty" required:"false"`
|
||||
|
||||
// manufacturer string (optional)
|
||||
// The manufacturer of the device.
|
||||
Manufacturer string `json:"manufacturer,omitempty" required:"false"`
|
||||
|
||||
// model string (optional)
|
||||
// The model of the device.
|
||||
Model string `json:"model,omitempty" required:"false"`
|
||||
|
||||
// name string (optional)
|
||||
// The name of the device.
|
||||
Name string `json:"name,omitempty" required:"false"`
|
||||
|
||||
// suggested_area string (optional)
|
||||
// Suggest an area if the device isn’t in one yet.
|
||||
SuggestedArea string `json:"suggested_area,omitempty" required:"false"`
|
||||
|
||||
// sw_version string (optional)
|
||||
// The firmware version of the device.
|
||||
SwVersion string `json:"sw_version,omitempty" required:"false"`
|
||||
|
||||
// via_device string (optional)
|
||||
// Identifier of a device that routes messages between this device and Home Assistant.
|
||||
// Examples of such devices are hubs, or parent devices of a sub-device. This is used to show device topology in Home Assistant.
|
||||
ViaDevice string `json:"via_device,omitempty" required:"false"`
|
||||
}
|
||||
|
||||
|
||||
// Availability - availability list (optional)
|
||||
// A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
|
||||
type Availability struct {
|
||||
// payload_available string (optional, default: online)
|
||||
// The payload that represents the available state.
|
||||
PayloadAvailable string `json:"payload_available,omitempty" required:"false"`
|
||||
|
||||
// payload_not_available string (optional, default: offline)
|
||||
// The payload that represents the unavailable state.
|
||||
PayloadNotAvailable string `json:"payload_not_available,omitempty" required:"false"`
|
||||
|
||||
// topic string REQUIRED
|
||||
// An MQTT topic subscribed to receive availability (online/offline) updates.
|
||||
Topic string `json:"topic,omitempty" required:"true"`
|
||||
|
||||
// value_template template (optional)
|
||||
// Defines a template to extract device’s availability from the topic.
|
||||
// To determine the devices’ availability result of this template will be compared to payload_available and payload_not_available.
|
||||
ValueTemplate string `json:"value_template,omitempty" required:"false"`
|
||||
}
|
||||
|
||||
type List []string
|
||||
type String string
|
||||
type Template string
|
||||
type Boolean bool
|
||||
type Integer int64
|
||||
type Icon string
|
||||
type DeviceClass string
|
||||
type Float float64
|
198
mmHa/update.go
Normal file
198
mmHa/update.go
Normal file
@ -0,0 +1,198 @@
|
||||
package mmHa
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/MickMake/GoUnify/Only"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
||||
const LabelUpdate = "update"
|
||||
|
||||
|
||||
func (m *Mqtt) UpdatePublishConfig(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
config.FixConfig()
|
||||
if !config.IsUpdate() {
|
||||
break
|
||||
}
|
||||
|
||||
ok, newDevice := m.NewDevice(config)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
|
||||
payload := Update {
|
||||
Device: newDevice,
|
||||
Name: String(JoinStrings(m.DeviceName, config.Name)),
|
||||
// StateTopic: JoinStringsForTopic(m.switchPrefix, id, "state"),
|
||||
CommandTopic: String(JoinStringsForTopic(m.Prefix, LabelUpdate, m.ClientId, id, "cmd")),
|
||||
ObjectId: String(id),
|
||||
UniqueId: String(id),
|
||||
Qos: 0,
|
||||
Retain: true,
|
||||
|
||||
// PayloadOn: "true",
|
||||
// PayloadOff: "false",
|
||||
// StateOn: "true",
|
||||
// StateOff: "false",
|
||||
// ValueTemplate: config.ValueTemplate,
|
||||
Icon: Icon(config.Icon),
|
||||
}
|
||||
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelUpdate, m.ClientId, id, "config")
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
func (m *Mqtt) UpdatePublishValue(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
if !config.IsUpdate() {
|
||||
break
|
||||
}
|
||||
|
||||
if config.IgnoreUpdate {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelUpdate, m.ClientId, id, "state")
|
||||
|
||||
value := config.Value.String()
|
||||
if value == "--" {
|
||||
value = ""
|
||||
}
|
||||
|
||||
// @TODO - Real hack here. Need to properly check for JSON.
|
||||
if strings.Contains(config.Value.String(), `{`) || strings.Contains(config.Value.String(), `":`) {
|
||||
t := m.client.Publish(tag, 0, true, config.Value.String())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
payload := MqttState{
|
||||
LastReset: config.LastReset, // m.GetLastReset(config.FullId),
|
||||
Value: value,
|
||||
}
|
||||
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
type Update struct {
|
||||
// A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
|
||||
Availability *Availability `json:"availability,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive availability (online/offline) updates. Must not be used together with availability.
|
||||
AvailabilityTopic String `json:"availability_topic,omitempty"`
|
||||
|
||||
// When availability is configured, this controls the conditions needed to set the entity to available. Valid entries are all, any, and latest. If set to all, payload_available must be received on all configured availability topics before the entity is marked as online. If set to any, payload_available must be received on at least one configured availability topic before the entity is marked as online. If set to latest, the last payload_available or payload_not_available received on any configured availability topic controls the availability.
|
||||
AvailabilityMode String `json:"availability_mode,omitempty" default:"latest"`
|
||||
|
||||
// Defines a template to extract device’s availability from the availability_topic. To determine the devices’s availability result of this template will be compared to payload_available and payload_not_available.
|
||||
AvailabilityTemplate Template `json:"availability_template,omitempty"`
|
||||
|
||||
// The MQTT topic to publish payload_install to start installing process.
|
||||
CommandTopic String `json:"command_topic,omitempty"`
|
||||
|
||||
// Information about the device this Update is a part of to tie it into the device registry. Only works through MQTT discovery and when unique_id is set. At least one of identifiers or connections must be present to identify the device.
|
||||
Device Device `json:"device,omitempty"`
|
||||
|
||||
// The type/class of the update to set the icon in the frontend.
|
||||
DeviceClass DeviceClass `json:"device_class,omitempty" default:"None"`
|
||||
|
||||
// Flag which defines if the entity should be enabled when first added.
|
||||
EnabledByDefault Boolean `json:"enabled_by_default,omitempty" default:"true"`
|
||||
|
||||
// The encoding of the payloads received and published messages. Set to "" to disable decoding of incoming payload.
|
||||
Encoding String `json:"encoding,omitempty" default:"utf"`
|
||||
|
||||
// The category of the entity.
|
||||
EntityCategory String `json:"entity_category,omitempty" default:"None"`
|
||||
|
||||
// Picture URL for the entity.
|
||||
EntityPicture String `json:"entity_picture,omitempty"`
|
||||
|
||||
// Icon for the entity.
|
||||
Icon Icon `json:"icon,omitempty"`
|
||||
|
||||
// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic.
|
||||
JsonAttributesTemplate Template `json:"json_attributes_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive a JSON dictionary payload and then set as entity attributes. Implies force_update of the current select state when a message is received on this topic.
|
||||
JsonAttributesTopic String `json:"json_attributes_topic,omitempty"`
|
||||
|
||||
// Defines a template to extract the latest version value.
|
||||
LatestVersionTemplate Template `json:"latest_version_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive an update of the latest version.
|
||||
LatestVersionTopic String `json:"latest_version_topic,omitempty"`
|
||||
|
||||
// The name of the Select.
|
||||
Name String `json:"name,omitempty"`
|
||||
|
||||
// Used instead of name for automatic generation of entity_id
|
||||
ObjectId String `json:"object_id,omitempty"`
|
||||
|
||||
// The MQTT payload to start installing process.
|
||||
PayloadInstall String `json:"payload_install,omitempty"`
|
||||
|
||||
// The maximum QoS level of the state topic. Default is 0 and will also be used to publishing messages.
|
||||
Qos Integer `json:"qos,omitempty" default:"0"`
|
||||
|
||||
// Summary of the release notes or changelog. This is suitable a brief update description of max 255 characters.
|
||||
ReleaseSummary String `json:"release_summary,omitempty"`
|
||||
|
||||
// URL to the full release notes of the latest version available.
|
||||
ReleaseUrl String `json:"release_url,omitempty"`
|
||||
|
||||
// If the published message should have the retain flag on or not.
|
||||
Retain Boolean `json:"retain,omitempty" default:"false"`
|
||||
|
||||
// The MQTT topic subscribed to receive state updates. The state update may be either JSON or a simple string with installed_version value. When a JSON payload is detected, the state value of the JSON payload should supply the installed_version and can optional supply: latest_version, title, release_summary, release_url or an entity_picture URL.
|
||||
StateTopic String `json:"state_topic,omitempty"`
|
||||
|
||||
// Title of the software, or firmware update. This helps to differentiate between the device or entity name versus the title of the software installed.
|
||||
Title String `json:"title,omitempty"`
|
||||
|
||||
// An ID that uniquely identifies this Select. If two Selects have the same unique ID Home Assistant will raise an exception.
|
||||
UniqueId String `json:"unique_id,omitempty"`
|
||||
|
||||
// Defines a template to extract the installed_version state value or to render to a valid JSON payload on from the payload received on state_topic.
|
||||
ValueTemplate Template `json:"value_template,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
func (c *Update) Json() string {
|
||||
j, _ := json.Marshal(*c)
|
||||
return string(j)
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsUpdate() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.Units == LabelUpdate {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
202
mmHa/vacuum.go
Normal file
202
mmHa/vacuum.go
Normal file
@ -0,0 +1,202 @@
|
||||
package mmHa
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/MickMake/GoUnify/Only"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
||||
const LabelVacuum = "vacuum"
|
||||
|
||||
func (m *Mqtt) VacuumPublishConfig(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
config.FixConfig()
|
||||
if !config.IsVacuum() {
|
||||
break
|
||||
}
|
||||
|
||||
ok, newDevice := m.NewDevice(config)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
|
||||
payload := Vacuum {
|
||||
Device: newDevice,
|
||||
Name: String(JoinStrings(m.DeviceName, config.Name)),
|
||||
// StateTopic: JoinStringsForTopic(m.switchPrefix, id, "state"),
|
||||
CommandTopic: String(JoinStringsForTopic(m.Prefix, LabelVacuum, m.ClientId, id, "cmd")),
|
||||
ObjectId: String(id),
|
||||
UniqueId: String(id),
|
||||
Qos: 0,
|
||||
Retain: true,
|
||||
|
||||
// PayloadOn: "true",
|
||||
// PayloadOff: "false",
|
||||
// StateOn: "true",
|
||||
// StateOff: "false",
|
||||
// ValueTemplate: config.ValueTemplate,
|
||||
// Icon: Icon(config.Icon),
|
||||
}
|
||||
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelVacuum, m.ClientId, id, "config")
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
func (m *Mqtt) VacuumPublishValue(config EntityConfig) error {
|
||||
|
||||
for range Only.Once {
|
||||
if !config.IsVacuum() {
|
||||
break
|
||||
}
|
||||
|
||||
if config.IgnoreUpdate {
|
||||
break
|
||||
}
|
||||
|
||||
id := JoinStringsForId(m.DeviceName, config.FullId)
|
||||
tag := JoinStringsForTopic(m.Prefix, LabelVacuum, m.ClientId, id, "state")
|
||||
|
||||
value := config.Value.String()
|
||||
if value == "--" {
|
||||
value = ""
|
||||
}
|
||||
|
||||
// @TODO - Real hack here. Need to properly check for JSON.
|
||||
if strings.Contains(config.Value.String(), `{`) || strings.Contains(config.Value.String(), `":`) {
|
||||
t := m.client.Publish(tag, 0, true, config.Value.String())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
payload := MqttState{
|
||||
LastReset: config.LastReset, // m.GetLastReset(config.FullId),
|
||||
Value: value,
|
||||
}
|
||||
|
||||
t := m.client.Publish(tag, 0, true, payload.Json())
|
||||
if !t.WaitTimeout(m.Timeout) {
|
||||
m.err = t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return m.err
|
||||
}
|
||||
|
||||
type Vacuum struct {
|
||||
// A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
|
||||
Availability *Availability `json:"availability,omitempty"`
|
||||
|
||||
// When availability is configured, this controls the conditions needed to set the entity to available. Valid entries are all, any, and latest. If set to all, payload_available must be received on all configured availability topics before the entity is marked as online. If set to any, payload_available must be received on at least one configured availability topic before the entity is marked as online. If set to latest, the last payload_available or payload_not_available received on any configured availability topic controls the availability.
|
||||
AvailabilityMode String `json:"availability_mode,omitempty" default:"latest"`
|
||||
|
||||
// Defines a template to extract device’s availability from the availability_topic. To determine the devices’s availability result of this template will be compared to payload_available and payload_not_available.
|
||||
AvailabilityTemplate Template `json:"availability_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive availability (online/offline) updates. Must not be used together with availability.
|
||||
AvailabilityTopic String `json:"availability_topic,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to control the vacuum.
|
||||
CommandTopic String `json:"command_topic,omitempty"`
|
||||
|
||||
// Information about the device this switch is a part of to tie it into the device registry. Only works through MQTT discovery and when unique_id is set. At least one of identifiers or connections must be present to identify the device.
|
||||
Device Device `json:"device,omitempty"`
|
||||
|
||||
// The encoding of the payloads received and published messages. Set to "" to disable decoding of incoming payload.
|
||||
Encoding String `json:"encoding,omitempty" default:"utf"`
|
||||
|
||||
// List of possible fan speeds for the vacuum.
|
||||
FanSpeedList List `json:"fan_speed_list,omitempty"`
|
||||
|
||||
// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTemplate Template `json:"json_attributes_template,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive a JSON dictionary payload and then set as sensor attributes. Usage example can be found in MQTT sensor documentation.
|
||||
JsonAttributesTopic String `json:"json_attributes_topic,omitempty"`
|
||||
|
||||
// The name of the vacuum.
|
||||
Name String `json:"name,omitempty" default:"MQTT"`
|
||||
|
||||
// Used instead of name for automatic generation of entity_id
|
||||
ObjectId String `json:"object_id,omitempty"`
|
||||
|
||||
// The payload that represents the available state.
|
||||
PayloadAvailable String `json:"payload_available,omitempty" default:"online"`
|
||||
|
||||
// The payload to send to the command_topic to begin a spot cleaning cycle.
|
||||
PayloadCleanSpot String `json:"payload_clean_spot,omitempty" default:"clean_spot"`
|
||||
|
||||
// The payload to send to the command_topic to locate the vacuum (typically plays a song).
|
||||
PayloadLocate String `json:"payload_locate,omitempty" default:"locate"`
|
||||
|
||||
// The payload that represents the unavailable state.
|
||||
PayloadNotAvailable String `json:"payload_not_available,omitempty" default:"offline"`
|
||||
|
||||
// The payload to send to the command_topic to pause the vacuum.
|
||||
PayloadPause String `json:"payload_pause,omitempty" default:"pause"`
|
||||
|
||||
// The payload to send to the command_topic to tell the vacuum to return to base.
|
||||
PayloadReturnToBase String `json:"payload_return_to_base,omitempty" default:"return_to_base"`
|
||||
|
||||
// The payload to send to the command_topic to begin the cleaning cycle.
|
||||
PayloadStart String `json:"payload_start,omitempty" default:"start"`
|
||||
|
||||
// The payload to send to the command_topic to stop cleaning.
|
||||
PayloadStop String `json:"payload_stop,omitempty" default:"stop"`
|
||||
|
||||
// The maximum QoS level of the state topic.
|
||||
Qos Integer `json:"qos,omitempty" default:"0"`
|
||||
|
||||
// If the published message should have the retain flag on or not.
|
||||
Retain Boolean `json:"retain,omitempty" default:"false"`
|
||||
|
||||
// The schema to use. Must be state to select the state schema.
|
||||
Schema String `json:"schema,omitempty" default:"legacy"`
|
||||
|
||||
// The MQTT topic to publish custom commands to the vacuum.
|
||||
SendCommandTopic String `json:"send_command_topic,omitempty"`
|
||||
|
||||
// The MQTT topic to publish commands to control the vacuum’s fan speed.
|
||||
SetFanSpeedTopic String `json:"set_fan_speed_topic,omitempty"`
|
||||
|
||||
// The MQTT topic subscribed to receive state messages from the vacuum. Messages received on the state_topic must be a valid JSON dictionary, with a mandatory state key and optionally battery_level and fan_speed keys as shown in the example.
|
||||
StateTopic String `json:"state_topic,omitempty"`
|
||||
|
||||
// supported_features string | list (optional)
|
||||
// List of features that the vacuum supports (possible values are start, stop, pause, return_home, battery, status, locate, clean_spot, fan_speed, send_command).
|
||||
SupportedFeatures List `json:"supported_features,omitempty"`
|
||||
|
||||
// Default: start, stop, return_home, status, battery, clean_spot
|
||||
// An ID that uniquely identifies this vacuum. If two vacuums have the same unique ID, Home Assistant will raise an exception.
|
||||
UniqueId String `json:"unique_id,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
func (c *Vacuum) Json() string {
|
||||
j, _ := json.Marshal(*c)
|
||||
return string(j)
|
||||
}
|
||||
|
||||
func (config *EntityConfig) IsVacuum() bool {
|
||||
var ok bool
|
||||
|
||||
for range Only.Once {
|
||||
if config.Units == LabelVacuum {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user