yeelight

package
v0.0.0-...-6312dd6 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2022 License: MIT Imports: 12 Imported by: 0

README

Please pay attention that in January Yeelight disabled some LAN features. This library like all similar will work only with the older version of the device firmware. In my case on the version, v2.0.6_0041 discovery doesn't work.

Right now I don't have information on when it will be back. You can find more information here https://forum.yeelight.com/t/topic/22664/127

Installation SDK

Yeelight SDK writen on Golang. Package supports 90% (for now) off all features present in the official documentation.

Installation
go get github.com/oherych/yeelight
Example
package main

import (
	"context"
	"errors"
	"fmt"
	"log"
	"time"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
	defer cancel()

	devices, err := yeelight.Discovery(ctx)
	if err != nil && !errors.Is(err, context.DeadlineExceeded) {
		log.Fatalln(err)
	}

	for _, device := range devices {
		fmt.Println(`------`)
		fmt.Printf("Device '%s' [ID:%s Version:%s]\n", device.Name, device.ID, device.FirmwareVersion)
		fmt.Printf("Adress: %s\n", device.Location)
		fmt.Printf("Power: %s\n", power(device.Power))

		// create new client for work with device
		client := yeelight.New(device.Location)

		// read all properties
		properties, err := client.GetProperties(context.Background(), []string{yeelight.PropertyPower, yeelight.PropertyColorMode, yeelight.PropertyBright})
		if err != nil {
			log.Fatalln(err)
		}

		fmt.Println("Properties:")
		for name, value := range properties {
			fmt.Println("> ", name, ":", value)
		}

		// change power to ON
		err = client.Power(context.Background(), true, yeelight.PowerModeDefault, yeelight.EffectSudden, time.Second)
		if err != nil {
			log.Fatalln(err)
		}
	}
}

func power(on bool) string {
	if on {
		return "ON"
	}

	return "OFF"
}
Output
------
Device 'my_super' [ID:0x00000000157ef201 Version:20]
Address: 192.168.1.79:55443
Power: OFF
Properties:
>  bright : 1
>  power : on
>  color_mode : 1
Contributing

Unfortunately, I have only one device and I don't have it possible to test all features. I will be grateful for the help with testing and feedback. This is the main priority for the current period.

TODO
  • Implement methods set_scene and bg_set_scene
  • Implement updates listener

Documentation

Index

Constants

View Source
const (
	MethodGetProp               = "get_prop"
	MethodSetColorTemperature   = "set_ct_abx"
	MethodSetRGB                = "set_rgb"
	MethodSetHSV                = "set_hsv"
	MethodSetBright             = "set_bright"
	MethodSetPower              = "set_power"
	MethodToggle                = "toggle"
	MethodSetDefault            = "set_default"
	MethodStartCF               = "start_cf"
	MethodStopCF                = "stop_cf"
	MethodSetScene              = "set_scene" // TODO: implement me
	MethodCronAdd               = "cron_add"
	MethodCronGet               = "cron_get"
	MethodCronDelete            = "cron_del"
	MethodSetAdjust             = "set_adjust"
	MethodSetMusic              = "set_music"
	MethodSetName               = "set_name"
	MethodSetBgRGB              = "bg_set_rgb"
	MethodSetBgHSV              = "bg_set_hsv"
	MethodSetBgColorTemperature = "bg_set_ct_abx"
	MethodBgStartCF             = "bg_start_cf"
	MethodBgStopCF              = "bg_stop_cf"
	MethodBgSetScene            = "bg_set_scene" // TODO: implement me
	MethodBgSetDefault          = "bg_set_default"
	MethodBgSetPower            = "bg_set_power"
	MethodSetBgBright           = "bg_set_bright"
	MethodSetBgAdjust           = "bg_set_adjust"
	MethodBgToggle              = "bg_toggle"
	MethodDevToggle             = "dev_toggle"
	MethodAdjustBright          = "adjust_bright"
	MethodAdjustCt              = "adjust_ct"
	MethodAdjustColor           = "adjust_color"
	MethodBgAdjustBright        = "bg_adjust_bright"
	MethodBgAdjustCt            = "bg_adjust_ct"
	MethodBgAdjustColor         = "bg_adjust_color"
)

