update more stuff
This commit is contained in:
159
mqtt/discovery.go
Normal file
159
mqtt/discovery.go
Normal file
@@ -0,0 +1,159 @@
|
||||
package mqtt
|
||||
|
||||
import (
|
||||
"app/datastore"
|
||||
"app/telldus"
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
// 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"`
|
||||
}
|
||||
|
||||
// SensorDiscovery represents Home Assistant sensor discovery payload
|
||||
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"`
|
||||
}
|
||||
|
||||
// PublishAllDiscovery publishes Home Assistant discovery messages for all devices and sensors
|
||||
func (c *Client) PublishAllDiscovery() error {
|
||||
if err := c.publishDeviceDiscovery(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.publishSensorDiscovery(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// publishDeviceDiscovery publishes discovery for all devices
|
||||
func (c *Client) publishDeviceDiscovery() error {
|
||||
for device := range c.store.ListDevices() {
|
||||
if err := c.PublishDeviceDiscovery(device.ID); err != nil {
|
||||
log.Printf("Error publishing discovery for device %d: %v", device.ID, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// PublishDeviceDiscovery publishes Home Assistant discovery for a single device
|
||||
func (c *Client) PublishDeviceDiscovery(deviceID int) error {
|
||||
device, err := c.store.GetDevice(deviceID)
|
||||
if err != nil {
|
||||
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)
|
||||
|
||||
c.client.Publish(topic, 0, true, payload)
|
||||
return nil
|
||||
}
|
||||
|
||||
// publishSensorDiscovery publishes discovery for all sensors
|
||||
func (c *Client) publishSensorDiscovery() error {
|
||||
var protocol, model string
|
||||
var id, dataTypes int
|
||||
ret := telldus.Sensor(&protocol, &model, &id, &dataTypes)
|
||||
|
||||
for ret == 0 {
|
||||
sensor, err := c.store.GetSensorByIdentity(protocol, model, id)
|
||||
if err != nil || sensor.Hidden {
|
||||
log.Printf("Sensor %s %s %d not in DB or hidden, skipping", protocol, model, id)
|
||||
ret = telldus.Sensor(&protocol, &model, &id, &dataTypes)
|
||||
continue
|
||||
}
|
||||
|
||||
if err := c.publishSensorDiscoveryForSensor(sensor, dataTypes); err != nil {
|
||||
log.Printf("Error publishing discovery for sensor %s %s %d: %v", protocol, model, id, err)
|
||||
}
|
||||
|
||||
ret = telldus.Sensor(&protocol, &model, &id, &dataTypes)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// PublishSensorDiscovery publishes Home Assistant discovery for a single sensor
|
||||
func (c *Client) PublishSensorDiscovery(protocol, model string, id int) error {
|
||||
sensor, err := c.store.GetSensorByIdentity(protocol, model, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("sensor %s %s %d not found: %w", protocol, model, id, err)
|
||||
}
|
||||
|
||||
// Get current data types from telldus
|
||||
var p, m string
|
||||
var sensorID, dataTypes int
|
||||
ret := telldus.Sensor(&p, &m, &sensorID, &dataTypes)
|
||||
|
||||
// Find matching sensor
|
||||
for ret == 0 {
|
||||
if p == protocol && m == model && sensorID == id {
|
||||
return c.publishSensorDiscoveryForSensor(sensor, dataTypes)
|
||||
}
|
||||
ret = telldus.Sensor(&p, &m, &sensorID, &dataTypes)
|
||||
}
|
||||
|
||||
return fmt.Errorf("sensor %s %s %d not found in telldus", protocol, model, id)
|
||||
}
|
||||
|
||||
// publishSensorDiscoveryForSensor publishes discovery messages for a sensor's data types
|
||||
func (c *Client) publishSensorDiscoveryForSensor(sensor *datastore.Sensor, dataTypes int) error {
|
||||
if dataTypes&telldus.DataTypeTemperature != 0 && sensor.TemperatureUniqueID != "" {
|
||||
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 != "" {
|
||||
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)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
102
mqtt/mqtt.go
Normal file
102
mqtt/mqtt.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package mqtt
|
||||
|
||||
import (
|
||||
"app/datastore"
|
||||
"app/telldus"
|
||||
"fmt"
|
||||
|
||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
)
|
||||
|
||||
// Client handles all MQTT operations
|
||||
type Client struct {
|
||||
client mqtt.Client
|
||||
store *datastore.DataStore
|
||||
subscriptions []string
|
||||
}
|
||||
|
||||
// Config holds MQTT connection configuration
|
||||
type Config struct {
|
||||
BrokerURL string
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
|
||||
// New creates a new MQTT client
|
||||
func New(cfg Config, store *datastore.DataStore) (*Client, error) {
|
||||
opts := mqtt.NewClientOptions().AddBroker(cfg.BrokerURL)
|
||||
opts.SetUsername(cfg.Username)
|
||||
opts.SetPassword(cfg.Password)
|
||||
|
||||
client := mqtt.NewClient(opts)
|
||||
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
||||
return nil, token.Error()
|
||||
}
|
||||
|
||||
return &Client{
|
||||
client: client,
|
||||
store: store,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Close disconnects the MQTT client
|
||||
func (c *Client) Close() {
|
||||
c.client.Disconnect(250)
|
||||
}
|
||||
|
||||
// PublishDeviceState publishes a device state change
|
||||
func (c *Client) PublishDeviceState(deviceID int, state string) {
|
||||
topic := fmt.Sprintf("telldus/device/%d/state", deviceID)
|
||||
c.client.Publish(topic, 0, false, state)
|
||||
}
|
||||
|
||||
// PublishSensorValue publishes a sensor value
|
||||
func (c *Client) PublishSensorValue(protocol, model string, id int, dataType int, value string) {
|
||||
var topic string
|
||||
switch dataType {
|
||||
case telldus.DataTypeTemperature:
|
||||
topic = fmt.Sprintf("telldus/sensor/%s/%s/%d/temperature", protocol, model, id)
|
||||
case telldus.DataTypeHumidity:
|
||||
topic = fmt.Sprintf("telldus/sensor/%s/%s/%d/humidity", protocol, model, id)
|
||||
default:
|
||||
return
|
||||
}
|
||||
c.client.Publish(topic, 0, false, value)
|
||||
}
|
||||
|
||||
// UnsubscribeFromDeviceCommands unsubscribes from all tracked command topics
|
||||
func (c *Client) UnsubscribeFromDeviceCommands() {
|
||||
if len(c.subscriptions) > 0 {
|
||||
if token := c.client.Unsubscribe(c.subscriptions...); token.Wait() && token.Error() != nil {
|
||||
// Log error but don't fail
|
||||
}
|
||||
c.subscriptions = nil
|
||||
}
|
||||
}
|
||||
|
||||
// SubscribeToDeviceCommands subscribes to command topics for all devices
|
||||
func (c *Client) SubscribeToDeviceCommands() error {
|
||||
// Unsubscribe from existing subscriptions first
|
||||
c.UnsubscribeFromDeviceCommands()
|
||||
|
||||
numDevices := telldus.GetNumberOfDevices()
|
||||
for i := 0; i < numDevices; i++ {
|
||||
deviceID := telldus.GetDeviceId(i)
|
||||
topic := fmt.Sprintf("telldus/device/%d/set", deviceID)
|
||||
|
||||
// Capture deviceID in closure
|
||||
id := deviceID
|
||||
c.client.Subscribe(topic, 0, func(client mqtt.Client, msg mqtt.Message) {
|
||||
payload := string(msg.Payload())
|
||||
if payload == "ON" {
|
||||
telldus.TurnOn(id)
|
||||
} else if payload == "OFF" {
|
||||
telldus.TurnOff(id)
|
||||
}
|
||||
})
|
||||
|
||||
// Track subscription
|
||||
c.subscriptions = append(c.subscriptions, topic)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user