<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import logging <EOL> import salt . utils <EOL> log = logging . getLogger ( __name__ ) <EOL> __virtualname__ = '<STR_LIT>' <EOL> last_state = { } <EOL> last_state_extra = { '<STR_LIT:value>' : False } <EOL> def __virtual__ ( ) : <EOL> which_result = salt . utils . which ( '<STR_LIT>' ) <EOL> if which_result is None : <EOL> return False <EOL> else : <EOL> return __virtualname__ <EOL> def validate ( config ) : <EOL> '''<STR_LIT>''' <EOL> if not isinstance ( config , dict ) : <EOL> log . info ( '<STR_LIT>' ) <EOL> return False , ( '<STR_LIT>' ) <EOL> elif '<STR_LIT>' not in config . keys ( ) : <EOL> log . info ( '<STR_LIT>' ) <EOL> return False , ( '<STR_LIT>' ) <EOL> else : <EOL> states = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:host>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if any ( s not in states for s in config [ '<STR_LIT>' ] ) : <EOL> log . info ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( states ) ) ) <EOL> return False , ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( states ) ) ) <EOL> return True , '<STR_LIT>' <EOL> def beacon ( config ) : <EOL> '''<STR_LIT>''' <EOL> log . trace ( '<STR_LIT>' ) <EOL> ret = [ ] <EOL> _validate = validate ( config ) <EOL> if not _validate [ <NUM_LIT:0> ] : <EOL> return ret <EOL> out = __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' , runas = config . get ( '<STR_LIT:user>' , None ) ) <EOL> lines = out . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:1> : ] <EOL> last_state_devices = last_state . keys ( ) <EOL> found_devices = [ ] <EOL> for line in lines : <EOL> try : <EOL> device , state = line . split ( '<STR_LIT:\t>' ) <EOL> found_devices . append ( device ) <EOL> if device not in last_state_devices or ( '<STR_LIT:state>' in last_state [ device ] and last_state [ device ] [ '<STR_LIT:state>' ] != state ) : <EOL> if state in config [ '<STR_LIT>' ] : <EOL> ret . append ( { '<STR_LIT>' : device , '<STR_LIT:state>' : state , '<STR_LIT>' : state } ) <EOL> last_state [ device ] = { '<STR_LIT:state>' : state } <EOL> if '<STR_LIT>' in config : <EOL> val = last_state . get ( device , { } ) <EOL> cmd = '<STR_LIT>' . format ( device ) <EOL> battery_levels = __salt__ [ '<STR_LIT>' ] ( cmd , runas = config . get ( '<STR_LIT:user>' , None ) ) . split ( '<STR_LIT:\n>' ) <EOL> for l in battery_levels : <EOL> battery_level = int ( l ) <EOL> if <NUM_LIT:0> < battery_level < <NUM_LIT:100> : <EOL> if '<STR_LIT>' not in val or battery_level != val [ '<STR_LIT>' ] : <EOL> if ( '<STR_LIT>' not in val or val [ '<STR_LIT>' ] > config [ '<STR_LIT>' ] ) and battery_level <= config [ '<STR_LIT>' ] : <EOL> ret . append ( { '<STR_LIT>' : device , '<STR_LIT>' : battery_level , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> if device not in last_state : <EOL> last_state [ device ] = { } <EOL> last_state [ device ] . update ( { '<STR_LIT>' : battery_level } ) <EOL> except ValueError : <EOL> continue <EOL> for device in last_state_devices : <EOL> if device not in found_devices : <EOL> if '<STR_LIT>' in config [ '<STR_LIT>' ] : <EOL> ret . append ( { '<STR_LIT>' : device , '<STR_LIT:state>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> del last_state [ device ] <EOL> if '<STR_LIT>' in config and config [ '<STR_LIT>' ] is True : <EOL> if len ( lines ) == <NUM_LIT:0> and not last_state_extra [ '<STR_LIT>' ] : <EOL> ret . append ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> last_state_extra [ '<STR_LIT>' ] = len ( lines ) == <NUM_LIT:0> <EOL> return ret </s>
