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 ¶
- func Scan(ctx context.Context, timeout time.Duration, callback func(addr string)) error
- type Bot
- func (b *Bot) Disconnect() error
- func (b *Bot) Down(wait bool) error
- func (b *Bot) GetInfo() (*BotInfo, error)
- func (b *Bot) GetTimers(cnt int) ([]*Timer, error)
- func (b *Bot) Off(wait bool) error
- func (b *Bot) On(wait bool) error
- func (b *Bot) Press(wait bool) error
- func (b *Bot) SetPassword(pw string)
- func (b *Bot) Subscribe() error
- func (b *Bot) Up(wait bool) error
- type BotInfo
- type Timer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Bot ¶
type Bot struct { Addr string // contains filtered or unexported fields }
Bot represents SwitchBot device.
func Connect ¶
Connect connects to SwitchBot filter by addr argument. If connection failed within timeout, Connect returns error.
func (*Bot) Disconnect ¶
Disconnect disconnects current SwitchBot connection.
func (*Bot) Off ¶
Off triggers off function for the SwitchBot. SwitchBot must be set to On/Off mode.
func (*Bot) Press ¶
Press triggers press function for the SwitchBot. SwitchBot must be set to press mode.
func (*Bot) SetPassword ¶
SetPassword sets SwitchBot's password. If SwitchBot is configured to use password authentication, you need to call SetPassword before calling Press/On/Off function.
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 ¶
NewBotInfoWithRawInfo initialize BotInfo with raw byte data. This works with switchbot.GetInfo.