tapo

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2023 License: MIT Imports: 30 Imported by: 2

README

tapo

Go library for managing Tapo P100 and P110 plugs.

See cmd/tapo for a sample CLI.

See cmd/tapoweb for a sample web interface.

Documentation

Index

Constants

View Source
const DiscoverV1InitializationVector = 0xab

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is a tp-link cloud client for cloud-based operations.

func NewClient

func NewClient(logger *log.Logger) *Client

func (*Client) CloudList

func (c *Client) CloudList() ([]Device, error)

func (*Client) CloudLogin

func (c *Client) CloudLogin(username, password string) error

func (*Client) Discover

func (c *Client) Discover() (map[string]DiscoverResponse, []DiscoverResponse, error)

type Device

type Device struct {
	DeviceType   string  `json:"deviceType"`
	Role         int     `json:"role"`
	FwVer        string  `json:"fwVer"`
	AppServerURL string  `json:"appServerUrl"`
	DeviceRegion string  `json:"deviceRegion"`
	DeviceID     string  `json:"deviceId"`
	DeviceName   string  `json:"deviceName"`
	DeviceHwVer  string  `json:"deviceHwVer"`
	Alias        string  `json:"alias"`
	DeviceMAC    tapoMAC `json:"deviceMac"`
	OemID        string  `json:"oemId"`
	DeviceModel  string  `json:"deviceModel"`
	HwID         string  `json:"hwId"`
	FwID         string  `json:"fwId"`
	IsSameRegion bool    `json:"isSameRegion"`
	Status       int     `json:"status"`
	// Computed values
	DecodedAlias string
}

type DeviceInfo

type DeviceInfo struct {
	DeviceID           string `json:"device_id"`
	FWVersion          string `json:"fw_ver"`
	HWVersion          string `json:"hw_ver"`
	Type               string `json:"type"`
	Model              string `json:"model"`
	MAC                string `json:"mac"`
	HWID               string `json:"hw_id"`
	FWID               string `json:"fw_id"`
	OEMID              string `json:"oem_id"`
	IP                 string `json:"ip"`
	TimeDiff           int    `json:"time_diff"`
	SSID               string `json:"ssid"`
	RSSI               int    `json:"rssi"`
	SignalLevel        int    `json:"signal_level"`
	Latitude           int    `json:"latitude"`
	Longitude          int    `json:"longitude"`
	Lang               string `json:"lang"`
	Avatar             string `json:"avatar"`
	Region             string `json:"region"`
	Specs              string `json:"specs"`
	Nickname           string `json:"nickname"`
	HasSetLocationInfo bool   `json:"has_set_location_info"`
	DeviceON           bool   `json:"device_on"`
	OnTime             int    `json:"on_time"`
	DefaultStates      struct {
		Type string `json:"type"`
		// TODO add the structure for State
		State *json.RawMessage `json:"state"`
	} `json:"default_states"`
	OverHeated            bool   `json:"overheated"`
	PowerProtectionStatus string `json:"power_protection_status,omitempty"`
	Location              string `json:"location,omitempty"`

	// Computed values below.
	// DecodedSSID is the decoded version of the base64-encoded SSID field.
	DecodedSSID string
	// DecodedNickname is the decoded version of the base64-encoded Nickname field.
	DecodedNickname string
}

TODO differentiate fields between P100 and P110

type DeviceUsage

type DeviceUsage struct {
	TimeUsage struct {
		Today  int `json:"today"`
		Past7  int `json:"past7"`
		Past30 int `json:"past30"`
	} `json:"time_usage"`
	PowerUsage struct {
		Today  int `json:"today"`
		Past7  int `json:"past7"`
		Past30 int `json:"past30"`
	} `json:"power_usage"`
	SavedPower struct {
		Today  int `json:"today"`
		Past7  int `json:"past7"`
		Past30 int `json:"past30"`
	} `json:"saved_power"`
}

type DiscoverResponse

