Skip to content

Commit

Permalink
resolves #12
Browse files Browse the repository at this point in the history
  • Loading branch information
mbtown01 committed May 28, 2020
1 parent e1e777e commit 7c5caba
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/services/SettingsService.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def setServiceProvider(self, provider: ServiceProvider):
self.__mode = SettingsService.Mode.AUTO
self.__lastOverridePrice = None
self.__isInPriceOverride = False
self.__windowPrices = []

self.__programs = {}
for name in programs:
Expand Down Expand Up @@ -224,9 +225,23 @@ def __powerPriceChanged(self, event: PowerPriceChangedEvent):
determine whether the min/max values need updating and if so,
applies the new settings. Returs True if new settings were applied,
False otherwise """
eventBus = self._getService(EventBus)

# Here we're simply assuming that price average resets every 15
# minutes. In production we'll get an update roughly every 5
# minutes and the average of the three is the correct price to
# use when looking up an override value
window = int(eventBus.now/900)*900
windowThird = int((eventBus.now-window)/300)
if windowThird == 0:
self.__windowPrices = []

self.__windowPrices.append(event.price)
priceAverage = sum(self.__windowPrices)/len(self.__windowPrices)

eventBus = self._getService(EventBus)
for override in self.__currentProgram.priceOverrides:
if event.price >= override.price:
if priceAverage >= override.price:
if self.__lastOverridePrice == override.price:
return

Expand Down

0 comments on commit 7c5caba

Please sign in to comment.