tradfri

package module
v0.0.0-...-afd32d4 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2019 License: MIT Imports: 12 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-tradfri

Go SDK for IKEA TRÅDFRI Gateway using mDNS, DTLS and CoAP

Features

  • discover gateways using mDNS
  • exchange pre-shared-key using security code
  • gateway info
  • list devices, get device details, update light & outlet settings
  • list groups, get group details
  • list scenes, get scene details

Libraries

References

Examples

discover gatways in network

func main() {
	addresses, err := tradfri.Discover()

	for _, address := range addresses {
		log.WithFields(log.Fields{
			"address": address,
		}).Info("discovered")
    }
}

get and store pre-shared-key

func main() {
    psk, err := psk(address, clientID, securityCode)
    
    client, err := tradfri.New(address, clientID, psk)    
    defer client.Close()
}

func psk(address, clientID, securityCode string) ([]byte, error) {
	filename := ".tradfri_" + clientID

	if data, err := ioutil.ReadFile(filename); err == nil {
		return data, nil
	}

	psk, err := tradfri.PSK(address, clientID, securityCode)

	if err != nil {
		return nil, err
	}

	if err := ioutil.WriteFile(filename, []byte(psk), 0644); err != nil {
		return nil, err
	}

	return psk, nil
}

set color of a specific bulb

func main() {
    client, err := tradfri.New(address, clientID, psk)    
    defer client.Close()

	color := "dc4b31"

    client.UpdateDevice(deviceID, tradfri.DeviceSettings{
	    LightSettings: []tradfri.LightSettings{
            tradfri.LightSettings{
                Color: &color,
            },
        },
    })
}

turn off specific outlet

