Skip to content

Commit

Permalink
Add better handling of lights via introducing BaseDevice
Browse files Browse the repository at this point in the history
Add BaseDevice and implement a couple of the base interfaces
  • Loading branch information
kanimaru committed Mar 13, 2023
1 parent 9730fe4 commit a8b8745
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
6 changes: 6 additions & 0 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ type Config interface {
GetBattery() *uint8
}

// StateLight is the base for all lights
type StateLight interface {
GetAlert() interface{}
GetBrightness() int
IsOn() bool
}

// StateDevice is the base for all "light" devices also for OnOff Sockets
type StateDevice interface {
IsOn() bool
}
20 changes: 20 additions & 0 deletions http/client_light.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ type LightResponseStateDetail struct {
Reachable *bool `json:"reachable"`
}

func (l LightResponseStateDetail) IsOn() bool {
if l.On != nil {
return *l.On
} else {
return false
}
}

func (l LightResponseStateDetail) GetAlert() interface{} {
return l.Alert
}

func (l LightResponseStateDetail) GetBrightness() int {
if l.Bri != nil {
return *l.Bri
} else {
return 0
}
}

type LightResponseState[STATE any] struct {

// The color capabilities as reported by the light.
Expand Down
27 changes: 16 additions & 11 deletions ws/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,23 @@ type StateDaylight struct {
// FIXME HERE ARE MISSING FIELDS
}

type StateBaseDevice struct {
On bool `json:"on,omitempty"`
Reachable bool `json:"reachable,omitempty"`
}

func (s StateBaseDevice) IsOn() bool {
return s.On
}

func (s StateBaseDevice) IsReachable() bool {
return s.Reachable
}

type StateBaseLight struct {
StateBaseDevice
Alert interface{} `json:"alert,omitempty"`
Bri int `json:"bri,omitempty"`
On bool `json:"on,omitempty"`
}

func (s StateBaseLight) GetAlert() interface{} {
Expand All @@ -21,34 +34,26 @@ func (s StateBaseLight) GetBrightness() int {
return s.Bri
}

func (s StateBaseLight) IsOn() bool {
return s.On
}

type StateDimmablelight struct {
StateBaseLight
Reachable bool `json:"reachable,omitempty"`
}

type StateColortemperaturelight struct {
StateBaseLight
Colormode string `json:"colormode"`
Ct int `json:"ct"`
Reachable bool `json:"reachable"`
}

type StateExtendedcolorlight struct {
// FIXME HERE ARE MISSING FIELDS
StateBaseLight
Colormode string `json:"colormode"`
Ct int `json:"ct"`
Reachable bool `json:"reachable"`
}

type StateOnOffpluginunit struct {
Alert interface{} `json:"alert"`
On bool `json:"on"`
Reachable bool `json:"reachable"`
StateBaseDevice
Alert interface{} `json:"alert"`
}

type StateZHATemperature struct {
Expand Down

0 comments on commit a8b8745

Please sign in to comment.