def makeMsg(self, offset, laddr, group, linkType, data): msg = Msg.s_makeMessage("SendExtendedMessage") msg.setAddress("toAddress", InsteonAddress(self.getAddress())) msg.setByte("messageFlags", 0x1f) msg.setByte("command1", 0x2f) msg.setByte("command2", 0x00) msg.setByte("userData1", 0x00) # don't care info msg.setByte("userData2", 0x02) # set database msg.setByte("userData3", offset >> 8) # high byte msg.setByte("userData4", offset & 0xff) # low byte msg.setByte("userData5", 8) # number of bytes set: 1...8 msg.setByte("userData6", linkType) msg.setByte("userData7", group) msg.setByte("userData8", laddr.getHighByte()) msg.setByte("userData9", laddr.getMiddleByte()) msg.setByte("userData10", laddr.getLowByte()) # depends on mode: could be e.g. trigger point msg.setByte("userData11", data[0]) msg.setByte("userData12", data[1]) # unused? msg.setByte("userData13", data[2]) # unused? rb = msg.getBytes("command1", 15) checksum = (~sum(rb) + 1) & 0xFF msg.setByte("userData14", checksum) return msg
def getDDBCount(self): """getDDBCount() Data Base Delta flag gets incremented with any change in the Database """ iofun.writeMsg( message.createStdMsg(InsteonAddress(self.getAddress()), 0x0F, 0x1F, 0x01, -1))
def startManualChangeDown(self): """startManualChangeDown() start manual change, dim down""" iofun.writeMsg( message.createStdMsg(InsteonAddress(self.getAddress()), 0x0F, 0x17, 0x00, -1))
def incrementalBright(self): """incrementalBright() brighten light incrementally""" iofun.writeMsg( message.createStdMsg(InsteonAddress(self.getAddress()), 0x0F, 0x15, 0x00, -1))
def onFast(self, level=0xFF): """onFast(level) switch light on fast, to given level""" iofun.writeMsg( message.createStdMsg(InsteonAddress(self.getAddress()), 0x0F, 0x12, level, -1))
def off(self): """off() switch off""" iofun.writeMsg(message.createStdMsg( InsteonAddress(self.getAddress()), 0x0F, 0x13, 0x00, -1))
def rampRateOn(self, level=0xFF, rate = 0x1f): """rampRateOn(level, rate) switch on to given light level (0-255) at given rate (0-31)""" cmd2 = (level & 0xf0) | ((rate & 0x1f) >> 1); iofun.writeMsg(message.createStdMsg( InsteonAddress(self.getAddress()), 0x0F, 0x2e, cmd2, -1))
def addDev(dev): devNameMap[dev.getName()] = dev addr = InsteonAddress(dev.getAddress()).toString() devAddressMap[addr] = dev
def instantOn(self, level): """instantOn(level) switch on instantly to level""" iofun.writeMsg( message.createStdMsg(InsteonAddress(self.getAddress()), 0x0F, 0x21, level, -1))
def fastOff(self): """fastOff() switch off immediately""" iofun.writeMsg( message.createStdMsg(InsteonAddress(self.getAddress()), 0x0F, 0x14, 0, -1))
def fastOn(self, level): """fastOn(level) switch immediately to level""" iofun.writeMsg( message.createStdMsg(InsteonAddress(self.getAddress()), 0x0F, 0x12, level, -1))
def startWatch(self): """startWatch() modem will print all incoming messages on terminal""" self.querier = Querier(InsteonAddress("00.00.00")) self.querier.setMsgHandler(MsgDumper("modem")) self.querier.startWait(10000)
def beep(self): """beep() sends beep command to the device""" msg = message.createStdMsg(InsteonAddress(self.address), 0x0f, 0x30, 0x00, -1) iofun.writeMsg(msg)
def sendext2(self, cmd1, cmd2, data): msg = message.createExtendedMsg2(InsteonAddress(self.address), cmd1, cmd2, data) self.__sendMsg(msg) return msg
def getActiveRecordsAsArray(self): searchRec = {"offset" : 0, "addr": InsteonAddress("00.00.00"), "type" : 0, "group" : 0, "data" : [0,0,0]} active = self.findActiveRecords(searchRec, False, False, False) return active
def instantOff(self): """instantOff() switch off instantly""" iofun.writeMsg( message.createStdMsg(InsteonAddress(self.getAddress()), 0x0F, 0x21, 0, -1))
def removeAddress(self, addr): searchRec = {"offset" : 0, "addr": InsteonAddress(addr), "type" : 0, "group" : 0, "data" : [0,0,0]} remove = self.findActiveRecords(searchRec, True, False, False) for i in remove: del self.records[i["offset"]]
def __init__(self, dev, linkAddr, g, level, ramprate, button, isContr): self.dev = dev self.linkAddr = InsteonAddress(linkAddr) self.group = g self.data = [level, ramprate, button] self.isController = isContr
def on(self, level=0xFF): """on(level) switch on to given light level""" iofun.writeMsg(message.createStdMsg( InsteonAddress(self.getAddress()), 0x0F, 0x11, level, -1))
def addEmptyRecordAtEnd(self, db): above, stopaddr, below = db.findStopRecordAddresses() self.dev.setRecord(below, InsteonAddress("00.00.00"), 0x00, 0x00, [0, 0, 0]) return stopaddr
def rampRateOff(self, rate = 0x1f): """rampRateOff(rate) switch off at rate (0-31)""" cmd2 = (rate & 0x1f) >> 1 iofun.writeMsg(message.createStdMsg( InsteonAddress(self.getAddress()), 0x0F, 0x2f, cmd2, -1))
def __init__(self, dev, linkAddr): self.dev = dev self.linkAddr = InsteonAddress(linkAddr)
def beep(self): """beep() make device beep""" iofun.writeMsg(message.createStdMsg( InsteonAddress(self.getAddress()), 0x0F, 0x30, 0x00, -1))
class Device: name = "" address = InsteonAddress() db = None dbbuilder = None querier = None def __init__(self, name, addr): self.name = name self.address = addr self.db = DB() self.querier = Querier(addr) addDev(self) def modifyDB(self, listener): self.dbbuilder.setListener(listener) # after db download complete, listener will perform action self.getdb() def setRecord(self, offset, laddr, group, linkType, data): msg = self.makeMsg(offset, laddr, group, linkType,data) self.querier.setMsgHandler(MsgHandler("got set record")) self.querier.sendMsg(msg) def makeMsg(self, offset, laddr, group, linkType, data): msg = Msg.s_makeMessage("SendExtendedMessage") msg.setAddress("toAddress", InsteonAddress(self.getAddress())) msg.setByte("messageFlags", 0x1f) msg.setByte("command1", 0x2f) msg.setByte("command2", 0x00) msg.setByte("userData1", 0x00) # don't care info msg.setByte("userData2", 0x02) # set database msg.setByte("userData3", offset >> 8) # high byte msg.setByte("userData4", offset & 0xff) # low byte msg.setByte("userData5", 8) # number of bytes set: 1...8 msg.setByte("userData6", linkType) msg.setByte("userData7", group) msg.setByte("userData8", laddr.getHighByte()) msg.setByte("userData9", laddr.getMiddleByte()) msg.setByte("userData10", laddr.getLowByte()) # depends on mode: could be e.g. trigger point msg.setByte("userData11", data[0]) msg.setByte("userData12", data[1]) # unused? msg.setByte("userData13", data[2]) # unused? rb = msg.getBytes("command1", 15); checksum = (~sum(rb) + 1) & 0xFF msg.setByte("userData14", checksum) return msg def getName(self): return self.name def getAddress(self): return self.address def getdb(self): """getdb() download the device database and print it on the console""" iofun.out("getting db, be patient!") self.dbbuilder.clear() self.dbbuilder.start() def printdb(self): """printdb() print the downloaded link database to the console""" self.dbbuilder.printdb() def getId(self): """getId() get category, subcategory, firmware, hardware version""" self.querier.setMsgHandler(IdMsgHandler("id")) self.querier.querysd(0x10, 0x00) # # link database management # def addController(self, addr, group, data = None): """addController(addr, group[, data]) add device with "addr" as controller for group "group", with link data "data" """ data = data if data else [00, 00, group]; self.modifyDB(LinkRecordAdder(self, addr, group, data, True)) def removeController(self, addr, group): """removeController(addr, group) remove device with "addr" as controller for group "group", with link data "data" """ self.modifyDB(LinkRecordRemover(self, addr, group, True)) def addResponder(self, addr, group, data = None): """addResponder(addr, group[, data]) add device with "addr" as responder for group "group", with link data "data" """ data = data if data else [00, 00, group]; self.modifyDB(LinkRecordAdder(self, addr, group, data, False)) def removeResponder(self, addr, group): """removeResponder(addr, group) remove device with "addr" as responder for group "group" """ self.modifyDB(LinkRecordRemover(self, addr, group, False)) def removeDevice(self, addr): """removeDevice(addr): removes all links to device with address "addr" from device database""" self.modifyDB(DeviceRemover(self, addr)) def replaceDevice(self, oldAddr, newAddr): """replaceDevice(oldAddr, newAddr): replaces all linkdb occurrences of oldAddr with newAddr """ self.dbbuilder.setListener(AddressReplacer(self, oldAddr, newAddr)) # after db download is complete, listener will perform action self.getdb() def removeLastRecord(self): """removeLastRecord() removes the last device in the link database""" self.modifyDB(LastRecordRemover(self)) def nukeDB(self): """nukeDB() really WIPES OUT all records in the device's database!""" self.modifyDB(LastNRecordRemover(self, -1)) def setOnLevelResponder(self, addr, group, level, ramprate = 28, button = 1): """setOnLevelResponder(addr, group, level, ramprate = 28, button = 1) sets (on level, ramp rate, button) for controller with "addr" and group "group" """ self.modifyDB(OnLevelModifier(self, addr, group, level, ramprate, button, False)) def enterLinkingMode(self, group): """enterLinkingMode(group) causes the device to enter linking mode""" self.querier.setMsgHandler(MsgHandler("enter linking mode")) self.querier.querysd(0x09, group); def exitLinkingMode(self): """exitLinkingMode() causes the device to exit linking mode""" self.querier.setMsgHandler(MsgHandler("exit linking mode")) self.querier.querysd(0x08, 0x01); def enterUnlinkingMode(self, group): """enterUnlinkingMode(group) causes the device to enter unlinking mode""" self.querier.setMsgHandler(MsgHandler("enter unlinking mode")) self.querier.querysd(0x0A, group);
def offFast(self): """offFast() switch light off fast""" iofun.writeMsg( message.createStdMsg(InsteonAddress(self.getAddress()), 0x0F, 0x14, 0x00, -1))
def __init__(self, dev, linkAddr, g, isContr): self.dev = dev self.linkAddr = InsteonAddress(linkAddr) self.group = g; self.isController = isContr
def incrementalDim(self): """incrementalDim() dim light incrementally""" iofun.writeMsg( message.createStdMsg(InsteonAddress(self.getAddress()), 0x0F, 0x16, 0x00, -1))
def __init__(self, dev, oldAddr, newAddr): self.dev = dev self.oldAddr = InsteonAddress(oldAddr) self.newAddr = InsteonAddress(newAddr)
def stopManualChange(self): """stopManualChange() stop manual change""" iofun.writeMsg( message.createStdMsg(InsteonAddress(self.getAddress()), 0x0F, 0x18, 0x00, -1))
def getFlags(self): """getFlags() Reads and displays operating flags""" iofun.writeMsg( message.createStdMsg(InsteonAddress(self.getAddress()), 0x0F, 0x1F, 0x00, -1))