switchbot

package
v2.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package switchbot provides BLE Client for SwitchBot.

Codebase of this package is strongly referring to https://github.com/OpenWonderLabs/python-host

Example (Press)
package main

import (
	"context"
	"log"
	"os"
	"time"

	"github.com/yasuoza/switchbot-ble-go/v2/pkg/switchbot"
)

func main() {
	ctx := context.Background()
	timeout := 5 * time.Second

	// Scan SwitchBots.
	var addrs []string
	err := switchbot.Scan(ctx, timeout, func(addr string) {
		addrs = append(addrs, addr)
	})
	if err != nil {
		log.Fatal(err)
	}

	// If there is no SwitchBot, err is nil and length of addresses is 0.
	if len(addrs) == 0 {
		log.Println("SwitchBot not found")
		os.Exit(0)
	}

	// First, connect to SwitchBot.
	addr := addrs[0]
	log.Printf("Connecting to SwitchBot %s\n", addr)
	bot, err := switchbot.Connect(ctx, addr, timeout)
	if err != nil {
		log.Fatal(err)
	}

	// Trigger Press.
	log.Printf("Connected to SwitchBot %s. Trigger Press\n", addr)
	bot.Press(false)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Scan

func Scan(ctx context.Context, timeout time.Duration, callback func(addr string)) error

Scan scans nearby SwitchBots. Callback function will be executed with MAC address once a SwitchBot is found. If any SwitchBots are not found, it returns nothing(no timeout error).

Types

type Bot

type Bot struct {
	Addr string
	// contains filtered or unexported fields
}

Bot represents SwitchBot device.

func Connect

func Connect(ctx context.Context, addr string, timeout time.Duration) (*Bot, error)

Connect connects to SwitchBot filter by addr argument. If connection failed within timeout, Connect returns error.

func NewBot

func NewBot(addr string) *Bot

NewBot initializes bot object.

func (*Bot) Disconnect

func (b *Bot) Disconnect() error

Disconnect disconnects current SwitchBot connection.

func (*Bot) Down

func (b *Bot) Down(wait bool) error

Down triggers down function for the SwitchBot.

func (*Bot) GetInfo

func (b *Bot) GetInfo() (*BotInfo, error)

GetInfo retrieves bot's settings.

func (*Bot) GetTimers

func (b *Bot) GetTimers(cnt int) ([]*Timer, error)

GetTimers retrieves bot's timer settings.

func (*Bot) Off

func (b *Bot) Off(wait bool) error

Off triggers off function for the SwitchBot. SwitchBot must be set to On/Off mode.

func (*Bot) On

func (b *Bot) On(wait bool) error

On triggers on function for the SwitchBot. SwitchBot must be set to On/Off mode.

func (*Bot) Press

func (b *Bot) Press(wait bool) error

Press triggers press function for the SwitchBot. SwitchBot must be set to press mode.

func (*Bot) SetPassword

func (b *Bot) SetPassword(pw string)

SetPassword sets SwitchBot's password. If SwitchBot is configured to use password authentication, you need to call SetPassword before calling Press/On/Off function.

func (*Bot) Subscribe

func (b *Bot) Subscribe() error

Subscribe subscribes to bot and waiting notification from SwitchBot.

func (*Bot) Up

func (b *Bot) Up(wait bool) error

Up triggers down function for the SwitchBot.

type BotInfo

type BotInfo struct {
	Battery    int     `json:"battery"`
	Firmware   float64 `json:"firmware"`
	TimerCount int     `json:"timer_count"`
	StateMode  bool    `json:"state_mode"`
	Inverse    bool    `json:"inverse"`
	HoldSec    int     `json:"hold_sec"`
}

BotInfo represents current SwitchBot's information.

func NewBotInfoWithRawInfo

func NewBotInfoWithRawInfo(info []byte) *BotInfo

NewBotInfoWithRawInfo initialize BotInfo with raw byte data. This works with switchbot.GetInfo.

func (*BotInfo) String

func (i *BotInfo) String() string

String returns formatted information

type Timer

type Timer struct {
	Enabled  bool
	Weekdays [7]bool
	Hour     int
	Minutes  int
	Action   int
}

Timer represents Timer configuration.

func ParseTimerBytes

func ParseTimerBytes(val []byte) *Timer

ParseTimerBytes parses bytes to timer object.

Jump to

Keyboard shortcuts

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