|
|
|
@ -1,48 +1,70 @@
|
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
import json
|
|
|
|
|
import logging
|
|
|
|
|
import os
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
from fritzbox import FritzBox
|
|
|
|
|
from homeassistant import HomeAssistantAPI
|
|
|
|
|
import logging
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
sensor_mappings = {}
|
|
|
|
|
thermostate_mappings = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def handle_event(idx: int):
|
|
|
|
|
global ha, fb
|
|
|
|
|
logging.debug(f"Wait for events for {idx}")
|
|
|
|
|
|
|
|
|
|
while event := await ha.events[idx].get():
|
|
|
|
|
try:
|
|
|
|
|
entity_id = event["data"]["entity_id"]
|
|
|
|
|
if entity_id in sensor_mappings.keys() or entity_id in thermostate_mappings.keys():
|
|
|
|
|
if (
|
|
|
|
|
entity_id in sensor_mappings.keys()
|
|
|
|
|
or entity_id in thermostate_mappings.keys()
|
|
|
|
|
):
|
|
|
|
|
state = await ha.get_device_state(entity_id)
|
|
|
|
|
new_state = event["data"]["new_state"]
|
|
|
|
|
logging.debug(f"received changed state from {entity_id}")
|
|
|
|
|
if entity_id in thermostate_mappings.keys() and state["state"] != "unavailable":
|
|
|
|
|
logging.info(
|
|
|
|
|
f"received changed state from {entity_id} {entity_id in thermostate_mappings.keys()} {state['state']} {entity_id in sensor_mappings.keys()}"
|
|
|
|
|
)
|
|
|
|
|
if (
|
|
|
|
|
entity_id in thermostate_mappings.keys()
|
|
|
|
|
and state["state"] != "unavailable"
|
|
|
|
|
):
|
|
|
|
|
therm_temp = new_state["attributes"]["current_temperature"]
|
|
|
|
|
therm_name = new_state["attributes"]["friendly_name"]
|
|
|
|
|
sensor = thermostate_mappings[entity_id]
|
|
|
|
|
sensor_state = await ha.get_device_state(sensor)
|
|
|
|
|
sensor_temp = round(float(sensor_state["attributes"]["temperature"]) * 2) / 2
|
|
|
|
|
sensor_temp = round(float(sensor_state["state"]) * 2) / 2
|
|
|
|
|
logging.info(f"temps: {therm_temp} {sensor_temp}")
|
|
|
|
|
if therm_temp != sensor_temp:
|
|
|
|
|
logging.info(f"{therm_name}: {therm_temp}\n{sensor}: {sensor_state['attributes']['temperature']} ({sensor_temp})")
|
|
|
|
|
logging.info(
|
|
|
|
|
f"{therm_name}: {therm_temp}\n{sensor}: {sensor_state['state']} ({sensor_temp})"
|
|
|
|
|
)
|
|
|
|
|
fb.correct_offset(therm_name, sensor_temp)
|
|
|
|
|
|
|
|
|
|
elif entity_id in sensor_mappings.keys():
|
|
|
|
|
sensor_temp = round(float(new_state["attributes"]["temperature"]) * 2) / 2
|
|
|
|
|
logging.info(f"here {sensor_mappings} {entity_id}")
|
|
|
|
|
logging.info(f"{new_state}")
|
|
|
|
|
sensor_temp = round(float(new_state["state"]) * 2) / 2
|
|
|
|
|
logging.info(f"entry: {sensor_mappings[entity_id]}")
|
|
|
|
|
for thermostate in sensor_mappings[entity_id]:
|
|
|
|
|
logging.info(thermostate)
|
|
|
|
|
therm_state = await ha.get_device_state(thermostate)
|
|
|
|
|
logging.info(f"{thermostate} {therm_state}")
|
|
|
|
|
if therm_state["state"] == "unavailable":
|
|
|
|
|
continue
|
|
|
|
|
therm_temp = float(therm_state["attributes"]["current_temperature"])
|
|
|
|
|
therm_temp = float(
|
|
|
|
|
therm_state["attributes"]["current_temperature"]
|
|
|
|
|
)
|
|
|
|
|
therm_name = therm_state["attributes"]["friendly_name"]
|
|
|
|
|
logging.info(f"Temps: {therm_temp} {sensor_temp}")
|
|
|
|
|
if therm_temp != sensor_temp:
|
|
|
|
|
logging.info(f"{therm_name}: {therm_temp}\n{entity_id}: {new_state['attributes']['temperature']} ({sensor_temp})")
|
|
|
|
|
logging.info(
|
|
|
|
|
f"{therm_name}: {therm_temp}\n{entity_id}: {new_state['state']} ({sensor_temp})"
|
|
|
|
|
)
|
|
|
|
|
fb.correct_offset(therm_name, sensor_temp)
|
|
|
|
|
except KeyError:
|
|
|
|
|
pass
|
|
|
|
@ -59,23 +81,31 @@ async def init(ha: HomeAssistantAPI, fb: FritzBox):
|
|
|
|
|
await ha.wait_for_close()
|
|
|
|
|
logging.info("Websocket closed, shutting down..")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
|
config_path = sys.argv[1]
|
|
|
|
|
config = json.load(open(config_path))
|
|
|
|
|
level = logging.INFO
|
|
|
|
|
if "log_level" in config:
|
|
|
|
|
print(f"Setting log_level {config['log_level']}")
|
|
|
|
|
if config["log_level"] == "DEBUG":
|
|
|
|
|
level = logging.DEBUG
|
|
|
|
|
logging.basicConfig(level=level, format="[%(asctime)s] [%(levelname)s] %(message)s")
|
|
|
|
|
logging.basicConfig(
|
|
|
|
|
level=level, format="[%(asctime)s] [%(levelname)s] %(message)s"
|
|
|
|
|
)
|
|
|
|
|
logging.debug(config)
|
|
|
|
|
|
|
|
|
|
global fb
|
|
|
|
|
fb = FritzBox(url=config["fritzbox"]["url"],
|
|
|
|
|
user=config["fritzbox"]["username"],
|
|
|
|
|
password=config["fritzbox"]["password"],
|
|
|
|
|
update_timeout=config["update_timeout"],
|
|
|
|
|
dry_run=False)
|
|
|
|
|
fb = FritzBox(
|
|
|
|
|
url=config["fritzbox"]["url"],
|
|
|
|
|
user=config["fritzbox"]["username"],
|
|
|
|
|
password=config["fritzbox"]["password"],
|
|
|
|
|
update_timeout=config["update_timeout"],
|
|
|
|
|
dry_run=False,
|
|
|
|
|
)
|
|
|
|
|
supervisor_url = "ws://supervisor/core/websocket"
|
|
|
|
|
if "SUPERVISOR_URL" in os.environ:
|
|
|
|
|
supervisor_url = os.environ["SUPERVISOR_URL"]
|
|
|
|
|
supervisor_token = os.environ["SUPERVISOR_TOKEN"]
|
|
|
|
|
global ha
|
|
|
|
|
ha = HomeAssistantAPI(supervisor_token, supervisor_url)
|
|
|
|
@ -85,10 +115,11 @@ async def main():
|
|
|
|
|
sensor_mappings[mapping["sensor"]] = []
|
|
|
|
|
sensor_mappings[mapping["sensor"]].append(mapping["thermostate"])
|
|
|
|
|
thermostate_mappings[mapping["thermostate"]] = mapping["sensor"]
|
|
|
|
|
|
|
|
|
|
logging.debug(f"Mappings: {sensor_mappings} {thermostate_mappings}")
|
|
|
|
|
try:
|
|
|
|
|
await init(ha, fb)
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
asyncio.run(main())
|
|
|
|
|