From 069b8b3e0ea03e99a697d9338a7504c07881fd59 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Tue, 21 Oct 2025 20:19:13 +0200 Subject: [PATCH] collect outside temperature --- fritz_rest.py | 6 +++--- fritz_smart_home.py | 32 ++++++++++++++++++++++++++++++++ we_collector.py | 31 +++++++++++++++++++++++++++++-- 3 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 fritz_smart_home.py diff --git a/fritz_rest.py b/fritz_rest.py index 2f5ea91..bd09ce5 100644 --- a/fritz_rest.py +++ b/fritz_rest.py @@ -94,15 +94,15 @@ def send_response(box_url: str, username: str, challenge_response: str) -> str: return xml.find("SID").text def main(): - url = "http://192.168.22.4" + host = "http://192.168.22.4" username = "fritz6632" password = "body9517" - sid = get_sid(url, username, password) + sid = get_sid(host, username, password) print(f"Successful login for user: {username}") print(f"sid: {sid}") ain = 116570135300 cmd = "gettemperature" - with urllib.request.urlopen(f"{url}/webservices/homeautoswitch.lua?ain={ain}&switchcmd={cmd}&sid={sid}") as f: + with urllib.request.urlopen(f"{host}/webservices/homeautoswitch.lua?ain={ain}&switchcmd={cmd}&sid={sid}") as f: value = float(f.read().decode())/10 print(f"{cmd}: {value} °C") diff --git a/fritz_smart_home.py b/fritz_smart_home.py new file mode 100644 index 0000000..7041208 --- /dev/null +++ b/fritz_smart_home.py @@ -0,0 +1,32 @@ +import urllib.request +from fritz_rest import get_sid + +class FritzSmartHome: + def __init__(self, host: str): + self.host = host + self.sid = None + + def session_start(self, username: str, password: str): + self.sid = get_sid(self.host, username, password) + + def session_end(self): + self.sid = None + + def query(self, ain: str, cmd: str): + if self.sid is None: + raise Exception("Start Session") + with urllib.request.urlopen(f"{self.host}/webservices/homeautoswitch.lua?ain={ain}&switchcmd={cmd}&sid={self.sid}") as f: + return f.read() + +def get_temperature(hnd: FritzSmartHome, ain: str): + response = hnd.query(ain, 'gettemperature') + value = float(response.decode()) / 10 + return value + +if __name__ == '__main__': + f = FritzSmartHome("http://192.168.22.4") + f.session_start("fritz6632", "body9517") + temp = get_temperature(f, "116570135300") + + print(f"Temp = {temp}°C") + diff --git a/we_collector.py b/we_collector.py index 4847f0e..65cafda 100644 --- a/we_collector.py +++ b/we_collector.py @@ -23,6 +23,12 @@ domains = [ "measurements" ] +from fritz_smart_home import FritzSmartHome, get_temperature +fritz_host = "http://192.168.22.4" +fritz_username = "fritz6632" +fritz_password = "body9517" +fritz_ain_temperature = "116570135300" + class WeCollector: def __init__(self, user): # Init collecting records @@ -31,10 +37,12 @@ class WeCollector: self._wci = None self._endpoint = None self._record_list = None - self._vehicle_data_last = None + self._vehicle_data_last = {} self._f_mgr = FolderMgr() self.create_session() self._file_timestamp = None + self.fritz = FritzSmartHome(fritz_host) + self._other_data_last = {} def create_session(self): expired_token_for_test = 'eyJraWQiOiI0ODEyODgzZi05Y2FiLTQwMWMtYTI5OC0wZmEyMTA5Y2ViY2EiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIxMDQyNmRjZC1mYzljLTRkODctODBmYy0yMDIyNzdmYzhkYzIiLCJhdWQiOiJhMjRmYmE2My0zNGIzLTRkNDMtYjE4MS05NDIxMTFlNmJkYThAYXBwc192dy1kaWxhYl9jb20iLCJzY3AiOiJvcGVuaWQgcHJvZmlsZSBiYWRnZSBjYXJzIGRlYWxlcnMgdmluIiwiYWF0IjoiaWRlbnRpdHlraXQiLCJpc3MiOiJodHRwczovL2lkZW50aXR5LnZ3Z3JvdXAuaW8iLCJqdHQiOiJhY2Nlc3NfdG9rZW4iLCJleHAiOjE3Mjg5ODQzMjAsImlhdCI6MTcyODk4MDcyMCwibGVlIjpbIlZPTEtTV0FHRU4iXSwianRpIjoiYWQ0MDhkMDEtYmQxMy00YWZkLWIyMWEtYmZiMTE5NTFhOGE3In0.r78z00BS0Je7mjUcukmrkuSiPO9F8me-bLs3ZZF6BokhPX8uqu_2j7I3Pu-8C1xwFBb-YDs6rr0iiVUeOeiKM4O-jOik8ny57sMKdYJ_FHZNo-GLMA_WPppNaGWkM8HojIxEW4mrO3Zqd9f_vDrTAYukG4WyULbxq_OH6c6yQdmEx4M1PGisVB0DI6DG0Dl4748BCJMA_TyNvKagQHZuRhT0ma7kj_GVSqlGGtW0erwPBwTHY_X_wDitngod2iUw9nRtli-HkJKqgER82jSu2k5b36ePglsRqx6k4ls8Tiz6ykgcjuuW4_zdsfUNuhBElVd1QwCzolQ4e7IzHbVaRZbub8tlisrHZMskGJmNJeAUv9xZMJ8o6YNyAz__WPvP6iubrCIixSKOThv-YLXa0cFkCPmoaLR9eeTy6sWFsFvslTdD-a0zrrdP8ais4g5Cxao6ph3NV1xCgkEldImI3n1hxeMLefe6sqfoZ6Ogz-EDVPBxS9qpazPWN6EQyyYLXQZA0s6p5hcrjVq7pD-5yXjeFhCrymzizaNm0JhVF_L36F3D5a3s8tnQ2kaMfdK6jdHQyauxL-8SHfAgk_py33XOcgGQkp4aOXN3pm7UhutQ8fAS9kMoquqikuxTAu-UlWAYRhDUaypab1F51f50eK84s1jpgWDAQ1aL7PIgwj8' @@ -59,10 +67,27 @@ class WeCollector: self._record_id = 0 self._record_list = [] self._vehicle_data_last = {} + self._other_data_last = {} self._file_timestamp = file_timestamp def request(self, _date, timestamp): + # Establish connection to fritz + self.fritz.session_start(fritz_username, fritz_password) + try: + # Fritz get temperature + temp_deg_c = get_temperature(self.fritz, fritz_ain_temperature) + print(f"Retrieve fritz box data: Temperature = {temp_deg_c} °C") + except Exception as e: + temp_deg_c = -273.15 + print(e) + + try: + + other_data = {"temperature" : {"name": temp_deg_c, "value": temp_deg_c, "unit": "celsius"}} + other_data_diff = jay_diff_full(self._other_data_last, other_data) + self._other_data_last = other_data + print(f"{timestamp}: Retrieve vehicle data for {self._user['username']}") # Create data difference @@ -70,13 +95,15 @@ class WeCollector: vehicle_data_diff = jay_diff_full(self._vehicle_data_last, vehicle_data) self._vehicle_data_last = vehicle_data + # Put vehicle data difference onto record list self._record_list.append({ 'info': {'record_id': self._record_id, 'timestamp': timestamp, 'is_delta': (self._record_id > 0), 'username': self._user['username'], 'vin': self._user['vin']}, - 'data': vehicle_data_diff}) + 'data': vehicle_data_diff, + 'other_data': other_data_diff}) # Store record list print(f"{timestamp}: Store record #{self._record_id:04d}")