/
button.go
256 lines (250 loc) · 10.9 KB
/
button.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package entities
import (
"encoding/json"
"fmt"
mqtt "github.com/eclipse/paho.mqtt.golang"
strcase "github.com/iancoleman/strcase"
common "github.com/kjbreil/hass-mqtt/common"
"log"
"reflect"
"strings"
"time"
)
// //////////////////////////////////////////////////////////////////////////////
// Do not modify this file, it is automatically generated
// //////////////////////////////////////////////////////////////////////////////
type Button struct {
AvailabilityMode *string `json:"availability_mode,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."
AvailabilityTemplate *string `json:"availability_template,omitempty"` // "Defines a [template](/docs/configuration/templating/#using-templates-with-the-mqtt-integration) 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`."
AvailabilityTopic *string `json:"availability_topic,omitempty"` // "The MQTT topic subscribed to receive availability (online/offline) updates. Must not be used together with `availability`."
availabilityFunc func() string
CommandTemplate *string `json:"command_template,omitempty"` // "Defines a [template](/docs/configuration/templating/#using-templates-with-the-mqtt-integration) to generate the payload to send to `command_topic`."
CommandTopic *string `json:"command_topic,omitempty"` // "The MQTT topic to publish commands to trigger the button."
commandFunc func(mqtt.Message, mqtt.Client)
Device Device `json:"device,omitempty"` // Device configuration parameters
DeviceClass *string `json:"device_class,omitempty"` // "The [type/class](/integrations/button/#device-class) of the button to set the icon in the frontend."
EnabledByDefault *bool `json:"enabled_by_default,omitempty"` // "Flag which defines if the entity should be enabled when first added."
Encoding *string `json:"encoding,omitempty"` // "The encoding of the published messages."
EntityCategory *string `json:"entity_category,omitempty"` // "The [category](https://developers.home-assistant.io/docs/core/entity#generic-properties) of the entity."
Icon *string `json:"icon,omitempty"` // "[Icon](/docs/configuration/customizing-devices/#icon) for the entity."
JsonAttributesTemplate *string `json:"json_attributes_template,omitempty"` // "Defines a [template](/docs/configuration/templating/#using-templates-with-the-mqtt-integration) to extract the JSON dictionary from messages received on the `json_attributes_topic`. Usage example can be found in [MQTT sensor](/integrations/sensor.mqtt/#json-attributes-template-configuration) documentation."
JsonAttributesTopic *string `json:"json_attributes_topic,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](/integrations/sensor.mqtt/#json-attributes-topic-configuration) documentation."
jsonAttributesFunc func() string
Name *string `json:"name,omitempty"` // "The name to use when displaying this button."
ObjectId *string `json:"object_id,omitempty"` // "Used instead of `name` for automatic generation of `entity_id`"
PayloadAvailable *string `json:"payload_available,omitempty"` // "The payload that represents the available state."
PayloadNotAvailable *string `json:"payload_not_available,omitempty"` // "The payload that represents the unavailable state."
PayloadPress *string `json:"payload_press,omitempty"` // "The payload To send to trigger the button."
Qos *int `json:"qos,omitempty"` // "The maximum QoS level of the state topic. Default is 0 and will also be used to publishing messages."
Retain *bool `json:"retain,omitempty"` // "If the published message should have the retain flag on or not."
UniqueId *string `json:"unique_id,omitempty"` // "An ID that uniquely identifies this button entity. If two buttons have the same unique ID, Home Assistant will raise an exception."
MQTT *MQTTFields `json:"-"` // MQTT configuration parameters
states buttonState // Internal Holder of States
States *ButtonState `json:"-"` // External state update location
}
func NewButton(o *ButtonOptions) (*Button, error) {
var b Button
b.States = &o.states
if !reflect.ValueOf(o.availabilityMode).IsZero() {
b.AvailabilityMode = &o.availabilityMode
}
if !reflect.ValueOf(o.availabilityTemplate).IsZero() {
b.AvailabilityTemplate = &o.availabilityTemplate
}
if !reflect.ValueOf(o.availabilityFunc).IsZero() {
b.availabilityFunc = o.availabilityFunc
}
if !reflect.ValueOf(o.commandTemplate).IsZero() {
b.CommandTemplate = &o.commandTemplate
}
if !reflect.ValueOf(o.commandFunc).IsZero() {
b.commandFunc = o.commandFunc
}
if !reflect.ValueOf(o.deviceClass).IsZero() {
b.DeviceClass = &o.deviceClass
}
if !reflect.ValueOf(o.enabledByDefault).IsZero() {
b.EnabledByDefault = &o.enabledByDefault
}
if !reflect.ValueOf(o.encoding).IsZero() {
b.Encoding = &o.encoding
}
if !reflect.ValueOf(o.entityCategory).IsZero() {
b.EntityCategory = &o.entityCategory
}
if !reflect.ValueOf(o.icon).IsZero() {
b.Icon = &o.icon
}
if !reflect.ValueOf(o.jsonAttributesTemplate).IsZero() {
b.JsonAttributesTemplate = &o.jsonAttributesTemplate
}
if !reflect.ValueOf(o.jsonAttributesFunc).IsZero() {
b.jsonAttributesFunc = o.jsonAttributesFunc
}
if !reflect.ValueOf(o.name).IsZero() {
b.Name = &o.name
} else {
return nil, fmt.Errorf("name not defined")
}
if !reflect.ValueOf(o.objectId).IsZero() {
b.ObjectId = &o.objectId
}
if !reflect.ValueOf(o.payloadAvailable).IsZero() {
b.PayloadAvailable = &o.payloadAvailable
}
if !reflect.ValueOf(o.payloadNotAvailable).IsZero() {
b.PayloadNotAvailable = &o.payloadNotAvailable
}
if !reflect.ValueOf(o.payloadPress).IsZero() {
b.PayloadPress = &o.payloadPress
}
if !reflect.ValueOf(o.qos).IsZero() {
b.Qos = &o.qos
}
if !reflect.ValueOf(o.retain).IsZero() {
b.Retain = &o.retain
}
if !reflect.ValueOf(o.uniqueId).IsZero() {
b.UniqueId = &o.uniqueId
} else {
uniqueId := strcase.ToDelimited(o.name, uint8(0x2d))
uniqueId = strings.ReplaceAll(uniqueId, "'", "_")
b.UniqueId = &uniqueId
}
return &b, nil
}
type buttonState struct {
Availability *string
JsonAttributes *string
}
type ButtonState struct {
JsonAttributes string
}
func (d *Button) JsonAttributes(s string) {
d.States.JsonAttributes = s
d.UpdateState()
}
func (d *Button) GetRawId() string {
return "button"
}
func (d *Button) AddMessageHandler() {
d.MQTT.MessageHandler = MakeMessageHandler(d)
}
func (d *Button) GetUniqueId() string {
return *d.UniqueId
}
func (d *Button) GetDomainEntity() string {
return fmt.Sprintf("button.%s", strings.ReplaceAll(*d.UniqueId, "-", "_"))
}
func (d *Button) GetName() string {
return *d.Name
}
func (d *Button) PopulateDevice(Manufacturer string, SoftwareName string, InstanceName string, SWVersion string, Identifier string) {
d.Device.Manufacturer = &Manufacturer
d.Device.Model = &SoftwareName
d.Device.Name = &InstanceName
d.Device.SwVersion = &SWVersion
d.Device.Identifiers = &Identifier
}
func (d *Button) UpdateState() {
if d.AvailabilityTopic != nil {
state := d.availabilityFunc()
if d.states.Availability == nil || state != *d.states.Availability || (d.MQTT.ForceUpdate != nil && *d.MQTT.ForceUpdate) {
token := (*d.MQTT.Client).Publish(*d.AvailabilityTopic, byte(*d.Qos), true, state)
token.WaitTimeout(common.WaitTimeout)
d.states.Availability = &state
}
}
if d.JsonAttributesTopic != nil {
state := d.jsonAttributesFunc()
if d.states.JsonAttributes == nil || state != *d.states.JsonAttributes || (d.MQTT.ForceUpdate != nil && *d.MQTT.ForceUpdate) {
token := (*d.MQTT.Client).Publish(*d.JsonAttributesTopic, byte(*d.Qos), true, state)
token.WaitTimeout(common.WaitTimeout)
d.states.JsonAttributes = &state
}
}
}
func (d *Button) Subscribe() {
c := *d.MQTT.Client
message, err := json.Marshal(d)
if err != nil {
log.Fatal(err)
}
if d.CommandTopic != nil {
t := c.Subscribe(*d.CommandTopic, 0, d.MQTT.MessageHandler)
t.WaitTimeout(common.WaitTimeout)
if t.Error() != nil {
log.Fatal(t.Error())
}
}
token := c.Publish(GetDiscoveryTopic(d), 2, true, message)
token.WaitTimeout(common.WaitTimeout)
d.availabilityFunc()
d.UpdateState()
}
func (d *Button) UnSubscribe() {
c := *d.MQTT.Client
token := c.Publish(*d.AvailabilityTopic, 2, false, "offline")
token.WaitTimeout(common.WaitTimeout)
if d.CommandTopic != nil {
t := c.Unsubscribe(*d.CommandTopic)
t.WaitTimeout(common.WaitTimeout)
if t.Error() != nil {
log.Fatal(t.Error())
}
}
}
func (d *Button) AnnounceAvailable() {
c := *d.MQTT.Client
token := c.Publish(*d.AvailabilityTopic, 2, true, "online")
token.WaitTimeout(common.WaitTimeout)
}
func (d *Button) Initialize() {
if d.Qos == nil {
d.Qos = new(int)
*d.Qos = int(0)
}
if d.Retain == nil {
d.Retain = new(bool)
*d.Retain = false
}
if d.UniqueId == nil {
d.UniqueId = new(string)
*d.UniqueId = strcase.ToDelimited(*d.Name, uint8(0x2d))
}
if d.availabilityFunc == nil {
d.availabilityFunc = AvailabilityFunc
}
if d.MQTT == nil {
d.MQTT = new(MQTTFields)
}
d.AddMessageHandler()
d.populateTopics()
}
func (d *Button) DeleteTopic() {
c := *d.MQTT.Client
token := c.Publish(GetDiscoveryTopic(d), 0, true, "")
token.WaitTimeout(common.WaitTimeout)
time.Sleep(common.HADiscoveryDelay)
}
func (d *Button) populateTopics() {
if d.availabilityFunc != nil {
d.AvailabilityTopic = new(string)
*d.AvailabilityTopic = GetTopic(d, "availability_topic")
}
if d.commandFunc != nil {
d.CommandTopic = new(string)
*d.CommandTopic = GetTopic(d, "command_topic")
common.TopicStore[*d.CommandTopic] = &d.commandFunc
}
if d.jsonAttributesFunc != nil {
d.JsonAttributesTopic = new(string)
*d.JsonAttributesTopic = GetTopic(d, "json_attributes_topic")
}
}
func (d *Button) SetMQTTFields(fields MQTTFields) {
*d.MQTT = fields
}
func (d *Button) GetMQTTFields() (fields MQTTFields) {
return *d.MQTT
}