func main() {
	client, err := tradfri.New(address, clientID, psk)    
	defer client.Close()

	power := 0
	
	client.UpdateDevice(deviceID, tradfri.DeviceSettings{
	 	OutletSettings: []tradfri.OutletSettings{
	 		tradfri.OutletSettings{
	 			Power: &power,
	 		},
	 	},
	})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Discover

func Discover() ([]string, error)

func PSK

func PSK(address, id, code string) ([]byte, error)

Types

type DeviceInfo

type DeviceInfo struct {
	ID   int    `json:"9003"` // Device ID
	Name string `json:"9001"` // Device Name

	Type DeviceType `json:"5750"` // Application Type

	Metadata struct {
		Manufacturer string      `json:"0"`
		Model        string      `json:"1"`
		Serial       string      `json:"2"`
		Firmware     string      `json:"3"`
		PowerSource  PowerSource `json:"6"`
	} `json:"3"`

	Speaker *struct {
		Devices []string `json:"9115"`
	} `json:"15017"`

	LightSettings   []LightSettings   `json:"3311"`
	OutletSettings  []OutletSettings  `json:"3312"`
	SwitchSettings  []SwitchSettings  `json:"15009"`
	SpeakerSettings []SpeakerSettings `json:"15018"`

	EpochCreated int64 `json:"9002"`
	EpochUpdated int64 `json:"9020"`
}

func (*DeviceInfo) CreatedAt

func (d *DeviceInfo) CreatedAt() time.Time

func (*DeviceInfo) UpdatedAt

func (d *DeviceInfo) UpdatedAt() time.Time

type DeviceSettings

type DeviceSettings struct {
	LightSettings  []LightSettings  `json:"3311,omitempty"`
	OutletSettings []OutletSettings `json:"3312,omitempty"`
}

type DeviceType

type DeviceType int
const DeviceTypeBulb DeviceType = 2
const DeviceTypeControlOutlet DeviceType = 3
const DeviceTypeSoundRemote DeviceType = 8
const DeviceTypeSwitch DeviceType = 0

type DiscoveryInfo

type DiscoveryInfo struct {
	Name string
	Host string

	Address string
}

type GatewayInfo

type GatewayInfo struct {
	ID string `json:"9081"`

	Firmware string `json:"9029"`

	Time       time.Time `json:"9060"`
	TimeServer string    `json:"9023"`

	EpochNow     int64 `json:"9059"`
	EpochCreated int64 `json:"9069"`
}

func (*GatewayInfo) CreatedAt

func (g *GatewayInfo) CreatedAt() time.Time

func (*GatewayInfo) Now

func (g *GatewayInfo) Now() time.Time

type GroupInfo

type GroupInfo struct {
	ID   int    `json:"9003"` // Group ID
	Name string `json:"9001"` // Group Name

	EpochCreated int64 `json:"9002"`
}

func (*GroupInfo) CreatedAt

func (g *GroupInfo) CreatedAt() time.Time

type LightSettings

type LightSettings struct {
	Power  *int `json:"5850,omitempty"`
	Dimmer *int `json:"5851,omitempty"`

	ColorHue        *int    `json:"5707,omitempty"`
	ColorSaturation *int    `json:"5708,omitempty"`
	ColorX          *int    `json:"5709,omitempty"`
	ColorY          *int    `json:"5710,omitempty"`
	Color           *string `json:"5706,omitempty"`

	//ColorTemperature *int    `json:"5711,omitempty"`
	Duration *int `json:"5712,omitempty"`

	Device *int `json:"9003,omitempty"`
}

type OutletSettings

type OutletSettings struct {
	Power  *int `json:"5850,omitempty"`
	Dimmer *int `json:"5851,omitempty"`

	Device *int `json:"9003,omitempty"`
}

type PowerSource

type PowerSource int
const PowerSourceAC PowerSource = 6
const PowerSourceBattery PowerSource = 3
const PowerSourceBatteryExternal PowerSource = 2
const PowerSourceBatteryInternal PowerSource = 1
const PowerSourceDC PowerSource = 0
const PowerSourceEthernet PowerSource = 4
const PowerSourceSolar PowerSource = 7
const PowerSourceUSB PowerSource = 5

type SceneInfo

type SceneInfo struct {
	ID   int    `json:"9003"` // Group ID
	Name string `json:"9001"` // Group Name

	EpochCreated int64 `json:"9002"`

	LightSettings []LightSettings `json:"15013"`
}

func (*SceneInfo) CreatedAt

func (s *SceneInfo) CreatedAt() time.Time

type SpeakerSettings

type SpeakerSettings struct {
	Device *int `json:"9003,omitempty"`
}

type SwitchSettings

type SwitchSettings struct {
	Device *int `json:"9003,omitempty"`
}

type Tradfri

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

func New

func New(address, id string, psk []byte) (*Tradfri, error)

func (*Tradfri) Close

func (t *Tradfri) Close()

func (*Tradfri) Device

func (t *Tradfri) Device(deviceID int) (*DeviceInfo, error)

func (*Tradfri) Devices

func (t *Tradfri) Devices() ([]int, error)

func (*Tradfri) GetAsJson

func (t *Tradfri) GetAsJson(path string, out interface{}) error

func (*Tradfri) Group

func (t *Tradfri) Group(groupID int) (*GroupInfo, error)

func (*Tradfri) Groups

func (t *Tradfri) Groups() ([]int, error)

func (*Tradfri) Info

func (t *Tradfri) Info() (*GatewayInfo, error)

func (*Tradfri) MessageID

func (t *Tradfri) MessageID() uint16

func (*Tradfri) PutJsonChange

func (t *Tradfri) PutJsonChange(path string, data interface{}) error

func (*Tradfri) RoundTrip

func (t *Tradfri) RoundTrip(request coap.Message) (*coap.Message, error)

func (*Tradfri) Scene

func (t *Tradfri) Scene(groupID, sceneID int) (*SceneInfo, error)

func (*Tradfri) Scenes

func (t *Tradfri) Scenes(groupID int) ([]int, error)

func (*Tradfri) UpdateDevice

func (t *Tradfri) UpdateDevice(deviceID int, settings DeviceSettings) error

Jump to

Keyboard shortcuts

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