switchbot

package module
Version: v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2021 License: MIT Imports: 10 Imported by: 1

README

go-switchbot

test

A SwitchBot API client for Golang

SYNOPSIS

const openToken = "blahblahblah"

c := switchbot.New(openToken)

// get physical devices and show
pdev, _, _ := c.Device().List(context.Background())

for _, d := range pdev {
	fmt.Printf("%s\t%s\n", d.Type, d.Name)
}

Get Open Token

To use SwitchBot API, you need to get Open Token for auth. Follow steps below:

  1. Download the SwitchBot app on App Store or Google Play Store
  2. Register a SwitchBot account and log in into your account
  3. Generate an Open Token within the app a) Go to Profile > Preference b) Tap App Version 10 times. Developer Options will show up c) Tap Developer Options d) Tap Get Token
  4. Roll up your sleeves and get your hands dirty with SwitchBot OpenAPI!

Documentation

Index

Constants

View Source
const DefaultEndpoint = "https://api.switch-bot.com"

Variables

This section is empty.

Functions

This section is empty.

Types

type ACFanSpeed

type ACFanSpeed int

ACFanSpeed represents a fan speed mode for air conditioner.

const (
	ACAutoSpeed ACFanSpeed = iota + 1
	ACLow
	ACMedium
	ACHigh
)

type ACMode

type ACMode int

ACMode represents a mode for air conditioner.

const (
	ACAuto ACMode = iota + 1
	ACCool
	ACDry
	ACFan
	ACHeat
)

type Client

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

func New

func New(openToken string, opts ...Option) *Client

New returns a new switchbot client associated with given openToken. See https://github.com/OpenWonderLabs/SwitchBotAPI/blob/7a68353d84d07d439a11cb5503b634f24302f733/README.md#getting-started for getting openToken for SwitchBot API.

func (*Client) Device

func (c *Client) Device() *DeviceService

Device returns the Service object for device APIs.

func (*Client) Scene

func (c *Client) Scene() *SceneService

Scene returns the Service Object for scene APIs.

type Command

type Command interface {
	// contains filtered or unexported methods
}

Command is an interface which represents Commands for devices to be used (*Client).Device().Command() method.

func ACSetAll

func ACSetAll(temperature int, mode ACMode, fanSpeed ACFanSpeed, power PowerState) Command

ACSetAll returns a new command which set all state of air conditioner.

func ButtonPush

func ButtonPush(name string) Command

ButtonPush returns a command which triggers button push.

func Press

func Press() Command

Press returns a new command which trigger Bot's press command.

func SetMode

func SetMode(mode HumidifierMode) Command

SetMode sets a mode for Humidifier. mode can be take one of HumidifierMode constants or 0 - 100 value. To use exact value 0 - 100, you need to pass like HumidifierMode(38).

func SetPosition

func SetPosition(index int, mode SetPositionMode, position int) Command

SetPosition returns a new Command which sets curtain devices' position. The third argument `position` can be take 0 - 100 value, 0 means opened and 100 means closed. The position value will be treated as 0 if the given value is less than 0, or treated as 100 if the given value is over 100.

func TurnOff

func TurnOff() Command

TurnOff returns a nw Command which turns off Bot, plug, Curtain, Humidifier, or so on. For curtain devices, turn off is equivalent to set position to 100.

func TurnOn

func TurnOn() Command

TurnOn returns a new Command which turns on Bot, Plug, Curtain, Humidifier, or so on. For curtain devices, turn on is equivalent to set position to 0.

type Device

type Device struct {
	ID                   string             `json:"deviceId"`
	Name                 string             `json:"deviceName"`
	Type                 PhysicalDeviceType `json:"deviceType"`
	IsEnableCloudService bool               `json:"enableCloudService"`
	Hub                  string             `json:"hubDeviceId"`
	Curtains             []string           `json:"curtainDeviceesIds"`
	IsCalibrated         bool               `json:"calibrate"`
	IsGrouped            bool               `json:"group"`
	IsMaster             bool               `json:"master"`
	OpenDirection        string             `json:"openDirection"`
}

Device represents a physical SwitchBot device.

type DeviceService

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

DeviceService handles API calls related to devices. The devices API is used to access the properties and states of SwitchBot devices and to send control commands to those devices.

func (*DeviceService) Command

func (svc *DeviceService) Command(ctx context.Context, id string, cmd Command) error

func (*DeviceService) List

func (svc *DeviceService) List(ctx context.Context) ([]Device, []InfraredDevice, error)

List get a list of devices, which include physical devices and virtual infrared remote devices that have been added to the current user's account. The first returned value is a list of physical devices refer to the SwitchBot products. The second returned value is a list of virtual infrared remote devices such like air conditioner, TV, light, or so on. See also https://github.com/OpenWonderLabs/SwitchBotAPI/blob/7a68353d84d07d439a11cb5503b634f24302f733/README.md#get-device-list

func (*DeviceService) Status

func (svc *DeviceService) Status(ctx context.Context, id string) (DeviceStatus, error)

Status get the status of a physical device that has been added to the current user's account. Physical devices refer to the SwitchBot products. The first given argument `id` is a device ID which can be retrieved by (*Client).Device().List() function. See also https://github.com/OpenWonderLabs/SwitchBotAPI/blob/7a68353d84d07d439a11cb5503b634f24302f733/README.md#get-device-status

