<s> """<STR_LIT>""" <EOL> import logging <EOL> from datetime import timedelta <EOL> from homeassistant . components . device_tracker import DOMAIN <EOL> from homeassistant . const import CONF_HOST , CONF_PASSWORD , CONF_USERNAME <EOL> from homeassistant . helpers import validate_config <EOL> from homeassistant . util import Throttle <EOL> REQUIREMENTS = [ '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ] <EOL> MIN_TIME_BETWEEN_SCANS = timedelta ( seconds = <NUM_LIT:5> ) <EOL> _LOGGER = logging . getLogger ( __name__ ) <EOL> def get_scanner ( hass , config ) : <EOL> """<STR_LIT>""" <EOL> if not validate_config ( config , <EOL> { DOMAIN : [ ] } , <EOL> _LOGGER ) : <EOL> return None <EOL> scanner = FritzBoxScanner ( config [ DOMAIN ] ) <EOL> return scanner if scanner . success_init else None <EOL> class FritzBoxScanner ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , config ) : <EOL> """<STR_LIT>""" <EOL> self . last_results = [ ] <EOL> self . host = '<STR_LIT>' <EOL> self . username = '<STR_LIT>' <EOL> self . password = '<STR_LIT>' <EOL> self . success_init = True <EOL> import fritzconnection as fc <EOL> if CONF_HOST in config . keys ( ) : <EOL> self . host = config [ CONF_HOST ] <EOL> if CONF_USERNAME in config . keys ( ) : <EOL> self . username = config [ CONF_USERNAME ] <EOL> if CONF_PASSWORD in config . keys ( ) : <EOL> self . password = config [ CONF_PASSWORD ] <EOL> try : <EOL> self . fritz_box = fc . FritzHosts ( address = self . host , <EOL> user = self . username , <EOL> password = self . password ) <EOL> except ( ValueError , TypeError ) : <EOL> self . fritz_box = None <EOL> if self . fritz_box is None or not self . fritz_box . modelname : <EOL> self . success_init = False <EOL> if self . success_init : <EOL> _LOGGER . info ( "<STR_LIT>" , <EOL> self . fritz_box . modelname ) <EOL> self . _update_info ( ) <EOL> else : <EOL> _LOGGER . error ( "<STR_LIT>" <EOL> "<STR_LIT>" , self . host ) <EOL> def scan_devices ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _update_info ( ) <EOL> active_hosts = [ ] <EOL> for known_host in self . last_results : <EOL> if known_host [ "<STR_LIT:status>" ] == "<STR_LIT:1>" : <EOL> active_hosts . append ( known_host [ "<STR_LIT>" ] ) <EOL> return active_hosts <EOL> def get_device_name ( self , mac ) : <EOL> """<STR_LIT>""" <EOL> ret = self . fritz_box . get_specific_host_entry ( mac ) [ "<STR_LIT>" ] <EOL> if ret == { } : <EOL> return None <EOL> return ret <EOL> @ Throttle ( MIN_TIME_BETWEEN_SCANS ) <EOL> def _update_info ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . success_init : <EOL> return False <EOL> _LOGGER . info ( "<STR_LIT>" ) <EOL> self . last_results = self . fritz_box . get_hosts_info ( ) <EOL> return True </s>
