def gateway(request, testUtil, gatewayDeviceType, authToken): """ One gateway will be created per testcase (per function) """ # Max length is 36, chop off some characters if test name is too long gatewayId = str(uuid.uuid4()) gatewayUid = "d:%s:%s:%s" % (testUtil.ORG_ID, gatewayDeviceType.id, gatewayId) # Cleanup any old devices to ensure auth token will be correct for test if gatewayId in gatewayDeviceType.devices: logger.debug("Deleting device %s to start fresh for test %s" % (gatewayUid, request.function.__name__)) testUtil.appClient.registry.devices.delete({"typeId": gatewayDeviceType.id, "deviceId": gatewayId}) logger.debug("Creating device %s for test %s" % (gatewayUid, request.function.__name__)) try: deviceReq = DeviceCreateRequest(typeId=gatewayDeviceType.id, deviceId=gatewayId, authToken=authToken) gatewayCreateResponse = testUtil.appClient.registry.devices.create(deviceReq) gateway = testUtil.appClient.registry.devices[gatewayUid] gateway["authToken"] = gatewayCreateResponse.authToken except ApiException as ex: logging.exception("Unable to register device for test %s. API response: %s" % (request.function.__name__, ex.message)) yield gateway # Cleanup device after test is finished testUtil.appClient.registry.devices.delete({"typeId": gateway.typeId, "deviceId": gateway.deviceId})
def device(request, testUtil, deviceType, authToken): # Max length is 36, chop off some characters if test name is too long deviceId = str(uuid.uuid4()) deviceUid = "d:%s:%s:%s" % (testUtil.ORG_ID, deviceType.id, deviceId) # Cleanup any old devices to ensure auth token will be correct for test if deviceId in deviceType.devices: logger.debug("Deleting device %s to start fresh for test %s" % (deviceUid, request.function.__name__)) testUtil.appClient.registry.devices.delete({ "typeId": deviceType.id, "deviceId": deviceId }) logger.debug("Creating device %s for test %s" % (deviceUid, request.function.__name__)) try: deviceReq = DeviceCreateRequest(typeId=deviceType.id, deviceId=deviceId, authToken=authToken) testUtil.appClient.registry.devices.create(deviceReq) device = testUtil.appClient.registry.devices[deviceUid] except ApiException as ex: logging.exception( "Unable to register device for test %s. API response: %s" % (request.function.__name__, ex.message)) yield device # Cleanup device after test is finished testUtil.appClient.registry.devices.delete({ "typeId": deviceType.id, "deviceId": deviceId })
def testCreateAndUpdate1Device(self, deviceType): deviceUid = DeviceCreateRequest( typeId=deviceType.id, deviceId=str(uuid.uuid4()), authToken="NotVerySecretPassw0rd", deviceInfo=DeviceInfo(serialNumber="123", descriptiveLocation="Floor 3, Room 2") ) assert deviceUid.authToken == "NotVerySecretPassw0rd" assert deviceUid.location is None assert deviceUid.metadata is None deviceCreateResponse = self.appClient.registry.devices.create(deviceUid) assert deviceCreateResponse.typeId == deviceUid.typeId assert deviceCreateResponse.deviceId == deviceUid.deviceId assert deviceCreateResponse.success == True assert deviceCreateResponse.authToken == deviceUid.authToken assert deviceUid.deviceId in deviceType.devices deviceAfterCreate = deviceType.devices[deviceUid.deviceId] assert deviceAfterCreate.deviceInfo.serialNumber == "123" assert deviceAfterCreate.deviceInfo.descriptiveLocation == "Floor 3, Room 2" self.appClient.registry.devices.update(deviceUid, metadata={"foo": "bar"}) deviceAfterUpdate = deviceType.devices[deviceUid.deviceId] assert "foo" in deviceAfterUpdate.metadata assert deviceAfterUpdate.metadata["foo"] == "bar" assert deviceAfterUpdate.deviceInfo.description is None assert deviceAfterUpdate.deviceInfo.model is None # Update description only self.appClient.registry.devices.update(deviceUid, deviceInfo={"description": "hello"}) assert deviceType.devices[deviceUid.deviceId].deviceInfo.description == "hello" # Update model, verify that description wasn't wiped self.appClient.registry.devices.update(deviceUid, deviceInfo=DeviceInfo(model="foobar")) deviceAfter3rdUpdate = deviceType.devices[deviceUid.deviceId] assert "foo" in deviceAfter3rdUpdate.metadata assert deviceAfter3rdUpdate.metadata["foo"] == "bar" assert deviceAfter3rdUpdate.deviceInfo.description == "hello" assert deviceAfter3rdUpdate.deviceInfo.model == "foobar" assert deviceAfter3rdUpdate.deviceInfo.descriptiveLocation == "Floor 3, Room 2" assert deviceAfter3rdUpdate.deviceInfo.deviceClass == None assert deviceAfter3rdUpdate.deviceInfo.fwVersion == None assert deviceAfter3rdUpdate.deviceInfo.hwVersion == None assert deviceAfter3rdUpdate.deviceInfo.manufacturer== None assert str(deviceAfter3rdUpdate) == "[%s] hello" % (deviceAfter3rdUpdate.clientId) # Cleanup self.appClient.registry.devices.delete({"typeId": deviceUid.typeId, "deviceId": deviceUid.deviceId}) assert deviceUid.deviceId not in deviceType.devices
def testCreateAndUpdate1Device(self, deviceType): deviceUid = DeviceCreateRequest( typeId=deviceType.id, deviceId=str(uuid.uuid4()), authToken="NotVerySecretPassw0rd", deviceInfo=DeviceInfo(serialNumber="123", descriptiveLocation="Floor 3, Room 2")) self.appClient.registry.devices.create(deviceUid) assert_true(deviceUid.deviceId in deviceType.devices) deviceAfterCreate = deviceType.devices[deviceUid.deviceId] assert_equals("123", deviceAfterCreate.deviceInfo.serialNumber) assert_equals("Floor 3, Room 2", deviceAfterCreate.deviceInfo.descriptiveLocation) self.appClient.registry.devices.update(deviceUid, metadata={"foo": "bar"}) deviceAfterUpdate = deviceType.devices[deviceUid.deviceId] assert_true("foo" in deviceAfterUpdate.metadata) assert_equals("bar", deviceAfterUpdate.metadata["foo"]) assert_equals(None, deviceAfterUpdate.deviceInfo.description) assert_equals(None, deviceAfterUpdate.deviceInfo.model) # Update description only self.appClient.registry.devices.update( deviceUid, deviceInfo={"description": "hello"}) assert_equals( "hello", deviceType.devices[deviceUid.deviceId].deviceInfo.description) # Update model, verify that description wasn't wiped self.appClient.registry.devices.update( deviceUid, deviceInfo=DeviceInfo(model="foobar")) deviceAfter3rdUpdate = deviceType.devices[deviceUid.deviceId] assert_true("foo" in deviceAfter3rdUpdate.metadata) assert_equals("bar", deviceAfter3rdUpdate.metadata["foo"]) assert_equals("hello", deviceAfter3rdUpdate.deviceInfo.description) assert_equals("foobar", deviceAfter3rdUpdate.deviceInfo.model) assert_equals("Floor 3, Room 2", deviceAfter3rdUpdate.deviceInfo.descriptiveLocation) # Cleanup self.appClient.registry.devices.delete({ "typeId": deviceUid.typeId, "deviceId": deviceUid.deviceId }) assert_false(deviceUid.deviceId in deviceType.devices)
def testDeleteDevice(self, deviceType): deviceUid = DeviceCreateRequest( typeId=deviceType.id, deviceId=str(uuid.uuid4()), authToken="NotVerySecretPassw0rd", deviceInfo=DeviceInfo(serialNumber="123", descriptiveLocation="Floor 3, Room 2"), ) assert deviceUid.authToken == "NotVerySecretPassw0rd" assert deviceUid.location is None assert deviceUid.metadata is None deviceCreateResponse = self.appClient.registry.devices.create(deviceUid) assert deviceCreateResponse.typeId == deviceUid.typeId assert deviceCreateResponse.deviceId == deviceUid.deviceId assert deviceCreateResponse.success == True assert deviceCreateResponse.authToken == deviceUid.authToken assert deviceUid.deviceId in deviceType.devices del self.appClient.registry.devicetypes[deviceUid.typeId].devices[deviceUid.deviceId] assert deviceUid.deviceId not in deviceType.devices
def _poll(self): self.logger.info("Running poll loop (%s second interval) ... " % self.pollingInterval) self.lightify.update_all_light_status() lights = self.lightify.lights() for key in lights.keys(): #print(key) #print(format(key, 'x')) #print('-'.join(format(key, 'x')[i:i+2] for i in range(0,16,2))) light = lights[key] typeId = "lightify-%s" % light.devicetype().name deviceId = light.addr() deviceRegistry = self.client.registry # Register the device type if we need to if typeId not in self.knownDeviceTypes: if typeId in deviceRegistry.devicetypes: deviceType = deviceRegistry.devicetypes[typeId] self.knownDeviceTypes[typeId] = deviceType else: self.logger.info("Registering new device type: %s" % (typeId)) deviceType = deviceRegistry.devicetypes.create( {"id": typeId}) self.knownDeviceTypes[typeId] = deviceType # Register the device if we need to if deviceId not in self.knownDevices: if deviceId in deviceRegistry.devicetypes[typeId].devices: device = deviceRegistry.devicetypes[typeId].devices[ deviceId] self.knownDevices[deviceId] = device else: self.logger.info("Registering new device: %s:%s" % (typeId, deviceId)) createData = DeviceCreateRequest( typeId=typeId, deviceId=deviceId, deviceInfo=DeviceInfo(model=light.devicesubtype().name, fwVersion=light.version()), metadata={ "lightify-gateway": { "version": self.version } }) device = deviceRegistry.devices.create(createData) self.knownDevices[deviceId] = device # # light. # name() # idx() # addr() # reachable() # last_seen() # on() # lum() # temp() # red() # green() # blue() # rgb() # devicesubtype() # devicetype() # devicename() # version() # Alpha is only available from raw_values() (onoff, lum, temp, red, green, blue, alpha) = light.raw_values() # if online is false then override on to be false, the light can't be on if it's not even powered on reallyOn = (light.reachable() and light.on()) state = { "online": light.reachable(), "lastSeen": light.last_seen(), "on": reallyOn, "lum": light.lum() if reallyOn else 0, "temp": light.temp(), "colour": { "red": light.red(), "green": light.green(), "blue": light.blue() }, "alpha": alpha } # Publish the current state of the light self.client.publishEvent(typeId, deviceId, "state", "json", state) self.logger.debug("Published event for %s:%s: %s" % (typeId, deviceId, json.dumps(state)))