Code: Select all
from fritzconnection.core.fritzconnection import FritzConnection
from fritzconnection.lib.fritzhomeauto import FritzHomeAutomation
def retrieve_power_stats(address, user, password, timeout=5):
""" Retrieve and return power statistics from energy sensors. """
try:
# Initialize the FritzConnection
fc = FritzConnection(address=address, user=user, password=password, timeout=timeout)
log_message(f"FritzConnection initialized: {fc}")
# Initialize FritzHomeAutomation with the FritzConnection object
fh = FritzHomeAutomation(fc)
# Find all home automation devices
devices = fh.get_homeautomation_devices()
# Filter devices to find those that are energy sensors
energy_sensors = [d for d in devices if d.is_energy_sensor]
if not energy_sensors:
log_message("No energy sensors found.")
return None # Return None if no energy sensors are found
# Assuming you want to return the power stats of the first energy sensor found
power_device_ain = 'XXXXX XXXXXXX' # Device AIN
log_message(f"Device AIN: {power_device_ain}")
# Fetch device stats
# Get the power and energy values using the device AIN
device_info = fh.get_device_information_by_identifier(power_device_ain)
# Extract the power and energy values from the device information
energy_value = device_info.get('NewMultimeterPower')
# Divide energy_value by 100
if energy_value is not None:
energy_value = energy_value / 100
power_value = device_info.get('NewMultimeterEnergy')
log_message(f"energy_value: {energy_value}")
log_message(f"power_value: {power_value}")
return energy_value, power_value # Return the power and energy values