func TestGetCharacteristic(t *testing.T) { info := accessory.Info{ Name: "My Bridge", SerialNumber: "001", Manufacturer: "Google", Model: "Bridge", } a := accessory.New(info, accessory.TypeBridge) m := accessory.NewContainer() m.AddAccessory(a) aid := a.GetID() cid := a.Info.Name.GetID() values := idsString(aid, cid) controller := NewCharacteristicController(m) res, err := controller.HandleGetCharacteristics(values) if err != nil { t.Fatal(err) } b, err := ioutil.ReadAll(res) if err != nil { t.Fatal(err) } var chars data.Characteristics err = json.Unmarshal(b, &chars) if err != nil { t.Fatal(err) } if is, want := len(chars.Characteristics), 1; is != want { t.Fatalf("is=%v want=%v", is, want) } if x := chars.Characteristics[0].Value; x != "My Bridge" { t.Fatal(x) } }
func TestCharacteristicNotification(t *testing.T) { a := accessory.New(info, accessory.TypeOther) c := accessory.NewContainer() c.AddAccessory(a) buffer, err := Body(a, a.Info.Name.Characteristic) if err != nil { t.Fatal(err) } bytes, err := ioutil.ReadAll(buffer) if err != nil { t.Fatal(err) } if is, want := string(bytes), `{"characteristics":[{"aid":1,"iid":5,"value":"My Bridge"}]}`; is != want { t.Fatalf("is=%v want=%v", is, want) } }
func setupHomeKit() { aInfo := accessory.Info{ Name: "Hive Bridge", Manufacturer: "British Gas PLC", } a := accessory.New(aInfo, accessory.TypeBridge) tInfo := accessory.Info{ Name: "Heating", Manufacturer: "British Gas PLC", } t := accessory.NewThermostat(tInfo, 20.0, hive.MinTemp, hive.MaxTemp, 0.5) t.Thermostat.TargetTemperature.OnValueRemoteUpdate(targetTempChangeRequest) thermostat = t bInfo := accessory.Info{ Name: "Heating Boost", Manufacturer: "British Gas PLC", } b := accessory.NewSwitch(bInfo) b.Switch.On.OnValueRemoteUpdate(heatingBoostStateChangeRequest) heatingBoostSwitch = b sInfo := accessory.Info{ Name: "Hot Water", Manufacturer: "British Gas PLC", } h := accessory.NewSwitch(sInfo) h.Switch.On.OnValueRemoteUpdate(hotWaterStateChangeRequest) hotWaterSwitch = h config := hap.Config{ Pin: pin, } var err error transport, err = hap.NewIPTransport(config, a, t.Accessory, b.Accessory, h.Accessory) if err != nil { log.Fatal(err) } }
func TestCharacteristicNotificationResponse(t *testing.T) { a := accessory.New(info, accessory.TypeOther) resp, err := NewCharacteristicNotification(a, a.Info.Name.Characteristic) if err != nil { t.Fatal(err) } var buffer = new(bytes.Buffer) resp.Write(buffer) bytes, err := ioutil.ReadAll(buffer) if err != nil { t.Fatal(err) } bytes = FixProtocolSpecifier(bytes) if x := string(bytes); strings.HasPrefix(x, "EVENT/1.0 200 OK") == false { t.Fatal(x) } }