type DeviceStatus

type DeviceStatus struct {
	ID                     string             `json:"deviceId"`
	Type                   PhysicalDeviceType `json:"deviceType"`
	Hub                    string             `json:"hubDeviceId"`
	Power                  PowerState         `json:"power"`
	Humidity               int                `json:"humidity"`
	Temperature            float64            `json:"temperature"`
	NebulizationEfficiency int                `json:"nebulizationEfficiency"`
	IsAuto                 bool               `json:"auto"`
	IsChildLock            bool               `json:"childLock"`
	IsSound                bool               `json:"sound"`
	IsCalibrated           bool               `json:"calibrate"`
	IsGrouped              bool               `json:"group"`
	IsMoving               bool               `json:"moving"`
	SlidePosition          int                `json:"slidePosition"`
	FanMode                int                `json:"mode"`
	FanSpeed               int                `json:"speed"`
	IsShaking              bool               `json:"shaking"`
	ShakeCenter            int                `json:"shakeCenter"`
	ShakeRange             int                `json:"shakeRange"`
}

type HumidifierMode

type HumidifierMode int
const (
	AutoMode HumidifierMode = -1
	LowMode  HumidifierMode = 101
	MidMode  HumidifierMode = 102
	HighMode HumidifierMode = 103
)

type InfraredDevice

type InfraredDevice struct {
	ID   string            `json:"deviceId"`
	Name string            `json:"deviceName"`
	Type VirtualDeviceType `json:"remoteType"`
	Hub  string            `json:"hubDeviceId"`
}

InfraredDevice represents a virtual infrared remote device.

type Option

type Option func(*Client)

func WithEndpoint

func WithEndpoint(endpoint string) Option

WithEndpoint allows you to set an endpoint of SwitchBot API.

func WithHTTPClient

func WithHTTPClient(httpClient *http.Client) Option

WithHTTPClient allows you to pass your http client for a SwitchBot API client.

type PhysicalDeviceType

type PhysicalDeviceType string
const (
	// Hub is generally referred to these devices, SwitchBot Hub Model No. SwitchBot Hub S1/SwitchBot Hub Mini Model No. W0202200/SwitchBot Hub Plus Model No. SwitchBot Hub S1
	Hub PhysicalDeviceType = "Hub"
	// HubPlus is SwitchBot Hub Plus Model No. SwitchBot Hub S1
	HubPlus PhysicalDeviceType = "Hub Plus"
	// HubMini is SwitchBot Hub Mini Model No. W0202200
	HubMini PhysicalDeviceType = "Hub Mini"
	// Bot is SwitchBot Bot Model No. SwitchBot S1
	Bot PhysicalDeviceType = "Bot"
	// Curtain is SwitchBot Curtain Model No. W0701600
	Curtain PhysicalDeviceType = "Curtain"
	// Plug is SwitchBot Plug Model No. SP11
	Plug PhysicalDeviceType = "Plug"
	// Meter is SwitchBot Thermometer and Hygrometer Model No. SwitchBot MeterTH S1
	Meter PhysicalDeviceType = "Meter"
	// Humidifier is SwitchBot Humidifier Model No. W0801801
	Humidifier PhysicalDeviceType = "Humidifier"
	// SmartFan is SwitchBot Smart Fan Model No. W0601100
	SmartFan PhysicalDeviceType = "Smart Fan"
)

type PowerState

type PowerState string
const (
	PowerOn  PowerState = "ON"
	PowerOff PowerState = "OFF"
)

func (PowerState) ToLower

func (power PowerState) ToLower() string

type Scene

type Scene struct {
	ID   string `json:"sceneId"`
	Name string `json:"sceneName"`
}

Scene represents a manual scene created by the current user.

type SceneService

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

SceneService handles API calls related to scenes. The scene API is used to access the smart scenes created by a user and to execute manual scenes.

func (*SceneService) Execute

func (svc *SceneService) Execute(ctx context.Context, id string) error

Execute sends a request to execute a manual scene. The first given argument `id` is a scene ID which you want to execute, which can be retrieved by (*Client).Scene().List() function.

func (*SceneService) List

func (svc *SceneService) List(ctx context.Context) ([]Scene, error)

List get a list of manual scenes created by the current user. The first returned value is a list of scenes.

type SetPositionMode

type SetPositionMode int

SetPositionMode represents a mode for curtain devices' set position mode.

const (
	DefaultMode SetPositionMode = iota
	PerformanceMode
	SilentMode
)

type VirtualDeviceType

type VirtualDeviceType string
const (
	AirConditioner VirtualDeviceType = "Air Conditioner"
	TV             VirtualDeviceType = "TV"
	Light          VirtualDeviceType = "Light"
	IPTVStreamer   VirtualDeviceType = "IPTV/Streamer"
	SetTopBox      VirtualDeviceType = "Set Top Box"
	DVD            VirtualDeviceType = "DVD"
	Fan            VirtualDeviceType = "Fan"
	Projector      VirtualDeviceType = "Projector"
	Camera         VirtualDeviceType = "Camera"
	AirPurifier    VirtualDeviceType = "Air Purifier"
	Speaker        VirtualDeviceType = "Speaker"
	WaterHeater    VirtualDeviceType = "Water Heater"
	VacuumCleaner  VirtualDeviceType = "Vacuum Cleaner"
	Others         VirtualDeviceType = "Others"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto