def test_connect(self, mock_read, mock_send): """Test setup with minimum configuration.""" calls = 0 def read_data(expected): nonlocal calls calls += 1 if calls == 1: self.assertEqual(expected, 2) return bytearray(b'\x81D') if calls == 2: self.assertEqual(expected, 14) return bytearray(b'\x81D#a!\x10g\xffh\x00\x04\x00\xf0<') mock_read.side_effect = read_data light = flux_led.WifiLedBulb("192.168.1.166") self.assertEqual(mock_read.call_count, 2) self.assertEqual(mock_send.call_count, 2) self.assertEqual(mock_send.call_args, mock.call(bytearray(b'\x81\x8a\x8b'))) self.assertEqual( light.__str__(), "ON [Color: (103, 255, 104) Brightness: 255 raw state: 129,68,35,97,33,16,103,255,104,0,4,0,240,60,]" ) self.assertEqual(light.protocol, None) self.assertEqual(light.is_on, True) self.assertEqual(light.mode, "color") self.assertEqual(light.warm_white, 0) self.assertEqual(light.brightness, 255) self.assertEqual(light.getRgb(), (103, 255, 104)) self.assertEqual(light.rgbwcapable, False)
def __init__(self, device): """Initialize the light.""" import flux_led self._name = device['name'] self._ipaddr = device['ipaddr'] self._protocol = device[CONF_PROTOCOL] self._mode = device[ATTR_MODE] self.is_valid = True self._bulb = None try: self._bulb = flux_led.WifiLedBulb(self._ipaddr) if self._protocol: self._bulb.setProtocol(self._protocol) # After bulb object is created the status is updated. We can # now set the correct mode if it was not explicitly defined. if not self._mode: if self._bulb.rgbwcapable: self._mode = MODE_RGBW else: self._mode = MODE_RGB except socket.error: self.is_valid = False _LOGGER.error( "Failed to connect to bulb %s, %s", self._ipaddr, self._name)
def test_ww(self, mock_read, mock_send): calls = 0 def read_data(expected): nonlocal calls calls += 1 if calls == 1: self.assertEqual(expected, 2) return bytearray(b'\x81D') if calls == 2: self.assertEqual(expected, 14) return bytearray( b'\x81D#a!\x10\xb6\x00\x98\x00\x04\x00\xf0\xbc') if calls == 3: self.assertEqual(expected, 14) return bytearray( b'\x81D#a!\x10\x00\x00\x00\x19\x04\x00\x0f\xa6') mock_read.side_effect = read_data light = flux_led.WifiLedBulb("192.168.1.164") self.assertEqual(mock_read.call_count, 2) self.assertEqual(mock_send.call_count, 2) self.assertEqual(mock_send.call_args, mock.call(bytearray(b'\x81\x8a\x8b'))) self.assertEqual( light.__str__(), "ON [Color: (182, 0, 152) Brightness: 182 raw state: 129,68,35,97,33,16,182,0,152,0,4,0,240,188,]" ) self.assertEqual(light.protocol, None) self.assertEqual(light.is_on, True) self.assertEqual(light.mode, "color") self.assertEqual(light.warm_white, 0) self.assertEqual(light.brightness, 182) self.assertEqual(light.getRgb(), (182, 0, 152)) self.assertEqual(light.rgbwcapable, False) light.setWarmWhite255(25) self.assertEqual(mock_read.call_count, 2) self.assertEqual(mock_send.call_count, 3) self.assertEqual(mock_send.call_args, mock.call(bytearray(b'1\x00\x00\x00\x19\x0f\x0f'))) light.update_state() self.assertEqual(mock_read.call_count, 3) self.assertEqual(mock_send.call_count, 4) self.assertEqual(mock_send.call_args, mock.call(bytearray(b'\x81\x8a\x8b'))) self.assertEqual( light.__str__(), "ON [Warm White: 9% raw state: 129,68,35,97,33,16,0,0,0,25,4,0,15,166,]" ) self.assertEqual(light.protocol, None) self.assertEqual(light.is_on, True) self.assertEqual(light.mode, "ww") self.assertEqual(light.warm_white, 0) self.assertEqual(light.brightness, 25) self.assertEqual(light.getRgb(), (255, 255, 255)) self.assertEqual(light.rgbwcapable, False)
def test_off_on(self, mock_read, mock_send): calls = 0 def read_data(expected): nonlocal calls calls += 1 if calls == 1: self.assertEqual(expected, 2) return bytearray(b'\x81D') if calls == 2: self.assertEqual(expected, 14) return bytearray(b'\x81D#a!\x10\x00\x00\x00\xa6\x04\x00\x0f3') if calls == 3: self.assertEqual(expected, 14) return bytearray(b'\x81D$a!\x10\x00\x00\x00\xa6\x04\x00\x0f4') mock_read.side_effect = read_data light = flux_led.WifiLedBulb("192.168.1.164") self.assertEqual( light.__str__(), "ON [Warm White: 65% raw state: 129,68,35,97,33,16,0,0,0,166,4,0,15,51,]" ) self.assertEqual(light.protocol, None) self.assertEqual(light.is_on, True) self.assertEqual(light.mode, "ww") self.assertEqual(light.warm_white, 0) self.assertEqual(light.brightness, 166) self.assertEqual(light.getRgb(), (255, 255, 255)) self.assertEqual(light.rgbwcapable, False) self.assertEqual(mock_read.call_count, 2) self.assertEqual(mock_send.call_count, 2) self.assertEqual(mock_send.call_args, mock.call(bytearray(b'\x81\x8a\x8b'))) light.turnOff() self.assertEqual(mock_read.call_count, 2) self.assertEqual(mock_send.call_count, 3) self.assertEqual(mock_send.call_args, mock.call(bytearray(b'q$\x0f'))) light.update_state() self.assertEqual(mock_read.call_count, 3) self.assertEqual(mock_send.call_count, 4) self.assertEqual(mock_send.call_args, mock.call(bytearray(b'\x81\x8a\x8b'))) self.assertEqual( light.__str__(), "False [Warm White: 65% raw state: 129,68,36,97,33,16,0,0,0,166,4,0,15,52,]" ) self.assertEqual(light.protocol, None) self.assertEqual(light.is_on, False) self.assertEqual(light.mode, "ww") self.assertEqual(light.warm_white, 0) self.assertEqual(light.brightness, 166) self.assertEqual(light.getRgb(), (255, 255, 255)) self.assertEqual(light.rgbwcapable, False)
def __init__(self, device): """Initialize the light.""" import flux_led self._name = device['name'] self._ipaddr = device['ipaddr'] self.is_valid = True self._bulb = None try: self._bulb = flux_led.WifiLedBulb(self._ipaddr) except socket.error: self.is_valid = False _LOGGER.error("Failed to connect to bulb %s, %s", self._ipaddr, self._name)
def set_pattern(counts, bulb_address_list): colors = [] if amb(counts): colors.append(colorBlue) if pa(counts): colors.append(colorPurple) if eng(counts): colors.append(colorRed) if sq(counts): colors.append(colorGreen) if wr(counts): colors.append(colorYellow) if len(colors) == 1: for address in bulb_address_list: logger.info(colors) bulb = flux_led.WifiLedBulb(address) bulb.setRgb(colors, True, None) else: for address in bulb_address_list: logger.info(colors) bulb = flux_led.WifiLedBulb(address) bulb.setCustomPattern(colors, 100, "jump")
def _connect(self): """Connect to Flux light.""" import flux_led self._bulb = flux_led.WifiLedBulb(self._ipaddr, timeout=5) if self._protocol: self._bulb.setProtocol(self._protocol) # After bulb object is created the status is updated. We can # now set the correct mode if it was not explicitly defined. if not self._mode: if self._bulb.rgbwcapable: self._mode = MODE_RGBW else: self._mode = MODE_RGB
def test_rgb(self, mock_read, mock_send): calls = 0 def read_data(expected): nonlocal calls calls += 1 if calls == 1: self.assertEqual(expected, 2) return bytearray(b'\x81D') if calls == 2: self.assertEqual(expected, 14) return bytearray(b'\x81D#a!\x10g\xffh\x00\x04\x00\xf0<') if calls == 3: self.assertEqual(expected, 14) return bytearray(b'\x81D#a!\x10\x01\x19P\x00\x04\x00\xf0\xd8') mock_read.side_effect = read_data light = flux_led.WifiLedBulb("192.168.1.164") self.assertEqual(mock_read.call_count, 2) self.assertEqual(mock_send.call_count, 2) self.assertEqual(mock_send.call_args, mock.call(bytearray(b'\x81\x8a\x8b'))) light.setRgb(1, 25, 80) self.assertEqual(mock_read.call_count, 2) self.assertEqual(mock_send.call_count, 3) self.assertEqual(mock_send.call_args, mock.call(bytearray(b'1\x01\x19P\x00\xf0\x0f'))) light.update_state() self.assertEqual(mock_read.call_count, 3) self.assertEqual(mock_send.call_count, 4) self.assertEqual(mock_send.call_args, mock.call(bytearray(b'\x81\x8a\x8b'))) self.assertEqual( light.__str__(), "ON [Color: (1, 25, 80) Brightness: 80 raw state: 129,68,35,97,33,16,1,25,80,0,4,0,240,216,]" ) self.assertEqual(light.protocol, None) self.assertEqual(light.is_on, True) self.assertEqual(light.mode, "color") self.assertEqual(light.warm_white, 0) self.assertEqual(light.brightness, 80) self.assertEqual(light.getRgb(), (1, 25, 80))
def __init__(self, device): """Initialize the light.""" import flux_led self._name = device['name'] self._ipaddr = device['ipaddr'] self._protocol = device[CONF_PROTOCOL] self._mode = device[ATTR_MODE] self.is_valid = True self._bulb = None try: self._bulb = flux_led.WifiLedBulb(self._ipaddr) if self._protocol: self._bulb.setProtocol(self._protocol) except socket.error: self.is_valid = False _LOGGER.error( "Failed to connect to bulb %s, %s", self._ipaddr, self._name)
def cli_lights(): bulb = [] while True: print("================= \n" "Lights App CLI Interface \n" "\n" "1: Lights On \n" "2: Lights off \n" "3: Lights to 5% \n" "4: Lights to 100% \n" "5: Set Lights IP \n" "6: Scan Network Devices \n" "0: Exit\n") userchoice = input("Please enter your choice: ") if userchoice == "": print("Please enter a selection... f****r") elif int(userchoice) == 1: active_bulb.turnOn() print("Lights On") elif int(userchoice) == 2: active_bulb.turnOff() print("Turning Off") elif int(userchoice) == 3: adjust_brightness(active_bulb, 5) elif int(userchoice) == 4: adjust_brightness(active_bulb, 100) elif int(userchoice) == 5: ip = input("Please enter the bulbs IP: ") active_bulb = flux_led.WifiLedBulb(ip) active_bulb.connect() elif int(userchoice) == 6: bulb_scanner.scan(3) print(bulb_scanner.found_bulbs) elif int(userchoice) == 0: break
def discover(self, *args, **kwargs): manifest = self.parent.config.get('manifest', {}) self.scanner.scan(timeout=3) devices = self.scanner.getBulbInfo() self.logger.info('%i bulbs found. Checking status and adding to ISY', len(devices)) for d in devices: led = flux_led.WifiLedBulb(d['ipaddr'], d['id'], d['model']) name = name = 'mh ' + str(led.ipaddr).replace('.', ' ') address = str(led.macaddr).lower() lnode = self.parent.get_node(address) if not lnode: self.logger.info('Adding new MagicHome LED: %s(%s)', name, address) self.parent.bulbs.append( MagicHomeLED(self.parent, self.parent.get_node('magichome'), address, name, led, manifest)) self.parent.long_poll() return True
import pyaudio import numpy as np import flux_led print("checking for errors") table = flux_led.WifiLedBulb("192.168.1.101") ceiling1 = flux_led.WifiLedBulb("192.168.1.109") ceiling2 = flux_led.WifiLedBulb("192.168.1.105") projector = flux_led.WifiLedBulb("192.168.1.103") #table.setPresetPattern(0x25, 50) print("no errors connected to every device") #red = 255, 0, 0 #start every light ceiling1.turnOn() ceiling2.turnOn() projector.turnOn() print("lights are on") ceiling1.setRgb(166, 0, 166) ceiling2.setRgb(0, 153, 0) projector.setRgb(44, 153, 142) print("set to default") CHUNK = 2**11 RATE = 44100 scene = 1 p = pyaudio.PyAudio() stream = p.open(format=pyaudio.paInt16,
#!/usr/bin/env python3 import flux_led from MagicHomeLightController import MagicHomeLightController light = flux_led.WifiLedBulb("192.168.86.182") lightController = MagicHomeLightController(light) lightController.flicker_lights([[255, 0, 0], [255, 0, 255]])
def test_original_ledenet(self, mock_read, mock_send): calls = 0 def read_data(expected): nonlocal calls calls += 1 if calls == 1: self.assertEqual(expected, 2) return bytearray(b'') if calls == 2: self.assertEqual(expected, 2) return bytearray(b'\f\x01') if calls == 3: self.assertEqual(expected, 11) return bytearray(b'f\x01#A!\x08\xff\x80*\x01\x99') if calls == 4: self.assertEqual(expected, 11) return bytearray(b'f\x01#A!\x08\x01\x19P\x01\x99') if calls == 5: self.assertEqual(expected, 11) return bytearray(b'f\x01$A!\x08\x01\x19P\x01\x99') if calls == 6: self.assertEqual(expected, 11) return bytearray(b'f\x01#A!\x08\x01\x19P\x01\x99') mock_read.side_effect = read_data light = flux_led.WifiLedBulb("192.168.1.164") self.assertEqual(mock_read.call_count, 3) self.assertEqual(mock_send.call_count, 3) self.assertEqual(mock_send.call_args, mock.call(bytearray(b'\xef\x01w'))) light.setRgb(1, 25, 80) self.assertEqual(mock_read.call_count, 3) self.assertEqual(mock_send.call_count, 4) self.assertEqual(mock_send.call_args, mock.call(bytearray(b'V\x01\x19P\xaa'))) light.update_state() self.assertEqual(mock_read.call_count, 4) self.assertEqual(mock_send.call_count, 5) self.assertEqual(mock_send.call_args, mock.call(bytearray(b'\xef\x01w'))) self.assertEqual( light.__str__(), "ON [Color: (1, 25, 80) Brightness: 80 raw state: 102,1,35,65,33,8,1,25,80,1,153,]" ) self.assertEqual(light.protocol, 'LEDENET_ORIGINAL') self.assertEqual(light.is_on, True) self.assertEqual(light.mode, "color") self.assertEqual(light.warm_white, 0) self.assertEqual(light.brightness, 80) self.assertEqual(light.getRgb(), (1, 25, 80)) light.turnOff() self.assertEqual(mock_read.call_count, 4) self.assertEqual(mock_send.call_count, 6) self.assertEqual(mock_send.call_args, mock.call(bytearray(b'\xcc$3'))) light.update_state() self.assertEqual(mock_read.call_count, 5) self.assertEqual(mock_send.call_count, 7) self.assertEqual(mock_send.call_args, mock.call(bytearray(b'\xef\x01w'))) self.assertEqual( light.__str__(), 'False [Color: (1, 25, 80) Brightness: 80 raw state: 102,1,36,65,33,8,1,25,80,1,153,]' ) self.assertEqual(light.protocol, 'LEDENET_ORIGINAL') self.assertEqual(light.is_on, False) self.assertEqual(light.mode, "color") self.assertEqual(light.warm_white, 0) self.assertEqual(light.brightness, 80) self.assertEqual(light.getRgb(), (1, 25, 80)) light.turnOn() self.assertEqual(mock_read.call_count, 5) self.assertEqual(mock_send.call_count, 8) self.assertEqual(mock_send.call_args, mock.call(bytearray(b'\xcc#3'))) light.update_state() self.assertEqual(mock_read.call_count, 6) self.assertEqual(mock_send.call_count, 9) self.assertEqual(mock_send.call_args, mock.call(bytearray(b'\xef\x01w'))) self.assertEqual( light.__str__(), "ON [Color: (1, 25, 80) Brightness: 80 raw state: 102,1,35,65,33,8,1,25,80,1,153,]" ) self.assertEqual(light.protocol, 'LEDENET_ORIGINAL') self.assertEqual(light.is_on, True) self.assertEqual(light.mode, "color") self.assertEqual(light.warm_white, 0) self.assertEqual(light.brightness, 80) self.assertEqual(light.getRgb(), (1, 25, 80))
def test_rgb_brightness(self, mock_read, mock_send): calls = 0 def read_data(expected): nonlocal calls calls += 1 if calls == 1: self.assertEqual(expected, 2) return bytearray(b'\x81D') if calls == 2: self.assertEqual(expected, 14) return bytearray(b'\x81D$a!\x10\xff[\xd4\x00\x04\x00\xf0\x9d') if calls == 3: self.assertEqual(expected, 14) return bytearray(b'\x81D#a!\x10\x03M\xf7\x00\x04\x00\xf0\xb5') mock_read.side_effect = read_data light = flux_led.WifiLedBulb("192.168.1.164") self.assertEqual(mock_read.call_count, 2) self.assertEqual(mock_send.call_count, 2) self.assertEqual(mock_send.call_args, mock.call(bytearray(b'\x81\x8a\x8b'))) self.assertEqual( light.__str__(), "False [Color: (255, 91, 212) Brightness: 255 raw state: 129,68,36,97,33,16,255,91,212,0,4,0,240,157,]" ) self.assertEqual(light.protocol, None) self.assertEqual(light.is_on, False) self.assertEqual(light.mode, "color") self.assertEqual(light.warm_white, 0) self.assertEqual(light.brightness, 255) self.assertEqual(light.getRgb(), (255, 91, 212)) light.turnOn() self.assertEqual(mock_read.call_count, 2) self.assertEqual(mock_send.call_count, 3) self.assertEqual(mock_send.call_args, mock.call(bytearray(b'q#\x0f'))) self.assertEqual( light.__str__(), "False [Color: (255, 91, 212) Brightness: 255 raw state: 129,68,36,97,33,16,255,91,212,0,4,0,240,157,]" ) self.assertEqual(light.protocol, None) self.assertEqual(light.is_on, True) self.assertEqual(light.mode, "color") self.assertEqual(light.warm_white, 0) self.assertEqual(light.brightness, 255) self.assertEqual(light.getRgb(), (255, 91, 212)) light.setRgb(1, 25, 80, brightness=247) self.assertEqual(mock_read.call_count, 2) self.assertEqual(mock_send.call_count, 4) self.assertEqual(mock_send.call_args, mock.call(bytearray(b'1\x03M\xf7\x00\xf0\x0f'))) self.assertEqual( light.__str__(), "False [Color: (255, 91, 212) Brightness: 255 raw state: 129,68,36,97,33,16,255,91,212,0,4,0,240,157,]" ) self.assertEqual(light.protocol, None) self.assertEqual(light.is_on, True) self.assertEqual(light.mode, "color") self.assertEqual(light.warm_white, 0) self.assertEqual(light.brightness, 255) self.assertEqual(light.getRgb(), (255, 91, 212)) light.update_state() self.assertEqual(mock_read.call_count, 3) self.assertEqual(mock_send.call_count, 5) self.assertEqual(mock_send.call_args, mock.call(bytearray(b'\x81\x8a\x8b'))) self.assertEqual( light.__str__(), "ON [Color: (3, 77, 247) Brightness: 247 raw state: 129,68,35,97,33,16,3,77,247,0,4,0,240,181,]" ) self.assertEqual(light.protocol, None) self.assertEqual(light.is_on, True) self.assertEqual(light.mode, "color") self.assertEqual(light.warm_white, 0) self.assertEqual(light.brightness, 247) self.assertEqual(light.getRgb(), (3, 77, 247))
def connect_bulb(self): if self.light_list.selectedItems is not None: selected_item = self.light_list.selectedItems() bulb_ip = selected_item[0].text() self.active_bulb = flux_led.WifiLedBulb(bulb_ip)
def led_connect(ip): led = flux_led.WifiLedBulb(ipaddr=ip, timeout=1) led.update_state() return led
def connect(self): try: self.controller = flux_led.WifiLedBulb(self.__ipaddr) except socket.timeout as e: log("Magic Light error: Connection")
answer = input(str+' (Y/n) ') if answer[0].lower() == 'n': answers.append([index, 0]) else: answers.append([index, 1]) print("Thank you for doing the testing of Magic Home RGB strip controllers.") print("Before starting, please open the Magic Home App and look how your strip setup is for checking.") print("When the test stops before finishes, please post the text into the thread on GitHub.") print ipaddress = input("Please enter the ip address of the strip controller: ") while re.match("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$", ipaddress) is None: print("Wrong IP Adress") ipaddress = input("Please enter the ip address of the strip controller: ") controller = flux.WifiLedBulb(ipaddress) assertion(controller.connect(2), "Checking whether connection to controller works... ") print("Received controller status: " + str(hexlify(controller.query_state()))) assertion(controller.stripprotocol == True, "Checking whether controller is a strip controller... ") stripdata = controller.query_strip_state() assertion(stripdata != False, "Checking if the strip controller status can be received... ") assertion(stripdata[0] == 0x63, "Checking whether the strip setup data can be understood... ") print("Received strip data: "+str(hexlify(stripdata))) led_count = (stripdata[1] << 8) + stripdata[2] try: ic = flux.StripIC(stripdata[3:10]) except: try: