def deviceInfo(self): # Unpack the deviceInfo dictionary into keyword arguments so that we # can return a DeviceInfo object instead of a plain dictionary if "deviceInfo" in self: return DeviceInfo(**self["deviceInfo"]) else: return DeviceInfo()
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 testUpdateDeviceInfo(self, deviceType): self.appClient.registry.devicetypes.update( deviceType.id, deviceInfo=DeviceInfo(serialNumber="111")) updatedDeviceType = self.appClient.registry.devicetypes[deviceType.id] assert updatedDeviceType.deviceInfo.serialNumber == "111"
def testCreatePreReqs(self): # LI test_schema_name = TestDeviceState.testLiSchemaName assert self.doesSchemaNameExist(test_schema_name) == False testLIName = TestDeviceState.testLogicalInterfaceName assert self.doesLINameExist(testLIName) == False # Create a schema TestDeviceState.createdLISchema = self.createSchema( test_schema_name, "liSchema.json", TestDeviceState.testLISchema, "Test schema description") # Create a Logical Interface TestDeviceState.createdLI = self.createLI( testLIName, "Test Logical Interface description", TestDeviceState.createdLISchema.id) # PI test_schema_name = TestDeviceState.testEventSchemaName assert self.doesSchemaNameExist(test_schema_name) == False test_eventType_name = TestDeviceState.testEventTypeName assert self.doesEventTypeNameExist(test_eventType_name) == False testPIName = TestDeviceState.testPhysicalInterfaceName assert self.doesPINameExist(testPIName) == False # Create a schema TestDeviceState.createdEventSchema = self.createSchema( test_schema_name, "eventSchema.json", TestDeviceState.testEventSchema, "Test schema description") # Create an eventType TestDeviceState.createdEventType = self.createEventType( test_eventType_name, "Test event type description", TestDeviceState.createdEventSchema.id) # Create a Physical Interface TestDeviceState.createdPI = self.createPI( testPIName, "Test Physical Interface description") # Associate event with PI TestDeviceState.createdPI.events.create({ "eventId": TestDeviceState.testEventId, "eventTypeId": TestDeviceState.createdEventType.id }) test_dt_name = TestDeviceState.testDeviceTypeName assert self.doesDTNameExist(test_dt_name) == False # Create a Device Type TestDeviceState.createdDT = self.createAndCheckDT( test_dt_name, "Test Device Type description") # Associate PI TestDeviceState.createdDT.physicalInterface = TestDeviceState.createdPI # Associate the LI TestDeviceState.createdDT.logicalInterfaces.create( TestDeviceState.createdLI) # Mappings TestDeviceState.createdDT.mappings.create({ "logicalInterfaceId": TestDeviceState.createdLI.id, "notificationStrategy": "on-state-change", "propertyMappings": { TestDeviceState.testEventId: { "temperature": "$event.temperature", "humidity": "$event.humidity", "publishTimestamp": "$event.publishTimestamp", } }, }) # Validate and Activate the LI TestDeviceState.createdDT.validate() TestDeviceState.createdDT.activate() # register a device TestDeviceState.createdDevice = TestDeviceState.createdDT.devices.create( { "deviceId": str(uuid.uuid4()), "authToken": "NotVerySecretPassw0rd", "deviceInfo": DeviceInfo(serialNumber="123", descriptiveLocation="Floor 3, Room 2"), })
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)))