type DiscoverResponse struct {
	Result struct {
		DeviceID          string             `json:"device_id"`
		Owner             string             `json:"owner"`
		DeviceType        string             `json:"device_type"`
		DeviceModel       string             `json:"device_model"`
		IP                xjson.IP           `json:"ip"`
		MAC               xjson.HardwareAddr `json:"mac"`
		IsSupportIOTCloud bool               `json:"is_support_iot_clout"`
		ObdSrc            string             `json:"obd_src"`
		FactoryDefault    bool               `json:"factory_default"`
		MgtEncryptSchm    struct {
			IsSupportHTTPS bool   `json:"is_support_https"`
			EncryptType    string `json:"encrypt_type"`
			HTTPPort       int    `json:"http_port"`
			Lv             int    `json:"lv"`
		} `json:"mgt_encrypt_schm"`
		ErrorCode int `json:"error_code"`
	} `json:"result"`
}

type DiscoverV1Request

type DiscoverV1Request struct {
	System           GetSysinfo `json:"system"`
	CnCloud          GetInfo    `json:"cnCloud"`
	IOTCommonCloud   GetInfo    `json:"smartlife.iot.common.cloud"`
	CamIpcameraCloud GetInfo    `json:"smartlife.cam.ipcamera.cloud"`
}

func NewDiscoverV1Request

func NewDiscoverV1Request() *DiscoverV1Request

type EnergyUsage

type EnergyUsage struct {
	TodayRuntime      int    `json:"today_runtime"`
	MonthRuntime      int    `json:"month_runtime"`
	TodayEnergy       int    `json:"today_energy"`
	MonthEnergy       int    `json:"month_energy"`
	LocalTime         string `json:"local_time"`
	ElectricityCharge [3]int `json:"electricity_charge"`
	CurrentPower      int    `json:"current_power"`
}

type GetDeviceInfoRequest

type GetDeviceInfoRequest struct {
	Method          string `json:"method"`
	RequestTimeMils int    `json:"requestTimeMils"`
}

func NewGetDeviceInfoRequest

func NewGetDeviceInfoRequest() *GetDeviceInfoRequest

type GetDeviceInfoResponse

type GetDeviceInfoResponse struct {
	ErrorCode TapoError  `json:"error_code"`
	Result    DeviceInfo `json:"result"`
}

type GetDeviceUsageRequest

type GetDeviceUsageRequest struct {
	Method          string `json:"method"`
	RequestTimeMils int    `json:"requestTimeMils"`
}

func NewGetDeviceUsageRequest

func NewGetDeviceUsageRequest() *GetDeviceUsageRequest

type GetDeviceUsageResponse

type GetDeviceUsageResponse struct {
	ErrorCode TapoError   `json:"error_code"`
	Result    DeviceUsage `json:"result"`
}

type GetEnergyUsageRequest

type GetEnergyUsageRequest struct {
	Method          string `json:"method"`
	RequestTimeMils int    `json:"requestTimeMils"`
}

func NewGetEnergyUsageRequest

func NewGetEnergyUsageRequest() *GetEnergyUsageRequest

type GetEnergyUsageResponse

type GetEnergyUsageResponse struct {
	ErrorCode TapoError   `json:"error_code"`
	Result    EnergyUsage `json:"result"`
}

type GetInfo

type GetInfo struct {
	GetInfo map[string]string `json:"get_info"`
}

type GetSysinfo

type GetSysinfo struct {
	GetSysinfo map[string]string `json:"get_sysinfo"`
}

type HandshakeRequest

type HandshakeRequest struct {
	Method          string `json:"method"`
	RequestTimeMils int    `json:"requestTimeMils"`
	Params          struct {
		Key string `json:"key"`
	} `json:"params"`
}

func NewHandshakeRequest

func NewHandshakeRequest(key string) *HandshakeRequest

type HandshakeResponse

type HandshakeResponse struct {
	ErrorCode TapoError `json:"error_code"`
	Result    struct {
		Key string `json:"key"`
	}
}

type KlapSession

