More changes i guess

This commit is contained in:
Timon Horlboge 2022-12-04 13:59:02 +01:00
parent 924eb57b34
commit 962e2c84af
2 changed files with 15 additions and 9 deletions

View File

@ -8,7 +8,7 @@ import re
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
from asyncio import Task from asyncio import Task
from datetime import datetime, timedelta from datetime import datetime, timedelta
from typing import Optional, Dict, List from typing import Optional, Dict, List, Union
import requests import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
@ -243,7 +243,7 @@ class FritzBox:
return device return device
def set_offset(self, device: Device) -> None: def set_offset(self, device: Union[Device, Dict]) -> None:
if self.dry_run: if self.dry_run:
logging.warning("No updates in dry-run-mode") logging.warning("No updates in dry-run-mode")
return return
@ -298,7 +298,8 @@ class FritzBox:
logging.info(f"Last update for {device_name} {elapsed} ago") logging.info(f"Last update for {device_name} {elapsed} ago")
delta = timedelta(minutes=self.update_timeout) delta = timedelta(minutes=self.update_timeout)
if device_name not in self.update_time.keys() or elapsed > delta: if device_name not in self.update_time.keys() or elapsed > delta:
device: Optional[Device] = self.get_device_data(name=device_name) device: Optional[Union[Dict, Device]] = self.get_device_data(name=device_name)
logging.info(f"device: {device}")
if device is None: if device is None:
return return
@ -314,4 +315,4 @@ class FritzBox:
else: else:
device.set_offset(new_offset) device.set_offset(new_offset)
self.set_offset(device) self.set_offset(device)
self.update_time[device.display_name] = datetime.now() self.update_time[device_name] = datetime.now()

View File

@ -14,7 +14,7 @@ thermostate_mappings = {}
async def handle_event(idx: int): async def handle_event(idx: int):
logging.debug(f"Wait for events for {idx}") logging.info(f"Wait for events for {idx}")
while event := await ha.events[idx].get(): while event := await ha.events[idx].get():
try: try:
@ -28,14 +28,19 @@ async def handle_event(idx: int):
therm_name = new_state["attributes"]["friendly_name"] therm_name = new_state["attributes"]["friendly_name"]
sensor = thermostate_mappings[entity_id] sensor = thermostate_mappings[entity_id]
sensor_state = await ha.get_device_state(sensor) sensor_state = await ha.get_device_state(sensor)
sensor_temp = round(float(sensor_state["attributes"]["temperature"]) * 2) / 2 sensor_state_temp = sensor_state["attributes"]["temperature"] if "temperatur" in sensor_state[
"attributes"] else sensor_state["state"]
sensor_temp = round(float(sensor_state_temp) * 2) / 2
if therm_temp != sensor_temp: if therm_temp != sensor_temp:
logging.info( logging.info(
f"{therm_name}: {therm_temp}\n{sensor}: {sensor_state_temp} ({sensor_temp})") f"{therm_name}: {therm_temp}\n{sensor}: {sensor_state_temp} ({sensor_temp})")
fb.correct_offset(therm_name, sensor_temp) fb.correct_offset(therm_name, sensor_temp)
elif entity_id in sensor_mappings.keys(): elif entity_id in sensor_mappings.keys():
sensor_temp = round(float(new_state["attributes"]["temperature"]) * 2) / 2 new_state_temp = new_state["attributes"]["temperature"] if "temperatur" in new_state[
"attributes"] else new_state["state"]
sensor_temp = round(float(new_state_temp) * 2) / 2
logging.info(sensor_temp)
for thermostate in sensor_mappings[entity_id]: for thermostate in sensor_mappings[entity_id]:
therm_state = await ha.get_device_state(thermostate) therm_state = await ha.get_device_state(thermostate)
if therm_state["state"] == "unavailable": if therm_state["state"] == "unavailable":
@ -46,8 +51,8 @@ async def handle_event(idx: int):
logging.info( logging.info(
f"{therm_name}: {therm_temp}\n{entity_id}: {new_state_temp} ({sensor_temp})") f"{therm_name}: {therm_temp}\n{entity_id}: {new_state_temp} ({sensor_temp})")
fb.correct_offset(therm_name, sensor_temp) fb.correct_offset(therm_name, sensor_temp)
except KeyError: except KeyError as e:
pass logging.error(e)
async def init(ha: HomeAssistantAPI, fb: FritzBox): async def init(ha: HomeAssistantAPI, fb: FritzBox):