slask
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package mqtt
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
@@ -8,23 +9,45 @@ import (
|
||||
"git.k7n.net/mats/go-telldus/pkg/telldus"
|
||||
)
|
||||
|
||||
// DeviceDiscovery represents Home Assistant device discovery payload
|
||||
type DeviceDiscovery struct {
|
||||
Name string `json:"name"`
|
||||
CommandTopic string `json:"command_topic"`
|
||||
StateTopic string `json:"state_topic"`
|
||||
UniqueID string `json:"unique_id"`
|
||||
Device map[string]interface{} `json:"device"`
|
||||
// HADevice represents a device in Home Assistant MQTT discovery
|
||||
type HADevice struct {
|
||||
Identifiers []string `json:"identifiers"`
|
||||
Name string `json:"name"`
|
||||
Manufacturer string `json:"manufacturer"`
|
||||
Model string `json:"model,omitempty"`
|
||||
}
|
||||
|
||||
// SensorDiscovery represents Home Assistant sensor discovery payload
|
||||
// SwitchDiscovery represents Home Assistant MQTT switch discovery config
|
||||
// Reference: https://www.home-assistant.io/integrations/switch.mqtt/
|
||||
type SwitchDiscovery struct {
|
||||
Name string `json:"name"`
|
||||
CommandTopic string `json:"command_topic"`
|
||||
StateTopic string `json:"state_topic"`
|
||||
UniqueID string `json:"unique_id"`
|
||||
Device HADevice `json:"device"`
|
||||
PayloadOn string `json:"payload_on,omitempty"`
|
||||
PayloadOff string `json:"payload_off,omitempty"`
|
||||
StateOn string `json:"state_on,omitempty"`
|
||||
StateOff string `json:"state_off,omitempty"`
|
||||
OptimisticMode bool `json:"optimistic,omitempty"`
|
||||
Qos int `json:"qos,omitempty"`
|
||||
Retain bool `json:"retain,omitempty"`
|
||||
AvailabilityTopic string `json:"availability_topic,omitempty"`
|
||||
}
|
||||
|
||||
// SensorDiscovery represents Home Assistant MQTT sensor discovery config
|
||||
// Reference: https://www.home-assistant.io/integrations/sensor.mqtt/
|
||||
type SensorDiscovery struct {
|
||||
Name string `json:"name"`
|
||||
StateTopic string `json:"state_topic"`
|
||||
UnitOfMeasurement string `json:"unit_of_measurement,omitempty"`
|
||||
DeviceClass string `json:"device_class,omitempty"`
|
||||
UniqueID string `json:"unique_id"`
|
||||
Device map[string]interface{} `json:"device"`
|
||||
Name string `json:"name"`
|
||||
StateTopic string `json:"state_topic"`
|
||||
UniqueID string `json:"unique_id"`
|
||||
Device HADevice `json:"device"`
|
||||
UnitOfMeasurement string `json:"unit_of_measurement,omitempty"`
|
||||
DeviceClass string `json:"device_class,omitempty"`
|
||||
StateClass string `json:"state_class,omitempty"`
|
||||
ValueTemplate string `json:"value_template,omitempty"`
|
||||
Qos int `json:"qos,omitempty"`
|
||||
AvailabilityTopic string `json:"availability_topic,omitempty"`
|
||||
}
|
||||
|
||||
// PublishAllDiscovery publishes Home Assistant discovery messages for all devices and sensors
|
||||
@@ -55,19 +78,29 @@ func (c *Client) PublishDeviceDiscovery(deviceID int) error {
|
||||
return fmt.Errorf("device %d not found: %w", deviceID, err)
|
||||
}
|
||||
|
||||
topic := fmt.Sprintf("homeassistant/switch/%s/config", device.UniqueID)
|
||||
payload := fmt.Sprintf(`{
|
||||
"name": "%s",
|
||||
"command_topic": "telldus/device/%d/set",
|
||||
"state_topic": "telldus/device/%d/state",
|
||||
"unique_id": "%s",
|
||||
"device": {
|
||||
"identifiers": ["telldus_%d"],
|
||||
"name": "%s",
|
||||
"manufacturer": "Telldus"
|
||||
}
|
||||
}`, device.Name, deviceID, deviceID, device.UniqueID, deviceID, device.Name)
|
||||
discovery := SwitchDiscovery{
|
||||
Name: device.Name,
|
||||
CommandTopic: fmt.Sprintf("telldus/device/%d/set", deviceID),
|
||||
StateTopic: fmt.Sprintf("telldus/device/%d/state", deviceID),
|
||||
UniqueID: device.UniqueID,
|
||||
PayloadOn: "ON",
|
||||
PayloadOff: "OFF",
|
||||
StateOn: "ON",
|
||||
StateOff: "OFF",
|
||||
Device: HADevice{
|
||||
Identifiers: []string{fmt.Sprintf("telldus_%d", deviceID)},
|
||||
Name: device.Name,
|
||||
Manufacturer: "Telldus",
|
||||
Model: fmt.Sprintf("%s %s", device.Protocol, device.Model),
|
||||
},
|
||||
}
|
||||
|
||||
payload, err := json.Marshal(discovery)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal discovery: %w", err)
|
||||
}
|
||||
|
||||
topic := fmt.Sprintf("homeassistant/switch/%s/config", device.UniqueID)
|
||||
c.client.Publish(topic, 0, true, payload)
|
||||
return nil
|
||||
}
|
||||
@@ -120,39 +153,50 @@ func (c *Client) PublishSensorDiscovery(protocol, model string, id int) error {
|
||||
|
||||
// publishSensorDiscoveryForSensor publishes discovery messages for a sensor's data types
|
||||
func (c *Client) publishSensorDiscoveryForSensor(sensor *datastore.Sensor, dataTypes int) error {
|
||||
sensorDevice := HADevice{
|
||||
Identifiers: []string{fmt.Sprintf("telldus_sensor_%s_%s_%d", sensor.Protocol, sensor.Model, sensor.ID)},
|
||||
Name: sensor.Name,
|
||||
Manufacturer: "Telldus",
|
||||
Model: fmt.Sprintf("%s %s", sensor.Protocol, sensor.Model),
|
||||
}
|
||||
|
||||
if dataTypes&telldus.DataTypeTemperature != 0 && sensor.TemperatureUniqueID != "" {
|
||||
discovery := SensorDiscovery{
|
||||
Name: fmt.Sprintf("%s Temperature", sensor.Name),
|
||||
StateTopic: fmt.Sprintf("telldus/sensor/%s/%s/%d/temperature", sensor.Protocol, sensor.Model, sensor.ID),
|
||||
UniqueID: sensor.TemperatureUniqueID,
|
||||
UnitOfMeasurement: "°C",
|
||||
DeviceClass: "temperature",
|
||||
StateClass: "measurement",
|
||||
Device: sensorDevice,
|
||||
}
|
||||
|
||||
payload, err := json.Marshal(discovery)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal temperature discovery: %w", err)
|
||||
}
|
||||
|
||||
topic := fmt.Sprintf("homeassistant/sensor/%s/config", sensor.TemperatureUniqueID)
|
||||
payload := fmt.Sprintf(`{
|
||||
"name": "%s Temperature",
|
||||
"state_topic": "telldus/sensor/%s/%s/%d/temperature",
|
||||
"unit_of_measurement": "°C",
|
||||
"device_class": "temperature",
|
||||
"unique_id": "%s",
|
||||
"device": {
|
||||
"identifiers": ["telldus_sensor_%s_%s_%d"],
|
||||
"name": "%s",
|
||||
"manufacturer": "Telldus"
|
||||
}
|
||||
}`, sensor.Name, sensor.Protocol, sensor.Model, sensor.ID, sensor.TemperatureUniqueID,
|
||||
sensor.Protocol, sensor.Model, sensor.ID, sensor.Name)
|
||||
c.client.Publish(topic, 0, true, payload)
|
||||
}
|
||||
|
||||
if dataTypes&telldus.DataTypeHumidity != 0 && sensor.HumidityUniqueID != "" {
|
||||
discovery := SensorDiscovery{
|
||||
Name: fmt.Sprintf("%s Humidity", sensor.Name),
|
||||
StateTopic: fmt.Sprintf("telldus/sensor/%s/%s/%d/humidity", sensor.Protocol, sensor.Model, sensor.ID),
|
||||
UniqueID: sensor.HumidityUniqueID,
|
||||
UnitOfMeasurement: "%",
|
||||
DeviceClass: "humidity",
|
||||
StateClass: "measurement",
|
||||
Device: sensorDevice,
|
||||
}
|
||||
|
||||
payload, err := json.Marshal(discovery)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal humidity discovery: %w", err)
|
||||
}
|
||||
|
||||
topic := fmt.Sprintf("homeassistant/sensor/%s/config", sensor.HumidityUniqueID)
|
||||
payload := fmt.Sprintf(`{
|
||||
"name": "%s Humidity",
|
||||
"state_topic": "telldus/sensor/%s/%s/%d/humidity",
|
||||
"unit_of_measurement": "%%",
|
||||
"device_class": "humidity",
|
||||
"unique_id": "%s",
|
||||
"device": {
|
||||
"identifiers": ["telldus_sensor_%s_%s_%d"],
|
||||
"name": "%s",
|
||||
"manufacturer": "Telldus"
|
||||
}
|
||||
}`, sensor.Name, sensor.Protocol, sensor.Model, sensor.ID, sensor.HumidityUniqueID,
|
||||
sensor.Protocol, sensor.Model, sensor.ID, sensor.Name)
|
||||
c.client.Publish(topic, 0, true, payload)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user