Skip to content

Commit

Permalink
Sensor state and config are interface{}
Browse files Browse the repository at this point in the history
  • Loading branch information
amimof committed Feb 5, 2018
1 parent 4a07a1b commit 6ab4c82
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 41 deletions.
4 changes: 2 additions & 2 deletions bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -1562,10 +1562,10 @@ func (b *Bridge) DeleteSensor(i int) error {
}

// UpdateSensorConfig updates the configuration of one sensor. The allowed configuration parameters depend on the sensor type
func (b *Bridge) UpdateSensorConfig(i int, config *SensorConfig) (*Response, error) {
func (b *Bridge) UpdateSensorConfig(i int, c interface{}) (*Response, error) {
var a []*APIResponse

data, err := json.Marshal(&config)
data, err := json.Marshal(&c)
if err != nil {
return nil, err
}
Expand Down
28 changes: 14 additions & 14 deletions sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package huego

// Sensor represents a bridge sensor https://developers.meethue.com/documentation/sensors-api
type Sensor struct {
State *SensorState `json:"state,omitempty"`
Config *SensorConfig `json:"config,omitempty"`
State interface{} `json:"state,omitempty"`
Config interface{} `json:"config,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
ModelID string `json:"modelid,omitempty"`
Expand All @@ -12,19 +12,19 @@ type Sensor struct {
ID int `json:",omitempty"`
}

// SensorState defines the state a sensor has
type SensorState struct {
Daylight string `json:"daylight,omitempty"`
LastUpdated string `json:"lastupdated,omitempty"`
}
// // SensorState defines the state a sensor has
// type SensorState struct {
// Daylight string `json:"daylight,omitempty"`
// LastUpdated string `json:"lastupdated,omitempty"`
// }

// SensorConfig defines the configuration of a sensor
type SensorConfig struct {
On bool `json:"on,omitempty"`
Configured bool `json:"configured,omitempty"`
SunriseOffset int `json:"sunriseoffset,omitempty"`
SunsetOffset int `json:"sunsetoffset,omitempty"`
}
// // SensorConfig defines the configuration of a sensor
// type SensorConfig struct {
// On bool `json:"on,omitempty"`
// Configured bool `json:"configured,omitempty"`
// SunriseOffset int `json:"sunriseoffset,omitempty"`
// SunsetOffset int `json:"sunsetoffset,omitempty"`
// }

// NewSensor defines a list of sensors discovered the last time the bridge performed a sensor discovery.
// Also stores the timestamp the last time a discovery was performed.
Expand Down
63 changes: 38 additions & 25 deletions sensor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ func TestGetSensors(t *testing.T) {
}
t.Logf("Found %d sensors", len(sensors))
for _, sensor := range sensors {
t.Logf("Sensor id=%d name=%s", sensor.ID, sensor.Name)
t.Logf("State:")
t.Logf(" Interface: %+v", sensor.State)
t.Logf("Config:")
t.Logf(" On: %+v", sensor.Config)
t.Logf("Name: %s", sensor.Name)
t.Logf("Type: %s", sensor.Type)
t.Logf("ModelID: %s", sensor.ModelID)
t.Logf("ManufacturerName: %s", sensor.ManufacturerName)
t.Logf("SwVersion: %s", sensor.SwVersion)
t.Logf("ID: %d", sensor.ID)
}
}

Expand All @@ -27,11 +36,20 @@ func TestGetSensor(t *testing.T) {
t.Logf("Found %d sensors", len(sensors))
for _, sensor := range sensors {
t.Logf("Getting sensor %d, skipping the rest", sensor.ID)
s, err := b.GetSensor(sensor.ID)
sensor, err := b.GetSensor(sensor.ID)
if err != nil {
t.Fatal(err)
}
t.Logf("Got sensor name=%s", s.Name)
t.Logf("State:")
t.Logf(" Interface: %+v", sensor.State)
t.Logf("Config:")
t.Logf(" On: %+v", sensor.Config)
t.Logf("Name: %s", sensor.Name)
t.Logf("Type: %s", sensor.Type)
t.Logf("ModelID: %s", sensor.ModelID)
t.Logf("ManufacturerName: %s", sensor.ManufacturerName)
t.Logf("SwVersion: %s", sensor.SwVersion)
t.Logf("ID: %d", sensor.ID)
break
}
}
Expand All @@ -43,24 +61,24 @@ func TestCreateSensor(t *testing.T) {
})
if err != nil {
t.Fatal(err)
} else {
t.Logf("Sensor created")
for k, v := range resp.Success {
t.Logf("%v: %s", k, v)
}
}
t.Logf("Sensor created")
for k, v := range resp.Success {
t.Logf("%v: %s", k, v)
}

}

func TestFindSensors(t *testing.T) {
b := huego.New(os.Getenv("HUE_HOSTNAME"), os.Getenv("HUE_USERNAME"))
resp, err := b.FindSensors()
if err != nil {
t.Fatal(err)
} else {
for k, v := range resp.Success {
t.Logf("%v: %s", k, v)
}
}
for k, v := range resp.Success {
t.Logf("%v: %s", k, v)
}

}

func TestGetNewSensors(t *testing.T) {
Expand All @@ -72,13 +90,9 @@ func TestGetNewSensors(t *testing.T) {
t.Logf("Sensors:")
for _, sensor := range newSensors.Sensors {
t.Logf("State:")
t.Logf(" Daylight: %s", sensor.State.Daylight)
t.Logf(" LastUpdated: %s", sensor.State.LastUpdated)
t.Logf(" Interface: %+v", sensor.State)
t.Logf("Config:")
t.Logf(" On: %t", sensor.Config.On)
t.Logf(" Configured: %t", sensor.Config.Configured)
t.Logf(" SunriseOffset: %d", sensor.Config.SunriseOffset)
t.Logf(" SunsetOffset: %d", sensor.Config.SunsetOffset)
t.Logf(" On: %+v", sensor.Config)
t.Logf("Name: %s", sensor.Name)
t.Logf("Type: %s", sensor.Type)
t.Logf("ModelID: %s", sensor.ModelID)
Expand All @@ -96,11 +110,11 @@ func TestUpdateSensor(t *testing.T) {
})
if err != nil {
t.Fatal(err)
} else {
for k, v := range resp.Success {
t.Logf("%v: %s", k, v)
}
}
for k, v := range resp.Success {
t.Logf("%v: %s", k, v)
}

}

func TestDeleteSensor(t *testing.T) {
Expand All @@ -109,7 +123,6 @@ func TestDeleteSensor(t *testing.T) {
err := b.DeleteSensor(id)
if err != nil {
t.Fatal(err)
} else {
t.Logf("Sensor %d deleted", id)
}
}
t.Logf("Sensor %d deleted", id)
}

0 comments on commit 6ab4c82

Please sign in to comment.