Skip to content

Commit

Permalink
feat: support hub alarms
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiankachlock committed Jul 12, 2024
1 parent ba7489c commit 6dd8db0
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 6 deletions.
12 changes: 6 additions & 6 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func main() {
panic(err)
}

// resp, err := client.Request("get_support_alarm_type_list", map[string]interface{}{})
resp, err := client.Request("play_alarm", map[string]interface{}{
"alarm_duration": 2,
"alarm_volume": "low",
"alarm_type": "Doorbell Ring 2",
})
resp, err := client.Request("get_support_alarm_type_list", map[string]interface{}{})
// resp, err := client.Request("play_alarm", map[string]interface{}{
// "alarm_duration": 2,
// "alarm_volume": "low",
// "alarm_type": "Connection 1",
// })
if err != nil {
panic(err)
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/api/request/hub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package request

type PlayAlarmParams struct {
Duration int `json:"alarm_duration"`
Volume AlarmVolume `json:"alarm_volume"`
Type string `json:"alarm_type"`
}

type AlarmVolume string

const (
AlarmVolumeLow AlarmVolume = "low"
AlarmVolumeMedium AlarmVolume = "normal"
AlarmVolumeHigh AlarmVolume = "high"
)
3 changes: 3 additions & 0 deletions pkg/api/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const (
RequestMultiple = "multipleRequest"
RequestGetTriggerLog = "get_trigger_logs"
RequestGetTemperatureHumidityRecords = "get_temp_humidity_records"
RequestSupportedAlarmTypes = "get_support_alarm_type_list"
RequestPlayAlarm = "play_alarm"
RequestStopAlarm = "stop_alarm"
)

var (
Expand Down
4 changes: 4 additions & 0 deletions pkg/api/response/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ type ChildDeviceComponent struct {
Id string
VerCode int
}

type AlarmsList struct {
AlarmTypes []string `json:"alarm_type_list"`
}
19 changes: 19 additions & 0 deletions pkg/api/response/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package response

import (
"encoding/json"
"errors"
"fmt"
)

const (
Expand All @@ -13,11 +15,26 @@ const (
ErrorSessionTimeOut = 9999
)

var (
ErrNonSuccessfulResponse = errors.New("non successful response")
)

type TapoResponse[T any] struct {
Result T `json:"result"`
ErrorCode int `json:"error_code"`
}

func (r TapoResponse[T]) IsOk() bool {
return r.ErrorCode == ResponseOk
}

func (r TapoResponse[T]) GetError() error {
if r.ErrorCode == ResponseOk {
return nil
}
return fmt.Errorf("%w: error code: %d", ErrNonSuccessfulResponse, r.ErrorCode)
}

// UnmarshalResponse unmarshals the response from the Tapo API.
func UnmarshalResponse[T any](data []byte) (TapoResponse[T], error) {
jsonData := TapoResponse[T]{}
Expand Down Expand Up @@ -53,3 +70,5 @@ type EnergyUsage struct {
type CurrentPower struct {
CurrentPower uint64 `json:"current_power"`
}

type EmptyResponse struct{}
27 changes: 27 additions & 0 deletions pkg/devices/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,30 @@ func (t *TapoHub) GetT300(nicknameOrId string) (bool, childdevices.DeviceInfoT30
info, err := device.AsT300()
return true, info, err
}

func (t *TapoHub) GetSupportedAlarms() (response.AlarmsList, error) {
resp, err := t.client.Request(request.RequestSupportedAlarmTypes, request.EmptyParams)
if err != nil {
return response.AlarmsList{}, err
}

data, err := response.UnmarshalResponse[response.AlarmsList](resp)
if err != nil {
return response.AlarmsList{}, err
}
return data.Result, nil
}

func (t *TapoHub) PlayAlarm(alarmType string, volume request.AlarmVolume, duration int) error {
_, err := t.client.Request(request.RequestPlayAlarm, request.PlayAlarmParams{
Type: alarmType,
Volume: volume,
Duration: duration,
})
return err
}

func (t *TapoHub) StopAlarm() error {
_, err := t.client.Request(request.RequestStopAlarm, request.EmptyParams)
return err
}

0 comments on commit 6dd8db0

Please sign in to comment.