list of all supported methods

View Source
const (
	PropertyPower        = "power"
	PropertyBright       = "bright"
	PropertyCT           = "ct"
	PropertyRGB          = "rgb"
	PropertyHue          = "hue"
	PropertySat          = "sat"
	PropertyColorMode    = "color_mode"
	PropertyFlowing      = "flowing"
	PropertyDelayOff     = "delayoff"
	PropertyFlowParams   = "flow_params"
	PropertyMusicOn      = "music_on"
	PropertyName         = "name"
	PropertyBgPower      = "bg_power"
	PropertyBgFlowing    = "bg_flowing"
	PropertyBgFlowParams = "bg_flow_params"
	PropertyBgCt         = "bg_ct"
	PropertyBgLMode      = "bg_lmode" // TODO: is this property correct?
	PropertyBgBright     = "bg_bright"
	PropertyBgRgb        = "bg_rgb"
	PropertyBgHue        = "bg_hue"
	PropertyBgSat        = "bg_sat"
	PropertyNlBr         = "nl_br"
	PropertyActiveMode   = "active_mode"
	BrightWithZero       = "bright_with_zero"
)

Properties

View Source
const (
	MinDuration = 30 * time.Millisecond

	MinAdjustPercentage = -100
	MaxAdjustPercentage = 100

	MinRGB = 0
	MaxRGB = 16777215 // (hex: 0xFFFFFF)

	MinHue = 0
	MaxHue = 359

	MinSat = 0
	MaxSat = 100

	MinBright = 0
	MaxBright = 100

	MinColorTemperature = 1700
	MaxColorTemperature = 6500
)

Variables

View Source
var (
	// ErrWrongNumberOfResultItems says that response has wrong number of result items
	ErrWrongNumberOfResultItems = errors.New("wrong number of result items")

	// ErrCronIsUnset says that cron is unset
	ErrCronIsUnset = errors.New("cron is unset")

	// ErrDurationTooSmall says that duration too small
	ErrDurationTooSmall = errors.New("duration too small")

	// ErrWrongAdjustPercentage says that wrong percentage
	ErrWrongAdjustPercentage = errors.New("wrong percentage")

	// ErrWrongRGBValue says that wrong RGB value
	ErrWrongRGBValue = errors.New("wrong RGB value")

	// ErrWrongHueValue says that wrong Hue value
	ErrWrongHueValue = errors.New("wrong Hue value")

	// ErrWrongSatValue says that wrong Sat value
	ErrWrongSatValue = errors.New("wrong Sat value")

	// ErrWrongBrightValue says that wrong Bright value
	ErrWrongBrightValue = errors.New("wrong Bright value")

	// ErrWrongColorTemperature says that wrong color temperature
	ErrWrongColorTemperature = errors.New("wrong color temperature")

	// ErrRequiredMinimumOneExpression says that required minimum one expression
	ErrRequiredMinimumOneExpression = errors.New("required minimum one expression")

	// ErrMissingPortInAddress says that missing port in address
	ErrMissingPortInAddress = errors.New("missing port in address")

	// ErrConnect says that connect error
	ErrConnect = errors.New("connect error")

	// ErrMethodNotSupported says that method not supported
	ErrMethodNotSupported = errors.New("method not supported")

	// ErrResponseJSONSyntax says that response json has syntax error
	ErrResponseJSONSyntax = errors.New("response json has syntax error")
)

Functions

func Properties

func Properties() []string

Properties return a list of all susceptible properties

func ValidateAdjustPercentage

func ValidateAdjustPercentage(value int) error

ValidateAdjustPercentage validate percentage for adjust_* methods validation rules in MinAdjustPercentage and MaxAdjustPercentage

func ValidateBright

func ValidateBright(value int) error

func ValidateColorTemperature

func ValidateColorTemperature(value int) error

func ValidateDuration

func ValidateDuration(duration time.Duration) error

ValidateDuration validate duration validation rules in MinDuration

func ValidateHue

func ValidateHue(value int) error

ValidateHue validate hue validation rules in MinHue and MaxHue

func ValidateRGB

func ValidateRGB(value int) error

ValidateRGB validate rgb validation rules in MinRGB and MaxRGB

func ValidateSat

func ValidateSat(value int) error

