diff --git a/custom_components/peaqhvac/manifest.json b/custom_components/peaqhvac/manifest.json index 89da14bb..25a4c9af 100644 --- a/custom_components/peaqhvac/manifest.json +++ b/custom_components/peaqhvac/manifest.json @@ -20,7 +20,7 @@ "iot_class": "calculated", "issue_tracker": "https://github.com/elden1337/hass-peaqhvac/issues", "requirements": [ - "peaqevcore==19.4.2" + "peaqevcore==19.5.1" ], "version": "2.0.0" } diff --git a/custom_components/peaqhvac/service/hvac/house_heater/house_heater_coordinator.py b/custom_components/peaqhvac/service/hvac/house_heater/house_heater_coordinator.py index 868d88fe..056217bb 100644 --- a/custom_components/peaqhvac/service/hvac/house_heater/house_heater_coordinator.py +++ b/custom_components/peaqhvac/service/hvac/house_heater/house_heater_coordinator.py @@ -1,7 +1,7 @@ from __future__ import annotations import logging -from datetime import datetime +from datetime import datetime, timedelta from typing import Tuple from custom_components.peaqhvac.service.hvac.const import LOW_DEGREE_MINUTES, WAITTIMER_TIMEOUT, HEATBOOST_TIMER @@ -76,8 +76,8 @@ def get_current_offset(self, offsets: dict) -> Tuple[int, bool]: self._hvac.hub.offset.max_price_lower(self._hvac.hub.sensors.get_tempdiff())]): return OFFSET_MIN_VALUE, True - desired_offset = self._set_calculated_offset(offsets) _force_update: bool = False + desired_offset = self._set_calculated_offset(offsets, _force_update) if desired_offset <= 0 and self.current_tempdiff <= 0: return self._get_lower_offset(), _force_update @@ -90,6 +90,8 @@ def get_current_offset(self, offsets: dict) -> Tuple[int, bool]: desired_offset = lowered_offset _force_update = True + if _force_update: + self._hvac.hub.observer.broadcast("update operation") return self._hvac.hub.offset.adjust_to_threshold(desired_offset), _force_update def _get_lower_offset(self) -> int: @@ -149,8 +151,8 @@ def _get_demand(self) -> Demand: ) return Demand.NoDemand - def _set_calculated_offset(self, offsets: dict) -> int: - self._update_current_offset(offsets=offsets) + def _set_calculated_offset(self, offsets: dict, _force_update: bool) -> int: + self._check_next_hour_offset(offsets=offsets, force_update=_force_update) ret = sum( [ self.current_offset, @@ -161,10 +163,10 @@ def _set_calculated_offset(self, offsets: dict) -> int: ) return int(round(ret, 0)) - def _update_current_offset(self, offsets: dict) -> None: - hour = datetime.now().hour - if datetime.now().hour < 23 and datetime.now().minute >= 50: - hour = datetime.now().hour + 1 + def _check_next_hour_offset(self, offsets: dict, force_update: bool) -> None: + hour = datetime.now().replace(minute=0, second=0, microsecond=0) + if datetime.now().minute >= 40: + hour += timedelta(hours=1) try: _offset = offsets[hour] except: @@ -172,28 +174,9 @@ def _update_current_offset(self, offsets: dict) -> None: "No Price-offsets have been calculated. Setting base-offset to 0." ) _offset = 0 - self.current_offset = _offset - - # def _add_temp_boost(self, pre_offset: int) -> int: - # if self._latest_boost.is_timeout(): - # if all( - # [ - # self._hvac.hvac_mode == HvacMode.Idle, - # self._hvac.hub.sensors.get_tempdiff() < 0, - # self._hvac.hub.sensors.temp_trend_indoors.gradient <= 0.3, - # ] - # ): - # _LOGGER.debug( - # "adding additional heating since there is no sunwarming happening and house is too cold." - # ) - # pre_offset += 1 - # self._latest_boost.update() - # else: - # pre_offset += 1 - # if self._hvac.hub.sensors.get_tempdiff() > 1: - # pre_offset -= 1 - # self._latest_boost.reset() - # return pre_offset + if self.current_offset != _offset: + force_update = True + self.current_offset = _offset def _add_temp_boost(self, pre_offset: int) -> int: if not self._latest_boost.is_timeout(): diff --git a/custom_components/peaqhvac/service/hvac/interfaces/ihvac.py b/custom_components/peaqhvac/service/hvac/interfaces/ihvac.py index 3e0dc106..dcf9cf2e 100644 --- a/custom_components/peaqhvac/service/hvac/interfaces/ihvac.py +++ b/custom_components/peaqhvac/service/hvac/interfaces/ihvac.py @@ -2,6 +2,7 @@ import logging from abc import abstractmethod +from datetime import datetime, timedelta from typing import TYPE_CHECKING, Tuple from custom_components.peaqhvac.service.hvac.house_ventilation import HouseVentilation @@ -104,7 +105,7 @@ def update_offset(self) -> bool: # todo: make async self.get_offsets() _hvac_offset = self.hvac_offset new_offset, force_update = self.house_heater.get_current_offset( - self.model.current_offset_dict + self.model.current_offset_dict_combined ) if new_offset != self.current_offset: self.current_offset = new_offset @@ -122,6 +123,14 @@ def get_offsets(self) -> None: # todo: make async if ret is not None: self.model.current_offset_dict = ret[0] self.model.current_offset_dict_tomorrow = ret[1] + self.model.current_offset_dict_combined = self.set_combined_offsets(ret[0], ret[1]) + + def set_combined_offsets(self, today: dict, tomorrow: dict) -> dict: + ret:dict[datetime, int] = {} + for hour in range(0, 24): + ret[datetime.now().replace(hour=hour, minute=0, second=0, microsecond=0)] = today.get(hour, 0) + ret[datetime.now().replace(hour=hour, minute=0, second=0,microsecond=0) + timedelta(days=1)] = tomorrow.get(hour, 0) + return ret @staticmethod def _get_sensors_for_callback(types: dict) -> list: diff --git a/custom_components/peaqhvac/service/models/ihvac_model.py b/custom_components/peaqhvac/service/models/ihvac_model.py index 496daddd..6432f911 100644 --- a/custom_components/peaqhvac/service/models/ihvac_model.py +++ b/custom_components/peaqhvac/service/models/ihvac_model.py @@ -6,6 +6,7 @@ class IHvacModel: current_offset: int = 0 current_offset_dict: dict = field(default_factory=lambda: {}) current_offset_dict_tomorrow: dict = field(default_factory=lambda: {}) + current_offset_dict_combined: dict = field(default_factory=lambda: {}) listenerentities: list = field(default_factory=lambda: [])