Skip to content

Commit

Permalink
Merge pull request #9 from gerard33/patch-4
Browse files Browse the repository at this point in the history
Some changes to #8
  • Loading branch information
robbinjanssen authored Apr 1, 2020
2 parents 2e17988 + 91e983c commit b53cf23
Showing 1 changed file with 24 additions and 28 deletions.
52 changes: 24 additions & 28 deletions custom_components/omnik_inverter/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import re
import pickle

VERSION = '1.1.0'
VERSION = '1.1.1'

CONF_CACHE_POWER_TODAY = 'cache_power_today'

Expand Down Expand Up @@ -159,33 +159,29 @@ def update(self):
nextValue = int(result[6])
nextTime = int(datetime.now().strftime('%H%M'))

# Check if caching is disabled
if (self.cache == False):
# Update the sensor state, divide by 100 to make it kWh
self._state = (nextValue / 100)
return

# Fetch data from the cache
try:
cache = pickle.load(open(cacheName, 'rb'))
except (OSError, IOError, EOFError):
cache = [0, 0]

# Set the cache values
cacheValue = int(cache[0])
cacheTime = int(cache[1])

# If somehow the currentPowerToday is lower than the cached version,
# keep the cached version
if nextValue < cacheValue:
nextValue = cacheValue

# If today has passed, use the actual value from the Omnik inverter
if cacheTime > nextTime:
nextValue = int(result[6])

# Store new stats
pickle.dump([nextValue, nextTime], open(cacheName, 'wb'))
# Check if caching is enabled
if self.cache:
# Fetch data from the cache
try:
cache = pickle.load(open(cacheName, 'rb'))
except (OSError, IOError, EOFError):
cache = [0, 0]

# Set the cache values
cacheValue = int(cache[0])
cacheTime = int(cache[1])

# If somehow the currentPowerToday is lower than the cached version,
# keep the cached version
if nextValue < cacheValue:
nextValue = cacheValue

# If today has passed, use the actual value from the Omnik inverter
if cacheTime > nextTime:
nextValue = int(result[6])

# Store new stats
pickle.dump([nextValue, nextTime], open(cacheName, 'wb'))

# Update the sensor state, divide by 100 to make it kWh
self._state = (nextValue / 100)
Expand Down

0 comments on commit b53cf23

Please sign in to comment.