ValidateSat validate sat validation rules in MinSat and MaxSat

Types

type AdjustAction

type AdjustAction string

AdjustAction is enum of Adjust Actions

const (
	AdjustActionIncrease AdjustAction = "increase"
	AdjustActionDecrease AdjustAction = "decrease"
	AdjustActionCircle   AdjustAction = "circle"
)

Adjust Actions options

type AdjustProp

type AdjustProp string

AdjustProp is enum of Adjust Properties

const (
	AdjustPropBright           AdjustProp = "bright"
	AdjustPropColorTemperature AdjustProp = "ct"
	AdjustPropColor            AdjustProp = "color"
)

Adjust Properties options

type CallResponse

type CallResponse struct {
	ID     int             `json:"id"`
	Result json.RawMessage `json:"result"`
	Error  json.RawMessage `json:"error"`
}

CallResponse contains raw response from device.

func (CallResponse) Bind

func (rr CallResponse) Bind(target interface{}) error

Bind response result to provided variable

func (CallResponse) ToError

func (rr CallResponse) ToError() error

ToError checks if the answer contains an error.

type Client

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

Client isRaw instance of Yeelight SDK Please use New() for creating

func New

func New(host string) Client

New create new client

func (Client) AddCron

func (c Client) AddCron(ctx context.Context, on bool, timeout time.Duration) error

AddCron method isRaw used to start a timer job on the smart LED parameter `on` currently can only be false. (means power off) parameter `timeout` isRaw the length of the timer

func (Client) AdjustBackgroundBright

func (c Client) AdjustBackgroundBright(ctx context.Context, percentage int, duration time.Duration) error

AdjustBackgroundBright method is used to adjust the brightness by specified percentage within specified duration.

func (Client) AdjustBackgroundColor

func (c Client) AdjustBackgroundColor(ctx context.Context, percentage int, duration time.Duration) error

AdjustBackgroundColor method is used to adjust the color within specified duration.

func (Client) AdjustBackgroundColorTemperature

func (c Client) AdjustBackgroundColorTemperature(ctx context.Context, percentage int, duration time.Duration) error

AdjustBackgroundColorTemperature method is used to adjust the color temperature by specified percentage within specified duration.

func (Client) AdjustBright

func (c Client) AdjustBright(ctx context.Context, percentage int, duration time.Duration) error

AdjustBright method is used to adjust the brightness by specified percentage within specified duration.

func (Client) AdjustColor

func (c Client) AdjustColor(ctx context.Context, percentage int, duration time.Duration) error

AdjustColor method is used to adjust the color within specified duration.

func (Client) AdjustColorTemperature

func (c Client) AdjustColorTemperature(ctx context.Context, percentage int, duration time.Duration) error

AdjustColorTemperature method is used to adjust the color temperature by specified percentage within specified duration.

func (Client) BackgroundPower

func (c Client) BackgroundPower(ctx context.Context, on bool, mode PowerMode, effect Effect, duration time.Duration) error

BackgroundPower method isRaw used to switch on or off the smart LED (software managed on/off).

func (Client) BackgroundToggle

func (c Client) BackgroundToggle(ctx context.Context) error

BackgroundToggle method isRaw used to toggle the smart LED.

func (Client) Call

func (c Client) Call(ctx context.Context, method string, params ...interface{}) (CallResponse, error)

Call method of device with provided parameters

func (Client) DeleteCron

func (c Client) DeleteCron(ctx context.Context, on bool) error

DeleteCron method isRaw used to stop the specified cron job parameter `on` currently can only be false. (means power off)

func (Client) DevToggle

func (c Client) DevToggle(ctx context.Context) error

DevToggle method is used to toggle the main light and background light at the same time.

func (Client) GetCron

func (c Client) GetCron(ctx context.Context, on bool) (time.Duration, error)

GetCron method isRaw used to retrieve the setting of the current cron job of the specified type parameter `on` currently can only be false. (means power off)

func (Client) GetProperties

func (c Client) GetProperties(ctx context.Context, properties []string) (map[string]string, error)

GetProperties method isRaw used to retrieve current property of smart LED Arg `properties` isRaw a list of requested properties. List of all possible properties available in function Properties()

func (Client) Listen

