Use FritzTopology to represent mesh networks
masterFritzTopology module (introduced in 1.14.0) allows you to represent and interact with the mesh-topology graph of your network.repository·master·Indexed 19 days ago
https://github.com/kbr/fritzconnectionA Python interface for communicating with AVM Fritz!Box routers. It provides access to the TR-064 protocol via call_action() and the (AHA)-HTTP-Interface via call_http() for network management, status monitoring, and home automation. The library includes the FritzMonitor module for real-time call monitoring via socket connections and a CLI tool for inspecting model-specific TR-064 APIs.
FritzTopology module (introduced in 1.14.0) allows you to represent and interact with the mesh-topology graph of your network.FritzConnection instance can use both the TR-064 (call_action) and the HTTP (call_http) interfaces side-by-side. This allows you to combine different communication methods to achieve complex automation or monitoring tasks on the same router.The TR-064 protocol is organized into a hierarchy:
WLANConfiguration1, DeviceInfo1). Service names often end in a numeric value to distinguish between multiple instances (like different WLAN bands).GetInfo, SetEnable).in direction) or received from (out direction) the router.Note that services starting with X_AVM are AVM-specific extensions and are not part of the standard TR-064 protocol.
The FritzHomeAutomation module uses the HomeAutomationDevice class to represent the properties and state of individual devices.
To interact with devices, use:
get_homeautomation_device(): To get a specific device.get_homeautomation_devices(): To get all devices.get_device_information_list(): To get a list of device information (replaces the deprecated device_informations()).# Get all devices
devices = automation.get_homeautomation_devices()
# Get info for a specific device
info = automation.get_device_information_list()Creating a FritzConnection instance is an I/O-heavy operation because it inspects the Fritz!Box API to discover available services. To avoid repeated slow inspections, you should create a single FritzConnection instance and pass it to the constructors of specialized library modules (like FritzWLAN or FritzHomeAutomation).
from fritzconnection import FritzConnection
from fritzconnection.lib.fritzhomeauto import FritzHomeAutomation
from fritzconnection.lib.fritzwlan import FritzWLAN
# Create the base connection once
fc = FritzConnection(address='192.168.178.1', password=<password>)
# Reuse the instance for different modules
fw = FritzWLAN(fc)
print(fw.total_host_number)
fh = FritzHomeAutomation(fc)To call actions on specific instances of a service (e.g., when multiple WAN connections exist), use long qualified names in the format servicename:index.
If the extension (e.g., :2) is omitted, fritzconnection defaults to :1 for backward compatibility.
connection = FritzConnection()
# Calling a specific service instance
info = connection.call_action('WANIPConnection:2', 'GetInfo')FritzConnection supports API cache integration. By saving the router API response to a cache file, you can significantly reduce the time required for instantiation (saving up to several seconds).To use fritzconnection, instantiate a FritzConnection object with the router's IP address, username, and password. This object provides access to both the TR-064 and HTTP interfaces for interacting with FRITZ!Box routers.
Available features depend on your specific router model and system software.
from fritzconnection import FritzConnection
# Initialize connection
fc = FritzConnection(address="192.168.178.1", user="user", password="pw")
# Print router model information
print(fc)Starting from version 1.2.0, fritzconnection supports TLS for router communication.
-e flag to enable encrypted connections.# CLI usage
# fritzconnection -e [options]The fritzmonitor module provides real-time information about incoming and outgoing phone calls via a separate socket connection (not TR-064).
To use it in your code:
FritzMonitor as a context manager to ensure the monitor thread is shut down correctly when finished..start() on the monitor instance to begin the connection and receive a queue.Queue object.string types in the AVM format.monitor.is_alive property to detect if the connection to the router has failed.import queue
from fritzconnection.core.fritzmonitor import FritzMonitor
def process_events(monitor, event_queue, healthcheck_interval=10):
while True:
try:
# Pull events from the queue
event = event_queue.get(timeout=healthcheck_interval)
except queue.Empty:
# Check if the connection is still alive if no events arrive
if not monitor.is_alive:
raise OSError("Error: fritzmonitor connection failed")
else:
# Process the AVM string event here
print(event)
def main():
try:
# Use as a context manager to ensure thread shutdown
with FritzMonitor(address='192.168.178.1') as monitor:
event_queue = monitor.start()
process_events(monitor, event_queue)
except (OSError, KeyboardInterrupt) as err:
print(err)
if __name__ == "__main__":
main()Install the core fritzconnection package using pip. It is highly recommended to perform the installation within a virtual environment (such as venv or conda) to manage dependencies cleanly.
Requirements:
$ pip install fritzconnectionFor fritzconnection to receive call events, the CallMonitor service must be activated on your Fritz!Box. You can activate or deactivate this service by dialing specific codes from any registered phone connected to the router.
activate: #96*5*
deactivate: #96*4*