<s> from driver_base import DriverBase <EOL> from . . import log <EOL> try : <EOL> from phue import Bridge <EOL> except : <EOL> error = "<STR_LIT>" <EOL> log . error ( error ) <EOL> raise ImportError ( error ) <EOL> import colorsys <EOL> class DriverHue ( DriverBase ) : <EOL> def __init__ ( self , num , ip , nameMap = None ) : <EOL> super ( DriverHue , self ) . __init__ ( num ) <EOL> if nameMap and len ( nameMap ) != self . numLEDs : <EOL> raise ValueError ( <EOL> "<STR_LIT>" ) <EOL> self . _bridge = Bridge ( ip ) <EOL> self . _bridge . connect ( ) <EOL> self . _transitionTime = <NUM_LIT:0> <EOL> self . _brightness = <NUM_LIT> <EOL> if nameMap : <EOL> self . _lights = self . _bridge . get_light_objects ( '<STR_LIT:name>' ) <EOL> self . _ids = nameMap <EOL> else : <EOL> self . _lights = self . _bridge . get_light_objects ( '<STR_LIT:id>' ) <EOL> self . _ids = [ l for l in self . _lights ] <EOL> if len ( self . _lights ) < self . numLEDs : <EOL> raise ValueError ( <EOL> "<STR_LIT>" ) <EOL> self . setTransitionTime ( <NUM_LIT:0> ) <EOL> def setMasterBrightness ( self , brightness ) : <EOL> if brightness < <NUM_LIT:0> or brightness > <NUM_LIT:255> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . _brightness = brightness <EOL> return True <EOL> def setTransitionTime ( self , time ) : <EOL> if time < <NUM_LIT:0.0> or time > <NUM_LIT> : <EOL> raise ValueError ( <EOL> "<STR_LIT>" ) <EOL> self . _transitionTime = int ( time * <NUM_LIT:10> ) <EOL> def _mapRange ( self , value , minFrom , maxFrom , minTo , maxTo ) : <EOL> return minTo + ( maxTo - minTo ) * ( ( value - minFrom ) / ( maxFrom - minFrom ) ) <EOL> def _rgb2hs ( self , rgb ) : <EOL> r = rgb [ <NUM_LIT:0> ] / <NUM_LIT> <EOL> g = rgb [ <NUM_LIT:1> ] / <NUM_LIT> <EOL> b = rgb [ <NUM_LIT:2> ] / <NUM_LIT> <EOL> h , s , v = colorsys . rgb_to_hsv ( r , g , b ) <EOL> h = int ( self . _mapRange ( h , <NUM_LIT:0.0> , <NUM_LIT:1.0> , <NUM_LIT:0> , <NUM_LIT> ) ) <EOL> s = int ( self . _mapRange ( s , <NUM_LIT:0.0> , <NUM_LIT:1.0> , <NUM_LIT:0> , <NUM_LIT> ) ) <EOL> return ( h , s ) <EOL> def update ( self , data ) : <EOL> pixels = [ tuple ( data [ ( p * <NUM_LIT:3> ) : ( p * <NUM_LIT:3> ) + <NUM_LIT:3> ] ) <EOL> for p in range ( len ( data ) / <NUM_LIT:3> ) ] <EOL> for i in range ( len ( self . _ids ) ) : <EOL> h , s = self . _rgb2hs ( pixels [ i ] ) <EOL> bri = self . _brightness <EOL> if s == <NUM_LIT:0> : <EOL> bri = <NUM_LIT:0> <EOL> cmd = { '<STR_LIT>' : s != <NUM_LIT:0> , '<STR_LIT>' : bri , '<STR_LIT>' : h , '<STR_LIT>' : s , <EOL> '<STR_LIT>' : self . _transitionTime } <EOL> self . _bridge . set_light ( self . _ids [ i ] , cmd ) </s>
