class FritzBoxScanner(DeviceScanner): """This class queries a FRITZ!Box router.""" def __init__(self, config): """Initialize the scanner.""" self.last_results = [] self.host = config[CONF_HOST] self.username = config[CONF_USERNAME] self.password = config[CONF_PASSWORD] self.success_init = True # Establish a connection to the FRITZ!Box. try: self.fritz_box = FritzHosts(address=self.host, user=self.username, password=self.password) except (ValueError, TypeError): self.fritz_box = None # At this point it is difficult to tell if a connection is established. # So just check for null objects. if self.fritz_box is None or not self.fritz_box.modelname: self.success_init = False if self.success_init: _LOGGER.info("Successfully connected to %s", self.fritz_box.modelname) self._update_info() else: _LOGGER.error( "Failed to establish connection to FRITZ!Box with IP: %s", self.host) def scan_devices(self): """Scan for new devices and return a list of found device ids.""" self._update_info() active_hosts = [] for known_host in self.last_results: if known_host["status"] and known_host.get("mac"): active_hosts.append(known_host["mac"]) return active_hosts def get_device_name(self, device): """Return the name of the given device or None if is not known.""" ret = self.fritz_box.get_specific_host_entry(device).get("NewHostName") if ret == {}: return None return ret def get_extra_attributes(self, device): """Return the attributes (ip, mac) of the given device or None if is not known.""" ip_device = self.fritz_box.get_specific_host_entry(device).get( "NewIPAddress") if not ip_device: return {} return {"ip": ip_device, "mac": device} def _update_info(self): """Retrieve latest information from the FRITZ!Box.""" if not self.success_init: return False _LOGGER.debug("Scanning") self.last_results = self.fritz_box.get_hosts_info() return True
#!/usr/bin/env python3 # faster to just check status of phone than all hosts from fritzconnection.lib.fritzhosts import FritzHosts import config fh = FritzHosts(password=config.fritz_pwd) he = fh.get_specific_host_entry(config.phone_mac) print(he['NewActive'])