Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Smart Home functionality for switch states, energy consumption an power values #15

Merged
merged 3 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@ Multigraph plugin, showing:
- CPU load
- CPU temperature

### Smart Home Temperature
Plugin: `fritzbox_smart_home_temperature.py`
### Smart Home
Plugin: `fritzbox_smart_home.py`
Multigraph plugin, showing:
- temperature sensors
- switch states
- power values
- energy consumption values

![Smart Home Temperature](doc/smart_home_temperature.png)

### Energy
Expand Down
2 changes: 1 addition & 1 deletion src/fritzbox_file_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def save(self, session_id):
with open(statefilename, 'w', encoding='utf8') as statefile:
statefile.write(session_id)

def load(self) -> None | str:
def load(self) -> "None | str":
statefilename = get_session_dir() + '/' + self.__get_session_filename()
if not os.path.exists(statefilename):
return None
Expand Down
112 changes: 112 additions & 0 deletions src/fritzbox_smart_home.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#!/usr/bin/env python3
"""
fritzbox_smart_home_temperature - A munin plugin for Linux to monitor AVM Fritzbox SmartHome temperatures

@see https://avm.de/fileadmin/user_upload/Global/Service/Schnittstellen/x_homeauto.pdf
"""

import sys
from fritzconnection import FritzConnection
from fritzbox_config import FritzboxConfig
from fritzbox_munin_plugin_interface import MuninPluginInterface,main_handler


class FritzboxSmartHome(MuninPluginInterface):
def print_stats(self):
smartHomeData = self.__retrieve_smart_home()
"""get the current cpu temperature"""

print("multigraph temperatures")
for data in smartHomeData:
if (data['NewTemperatureIsValid'] == 'VALID'):
print (f"t{data['NewDeviceId']}.value {float(data['NewTemperatureCelsius']) / 10}")
print("multigraph energy")
for data in smartHomeData:
if (data['NewMultimeterIsValid'] == 'VALID'):
print (f"e{data['NewDeviceId']}.value {data['NewMultimeterEnergy']}")
print("multigraph powers")
for data in smartHomeData:
if (data['NewMultimeterIsValid'] == 'VALID'):
print (f"p{data['NewDeviceId']}.value {float(data['NewMultimeterPower']) / 100}")
print("multigraph states")
for data in smartHomeData:
if (data['NewSwitchIsValid'] == 'VALID'):
state = 1
if data['NewSwitchState'] == 'OFF':
state = 0
print (f"s{data['NewDeviceId']}.value {state}")

def print_config(self):
smartHomeData = self.__retrieve_smart_home()
print("multigraph temperatures")
print("graph_title Smart Home temperature")
print("graph_vlabel degrees Celsius")
print("graph_category sensors")
print("graph_scale no")

for data in smartHomeData:
if (data['NewTemperatureIsValid'] == 'VALID'):
print (f"t{data['NewDeviceId']}.label {data['NewDeviceName']}")
print (f"t{data['NewDeviceId']}.type GAUGE")
print (f"t{data['NewDeviceId']}.graph LINE")
print (f"t{data['NewDeviceId']}.info Temperature [{data['NewProductName']}] Offset: {float(data['NewTemperatureOffset']) / 10}°C")

print("multigraph energy")
print("graph_title Smart Home energy consumption")
print("graph_vlabel Wh")
print("graph_category sensors")
print("graph_scale no")
print("graph_period hour")
for data in smartHomeData:
if (data['NewMultimeterIsValid'] == 'VALID'):
print (f"e{data['NewDeviceId']}.label {data['NewDeviceName']}")
print (f"e{data['NewDeviceId']}.type DERIVE")
print (f"e{data['NewDeviceId']}.graph LINE")
print (f"e{data['NewDeviceId']}.info Energy consumption (Wh) [{data['NewProductName']}]")

print("multigraph powers")
print("graph_title Smart Home powers")
print("graph_vlabel W")
print("graph_category sensors")
print("graph_scale no")
for data in smartHomeData:
if (data['NewMultimeterIsValid'] == 'VALID'):
print (f"p{data['NewDeviceId']}.label {data['NewDeviceName']}")
print (f"p{data['NewDeviceId']}.type GAUGE")
print (f"p{data['NewDeviceId']}.graph LINE")
print (f"p{data['NewDeviceId']}.info Power (W) [{data['NewProductName']}]")

print("multigraph states")
print("graph_title Smart Home switch states")
print("graph_vlabel State")
print("graph_category sensors")
print("graph_scale no")
for data in smartHomeData:
if (data['NewSwitchIsValid'] == 'VALID'):
print (f"s{data['NewDeviceId']}.label {data['NewDeviceName']}")
print (f"s{data['NewDeviceId']}.type GAUGE")
print (f"s{data['NewDeviceId']}.graph LINE")
print (f"s{data['NewDeviceId']}.info Switch state [{data['NewProductName']}]")

def __retrieve_smart_home(self):
smart_home_data = []
config = FritzboxConfig()

try:
connection = FritzConnection(address=config.server, user=config.user, password=config.password, use_tls=config.use_tls)
except Exception as e:
sys.exit("Couldn't get data: " + str(e))

for i in range(0, 20):
try:
data = connection.call_action('X_AVM-DE_Homeauto1', 'GetGenericDeviceInfos', arguments={'NewIndex': i})
smart_home_data.append(data)
except Exception as e:
# smart home device index does not exist, so we stop here
break

return smart_home_data


if __name__ == '__main__':
main_handler(FritzboxSmartHome())
55 changes: 0 additions & 55 deletions src/fritzbox_smart_home_temperature.py

This file was deleted.