type KlapSession struct {
	SessionID  string
	Expiry     time.Time
	LocalSeed  []byte
	RemoteSeed []byte
	UserHash   []byte
	// contains filtered or unexported fields
}

func NewKlapSession

func NewKlapSession(l *log.Logger) *KlapSession

func (*KlapSession) Addr

func (s *KlapSession) Addr() netip.Addr

func (*KlapSession) Handshake

func (s *KlapSession) Handshake(addr netip.Addr, username, password string) error

func (*KlapSession) Request

func (s *KlapSession) Request(payload []byte) ([]byte, error)

type LoginDeviceRequest

type LoginDeviceRequest struct {
	Method          string `json:"method"`
	RequestTimeMils int    `json:"requestTimeMils"`
	Params          struct {
		Username string `json:"username"`
		Password string `json:"password"`
	} `json:"params"`
}

func NewLoginDeviceRequest

func NewLoginDeviceRequest(username, password string) *LoginDeviceRequest

type LoginDeviceResponse

type LoginDeviceResponse struct {
	ErrorCode TapoError `json:"error_code"`
	Result    struct {
		Token string `json:"token"`
	} `json:"result"`
}

type PassthroughSession

type PassthroughSession struct {
	Key []byte
	IV  []byte
	ID  string
	// contains filtered or unexported fields
}

func NewPassthroughSession

func NewPassthroughSession(l *log.Logger) *PassthroughSession

func (*PassthroughSession) Addr

func (p *PassthroughSession) Addr() netip.Addr

func (*PassthroughSession) Handshake

func (p *PassthroughSession) Handshake(addr netip.Addr, username, password string) error

func (*PassthroughSession) Request

func (s *PassthroughSession) Request(requestBytes []byte) ([]byte, error)

type Plug

type Plug struct {
	Addr netip.Addr
	// contains filtered or unexported fields
}

func NewPlug

func NewPlug(addr netip.Addr, logger *log.Logger) *Plug

func (*Plug) GetDeviceInfo

func (p *Plug) GetDeviceInfo() (*DeviceInfo, error)

func (*Plug) GetDeviceUsage

func (p *Plug) GetDeviceUsage() (*DeviceUsage, error)

func (*Plug) GetEnergyUsage

func (p *Plug) GetEnergyUsage() (*EnergyUsage, error)

func (*Plug) Handshake

func (p *Plug) Handshake(username, password string) error

func (*Plug) IsOn

func (p *Plug) IsOn() (bool, error)

func (*Plug) Off

func (p *Plug) Off() error

func (*Plug) On

func (p *Plug) On() error

func (*Plug) SetDeviceInfo

func (p *Plug) SetDeviceInfo(deviceOn bool) error

type SecurePassthroughRequest

type SecurePassthroughRequest struct {
	Method string `json:"method"`
	Params struct {
		Request string `json:"request"`
	} `json:"params"`
}

func NewSecurePassthroughRequest

func NewSecurePassthroughRequest(innerRequest string) *SecurePassthroughRequest

type SecurePassthroughResponse

type SecurePassthroughResponse struct {
	ErrorCode TapoError `json:"error_code"`
	Result    struct {
		Response string `json:"response"`
	}
}

type Session

type Session interface {
	Handshake(addr netip.Addr, username, password string) error
	Request([]byte) ([]byte, error)
	Addr() netip.Addr
}

type SetDeviceInfoRequest

type SetDeviceInfoRequest struct {
	Method string `json:"method"`
	Params struct {
		DeviceOn bool `json:"device_on"`
	} `json:"params"`
}

func NewSetDeviceInfoRequest

func NewSetDeviceInfoRequest(deviceOn bool) *SetDeviceInfoRequest

type SetDeviceInfoResponse

type SetDeviceInfoResponse struct {
	ErrorCode TapoError `json:"error_code"`
	Result    struct {
		Response string `json:"response"`
	}
}

type TapoError

type TapoError int

func (TapoError) Error

func (te TapoError) Error() string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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