def test_Lighting1(self): x = lowlevel.Lighting1() self.assertIsNone(x.subtype) self.assertIsNone(x.seqnbr) self.assertIsNone(x.id_string) self.assertIsNone(x.cmnd) self.assertIsNone(x.rssi) x.load_receive( bytearray([0x07, 0x10, 0x00, 0x2a, 0x45, 0x05, 0x01, 0x70])) self.assertEqual(x.seqnbr, 42) self.assertEqual(x.id_string, "E5") self.assertEqual(list(x.data), [7, 16, 0, 42, 69, 5, 1, 112]) self.assertEqual(x.packetlength, 7) self.assertEqual(x.packettype, 16) self.assertEqual(x.subtype, 0) self.assertEqual(x.type_string, "X10 lighting") self.assertEqual(x.seqnbr, 42) self.assertEqual(x.housecode, 69) self.assertEqual(x.unitcode, 5) self.assertEqual(x.cmnd, 1) self.assertEqual(x.cmnd_string, "On") self.assertEqual(x.rssi_byte, 112) self.assertEqual(x.rssi, 7) self.assertEqual( x.__str__(), 'Lighting1 [subtype=X10 lighting, seqnbr=42, id=E5, cmnd=On, rssi=7]' ) self.assertTrue(x.has_type_string) x = lowlevel.Lighting1() x.set_transmit(0x00, 0x2a, 0x45, 0x05, 0x01) self.assertEqual(x.seqnbr, 42) self.assertEqual(x.id_string, "E5") self.assertEqual(list(x.data), [7, 16, 0, 42, 69, 5, 1, 0]) self.assertEqual(x.packetlength, 7) self.assertEqual(x.packettype, 16) self.assertEqual(x.subtype, 0) self.assertEqual(x.type_string, "X10 lighting") self.assertEqual(x.seqnbr, 42) self.assertEqual(x.housecode, 69) self.assertEqual(x.unitcode, 5) self.assertEqual(x.cmnd, 1) self.assertEqual(x.cmnd_string, "On") self.assertEqual(x.rssi_byte, 0) self.assertEqual(x.rssi, 0) x.parse_id(0, "E13") self.assertEqual(x.housecode, 69) self.assertEqual(x.unitcode, 13) self.assertRaises(ValueError, x.parse_id, 0, "Q1") self.assertRaises(ValueError, x.parse_id, 0, "AA")
def send_onoff(self, transport, on): """ Send an 'On' or 'Off' command using the given transport """ if self.packettype == 0x10: # Lighting1 pkt = lowlevel.Lighting1() pkt.set_transmit(self.subtype, 0, self.housecode, self.unitcode, on and 0x01 or 0x00) transport.send(pkt.data) elif self.packettype == 0x11: # Lighting2 pkt = lowlevel.Lighting2() pkt.set_transmit(self.subtype, 0, self.id_combined, self.unitcode, on and 0x01 or 0x00, 0x00) transport.send(pkt.data) elif self.packettype == 0x12: # Lighting3 pkt = lowlevel.Lighting3() pkt.set_transmit(self.subtype, 0, self.system, self.channel, on and 0x10 or 0x1a) transport.send(pkt.data) elif self.packettype == 0x14: # Lighting5 pkt = lowlevel.Lighting5() pkt.set_transmit(self.subtype, 0, self.id_combined, self.unitcode, on and 0x01 or 0x00, 0x00) transport.send(pkt.data) elif self.packettype == 0x15: # Lighting6 pkt = lowlevel.Lighting6() pkt.set_transmit(self.subtype, 0, self.id_combined, self.groupcode, self.unitcode, not on and 0x01 or 0x00, self.cmndseqnbr) self.cmndseqnbr = (self.cmndseqnbr + 1) % 5 transport.send(pkt.data) else: raise ValueError("Unsupported packettype")
def get_device(packettype, subtype, id_string): """ Return a device base on its identifying values """ if packettype == 0x10: # Lighting1 pkt = lowlevel.Lighting1() pkt.parse_id(subtype, id_string) return LightingDevice(pkt) elif packettype == 0x11: # Lighting2 pkt = lowlevel.Lighting2() pkt.parse_id(subtype, id_string) return LightingDevice(pkt) elif packettype == 0x12: # Lighting3 pkt = lowlevel.Lighting3() pkt.parse_id(subtype, id_string) return LightingDevice(pkt) elif packettype == 0x14: # Lighting5 pkt = lowlevel.Lighting5() pkt.parse_id(subtype, id_string) return LightingDevice(pkt) elif packettype == 0x15: # Lighting6 pkt = lowlevel.Lighting6() pkt.parse_id(subtype, id_string) return LightingDevice(pkt) else: raise ValueError("Unsupported packettype")