diff --git a/bridge.go b/bridge.go index 075eb33..d744209 100644 --- a/bridge.go +++ b/bridge.go @@ -19,7 +19,6 @@ type Bridge struct { } func (b *Bridge) getAPIPath(str ...string) (string, error) { - if strings.Index(strings.ToLower(b.Host), "http://") <= -1 && strings.Index(strings.ToLower(b.Host), "https://") <= -1 { b.Host = fmt.Sprintf("%s%s", "http://", b.Host) } @@ -33,6 +32,7 @@ func (b *Bridge) getAPIPath(str ...string) (string, error) { for _, p := range str { u.Path = path.Join(u.Path, p) } + return u.String(), nil } @@ -43,10 +43,10 @@ func (b *Bridge) Login(u string) *Bridge { } /* - - CONFIGURATION API - -*/ + * + * CONFIGURATION API + * + */ // GetConfig returns the bridge configuration func (b *Bridge) GetConfig() (*Config, error) { @@ -55,7 +55,6 @@ func (b *Bridge) GetConfig() (*Config, error) { // GetConfigContext returns the bridge configuration func (b *Bridge) GetConfigContext(ctx context.Context) (*Config, error) { - var config *Config url, err := b.getAPIPath("/config/") @@ -73,16 +72,15 @@ func (b *Bridge) GetConfigContext(ctx context.Context) (*Config, error) { return nil, err } - wl := make([]Whitelist, 0, len(config.WhitelistMap)) - for k, v := range config.WhitelistMap { + wl := make([]Whitelist, 0, len(*config.WhitelistMap)) + for k, v := range *config.WhitelistMap { v.Username = k wl = append(wl, v) } - config.Whitelist = wl + config.Whitelist = &wl return config, nil - } // CreateUser creates a user by adding n to the list of whitelists in the bridge. @@ -115,7 +113,6 @@ func (b *Bridge) CreateUserWithClientKeyContext(ctx context.Context, deviceType } func (b *Bridge) createUserWithContext(ctx context.Context, deviceType string, generateClientKey bool) (*Whitelist, error) { - var a []*APIResponse body := struct { @@ -168,7 +165,8 @@ func (b *Bridge) GetUsers() ([]Whitelist, error) { if err != nil { return nil, err } - return c.Whitelist, nil + + return *c.Whitelist, nil } // UpdateConfig updates the bridge configuration with c @@ -178,7 +176,6 @@ func (b *Bridge) UpdateConfig(c *Config) (*Response, error) { // UpdateConfigContext updates the bridge configuration with c func (b *Bridge) UpdateConfigContext(ctx context.Context, c *Config) (*Response, error) { - var a []*APIResponse url, err := b.getAPIPath("/config/") @@ -216,7 +213,6 @@ func (b *Bridge) DeleteUser(n string) error { // DeleteUserContext removes a whitelist item from whitelists on the bridge func (b *Bridge) DeleteUserContext(ctx context.Context, n string) error { - var a []*APIResponse url, err := b.getAPIPath("/config/whitelist/", n) @@ -237,7 +233,6 @@ func (b *Bridge) DeleteUserContext(ctx context.Context, n string) error { } return nil - } // GetFullState returns the entire bridge configuration. @@ -247,7 +242,6 @@ func (b *Bridge) GetFullState() (map[string]interface{}, error) { // GetFullStateContext returns the entire bridge configuration. func (b *Bridge) GetFullStateContext(ctx context.Context) (map[string]interface{}, error) { - var n map[string]interface{} url, err := b.getAPIPath("/") @@ -269,10 +263,10 @@ func (b *Bridge) GetFullStateContext(ctx context.Context) (map[string]interface{ } /* - - GROUP API - -*/ + * + * GROUP API + * + */ // GetGroups returns all groups known to the bridge func (b *Bridge) GetGroups() ([]Group, error) { @@ -281,7 +275,6 @@ func (b *Bridge) GetGroups() ([]Group, error) { // GetGroupsContext returns all groups known to the bridge func (b *Bridge) GetGroupsContext(ctx context.Context) ([]Group, error) { - var m map[string]Group url, err := b.getAPIPath("/groups/") @@ -311,7 +304,6 @@ func (b *Bridge) GetGroupsContext(ctx context.Context) ([]Group, error) { } return groups, err - } // GetGroup returns one group known to the bridge by its id @@ -321,7 +313,6 @@ func (b *Bridge) GetGroup(i int) (*Group, error) { // GetGroupContext returns one group known to the bridge by its id func (b *Bridge) GetGroupContext(ctx context.Context, i int) (*Group, error) { - g := &Group{ ID: i, } @@ -353,7 +344,6 @@ func (b *Bridge) SetGroupState(i int, l State) (*Response, error) { // SetGroupStateContext allows for setting the state of one group, controlling the state of all lights in that group. func (b *Bridge) SetGroupStateContext(ctx context.Context, i int, l State) (*Response, error) { - var a []*APIResponse id := strconv.Itoa(i) @@ -392,7 +382,6 @@ func (b *Bridge) UpdateGroup(i int, l Group) (*Response, error) { // UpdateGroupContext updates one group known to the bridge func (b *Bridge) UpdateGroupContext(ctx context.Context, i int, l Group) (*Response, error) { - var a []*APIResponse id := strconv.Itoa(i) @@ -431,7 +420,6 @@ func (b *Bridge) CreateGroup(g Group) (*Response, error) { // CreateGroupContext creates one new group with attributes defined by g func (b *Bridge) CreateGroupContext(ctx context.Context, g Group) (*Response, error) { - var a []*APIResponse url, err := b.getAPIPath("/groups/") @@ -471,7 +459,6 @@ func (b *Bridge) DeleteGroup(i int) error { func (b *Bridge) DeleteGroupContext(ctx context.Context, i int) error { var a []*APIResponse - id := strconv.Itoa(i) url, err := b.getAPIPath("/groups/", id) if err != nil { @@ -494,10 +481,10 @@ func (b *Bridge) DeleteGroupContext(ctx context.Context, i int) error { } /* - - LIGHT API - -*/ + * + * LIGHT API + * + */ // GetLights returns all lights known to the bridge func (b *Bridge) GetLights() ([]Light, error) { @@ -506,7 +493,6 @@ func (b *Bridge) GetLights() ([]Light, error) { // GetLightsContext returns all lights known to the bridge func (b *Bridge) GetLightsContext(ctx context.Context) ([]Light, error) { - m := map[string]Light{} url, err := b.getAPIPath("/lights/") @@ -536,7 +522,6 @@ func (b *Bridge) GetLightsContext(ctx context.Context) ([]Light, error) { } return lights, nil - } // GetLight returns one light with the id of i @@ -546,7 +531,6 @@ func (b *Bridge) GetLight(i int) (*Light, error) { // GetLightContext returns one light with the id of i func (b *Bridge) GetLightContext(ctx context.Context, i int) (*Light, error) { - light := &Light{ ID: i, } @@ -578,7 +562,6 @@ func (b *Bridge) SetLightState(i int, l State) (*Response, error) { // SetLightStateContext allows for controlling one light's state func (b *Bridge) SetLightStateContext(ctx context.Context, i int, l State) (*Response, error) { - var a []*APIResponse l.Reachable = false @@ -609,7 +592,6 @@ func (b *Bridge) SetLightStateContext(ctx context.Context, i int, l State) (*Res } return resp, nil - } // FindLights starts a search for new lights on the bridge. @@ -621,7 +603,6 @@ func (b *Bridge) FindLights() (*Response, error) { // FindLightsContext starts a search for new lights on the bridge. // Use GetNewLights() verify if new lights have been detected. func (b *Bridge) FindLightsContext(ctx context.Context) (*Response, error) { - var a []*APIResponse url, err := b.getAPIPath("/lights/") @@ -645,7 +626,6 @@ func (b *Bridge) FindLightsContext(ctx context.Context) (*Response, error) { } return resp, nil - } // GetNewLights returns a list of lights that were discovered last time FindLights() was executed. @@ -655,7 +635,6 @@ func (b *Bridge) GetNewLights() (*NewLight, error) { // GetNewLightsContext returns a list of lights that were discovered last time FindLights() was executed. func (b *Bridge) GetNewLightsContext(ctx context.Context) (*NewLight, error) { - var n map[string]interface{} url, err := b.getAPIPath("/lights/new") @@ -687,7 +666,6 @@ func (b *Bridge) GetNewLightsContext(ctx context.Context) (*NewLight, error) { } return result, nil - } // DeleteLight deletes one lights from the bridge @@ -697,7 +675,6 @@ func (b *Bridge) DeleteLight(i int) error { // DeleteLightContext deletes one lights from the bridge func (b *Bridge) DeleteLightContext(ctx context.Context, i int) error { - var a []*APIResponse id := strconv.Itoa(i) @@ -719,7 +696,6 @@ func (b *Bridge) DeleteLightContext(ctx context.Context, i int) error { } return nil - } // UpdateLight updates one light's attributes and state properties @@ -729,7 +705,6 @@ func (b *Bridge) UpdateLight(i int, light Light) (*Response, error) { // UpdateLightContext updates one light's attributes and state properties func (b *Bridge) UpdateLightContext(ctx context.Context, i int, light Light) (*Response, error) { - var a []*APIResponse id := strconv.Itoa(i) @@ -762,10 +737,10 @@ func (b *Bridge) UpdateLightContext(ctx context.Context, i int, light Light) (*R } /* - - RESOURCELINK API - -*/ + * + * RESOURCELINK API + * + */ // GetResourcelinks returns all resourcelinks known to the bridge func (b *Bridge) GetResourcelinks() ([]*Resourcelink, error) { @@ -774,7 +749,6 @@ func (b *Bridge) GetResourcelinks() ([]*Resourcelink, error) { // GetResourcelinksContext returns all resourcelinks known to the bridge func (b *Bridge) GetResourcelinksContext(ctx context.Context) ([]*Resourcelink, error) { - var r map[string]Resourcelink url, err := b.getAPIPath("/resourcelinks/") @@ -804,7 +778,6 @@ func (b *Bridge) GetResourcelinksContext(ctx context.Context) ([]*Resourcelink, } return resourcelinks, nil - } // GetResourcelink returns one resourcelink by its id defined by i @@ -814,7 +787,6 @@ func (b *Bridge) GetResourcelink(i int) (*Resourcelink, error) { // GetResourcelinkContext returns one resourcelink by its id defined by i func (b *Bridge) GetResourcelinkContext(ctx context.Context, i int) (*Resourcelink, error) { - g := &Resourcelink{ ID: i, } @@ -835,7 +807,6 @@ func (b *Bridge) GetResourcelinkContext(ctx context.Context, i int) (*Resourceli } return g, nil - } // CreateResourcelink creates one new resourcelink on the bridge @@ -845,7 +816,6 @@ func (b *Bridge) CreateResourcelink(s *Resourcelink) (*Response, error) { // CreateResourcelinkContext creates one new resourcelink on the bridge func (b *Bridge) CreateResourcelinkContext(ctx context.Context, s *Resourcelink) (*Response, error) { - var a []*APIResponse data, err := json.Marshal(&s) @@ -874,7 +844,6 @@ func (b *Bridge) CreateResourcelinkContext(ctx context.Context, s *Resourcelink) } return resp, nil - } // UpdateResourcelink updates one resourcelink with attributes defined by resourcelink @@ -921,7 +890,6 @@ func (b *Bridge) DeleteResourcelink(i int) error { // DeleteResourcelinkContext deletes one resourcelink with the id of i func (b *Bridge) DeleteResourcelinkContext(ctx context.Context, i int) error { - var a []*APIResponse id := strconv.Itoa(i) @@ -946,10 +914,10 @@ func (b *Bridge) DeleteResourcelinkContext(ctx context.Context, i int) error { } /* - - RULE API - -*/ + * + * RULE API + * + */ // GetRules returns all rules known to the bridge func (b *Bridge) GetRules() ([]*Rule, error) { @@ -958,7 +926,6 @@ func (b *Bridge) GetRules() ([]*Rule, error) { // GetRulesContext returns all rules known to the bridge func (b *Bridge) GetRulesContext(ctx context.Context) ([]*Rule, error) { - var r map[string]Rule url, err := b.getAPIPath("/rules/") @@ -988,7 +955,6 @@ func (b *Bridge) GetRulesContext(ctx context.Context) ([]*Rule, error) { } return rules, nil - } // GetRule returns one rule by its id of i @@ -998,7 +964,6 @@ func (b *Bridge) GetRule(i int) (*Rule, error) { // GetRuleContext returns one rule by its id of i func (b *Bridge) GetRuleContext(ctx context.Context, i int) (*Rule, error) { - g := &Rule{ ID: i, } @@ -1019,7 +984,6 @@ func (b *Bridge) GetRuleContext(ctx context.Context, i int) (*Rule, error) { } return g, nil - } // CreateRule creates one rule with attribues defined in s @@ -1029,7 +993,6 @@ func (b *Bridge) CreateRule(s *Rule) (*Response, error) { // CreateRuleContext creates one rule with attribues defined in s func (b *Bridge) CreateRuleContext(ctx context.Context, s *Rule) (*Response, error) { - var a []*APIResponse data, err := json.Marshal(&s) @@ -1058,7 +1021,6 @@ func (b *Bridge) CreateRuleContext(ctx context.Context, s *Rule) (*Response, err } return resp, nil - } // UpdateRule updates one rule by its id of i and rule configuration of rule @@ -1068,7 +1030,6 @@ func (b *Bridge) UpdateRule(i int, rule *Rule) (*Response, error) { // UpdateRuleContext updates one rule by its id of i and rule configuration of rule func (b *Bridge) UpdateRuleContext(ctx context.Context, i int, rule *Rule) (*Response, error) { - var a []*APIResponse data, err := json.Marshal(&rule) @@ -1106,7 +1067,6 @@ func (b *Bridge) DeleteRule(i int) error { // DeleteRuleContext deletes one rule from the bridge func (b *Bridge) DeleteRuleContext(ctx context.Context, i int) error { - var a []*APIResponse id := strconv.Itoa(i) @@ -1131,10 +1091,10 @@ func (b *Bridge) DeleteRuleContext(ctx context.Context, i int) error { } /* - - SCENE API - -*/ + * + * SCENE API + * + */ // GetScenes returns all scenes known to the bridge func (b *Bridge) GetScenes() ([]Scene, error) { @@ -1143,7 +1103,6 @@ func (b *Bridge) GetScenes() ([]Scene, error) { // GetScenesContext returns all scenes known to the bridge func (b *Bridge) GetScenesContext(ctx context.Context) ([]Scene, error) { - var m map[string]Scene url, err := b.getAPIPath("/scenes/") @@ -1166,7 +1125,6 @@ func (b *Bridge) GetScenesContext(ctx context.Context) ([]Scene, error) { } return scenes, err - } // GetScene returns one scene by its id of i @@ -1176,7 +1134,6 @@ func (b *Bridge) GetScene(i string) (*Scene, error) { // GetSceneContext returns one scene by its id of i func (b *Bridge) GetSceneContext(ctx context.Context, i string) (*Scene, error) { - g := &Scene{ID: i} l := struct { LightStates map[int]State `json:"lightstates"` @@ -1214,7 +1171,6 @@ func (b *Bridge) UpdateScene(id string, s *Scene) (*Response, error) { // UpdateSceneContext updates one scene and its attributes by id of i func (b *Bridge) UpdateSceneContext(ctx context.Context, id string, s *Scene) (*Response, error) { - var a []*APIResponse url, err := b.getAPIPath("/scenes/", id) @@ -1254,7 +1210,6 @@ func (b *Bridge) SetSceneLightState(id string, iid int, l *State) (*Response, er // SetSceneLightStateContext allows for setting the state of a light in a scene. // SetSceneLightStateContext accepts the id of the scene, the id of a light associated with the scene and the state object. func (b *Bridge) SetSceneLightStateContext(ctx context.Context, id string, iid int, l *State) (*Response, error) { - var a []*APIResponse lightid := strconv.Itoa(iid) @@ -1293,7 +1248,6 @@ func (b *Bridge) RecallScene(id string, gid int) (*Response, error) { // RecallSceneContext will recall a scene in a group identified by both scene and group identifiers func (b *Bridge) RecallSceneContext(ctx context.Context, id string, gid int) (*Response, error) { - var a []*APIResponse data, err := json.Marshal(struct { @@ -1334,7 +1288,6 @@ func (b *Bridge) CreateScene(s *Scene) (*Response, error) { // CreateSceneContext creates one new scene with its attributes defined in s func (b *Bridge) CreateSceneContext(ctx context.Context, s *Scene) (*Response, error) { - var a []*APIResponse data, err := json.Marshal(&s) @@ -1372,7 +1325,6 @@ func (b *Bridge) DeleteScene(id string) error { // DeleteSceneContext deletes one scene from the bridge func (b *Bridge) DeleteSceneContext(ctx context.Context, id string) error { - var a []*APIResponse url, err := b.getAPIPath("/scenes/", id) @@ -1396,10 +1348,10 @@ func (b *Bridge) DeleteSceneContext(ctx context.Context, id string) error { } /* - - SCHEDULE API - -*/ + * + * SCHEDULE API + * + */ // GetSchedules returns all schedules known to the bridge func (b *Bridge) GetSchedules() ([]*Schedule, error) { @@ -1408,7 +1360,6 @@ func (b *Bridge) GetSchedules() ([]*Schedule, error) { // GetSchedulesContext returns all schedules known to the bridge func (b *Bridge) GetSchedulesContext(ctx context.Context) ([]*Schedule, error) { - var r map[string]Schedule url, err := b.getAPIPath("/schedules/") @@ -1438,7 +1389,6 @@ func (b *Bridge) GetSchedulesContext(ctx context.Context) ([]*Schedule, error) { } return schedules, nil - } // GetSchedule returns one schedule by id defined in i @@ -1448,7 +1398,6 @@ func (b *Bridge) GetSchedule(i int) (*Schedule, error) { // GetScheduleContext returns one schedule by id defined in i func (b *Bridge) GetScheduleContext(ctx context.Context, i int) (*Schedule, error) { - g := &Schedule{ ID: i, } @@ -1469,7 +1418,6 @@ func (b *Bridge) GetScheduleContext(ctx context.Context, i int) (*Schedule, erro } return g, nil - } // CreateSchedule creates one schedule and sets its attributes defined in s @@ -1479,7 +1427,6 @@ func (b *Bridge) CreateSchedule(s *Schedule) (*Response, error) { // CreateScheduleContext creates one schedule and sets its attributes defined in s func (b *Bridge) CreateScheduleContext(ctx context.Context, s *Schedule) (*Response, error) { - var a []*APIResponse data, err := json.Marshal(&s) @@ -1508,7 +1455,6 @@ func (b *Bridge) CreateScheduleContext(ctx context.Context, s *Schedule) (*Respo } return resp, nil - } // UpdateSchedule updates one schedule by its id of i and attributes by schedule @@ -1518,7 +1464,6 @@ func (b *Bridge) UpdateSchedule(i int, schedule *Schedule) (*Response, error) { // UpdateScheduleContext updates one schedule by its id of i and attributes by schedule func (b *Bridge) UpdateScheduleContext(ctx context.Context, i int, schedule *Schedule) (*Response, error) { - var a []*APIResponse data, err := json.Marshal(&schedule) @@ -1556,7 +1501,6 @@ func (b *Bridge) DeleteSchedule(i int) error { // DeleteScheduleContext deletes one schedule from the bridge by its id of i func (b *Bridge) DeleteScheduleContext(ctx context.Context, i int) error { - var a []*APIResponse id := strconv.Itoa(i) @@ -1581,10 +1525,10 @@ func (b *Bridge) DeleteScheduleContext(ctx context.Context, i int) error { } /* - - SENSOR API - -*/ + * + * SENSOR API + * + */ // GetSensors returns all sensors known to the bridge func (b *Bridge) GetSensors() ([]Sensor, error) { @@ -1593,7 +1537,6 @@ func (b *Bridge) GetSensors() ([]Sensor, error) { // GetSensorsContext returns all sensors known to the bridge func (b *Bridge) GetSensorsContext(ctx context.Context) ([]Sensor, error) { - s := map[string]Sensor{} url, err := b.getAPIPath("/sensors/") @@ -1630,7 +1573,6 @@ func (b *Bridge) GetSensor(i int) (*Sensor, error) { // GetSensorContext returns one sensor by its id of i func (b *Bridge) GetSensorContext(ctx context.Context, i int) (*Sensor, error) { - r := &Sensor{ ID: i, } @@ -1652,7 +1594,6 @@ func (b *Bridge) GetSensorContext(ctx context.Context, i int) (*Sensor, error) { } return r, err - } // CreateSensor creates one new sensor @@ -1662,7 +1603,6 @@ func (b *Bridge) CreateSensor(s *Sensor) (*Response, error) { // CreateSensorContext creates one new sensor func (b *Bridge) CreateSensorContext(ctx context.Context, s *Sensor) (*Response, error) { - var a []*APIResponse data, err := json.Marshal(&s) @@ -1691,7 +1631,6 @@ func (b *Bridge) CreateSensorContext(ctx context.Context, s *Sensor) (*Response, } return resp, nil - } // FindSensors starts a search for new sensors. @@ -1703,7 +1642,6 @@ func (b *Bridge) FindSensors() (*Response, error) { // FindSensorsContext starts a search for new sensors. // Use GetNewSensorsContext() to verify if new sensors have been discovered in the bridge. func (b *Bridge) FindSensorsContext(ctx context.Context) (*Response, error) { - var a []*APIResponse url, err := b.getAPIPath("/sensors/") @@ -1727,7 +1665,6 @@ func (b *Bridge) FindSensorsContext(ctx context.Context) (*Response, error) { } return resp, nil - } // GetNewSensors returns a list of sensors that were discovered last time GetNewSensors() was executed. @@ -1737,7 +1674,6 @@ func (b *Bridge) GetNewSensors() (*NewSensor, error) { // GetNewSensorsContext returns a list of sensors that were discovered last time GetNewSensors() was executed. func (b *Bridge) GetNewSensorsContext(ctx context.Context) (*NewSensor, error) { - var n map[string]Sensor var result *NewSensor @@ -1774,7 +1710,6 @@ func (b *Bridge) GetNewSensorsContext(ctx context.Context) (*NewSensor, error) { resu := &NewSensor{sensors, result.LastScan} return resu, nil - } // UpdateSensor updates one sensor by its id and attributes by sensor @@ -1784,7 +1719,6 @@ func (b *Bridge) UpdateSensor(i int, sensor *Sensor) (*Response, error) { // UpdateSensorContext updates one sensor by its id and attributes by sensor func (b *Bridge) UpdateSensorContext(ctx context.Context, i int, sensor *Sensor) (*Response, error) { - var a []*APIResponse data, err := json.Marshal(&sensor) @@ -1822,7 +1756,6 @@ func (b *Bridge) DeleteSensor(i int) error { // DeleteSensorContext deletes one sensor from the bridge func (b *Bridge) DeleteSensorContext(ctx context.Context, i int) error { - var a []*APIResponse id := strconv.Itoa(i) @@ -1884,10 +1817,10 @@ func (b *Bridge) UpdateSensorConfigContext(ctx context.Context, i int, c interfa } /* - - CAPABILITIES API - -*/ + * + * CAPABILITIES API + * + */ // GetCapabilities returns a list of capabilities of resources supported in the bridge. func (b *Bridge) GetCapabilities() (*Capabilities, error) { @@ -1896,7 +1829,6 @@ func (b *Bridge) GetCapabilities() (*Capabilities, error) { // GetCapabilitiesContext returns a list of capabilities of resources supported in the bridge. func (b *Bridge) GetCapabilitiesContext(ctx context.Context) (*Capabilities, error) { - s := &Capabilities{} url, err := b.getAPIPath("/capabilities/") diff --git a/bridge_test.go b/bridge_test.go index 61a7edf..899e16e 100644 --- a/bridge_test.go +++ b/bridge_test.go @@ -28,7 +28,7 @@ func TestLogin(t *testing.T) { t.Fatal(err) } t.Logf("Logged in and got config which means that we are authorized") - t.Logf("Name: %s, SwVersion: %s", c.Name, c.SwVersion) + t.Logf("Name: %s, SwVersion: %s", *c.Name, *c.SwVersion) } func TestLoginUnauthorized(t *testing.T) { @@ -46,11 +46,10 @@ func TestLoginUnauthorized(t *testing.T) { func TestUpdateBridgeConfig(t *testing.T) { b := New(hostname, username) - c, err := b.GetConfig() - if err != nil { - t.Fatal(err) + c := Config{ + Name: strPtr("Test"), } - _, err = b.UpdateConfig(c) + _, err := b.UpdateConfig(&c) if err != nil { t.Fatal(err) } diff --git a/config.go b/config.go index 2f2fbbb..c4b7f12 100644 --- a/config.go +++ b/config.go @@ -2,44 +2,45 @@ package huego // Config holds the bridge hardware configuration type Config struct { - Name string `json:"name,omitempty"` - SwUpdate SwUpdate `json:"swupdate"` - SwUpdate2 SwUpdate2 `json:"swupdate2"` - WhitelistMap map[string]Whitelist `json:"whitelist"` - Whitelist []Whitelist `json:"-"` - PortalState PortalState `json:"portalstate"` - APIVersion string `json:"apiversion,omitempty"` - SwVersion string `json:"swversion,omitempty"` - ProxyAddress string `json:"proxyaddress,omitempty"` - ProxyPort uint16 `json:"proxyport,omitempty"` - LinkButton bool `json:"linkbutton,omitempty"` - IPAddress string `json:"ipaddress,omitempty"` - Mac string `json:"mac,omitempty"` - NetMask string `json:"netmask,omitempty"` - Gateway string `json:"gateway,omitempty"` - Dhcp bool `json:"dhcp,omitempty"` - PortalServices bool `json:"portalservices,omitempty"` - UTC string `json:"UTC,omitempty"` - LocalTime string `json:"localtime,omitempty"` - TimeZone string `json:"timezone,omitempty"` - ZigbeeChannel uint8 `json:"zigbeechannel,omitempty"` - ModelID string `json:"modelid,omitempty"` - BridgeID string `json:"bridgeid,omitempty"` - FactoryNew bool `json:"factorynew,omitempty"` - ReplacesBridgeID string `json:"replacesbridgeid,omitempty"` - DatastoreVersion string `json:"datastoreversion,omitempty"` - StarterKitID string `json:"starterkitid,omitempty"` - InternetService InternetService `json:"internetservices,omitempty"` + Name *string `json:"name,omitempty"` + SwUpdate *SwUpdate `json:"swupdate,omitempty"` + SwUpdate2 *SwUpdate2 `json:"swupdate2,omitempty"` + WhitelistMap *map[string]Whitelist `json:"whitelist,omitempty"` + Whitelist *[]Whitelist `json:"-"` + PortalState *PortalState `json:"portalstate,omitempty"` + APIVersion *string `json:"apiversion,omitempty"` + SwVersion *string `json:"swversion,omitempty"` + ProxyAddress *string `json:"proxyaddress,omitempty"` + ProxyPort *uint16 `json:"proxyport,omitempty"` + LinkButton *bool `json:"linkbutton,omitempty"` + IPAddress *string `json:"ipaddress,omitempty"` + Mac *string `json:"mac,omitempty"` + NetMask *string `json:"netmask,omitempty"` + Gateway *string `json:"gateway,omitempty"` + Dhcp *bool `json:"dhcp,omitempty"` + PortalServices *bool `json:"portalservices,omitempty"` + UTC *string `json:"UTC,omitempty"` + LocalTime *string `json:"localtime,omitempty"` + TimeZone *string `json:"timezone,omitempty"` + TouchLink *bool `json:"touchlink,omitempty"` + ZigbeeChannel *uint8 `json:"zigbeechannel,omitempty"` + ModelID *string `json:"modelid,omitempty"` + BridgeID *string `json:"bridgeid,omitempty"` + FactoryNew *bool `json:"factorynew,omitempty"` + ReplacesBridgeID *string `json:"replacesbridgeid,omitempty"` + DatastoreVersion *string `json:"datastoreversion,omitempty"` + StarterKitID *string `json:"starterkitid,omitempty"` + InternetService *InternetService `json:"internetservices,omitempty"` } // SwUpdate contains information related to software updates. Deprecated in 1.20 type SwUpdate struct { - CheckForUpdate bool `json:"checkforupdate,omitempty"` - DeviceTypes DeviceTypes `json:"devicetypes"` - UpdateState uint8 `json:"updatestate,omitempty"` - Notify bool `json:"notify,omitempty"` - URL string `json:"url,omitempty"` - Text string `json:"text,omitempty"` + CheckForUpdate *bool `json:"checkforupdate,omitempty"` + DeviceTypes *DeviceTypes `json:"devicetypes,omitempty"` + UpdateState *uint8 `json:"updatestate,omitempty"` + Notify *bool `json:"notify,omitempty"` + URL *string `json:"url,omitempty"` + Text *string `json:"text,omitempty"` } // SwUpdate2 contains information related to software updates diff --git a/config_test.go b/config_test.go index 6e82df3..2899b15 100644 --- a/config_test.go +++ b/config_test.go @@ -12,56 +12,72 @@ func TestGetConfig(t *testing.T) { if err != nil { t.Fatal(err) } - t.Logf("Name: %s", config.Name) + t.Logf("Name: %s", *config.Name) t.Logf("SwUpdate:") - t.Logf(" CheckForUpdate: %t", config.SwUpdate.CheckForUpdate) - t.Logf(" DeviceTypes:") - t.Logf(" Bridge: %t", config.SwUpdate.DeviceTypes.Bridge) - t.Logf(" Lights (length): %d", len(config.SwUpdate.DeviceTypes.Lights)) - t.Logf(" Sensors (length): %d", len(config.SwUpdate.DeviceTypes.Sensors)) - t.Logf(" UpdateState: %d", config.SwUpdate.UpdateState) - t.Logf(" Notify: %t", config.SwUpdate.Notify) - t.Logf(" URL: %s", config.SwUpdate.URL) - t.Logf(" Text: %s", config.SwUpdate.Text) + if config.SwUpdate != nil { + t.Logf(" CheckForUpdate: %t", *config.SwUpdate.CheckForUpdate) + t.Logf(" DeviceTypes:") + t.Logf(" Bridge: %t", config.SwUpdate.DeviceTypes.Bridge) + t.Logf(" Lights (length): %d", len(config.SwUpdate.DeviceTypes.Lights)) + t.Logf(" Sensors (length): %d", len(config.SwUpdate.DeviceTypes.Sensors)) + t.Logf(" UpdateState: %d", *config.SwUpdate.UpdateState) + t.Logf(" Notify: %t", *config.SwUpdate.Notify) + t.Logf(" URL: %s", *config.SwUpdate.URL) + t.Logf(" Text: %s", *config.SwUpdate.Text) + } t.Logf("SwUpdate2:") - t.Logf(" Bridge: %s", config.SwUpdate2.Bridge) - t.Logf(" State: %s", config.SwUpdate2.Bridge.State) - t.Logf(" LastInstall: %s", config.SwUpdate2.Bridge.LastInstall) - t.Logf(" CheckForUpdate: %t", config.SwUpdate2.CheckForUpdate) - t.Logf(" State: %s", config.SwUpdate2.State) - t.Logf(" Install: %t", config.SwUpdate2.Install) - t.Logf(" AutoInstall:") - t.Logf(" On: %t", config.SwUpdate2.AutoInstall.On) - t.Logf(" UpdateTime: %s", config.SwUpdate2.AutoInstall.UpdateTime) - t.Logf(" LastChange: %s", config.SwUpdate2.LastChange) - t.Logf(" LastInstall: %s", config.SwUpdate2.LastInstall) - t.Logf("Whitelist (length): %d", len(config.Whitelist)) + if config.SwUpdate2 != nil { + t.Logf(" Bridge: %s", config.SwUpdate2.Bridge) + t.Logf(" State: %s", config.SwUpdate2.Bridge.State) + t.Logf(" LastInstall: %s", config.SwUpdate2.Bridge.LastInstall) + t.Logf(" CheckForUpdate: %t", config.SwUpdate2.CheckForUpdate) + t.Logf(" State: %s", config.SwUpdate2.State) + t.Logf(" Install: %t", config.SwUpdate2.Install) + t.Logf(" AutoInstall:") + t.Logf(" On: %t", config.SwUpdate2.AutoInstall.On) + t.Logf(" UpdateTime: %s", config.SwUpdate2.AutoInstall.UpdateTime) + t.Logf(" LastChange: %s", config.SwUpdate2.LastChange) + t.Logf(" LastInstall: %s", config.SwUpdate2.LastInstall) + } + t.Logf("Whitelist (length): %d", len(*config.Whitelist)) t.Logf("PortalState:") t.Logf(" SignedOn: %t", config.PortalState.SignedOn) t.Logf(" Incoming: %t", config.PortalState.Incoming) t.Logf(" Outgoing: %t", config.PortalState.Outgoing) t.Logf(" Communication: %s", config.PortalState.Communication) - t.Logf("APIVersion: %s", config.APIVersion) - t.Logf("SwVersion: %s", config.SwVersion) - t.Logf("ProxyAddress: %s", config.ProxyAddress) - t.Logf("ProxyPort: %d", config.ProxyPort) - t.Logf("LinkButton: %t", config.LinkButton) - t.Logf("IPAddress: %s", config.IPAddress) - t.Logf("Mac: %s", config.Mac) - t.Logf("NetMask: %s", config.NetMask) - t.Logf("Gateway: %s", config.Gateway) - t.Logf("Dhcp: %t", config.Dhcp) - t.Logf("PortalServices: %t", config.PortalServices) - t.Logf("UTC: %s", config.UTC) - t.Logf("LocalTime: %s", config.LocalTime) - t.Logf("TimeZone: %s", config.TimeZone) - t.Logf("ZigbeeChannel: %d", config.ZigbeeChannel) - t.Logf("ModelID: %s", config.ModelID) - t.Logf("BridgeID: %s", config.BridgeID) - t.Logf("FactoryNew: %t", config.FactoryNew) - t.Logf("ReplacesBridgeID: %s", config.ReplacesBridgeID) - t.Logf("DatastoreVersion: %s", config.DatastoreVersion) - t.Logf("StarterKitID: %s", config.StarterKitID) + t.Logf("APIVersion: %s", *config.APIVersion) + t.Logf("SwVersion: %s", *config.SwVersion) + t.Logf("ProxyAddress: %s", *config.ProxyAddress) + t.Logf("ProxyPort: %d", *config.ProxyPort) + t.Logf("LinkButton: %t", *config.LinkButton) + t.Logf("IPAddress: %s", *config.IPAddress) + t.Logf("Mac: %s", *config.Mac) + t.Logf("NetMask: %s", *config.NetMask) + t.Logf("Gateway: %s", *config.Gateway) + t.Logf("Dhcp: %t", *config.Dhcp) + t.Logf("PortalServices: %t", *config.PortalServices) + t.Logf("UTC: %s", *config.UTC) + t.Logf("LocalTime: %s", *config.LocalTime) + t.Logf("TimeZone: %s", *config.TimeZone) + t.Logf("ZigbeeChannel: %d", *config.ZigbeeChannel) + if config.ModelID != nil { + t.Logf("ModelID: %s", *config.ModelID) + } + if config.BridgeID != nil { + t.Logf("BridgeID: %s", *config.BridgeID) + } + if config.FactoryNew != nil { + t.Logf("FactoryNew: %t", *config.FactoryNew) + } + if config.ReplacesBridgeID != nil { + t.Logf("ReplacesBridgeID: %s", *config.ReplacesBridgeID) + } + if config.DatastoreVersion != nil { + t.Logf("DatastoreVersion: %s", *config.DatastoreVersion) + } + if config.StarterKitID != nil { + t.Logf("StarterKitID: %s", *config.StarterKitID) + } } func TestGetConfigError(t *testing.T) { diff --git a/huego_test.go b/huego_test.go index c1188ff..fb22850 100644 --- a/huego_test.go +++ b/huego_test.go @@ -19,7 +19,6 @@ var hostname string var badHostname = "bad-hue-config" func init() { - hostname = os.Getenv("HUE_HOSTNAME") username = os.Getenv("HUE_USERNAME") @@ -436,7 +435,6 @@ func init() { httpmock.RegisterResponder("PUT", fmt.Sprintf("http://%s/api%s", badHostname, p), httpmock.NewBytesResponder(200, response)) httpmock.RegisterResponder("DELETE", fmt.Sprintf("http://%s/api%s", badHostname, p), httpmock.NewBytesResponder(200, response)) } - } func TestDiscoverAndLogin(t *testing.T) { @@ -471,3 +469,57 @@ func Test_unmarshalError(t *testing.T) { err := unmarshal([]byte(`not json`), s) assert.NotNil(t, err) } + +// strPtr returns pointer to a string. +func strPtr(val string) *string { + return &val +} + +// boolPtr returns pointer to an boolean. +func boolPtr(val bool) *bool { + return &val +} + +// intPtr returns pointer to an integer. +func intPtr(val int) *int { + return &val +} + +// uint8Ptr returns pointer to an uint8. +func uint8Ptr(val uint8) *uint8 { + return &val +} + +// uint16Ptr returns pointer to an uint16. +func uint16Ptr(val uint16) *uint16 { + return &val +} + +// float32Ptr returns pointer to a float32. +func float32Ptr(val float32) *float32 { + return &val +} + +// float64Ptr returns pointer to a float64. +func float64Ptr(val float64) *float64 { + return &val +} + +// TestPointers tests all the above pointer methods +func TestPointers(t *testing.T) { + s := "test" + b := true + i := int(123) + ui8 := uint8(123) + ui16 := uint16(123) + f32 := float32(1.234) + f64 := float64(1.234) + + assert.True(t, *strPtr(s) == s) + assert.True(t, *boolPtr(b) == b) + assert.True(t, *intPtr(i) == i) + assert.True(t, *uint8Ptr(ui8) == ui8) + assert.True(t, *uint16Ptr(ui16) == ui16) + assert.True(t, *float32Ptr(f32) == f32) + assert.True(t, *float64Ptr(f64) == f64) +}