def setup_platform(hass, config, add_devices, discovery_info=None): """Add all switches connected to Fritz Box.""" from fritzhome.fritz import FritzBox host = config.get(CONF_HOST) username = config.get(CONF_USERNAME) password = config.get(CONF_PASSWORD) # Hack: fritzhome only throws Exception. To prevent pylint from # complaining, we disable the warning here: # pylint: disable=W0703 # Log into Fritz Box fritz = FritzBox(host, username, password) try: fritz.login() except Exception: _LOGGER.error("Login to Fritz!Box failed") return # Add all actors to hass for actor in fritz.get_actors(): # Only add devices that support switching if actor.has_switch: data = FritzDectSwitchData(fritz, actor.actor_id) add_devices([FritzDectSwitch(hass, data, actor.name)], True)
def main(device=None, action=None): parser = argparse.ArgumentParser(description='fritzhome devices') parser.add_argument('--device', help='the device actor id') parser.add_argument('--action', help='on or off') args = parser.parse_args() box = FritzBox(HOSTNAME, USERNAME, PASSWORD) try: box.login() except Exception as e: print(str(e)) sys.exit(1) if args.device is not None: actor = box.get_actor_by_ain(urllib.unquote(args.device)) if actor is not None: if args.action == "on": actor.switch_on() elif args.action == "off": actor.switch_off() actors_on = [] actors_off = [] actors_offline = [] total_power = 0 for actor in sorted(box.get_actors(), key=lambda a: a.actor_id): power = actor.get_power() if power is None: actors_offline.append(actor) elif power == 0: actors_off.append(actor) else: actors_on.append(actor) total_power += power if actors_on: print("%d On (%d W)| color=red" % (len(actors_on), total_power / 1000)) else: print("All Off| color=green") print("---") for actor in actors_on: text = "{1} ({0}) is using {2} W - switch off".format(actor.actor_id, actor.name, actor.get_power() / 1000) action = make_call(sys.argv[0], "--device", urllib.quote(actor.actor_id), "--action", "off") print("%s|%s terminal=false refresh=true" % (text, action)) for actor in actors_off: text = "switch {1} ({0}) on".format(actor.actor_id, actor.name) action = make_call(sys.argv[0], "--device", urllib.quote(actor.actor_id), "--action", "on") print("%s|%s terminal=false refresh=true" % (text, action))
def setup_platform(hass, config, add_entities, discovery_info=None): """Add all switches connected to Fritz Box.""" host = config.get(CONF_HOST) username = config.get(CONF_USERNAME) password = config.get(CONF_PASSWORD) # Log into Fritz Box fritz = FritzBox(host, username, password) try: fritz.login() except Exception: # pylint: disable=broad-except _LOGGER.error("Login to Fritz!Box failed") return # Add all actors to hass for actor in fritz.get_actors(): # Only add devices that support switching if actor.has_switch: data = FritzDectSwitchData(fritz, actor.actor_id) data.is_online = True add_entities([FritzDectSwitch(hass, data, actor.name)], True)
def setup_platform(hass, config, add_devices, discovery_info=None): """Add all switches connected to Fritz Box.""" from fritzhome.fritz import FritzBox host = config.get(CONF_HOST) username = config.get(CONF_USERNAME) password = config.get(CONF_PASSWORD) # Log into Fritz Box fritz = FritzBox(host, username, password) try: fritz.login() except Exception: # pylint: disable=broad-except _LOGGER.error("Login to Fritz!Box failed") return # Add all actors to hass for actor in fritz.get_actors(): # Only add devices that support switching if actor.has_switch: data = FritzDectSwitchData(fritz, actor.actor_id) data.is_online = True add_devices([FritzDectSwitch(hass, data, actor.name)], True)
def fritzbox_query(searched_macs, ignored_macs): config = configparser.ConfigParser() config.read(os.path.join(settings.BASE_DIR, "config.ini")) ip = config["FritzBox"]["ip"] password = config["FritzBox"]["password"] if not ip or not password: raise FritzException("ip or password not specified") box = FritzBox(ip, None, password) try: box.login() except Exception: raise FritzException("Login failed") r = box.session.get(box.base_url + "/net/network_user_devices.lua", params={"sid": box.sid}) try: table = BeautifulSoup(r.text, "lxml").find(id="uiLanActive") except AttributeError: raise FritzException("Could not extract active devices.") rows = table.find_all("tr") present_macs = [] anonymous_count = 0 for row in rows: columns = row.find_all("td") if len(columns) >= 4: mac = columns[3].text.upper() if mac in searched_macs: present_macs.append(mac) elif mac not in ignored_macs: anonymous_count += 1 return present_macs, anonymous_count