|
| 1 | +package hassio |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + MQTT "github.com/eclipse/paho.mqtt.golang" |
| 7 | + "github.com/rainu/samsung-remote-mqtt/internal/mqtt" |
| 8 | + "go.uber.org/zap" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +type generalConfig struct { |
| 13 | + Name string `json:"name"` |
| 14 | + AvailabilityTopic string `json:"avty_t,omitempty"` |
| 15 | + PayloadAvailable string `json:"pl_avail,omitempty"` |
| 16 | + PayloadNotAvailable string `json:"pl_not_avail,omitempty"` |
| 17 | + UniqueId string `json:"uniq_id"` |
| 18 | + Icon string `json:"ic,omitempty"` |
| 19 | + Device device `json:"dev,omitempty"` |
| 20 | +} |
| 21 | + |
| 22 | +type sensorConfig struct { |
| 23 | + generalConfig |
| 24 | + |
| 25 | + StateTopic string `json:"stat_t"` |
| 26 | +} |
| 27 | + |
| 28 | +type triggerConfig struct { |
| 29 | + generalConfig |
| 30 | + |
| 31 | + CommandTopic string `json:"cmd_t"` |
| 32 | + StateTopic string `json:"stat_t"` |
| 33 | + PayloadStart string `json:"pl_on"` |
| 34 | + PayloadStop string `json:"pl_off"` |
| 35 | + StateRunning string `json:"stat_on"` |
| 36 | + StateStopped string `json:"stat_off"` |
| 37 | +} |
| 38 | + |
| 39 | +type device struct { |
| 40 | + Name string `json:"name,omitempty"` |
| 41 | + Ids []string `json:"ids"` |
| 42 | + Model string `json:"mdl,omitempty"` |
| 43 | + Manufacturer string `json:"mf,omitempty"` |
| 44 | + Version string `json:"sw,omitempty"` |
| 45 | +} |
| 46 | + |
| 47 | +type AvailabilityConfig struct { |
| 48 | + Topic string |
| 49 | + AvailablePayload string |
| 50 | + NotAvailablePayload string |
| 51 | +} |
| 52 | + |
| 53 | +type Client struct { |
| 54 | + parsedDeviceInfo deviceInfo |
| 55 | + DeviceInfo string |
| 56 | + RemoteName string |
| 57 | + |
| 58 | + HassioTopicPrefix string |
| 59 | + TopicPrefix string |
| 60 | + MqttClient MQTT.Client |
| 61 | +} |
| 62 | + |
| 63 | +type deviceInfo struct { |
| 64 | + Id string `json:"id"` |
| 65 | + Name string `json:"name"` |
| 66 | + Type string `json:"type"` |
| 67 | + Version string `json:"version"` |
| 68 | + Device struct { |
| 69 | + OS string `json:"OS"` |
| 70 | + FirmwareVersion string `json:"firmwareVersion"` |
| 71 | + Model string `json:"model"` |
| 72 | + ModelName string `json:"modelName"` |
| 73 | + Name string `json:"name"` |
| 74 | + Resolution string `json:"resolution"` |
| 75 | + SSID string `json:"ssid"` |
| 76 | + WifiMac string `json:"wifiMac"` |
| 77 | + } `json:"device"` |
| 78 | +} |
| 79 | + |
| 80 | +func (c *Client) PublishDiscoveryConfig() { |
| 81 | + zap.L().Info("Initialise homeassistant config.") |
| 82 | + |
| 83 | + //try to parse raw device information |
| 84 | + if err := json.Unmarshal([]byte(c.DeviceInfo), &c.parsedDeviceInfo); err != nil { |
| 85 | + zap.L().Debug("Could not parse raw device info: %s", zap.Error(err)) |
| 86 | + } |
| 87 | + |
| 88 | + targetTopic := fmt.Sprintf("%ssensor/%s_status/config", c.HassioTopicPrefix, c.deviceId()) |
| 89 | + payload := c.generatePayloadForStatus() |
| 90 | + c.MqttClient.Publish(targetTopic, byte(0), false, payload) |
| 91 | + |
| 92 | + for keyName, description := range SamsungRemoteKeys { |
| 93 | + for topic, payload := range c.generatePayloadForSendKey(keyName, description) { |
| 94 | + c.MqttClient.Publish(topic, byte(0), false, payload) |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func (c *Client) generatePayloadForStatus() []byte { |
| 100 | + conf := sensorConfig{ |
| 101 | + generalConfig: generalConfig{ |
| 102 | + Name: "Status", |
| 103 | + PayloadAvailable: mqtt.StatusOnline, |
| 104 | + PayloadNotAvailable: mqtt.StatusOffline, |
| 105 | + UniqueId: fmt.Sprintf("%s_status", c.deviceId()), |
| 106 | + Device: c.buildDevice(), |
| 107 | + }, |
| 108 | + StateTopic: fmt.Sprintf("%s/status", c.TopicPrefix), |
| 109 | + } |
| 110 | + |
| 111 | + payload, err := json.Marshal(conf) |
| 112 | + if err != nil { |
| 113 | + //the "marshalling" is relatively safe - it should never appear at runtime |
| 114 | + panic(err) |
| 115 | + } |
| 116 | + return payload |
| 117 | +} |
| 118 | + |
| 119 | +func (c *Client) generatePayloadForSendKey(key, description string) map[string][]byte { |
| 120 | + payloads := map[string][]byte{} |
| 121 | + var conf interface{} |
| 122 | + |
| 123 | + conf = triggerConfig{ |
| 124 | + generalConfig: generalConfig{ |
| 125 | + Name: fmt.Sprintf("%s - Button", description), |
| 126 | + Icon: "mdi:remote", |
| 127 | + UniqueId: fmt.Sprintf("%s_%s", c.deviceId(), key), |
| 128 | + Device: c.buildDevice(), |
| 129 | + AvailabilityTopic: fmt.Sprintf("%s/status", c.TopicPrefix), |
| 130 | + PayloadAvailable: mqtt.StatusOnline, |
| 131 | + PayloadNotAvailable: mqtt.StatusOffline, |
| 132 | + }, |
| 133 | + CommandTopic: fmt.Sprintf("%s/send-key", c.TopicPrefix), |
| 134 | + PayloadStart: key, |
| 135 | + StateTopic: fmt.Sprintf("%s/send-key/state", c.TopicPrefix), |
| 136 | + StateRunning: mqtt.PayloadStatusRunning, |
| 137 | + StateStopped: mqtt.PayloadStatusStopped, |
| 138 | + } |
| 139 | + |
| 140 | + payload, err := json.Marshal(conf) |
| 141 | + if err != nil { |
| 142 | + //the "marshalling" is relatively safe - it should never appear at runtime |
| 143 | + panic(err) |
| 144 | + } |
| 145 | + topic := fmt.Sprintf("%sswitch/%s/%s/config", c.HassioTopicPrefix, c.deviceId(), key) |
| 146 | + payloads[topic] = payload |
| 147 | + |
| 148 | + conf = sensorConfig{ |
| 149 | + generalConfig: generalConfig{ |
| 150 | + Name: fmt.Sprintf("%s - State", description), |
| 151 | + UniqueId: fmt.Sprintf("%s_%s_state", c.deviceId(), key), |
| 152 | + Device: c.buildDevice(), |
| 153 | + AvailabilityTopic: fmt.Sprintf("%s/status", c.TopicPrefix), |
| 154 | + PayloadAvailable: mqtt.StatusOnline, |
| 155 | + PayloadNotAvailable: mqtt.StatusOffline, |
| 156 | + }, |
| 157 | + StateTopic: fmt.Sprintf("%s/send-key/state", c.TopicPrefix), |
| 158 | + } |
| 159 | + |
| 160 | + payload, err = json.Marshal(conf) |
| 161 | + if err != nil { |
| 162 | + //the "marshalling" is relatively safe - it should never appear at runtime |
| 163 | + panic(err) |
| 164 | + } |
| 165 | + topic = fmt.Sprintf("%ssensor/%s_%s/result/config", c.HassioTopicPrefix, c.deviceId(), key) |
| 166 | + payloads[topic] = payload |
| 167 | + |
| 168 | + conf = sensorConfig{ |
| 169 | + generalConfig: generalConfig{ |
| 170 | + Name: fmt.Sprintf("%s - Result", description), |
| 171 | + UniqueId: fmt.Sprintf("%s_%s_result", c.deviceId(), key), |
| 172 | + Device: c.buildDevice(), |
| 173 | + AvailabilityTopic: fmt.Sprintf("%s/status", c.TopicPrefix), |
| 174 | + PayloadAvailable: mqtt.StatusOnline, |
| 175 | + PayloadNotAvailable: mqtt.StatusOffline, |
| 176 | + }, |
| 177 | + StateTopic: fmt.Sprintf("%s/send-key/result", c.TopicPrefix), |
| 178 | + } |
| 179 | + |
| 180 | + payload, err = json.Marshal(conf) |
| 181 | + if err != nil { |
| 182 | + //the "marshalling" is relatively safe - it should never appear at runtime |
| 183 | + panic(err) |
| 184 | + } |
| 185 | + topic = fmt.Sprintf("%ssensor/%s_%s/state/config", c.TopicPrefix, c.deviceId(), key) |
| 186 | + payloads[topic] = payload |
| 187 | + |
| 188 | + return payloads |
| 189 | +} |
| 190 | + |
| 191 | +func (c *Client) buildDevice() device { |
| 192 | + d := device{ |
| 193 | + Name: c.parsedDeviceInfo.Name, |
| 194 | + Ids: []string{c.deviceId()}, |
| 195 | + Manufacturer: "Samsung", |
| 196 | + Model: c.parsedDeviceInfo.Device.ModelName, |
| 197 | + Version: c.parsedDeviceInfo.Version, |
| 198 | + } |
| 199 | + |
| 200 | + if d.Name == "" { |
| 201 | + d.Name = c.RemoteName |
| 202 | + } |
| 203 | + |
| 204 | + return d |
| 205 | +} |
| 206 | + |
| 207 | +func (c *Client) deviceId() string { |
| 208 | + if c.parsedDeviceInfo.Device.WifiMac != "" { |
| 209 | + return strings.Replace(c.parsedDeviceInfo.Device.WifiMac, ":", "", -1) |
| 210 | + } |
| 211 | + if c.parsedDeviceInfo.Id != "" { |
| 212 | + return c.parsedDeviceInfo.Id |
| 213 | + } |
| 214 | + if c.parsedDeviceInfo.Name != "" { |
| 215 | + return c.parsedDeviceInfo.Name |
| 216 | + } |
| 217 | + |
| 218 | + return c.RemoteName |
| 219 | +} |
0 commit comments