goham

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2023 License: MIT Imports: 5 Imported by: 1

README

Goham

Goham is a Go module that provides an easy-to-use interface for creating MQTT sensors and publishing sensor data to a MQTT broker. The module is compatible with the Home Assistant autodiscovery feature, allowing the automatic creation of sensors in Home Assistant based on the published data.

Installation

To install the Goham module, run the following command:

go get -u github.com/mfmayer/goham

Usage

Here's a basic example of how to use the Goham module to create a MQTT sensor and publish sensor data:

package main

import (
	mqtt "github.com/eclipse/paho.mqtt.golang"
	"github.com/mfmayer/goham"
)

func main() {
	// Connect to the MQTT broker
	opts := mqtt.NewClientOptions().AddBroker("tcp://yourmqttbroker:1883")
	client := mqtt.NewClient(opts)
	token := client.Connect()
	token.Wait()

	// Create a publisher function that matches the MQTTPublisher interface
	mqttPublisher := goham.MQTTPublisherFunc(func(topic string, qos byte, retained bool, payload interface{}) {
		token := client.Publish(topic, qos, retained, payload)
    token.Wait()
	})

	// Create a new MQTT sensor
	sensor := goham.NewMQTTSensor(mqttPublisher, "kitchen")

	// Add a sensor value with a device class
	value := sensor.AddValue("temperature", goham.WithDeviceClass("temperature"))

	// Update and publish the sensor value
	value.Update(25.0)
}

Replace yourmqttbroker with the address of your MQTT broker and yourusername with your GitHub username or the username of the repository owner.

For more examples and available options, please refer to the source code and comments.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithDeviceClass

func WithDeviceClass(deviceClass string) addValueOption

WithDeviceClass to set a device class for the sensor value (see: https://www.home-assistant.io/integrations/sensor/#device-class)

func WithIcon

func WithIcon(icon string) addValueOption

WithUnitAndIcon can be used alternatively to WithDeviceClass to add a custom unit and icon

func WithSensorID

func WithSensorID(id string) newMQTTSensorOption

func WithSensorValueID

func WithSensorValueID(id string) addValueOption

func WithUnitOfMeasurement

func WithUnitOfMeasurement(unitOfMeasurement string) addValueOption

WithUnitAndIcon can be used alternatively to WithDeviceClass to add a custom unit and icon

Types

type MQTTPublisher

type MQTTPublisher interface {
	Publish(topic string, qos byte, retained bool, payload interface{})
}

type MQTTPublisherFunc

type MQTTPublisherFunc func(topic string, qos byte, retained bool, payload interface{})

MQTTPublisherFunc type is an adapter to allow the use of ordinary functions as MQTT publisher. If f is a fuction with the appropriate signature, MQTTPublisherFunc(f) is a MQTTPublisher that calls f

func (MQTTPublisherFunc) Publish

func (f MQTTPublisherFunc) Publish(topic string, qos byte, retained bool, payload interface{})

Publish calls f(topic, qos, retained, payload)

type MQTTSensor

type MQTTSensor struct {
	// contains filtered or unexported fields
}

func NewMQTTSensor

func NewMQTTSensor(client MQTTPublisher, sensorName string, opts ...newMQTTSensorOption) *MQTTSensor

func (*MQTTSensor) AddValue

func (ms *MQTTSensor) AddValue(valueName string, opts ...addValueOption) MQTTSensorValue

AddValue adds a new mqtt value to the mqtt sensor

func (MQTTSensor) ID

func (s MQTTSensor) ID() string

ID returns sensor's id

func (MQTTSensor) Name

func (s MQTTSensor) Name() string

Name returns sensor's name

type MQTTSensorValue

type MQTTSensorValue interface {
	SensorValue
	// Update and publish a sensor value
	Update(float64)
	// PublishConfig sends autodiscover configuration that can be understood by home assistant
	PublishConfig()
}

type Sensor

type Sensor interface {
	// Name returns the sensor name
	Name() string
	// ID returns the sensor id (consists only of the following characters: [a-zA-Z0-9_-])
	ID() string
}

Sensor interface

type SensorValue

type SensorValue interface {
	// Name should return a combination of sensor name and value name in a readable form
	Name() string
	// ID should return a combination of sensor name and value name using [a-zA-Z0-9_-] (alphanumerics, underscore and hyphen)
	ID() string
}

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL