From 5a14d251b4997ec6433e91626b4cc69868167e75 Mon Sep 17 00:00:00 2001 From: Horlabs Date: Fri, 28 Jan 2022 22:54:12 +0100 Subject: [PATCH] Fix --- fritz_temp_sync/homeassistant.py | 9 -------- fritz_temp_sync/run.sh | 2 +- fritz_temp_sync/sync_ha_fb.py | 36 ++++---------------------------- 3 files changed, 5 insertions(+), 42 deletions(-) diff --git a/fritz_temp_sync/homeassistant.py b/fritz_temp_sync/homeassistant.py index 61e8b4b..1f59e31 100755 --- a/fritz_temp_sync/homeassistant.py +++ b/fritz_temp_sync/homeassistant.py @@ -7,14 +7,6 @@ from asyncio import Queue, Task, Event, Lock from typing import Callable, Dict, Optional import websockets -""" -- sender fun, bekommt packete per queue -- receiver fun, schreibt packete in map id -> msg bzw events in queue(?) -- blockieren beim auf antwort warten per "pseudo-queue", in die sich alle wartenden eintragen und warten, dass sie leer ist, receiver leert queue wenn irgendeine nachricht rein kommt -- andere (auch außerhalb) können dann auf neue daten warten und ggf verarbeiten - -""" - class HomeAssistantAPI: def __init__(self, token: str, url: str) -> None: @@ -143,4 +135,3 @@ class HomeAssistantAPI: return device_state return None - diff --git a/fritz_temp_sync/run.sh b/fritz_temp_sync/run.sh index 57ac955..2e60ee2 100755 --- a/fritz_temp_sync/run.sh +++ b/fritz_temp_sync/run.sh @@ -1,3 +1,3 @@ #!/usr/bin/env bashio -python3 /srv/sync_ha_fb.py \ No newline at end of file +python3 /srv/sync_ha_fb.py /data/options.json \ No newline at end of file diff --git a/fritz_temp_sync/sync_ha_fb.py b/fritz_temp_sync/sync_ha_fb.py index d2ec8f2..ad586d7 100755 --- a/fritz_temp_sync/sync_ha_fb.py +++ b/fritz_temp_sync/sync_ha_fb.py @@ -1,11 +1,8 @@ #!/usr/bin/env python3 -""" -hier die verbindungen zu HA aufbauen etc -außerdem das vergleichen der werte und dass anstoßen der updates -""" + import asyncio import os -from typing import Dict +import sys from fritzbox import FritzBox from homeassistant import HomeAssistantAPI @@ -15,6 +12,7 @@ import json sensor_mappings = {} thermostate_mappings = {} + async def handle_event(idx: int): logging.debug(f"Wait for events for {idx}") @@ -85,42 +83,16 @@ async def init(ha: HomeAssistantAPI): asyncio.get_running_loop().stop() -async def migrate_config(config_path: str, ha: HomeAssistantAPI): - config = json.load(open(config_path)) - therm_ids = {} - for state in await ha.get_states(): - if state["entity_id"].startswith("climate.") and "friendly_name" in state["attributes"].keys(): - therm_ids[state["attributes"]["friendly_name"]] = state["entity_id"] - - mappings = [] - for mapping in config["mappings"]: - if not mapping["thermostate"].startswith("climate."): - mapping["thermostate"] = therm_ids[mapping["thermostate"]] - mappings.append(mapping) - config["mappings"] = mappings - json.dump(open(config_path), config) - - return config - - - logging.basicConfig(level=logging.INFO, format="[%(asctime)s] [%(levelname)s] %(message)s") -config_path = "/data/options.json" -config_path = "options.json" +config_path = sys.argv[1] config = json.load(open(config_path)) logging.debug(config) loop = asyncio.get_event_loop() fb = FritzBox(config["fritzbox"]["url"], config["fritzbox"]["password"]) supervisor_url = "ws://supervisor/core/websocket" -supervisor_url = "ws://192.168.124.187:8123/api/websocket" ha = HomeAssistantAPI(os.environ["SUPERVISOR_TOKEN"], supervisor_url) -if '"thermostate": "climate.' not in open(config_path).read(): - config = loop.run_until_complete(migrate_config(config_path, ha)) - logging.info(config) - exit() - for mapping in config["mappings"]: if mapping["sensor"] not in sensor_mappings.keys(): sensor_mappings[mapping["sensor"]] = []