func (c Client) Listen(ctx context.Context) (<-chan string, error)

func (Client) Power

func (c Client) Power(ctx context.Context, on bool, mode PowerMode, effect Effect, duration time.Duration) error

Power method isRaw used to switch on or off the smart LED (software managed on/off).

func (Client) SetAdjust

func (c Client) SetAdjust(ctx context.Context, action AdjustAction, prop AdjustProp) error

SetAdjust method is used to change brightness, CT or color of a smart LED without knowing the current value, it's main used by controllers.

func (Client) SetBackgroundAdjust

func (c Client) SetBackgroundAdjust(ctx context.Context, action AdjustAction, prop AdjustProp) error

SetBackgroundAdjust method is used to change brightness, CT or color of a smart LED without knowing the current value, it's main used by controllers.

func (Client) SetBackgroundBright

func (c Client) SetBackgroundBright(ctx context.Context, brightness int, effect Effect, duration time.Duration) error

SetBackgroundBright method isRaw used to change the brightness of a smart LED. "brightness" isRaw the target brightness. The type isRaw integer and ranges from 1 to 100. The brightness isRaw a percentage instead of a absolute value. 100 means maximum brightness while 1 means the minimum brightness.

func (Client) SetBackgroundColorTemperature

func (c Client) SetBackgroundColorTemperature(ctx context.Context, value int, effect Effect, duration time.Duration) error

SetBackgroundColorTemperature method isRaw used to change the color temperature of a smart LED. "value" isRaw the target color temperature. The type isRaw integer and range isRaw 1700 ~ 6500 (k).

func (Client) SetBackgroundDefault

func (c Client) SetBackgroundDefault(ctx context.Context) error

SetBackgroundDefault method isRaw used to save current state of smart LED in persistent memory. So if user powers off and then powers on the smart LED again (hard power reset), the smart LED will show last saved state.

func (Client) SetBackgroundHSV

func (c Client) SetBackgroundHSV(ctx context.Context, hue int, sat int, effect Effect, duration time.Duration) error

SetBackgroundHSV method isRaw used to change the color of a smart LED "hue" isRaw the target hue value, whose type isRaw integer. It should be expressed in decimal integer ranges from 0 to 359. "sat" isRaw the target saturation value whose type isRaw integer. It's range isRaw 0 to 100.

func (Client) SetBackgroundRGB

func (c Client) SetBackgroundRGB(ctx context.Context, value int, effect Effect, duration time.Duration) error

SetBackgroundRGB method isRaw used to change the color of a smart LED "value" isRaw the target color, whose type isRaw integer. It should be expressed in decimal integer ranges from 0 to 16777215 (hex: 0xFFFFFF)

func (Client) SetBright

func (c Client) SetBright(ctx context.Context, brightness int, effect Effect, duration time.Duration) error

SetBright method isRaw used to change the brightness of a smart LED. "brightness" isRaw the target brightness. The type isRaw integer and ranges from 1 to 100. The brightness isRaw a percentage instead of a absolute value. 100 means maximum brightness while 1 means the minimum brightness.

func (Client) SetColorTemperature

func (c Client) SetColorTemperature(ctx context.Context, value int, effect Effect, duration time.Duration) error

SetColorTemperature method isRaw used to change the color temperature of a smart LED. "value" isRaw the target color temperature. The type isRaw integer and range isRaw 1700 ~ 6500 (k).

func (Client) SetDefault

func (c Client) SetDefault(ctx context.Context) error

SetDefault method isRaw used to save current state of smart LED in persistent memory. So if user powers off and then powers on the smart LED again (hard power reset), the smart LED will show last saved state.

func (Client) SetHSV

func (c Client) SetHSV(ctx context.Context, hue int, sat int, effect Effect, duration time.Duration) error

SetHSV method isRaw used to change the color of a smart LED "hue" isRaw the target hue value, whose type isRaw integer. It should be expressed in decimal integer ranges from 0 to 359. "sat" isRaw the target saturation value whose type isRaw integer. It's range isRaw 0 to 100.

func (Client) SetMusic

func (c Client) SetMusic(ctx context.Context, on bool, musicHost string, port int) error

