Giter VIP home page Giter VIP logo

zigbee-steward's Introduction

ZigBee-Steward

Build Status

Overview

Bla bla bla

Example

In this example, we are going to send toggle command to IKEA TRÅDFRI bulb using Xiaomi Aqara Wireless Remote Switch. To prepare, follow the steps:

  1. Connect ZigBee stick cc2531 to USB;
  2. Flash it using instruction: https://www.zigbee2mqtt.io/getting_started/flashing_the_cc2531.html;
  3. Run the example;
  4. Pair IKEA TRÅDFRI bulb by repeating 6 ONs and 5 OFFs one by one (ON -> OFF, ON -> OFF, ON -> OFF, ON -> OFF, ON -> OFF, ON);
  5. Pair Xiaomi Aqara Wireless Remote Switch by holding the button for ~10 seconds until blue LEDs start blinking.

Now you can toggle the bulb using the remote switch. Just click on it.

import (
	"fmt"
	"github.com/davecgh/go-spew/spew"
	"github.com/dyrkin/zcl-go/cluster"
	"github.com/dyrkin/zigbee-steward"
	"github.com/dyrkin/zigbee-steward/configuration"
	"github.com/dyrkin/zigbee-steward/model"
	"sync"
)

//simple device database
var devices = map[string]*model.Device{}

func main() {

	conf := configuration.Default()
	conf.PermitJoin = true

	stewie := steward.New(conf)

	eventListener := func() {
		for {
			select {
			case device := <-stewie.Channels().OnDeviceRegistered():
				saveDevice(device)
			case device := <-stewie.Channels().OnDeviceUnregistered():
				deleteDevice(device)
			case device := <-stewie.Channels().OnDeviceBecameAvailable():
				saveDevice(device)
			case deviceIncomingMessage := <-stewie.Channels().OnDeviceIncomingMessage():
				fmt.Printf("Device received incoming message:\n%s", spew.Sdump(deviceIncomingMessage))
				toggleIkeaBulb(stewie, deviceIncomingMessage)
			}
		}
	}

	go eventListener()
	stewie.Start()
	infiniteWait()
}

func toggleIkeaBulb(stewie *steward.Steward, message *model.DeviceIncomingMessage) {
	if isXiaomiButtonSingleClick(message) {
		if ikeaBulb, registered := devices["TRADFRI bulb E27 W opal 1000lm"]; registered {
			toggleTarget(stewie, ikeaBulb.NetworkAddress)
		} else {
			fmt.Println("IKEA bulb is not available")
		}
	}
}

func toggleTarget(stewie *steward.Steward, networkAddress string) {
	go func() {
		stewie.Functions().Cluster().Local().OnOff().Toggle(networkAddress, 0xFF)
	}()
}

func isXiaomiButtonSingleClick(message *model.DeviceIncomingMessage) bool {
	command, ok := message.IncomingMessage.Data.Command.(*cluster.ReportAttributesCommand)

	return ok && message.Device.Manufacturer == "LUMI" &&
		message.Device.Model == "lumi.remote.b186acn01\x00\x00\x00" &&
		isSingleClick(command)
}

func isSingleClick(command *cluster.ReportAttributesCommand) bool {
	click, ok := command.AttributeReports[0].Attribute.Value.(uint64)
	return ok && click == uint64(1)
}

func saveDevice(device *model.Device) {
	fmt.Printf("Registering device:\n%s", spew.Sdump(device))
	devices[device.Model] = device
}

func deleteDevice(device *model.Device) {
	fmt.Printf("Unregistering device:\n%s", spew.Sdump(device))
	delete(devices, device.Model)
}

func infiniteWait() {
	wg := &sync.WaitGroup{}
	wg.Add(1)
	wg.Wait()
}

Full examples

zigbee-steward's People

Contributors

dyrkin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

zigbee-steward's Issues

Pair errors

Hi
Trying new version and can't pair devices:
10:45:47.069 ERROR steward Received message from unknown device [0x1704]
10:45:53.133 ERROR coordinator invalid transcation status: [StatusMacTransactionExpired]. Retries: 3
10:46:00.133 ERROR coordinator invalid transcation status: [StatusMacTransactionExpired]. Retries: 2
10:46:07.133 ERROR coordinator invalid transcation status: [StatusMacTransactionExpired]. Retries: 1
10:46:14.133 ERROR coordinator failure: invalid transcation status: [StatusMacTransactionExpired]
10:46:14.133 ERROR steward Unable to register device: invalid transcation status: [StatusMacTransactionExpired]

Tried different xiaomi devices, but always got this errors

Contributions and Licence?

Hello Dyrkin!

After doing some initial looking into zigbee2mqtt, I found your suite of Go libraries for powering the CC2531 USB dongle and it works much better than the z2m in a lot of scenarios (especially pairing devices).

I've done some work on zcl-go (adding more clusters) and I've started on some changes to zigbee-steward (non singleton database, more functions for clusters, etc).

It'd be great to get these back to you so others might be able to benefit from them, and I can stop maintaining nasty replace directives in my go.mod files ;).

If I raised a PR would you consider merging them in? And while I'm not hugely fussed about which open source license, it'd be good to have one attached to the projects to make it clear about the state of contributions, etc. For unp-go, znp-go, zcl-go and zigbee-steward.

I also appreciate that you've not commited to these for 8 months or so, I guess you may not be using them? Would you appreciate a helping hand in maintaining them?

FWIW, my interest is in improving my own home Zigbee ecosystem, dumping the Phillips Hue bridge and interfacing a lot of the lower cost sensors from the Chinese vendors along side the nicer Hue/Tradfri bulbs.

Cheers,

Peter.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.