示例#1
0
 def __perform_temperature(self, luminary, temperature, millis):
     command = Command.LIGHT_TEMPERATURE
     packet = PacketBuilder(self).on(command).with_(luminary).temperature(
         temperature).millis(millis).build()
     self.__do_read(packet, command)
     luminary.update_temperature(temperature)
     luminary.update_powered(True)
示例#2
0
 def __perform_luminance(self, luminary, millis, luminance):
     command = Command.LIGHT_LUMINANCE
     packet = PacketBuilder(self).on(command).with_(luminary).luminance(
         luminance).millis(millis).build()
     self.__do_read(packet, command)
     luminary.update_luminance(luminance)
     luminary.update_powered(True)
示例#3
0
 def __perform_rgb(self, luminary, r, g, b, millis):
     command = Command.LIGHT_COLOR
     packet = PacketBuilder(self).on(command).with_(luminary).rgb(
         r, g, b).millis(millis).build()
     self.__do_read(packet, command)
     luminary.update_rgb(r, g, b)
     luminary.update_powered(True)
示例#4
0
 def __perform_status_update(self, luminary):
     command = Command.STATUS_SINGLE
     packet = PacketBuilder(self).on(command).with_(luminary).build()
     buffer = self.__do_read(packet, command)
     (on, lum, temp, red, green, blue, h) = struct.unpack("<27x2BH4B16x", buffer)
     luminary.update_powered(on)
     luminary.set_luminance(lum)
     luminary.set_temperature(temp)
     luminary.set_rgb(red, green, blue)
示例#5
0
 def __fill_zone_list(self):
     """
     Filling zones list
     """
     command = Command.ZONE_LIST
     packet = PacketBuilder(self).on(command).build()
     buffer = self.__do_read(packet, command)
     (num, ) = struct.unpack("<H", buffer[7:9])
     for i in range(0, num):
         pos = 9 + i * 18
         payload = buffer[pos:pos + 18]
         (zone_id, name) = struct.unpack("<H16s", payload)
         name = self.__clean_name(name.decode(self.__charset))
         zone = LightifyZone(self, name, zone_id)
         self.__zones[self.__get_zone_uid(zone_id)] = zone
         self.__handle_zone_info(zone)
示例#6
0
    def __perform_search(self):
        """
        Search all devices attached to the Lightify network
        """
        command = Command.STATUS_ALL
        packet = PacketBuilder(self).on(command).data(struct.pack(
            '<B', 0x01)).build()
        data = self.__do_read(packet, command)[7:]
        (num_of_lights, ) = struct.unpack('<H', data[:2])
        record_size = 50
        for i in range(0, num_of_lights):
            pos = 2 + i * record_size
            payload = data[pos:pos + record_size]

            (device_id, device_address,
             dev_type) = struct.unpack('<HQB', payload[:11])
            (zone_id, status) = struct.unpack('<H?', payload[16:19])
            (lum, temp, r, g, b, w) = struct.unpack('<BHBBBB', payload[19:26])
            name = self.__clean_name(payload[26:].decode(self.__charset))
            device_type = DeviceType.find_by_type_id(dev_type)
            if device_type != DeviceType.Bulb:
                self.__logger.warning(
                    "Found unsupported Lightify device, type id: {}. Skipping."
                    .format(dev_type))
                continue
            is_rgb = (dev_type & BITMASK_RGB) == BITMASK_RGB
            is_tunable_white = (
                dev_type & BITMASK_TUNABLE_WHITE) == BITMASK_TUNABLE_WHITE
            is_pure_white = (dev_type
                             & BITMASK_PURE_WHITE) == BITMASK_PURE_WHITE
            capabilities = []
            if is_rgb:
                capabilities.append(Capability.RGB)
            if is_tunable_white:
                capabilities.append(Capability.TunableWhite)
            if is_pure_white:
                capabilities.append(Capability.PureWhite)
            light = LightifyLight(self, name, capabilities, device_address)
            light.update_luminance(lum)
            light.update_powered(status)
            light.update_rgb(r, g, b)
            light.update_temperature(temp)
            (mac, ) = struct.unpack('<Q', light.address())
            self.__devices[mac] = light
示例#7
0
 def __handle_zone_info(self, zone):
     """
     Filling zone information and grouping devices by zones.
     :param zone: Zone instance of LightifyZone.LightifyZone class
     """
     command = Command.ZONE_INFO
     packet = PacketBuilder(self).on(command).with_(zone).build()
     data = self.__do_read(packet, command)
     payload = data[7:]
     (zone_id, name, num) = struct.unpack("<H16sB", payload[:19])
     name = self.__clean_name(name.decode(self.__charset))
     self.__logger.debug("Idx %d: '%s' %d", zone_id, name, num)
     zone = self.__zones[self.__get_zone_uid(zone_id)]
     for i in range(0, num):
         pos = 7 + 19 + i * 8
         payload = data[pos:pos + 8]
         (addr, ) = struct.unpack("<Q", payload[:8])
         light = self.__find_device(addr)
         zone.add_device(light)
示例#8
0
 def __fill_zone_list(self):
     """
     Filling zones list
     """
     command = Command.ZONE_LIST
     packet = PacketBuilder(self).on(command).build()
     buffer = self.__do_read(packet, command)
     (num,) = struct.unpack("<H", buffer[7:9])
     for i in range(0, num):
         pos = 9 + i * 18
         payload = buffer[pos:pos + 18]
         (zone_id, name) = struct.unpack("<H16s", payload)
         clear_name = bytes()
         for b in name:
             if b != 0x00:
                 clear_name += bytes([b])
         zone = LightifyZone(self, clear_name.decode('UTF-8'), zone_id)
         self.__zones[self.__get_zone_uid(zone_id)] = zone
         self.__handle_zone_info(zone)
示例#9
0
 def __perform_switch(self, luminary, activate):
     command = Command.LIGHT_SWITCH
     packet = PacketBuilder(self).on(command).with_(luminary).switching(
         activate).build()
     self.__do_read(packet, command)
     luminary.update_powered(activate)