SetMusic method is used to start or stop music mode on a device. Under music mode, no property will be reported and no message quota is checked. "musicHost" the IP address of the music server. "port" the TCP port music application is listening on.

func (Client) SetName

func (c Client) SetName(ctx context.Context, name string) error

SetName method isRaw used to name the device. The name will be stored on the device and reported in discovering response.

func (Client) SetRGB

func (c Client) SetRGB(ctx context.Context, value int, effect Effect, duration time.Duration) error

SetRGB method isRaw used to change the color of a smart LED

func (Client) StartBackgroundColorFlow

func (c Client) StartBackgroundColorFlow(ctx context.Context, count int, action FlowAction, expressions []FlowExpression) error

StartBackgroundColorFlow method is used to start a color flow. Color flow is a series of smart LED visible state changing. It can be brightness changing, color changing or color temperature changing. `count` is the total number of visible state changing before color flow stopped. 0 means infinite loop on the state changing.

func (Client) StartColorFlow

func (c Client) StartColorFlow(ctx context.Context, count int, action FlowAction, expressions []FlowExpression) error

StartColorFlow method is used to start a color flow. Color flow is a series of smart LED visible state changing. It can be brightness changing, color changing or color temperature changing. `count` is the total number of visible state changing before color flow stopped. 0 means infinite loop on the state changing.

func (Client) StopBackgroundColorFlow

func (c Client) StopBackgroundColorFlow(ctx context.Context) error

StopBackgroundColorFlow method is used to stop a running color flow.

func (Client) StopColorFlow

func (c Client) StopColorFlow(ctx context.Context) error

StopColorFlow method is used to stop a running color flow.

func (Client) Toggle

func (c Client) Toggle(ctx context.Context) error

Toggle method isRaw used to toggle the smart LED.

type DiscoveryResultItem

type DiscoveryResultItem struct {
	ID              string
	Name            string
	Location        string
	Model           string
	FirmwareVersion string
	Support         []string
	Power           bool
}

DiscoveryResultItem contain information about result of discovery

func Discovery

func Discovery(ctx context.Context) (items []DiscoveryResultItem, err error)

Discovery scans the network for devices. This feature can`t stop itself. It MUST be stopped by context.

type Effect

type Effect string

Effect is enum of Effects

const (
	EffectSudden Effect = "sudden"
	EffectSmooth Effect = "smooth"
)

Effects

type FlowAction

type FlowAction int

FlowAction is enum of Flow Action

const (
	// FlowActionRecover means smart LED recover to the state before the color flow started.
	FlowActionRecover FlowAction = 0

	// FlowActionStay means smart LED stay at the state when the flow is stopped.
	FlowActionStay FlowAction = 1

	// FlowActionTurnOff means turn off the smart LED after the flow is stopped.
	FlowActionTurnOff FlowAction = 2
)

Color flow action

type FlowExpression

type FlowExpression struct {
	// Duration Gradual change time or sleep time. Minimum value 50 milliseconds.
	Duration time.Duration

	// Mode can be FlowModeColor, FlowColorTemperature, FlowSleep
	Mode FlowMode

	// Value is RGB value when mode is FlowModeColor, CT value when mode is FlowColorTemperature, Ignored when mode is FlowSleep.
	Value int

	// Brightness value, -1 or 1 ~ 100. Ignored when mode is FlowSleep.
	// When this value is -1, brightness in this tuple is ignored (only color or CT change takes effect).
	Brightness int
}

FlowExpression is the expression of the state changing series.

type FlowMode

type FlowMode int

FlowMode is enum of Flow Mode

const (
	FlowModeColor        FlowMode = 1
	FlowColorTemperature FlowMode = 2
	FlowSleep            FlowMode = 7
)

Color flow modes

type PowerMode

type PowerMode int

PowerMode is enum of Power Modes

const (
	PowerModeDefault         PowerMode = 0
	PowerModeCT              PowerMode = 1
	PowerModeRGB             PowerMode = 2
	PowerModeHSV             PowerMode = 3
	PowerModeColorFlow       PowerMode = 4
	PowerModeColorNightLight PowerMode = 5 // Ceiling light only
)

Power Modes options

type UnknownError

type UnknownError string

UnknownError contain raw unknown error

func (UnknownError) Error

func (e UnknownError) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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