yeelight

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2022 License: MIT Imports: 9 Imported by: 0

README

Yeelight Golang

A simple Golang library to control Xiaomi Yeelights device over LAN with TCP.

This solution offers a 1:1 implementation of the official docs from Xiaomi.

Installation

go get github.com/LordAur/yeelight

Usage

import "github.com/LordAur/yeelight"

func main() {
    y := yeelight.New(&yeelight.Config{
        IpAddress: "192.168.0.0",
        Port:      55443,
    })

    defer y.Close()

    r, err := y.GetProps("bright", "power", "ct")
    if err != nil {
        // ...
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Discovery

func Discovery(cidr string) ([]string, error)

This function is used to scan Xiaomi Yeelight device in your Local Area Network with CIDR IP Address.

func Listen

func Listen(ip string) (net.Conn, error)

This function is used to Listen Yeelight device by IP Address. You should use ReadMessage function to read tcp message.

Example:

conn, err := yeelight.Listen("192.168.100.7")
if err != nil {
	...
}

defer conn.Close()

for {
	_, err := yeelight.ReadMessage(conn)
	if err != nil {
		...
	}
}

Types

type Config

type Config struct {
	IpAddress string
	Port      int
	// contains filtered or unexported fields
}

func New

func New(c *Config) Config

func (*Config) AdjustBright

func (c *Config) AdjustBright(bright, duration int) (Response, error)

This function is used to adjust the brightness by specified bright percentage within specified duration. "bright" should fill with range -100 ~ 100. "duration" set the action duration with milisecond.

func (*Config) AdjustColorTemperature

func (c *Config) AdjustColorTemperature(bright, duration int) (Response, error)

This function is used to adjust the color temperature by specified bright percentage within specified duration. "bright" should fill with range -100 ~ 100. "duration" set the action duration with milisecond.

func (*Config) Close

func (c *Config) Close()

After you run the tcp you should close the tcp connection.

func (*Config) CronAdd

func (c *Config) CronAdd(timer int) (Response, error)

This function is used to added a cron job to turn off the lamp.

func (*Config) CronDelete

func (c *Config) CronDelete() (Response, error)

This function is used to delete cron job in queue.

func (*Config) CronGet

func (c *Config) CronGet() (Response, error)

This function is used to get cron jobs in queue.

func (*Config) GenerateRGB

func (c *Config) GenerateRGB(red, green, blue int) int

This function is used to generate RGB to decimal integer to represent the color. You should fill red, green and blue with integer.

func (*Config) GetProps

func (c *Config) GetProps(p ...interface{}) (Response, error)

This function is used to retrieve current property. The allowed values is "power", "bright", "ct", "rgb", "hue", "sat", "color_mode", "flowing", "delayoff", "flow_params", "music_on", "name", "bg_power", "bg_flowing", "bg_flow_params", "bg_ct", "bg_lmode", "bg_bright", "bg_rgb", "bg_hue", "bg_sat", "nl_br", "active_mode"

func (*Config) SetAdjust

func (c *Config) SetAdjust(action, prop string) (Response, error)

This function is used to adjust brightness, color tempterature or color. The allowed value "action" is increase, decrease and circle. The allowed value "prop" is bright, ct and color.

func (*Config) SetBright

func (c *Config) SetBright(brightness int, effect string, duration int) (Response, error)

This function is used to change the brightness. The allowed value brightness is in range 1 ~ 100. The allowed value effect is "sudden" and "smooth". For the duration action, it's should be more than 30 milliseconds.

func (*Config) SetColorFlow

func (c *Config) SetColorFlow(count, action int, exprs []FlowExpression) (Response, error)

This function is used to start a color flow. "count" is the number of visible state changing before color flow stopped. "action" is the action taken after the flow is stopped. 0 means smart LED recover to the state before the color flow started. 1 means smart LED stay at the state when the flow is stopped. 2 means turn off the smart LED after the flow is stopped.

"exprs" is the expression of the state changing series. Fill with "mode" 1 - color, 2 - color temperature, "duration" for the duration in milliseconds, "value" is following the "mode", color or color temperature.

func (*Config) SetColorTemp

func (c *Config) SetColorTemp(temp int, effect string, duration int) (Response, error)

This function is used to change the color temperature. The allowed value for temp is in range 1700 ~ 6500. The allowed value effect is "sudden" and "smooth". For the duration action, it's should be more than 30 milliseconds.

func (*Config) SetDefault

func (c *Config) SetDefault() (Response, error)

This function is used to save current state.

func (*Config) SetHueSaturation

func (c *Config) SetHueSaturation(hue int, sat int, effect string, duration int) (Response, error)

This function is used to change the color with hue and saturation. The allowed value hue is in range 0 ~ 359. The allowed value sat is in range 0 ~ 100. The allowed value effect is "sudden" and "smooth". For the duration action, it's should be more than 30 milliseconds.

func (*Config) SetName

func (c *Config) SetName(name string) (Response, error)

The function is used to change the device name, stored in device not cloud.

func (*Config) SetPower

func (c *Config) SetPower(power bool, effect string, duration int) (Response, error)

This function is used to switch on or off. The allowed value effect is "sudden" and "smooth". For the duration action, it's should be more than 30 milliseconds.

func (*Config) SetRGB

func (c *Config) SetRGB(red int, green int, blue int, effect string, duration int) (Response, error)

This function is used to change the color. The allowed value effect is "sudden" and "smooth". For the duration action, it's should be more than 30 milliseconds.

func (*Config) SetScene

func (c *Config) SetScene(scene Scene) (Response, error)

This function is used to set scene with color, hue saturation, color temperature or color flow.

func (*Config) StopColorFlow

func (c *Config) StopColorFlow() (Response, error)

The function is used to stop current color flow.

type FlowExpression

type FlowExpression struct {
	Duration   int
	Mode       int
	Value      int
	Brightness int
}

type ListenResponse

type ListenResponse struct {
	Method string `json:"method"`
	Params struct {
		Name             string `json:"name"`
		Power            string `json:"power"`
		ColorTemperature int    `json:"ct"`
		Brightness       int    `json:"bright"`
		Flowing          int    `json:"flowing"`
		FlowParams       string `json:"flow_params"`
		ColorMode        int    `json:"color_mode"`
		Rgb              int    `json:"rgb"`
		Hue              int    `json:"hue"`
		Saturation       int    `json:"sat"`
		DelayOff         int    `json:"delayoff"`
	} `json:"params"`
}

func ReadMessage

func ReadMessage(conn net.Conn) (ListenResponse, error)

This function is used to read TCP message from function Listen.

Example:

for {
	_, err := yeelight.ReadMessage(conn)
	if err != nil {
		...
	}
}

type Request

type Request struct {
	ID     int         `json:"id"`
	Method string      `json:"method"`
	Params interface{} `json:"params"`
}

type Response

type Response struct {
	ID     int           `json:"id"`
	Result []interface{} `json:"result"`
}

type Scene

type Scene struct {
	Action           string
	Color            int
	ColorTemperature int
	Hue              int
	Saturation       int
	ColorFlow        []FlowExpression
	Mode             int
	Brightness       int
	Duration         int
}

Jump to

Keyboard shortcuts

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