Skip to content

Commit

Permalink
Fix offsets for peaqevbound
Browse files Browse the repository at this point in the history
  • Loading branch information
magnuselden authored and magnuselden committed Apr 26, 2024
1 parent 4bda51f commit 80feb82
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 14 deletions.
2 changes: 1 addition & 1 deletion custom_components/peaqhvac/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"iot_class": "calculated",
"issue_tracker": "https://github.com/elden1337/hass-peaqhvac/issues",
"requirements": [
"peaqevcore==19.9.0"
"peaqevcore==19.9.2"
],
"version": "3.0.0"
}
Expand Down
3 changes: 3 additions & 0 deletions custom_components/peaqhvac/service/hub/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ def __init__(self, hass: HomeAssistant, hub_options: ConfigModel):
self.sensors = HubSensors(self, hub_options, self.state_machine, self.peaqev_discovered)
self.states = StateChanges(self, self.state_machine)
self.hvac = HvacFactory.create(self.state_machine, self.options, self)
#if not self.peaqev_discovered:
self.spotprice = SpotPriceFactory.create(hub=self, observer=self.observer, system=PeaqSystem.PeaqHvac, test=False, is_active=True)
# else:
# self.spotprice = self.sensors.peaqev_facade.spotprice
self.prognosis = WeatherPrognosis(self.state_machine, self.sensors.average_temp_outdoors, self.observer)
self.offset = OffsetFactory.create(self)
self.options.hub = self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,6 @@ async def async_update_system(self, operation: HvacOperations, set_val: any = No
domain,
) = self._set_operation_call_parameters(operation, _value)

"""
PREFLIGHT CHECK: Requesting to update hvac-Offset with value -2. Actual value: {'entity_id': 'number.f730_cu_3x400v_magnus_nibef_f730_cu_3x400v_heating_offset_climate_system_1'} for set_value
"""

_LOGGER.debug(
f"PREFLIGHT CHECK: Requesting to update hvac-{operation.name} with value {set_val}. Actual value: {params} for {call_operation}"
)
await self._hass.services.async_call(domain, call_operation, params)
_LOGGER.debug(
f"Requested to update hvac-{operation.name} with value {set_val}. Actual value: {params} for {call_operation}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ def __init__(self, hub, hours_type: Hoursselection = None): #type: ignore
self.model = OffsetModel(hub)
self.hours = hours_type
self.latest_raw_offset_update_hour: int = -1

self._hub.observer.add(ObserverTypes.PricesChanged, self.async_update_prices)
self._hub.observer.add(ObserverTypes.SpotpriceInitialized, self.async_update_prices)
self._hub.observer.add(ObserverTypes.PrognosisChanged, self._update_prognosis)
self._hub.observer.add(ObserverTypes.HvacPresetChanged, self._set_offset)
self._hub.observer.add(ObserverTypes.SetTemperatureChanged, self._set_offset)
Expand Down Expand Up @@ -107,8 +104,8 @@ def _update_offset(self, weather_adjusted_today: dict | None = None) -> dict:
# cache.update_cache((datetime.now() + timedelta(days=1)).date(), self.prices_tomorrow, tomorrow_values)

all_values = set_offset_dict(self.prices+self.prices_tomorrow, datetime.now(), self.min_price,{})
_LOGGER.debug("all_values", all_values, self.prices, self.prices_tomorrow, self.min_price)
offsets_per_day = self._calculate_offset_per_day(all_values, weather_adjusted_today)
_LOGGER.debug("offsets_per_day", offsets_per_day)
tolerance = self.model.tolerance if self.model.tolerance is not None else 3
for k, v in offsets_per_day.items():
if v > tolerance:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import logging

from peaqevcore.common.models.observer_types import ObserverTypes
from peaqevcore.services.hourselection.hoursselection import Hoursselection
from custom_components.peaqhvac.service.hvac.offset.offset_coordinator import OffsetCoordinator

Expand All @@ -11,9 +13,12 @@ class OffsetCoordinatorPeaqEv(OffsetCoordinator):

def __init__(self, hub, hours_type: Hoursselection = None): #type: ignore
_LOGGER.debug("found peaqev and will not init hourselection")
super().__init__(hub, hours_type)
self._prices = None
self._prices_tomorrow = None
super().__init__(hub, hours_type)
self._update_prices([hub.sensors.peaqev_facade.hours.prices, hub.sensors.peaqev_facade.hours.prices_tomorrow])
hub.sensors.peaqev_facade.peaqev_observer.add(ObserverTypes.PricesChanged, self.async_update_prices_blank)
hub.sensors.peaqev_facade.peaqev_observer.add(ObserverTypes.SpotpriceInitialized, self.async_update_prices_blank)

@property
def prices(self) -> list:
Expand All @@ -31,6 +36,13 @@ def min_price(self) -> float:
return 0

async def async_update_prices(self, prices) -> None:
self._update_prices(prices)

async def async_update_prices_blank(self) -> None:
self._update_prices([self._hub.sensors.peaqev_facade.hours.prices, self._hub.sensors.peaqev_facade.hours.prices_tomorrow])

def _update_prices(self, prices) -> None:
_LOGGER.debug("Updating prices")
if self._prices != prices[0]:
self._prices = prices[0]
if self._prices_tomorrow != prices[1]:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import logging

from peaqevcore.common.models.observer_types import ObserverTypes
from peaqevcore.services.hourselection.hoursselection import Hoursselection

from custom_components.peaqhvac.service.hvac.offset.offset_coordinator import OffsetCoordinator
Expand All @@ -12,6 +14,8 @@ class OffsetCoordinatorStandAlone(OffsetCoordinator):

def __init__(self, hub, hours_type: Hoursselection = None): # type: ignore
_LOGGER.debug("initializing an hourselection-instance")
hub.observer.add(ObserverTypes.PricesChanged, self.async_update_prices)
hub.observer.add(ObserverTypes.SpotpriceInitialized, self.async_update_prices)
super().__init__(hub, hours_type)

@property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ async def async_broadcast_separator(self, func, command: Command):
else:
await self.hub.state_machine.async_add_executor_job(
self._call_func, func, command
)
)
14 changes: 14 additions & 0 deletions custom_components/peaqhvac/service/peaqev_facade.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging

from homeassistant.core import HomeAssistant
from peaqevcore.common.spotprice.spotpricebase import SpotPriceBase
from peaqevcore.services.hourselection.initializers.hoursbase import Hours

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -31,6 +33,10 @@ def below_start_threshold(self) -> bool:
def average_this_month(self) -> float:
return 0

@property
def spotprice(self) -> SpotPriceBase |None:
return None

class PeaqevFacade(PeaqevFacadeBase):
def __init__(self, hass: HomeAssistant, peaqev_discovered: bool):
self._hass = hass
Expand All @@ -40,6 +46,14 @@ def __init__(self, hass: HomeAssistant, peaqev_discovered: bool):
# def add_callback(self, message, function):
# self._peaqevhub.observer.add(message, function)

@property
def peaqev_observer(self):
return self._peaqevhub.observer

@property
def hours(self) -> Hours |None:
return self._peaqevhub.hours

@property
def offsets(self) -> dict:
data = self._peaqevhub.hours.offsets
Expand Down

0 comments on commit 80feb82

Please sign in to comment.