Skip to content

Commit 2e17988

Browse files
Merge pull request #8 from robbinjanssen/rja/add-cache
Add caching configuration attribute
2 parents 86cca00 + e6037b1 commit 2e17988

2 files changed

Lines changed: 30 additions & 5 deletions

File tree

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ sensor:
3838
host: 192.168.100.100
3939
```
4040
41+
By default caching the power today value is enabled, you can disable it using the `cache_power_today` configuration attribute. Check "How does it work?" when/why you might need to disable it.
42+
43+
``` YAML
44+
sensor:
45+
- platform: omnik_inverter
46+
host: 192.168.100.100
47+
cache_power_today: false
48+
```
49+
4150
## How does it work?
4251

4352
The web interface has a javascript file that contains the actual values. This is updated every minute (afaik). Check it out in your browser at `http://<your omnik ip address>/js/status.js`
@@ -58,7 +67,9 @@ This custom component basically requests the URL, looks for the _webData_ part a
5867
- `sensor.solar_power_today` (kWh)
5968
- `sensor.solar_power_total` (kWh)
6069

61-
> Note: I ran into the problem that my Omnik inverter resets the `solar_power_today` to 0.0 after 21:00. This component therefor caches the value and only resets to 0.0 after midnight.
70+
### Caching power today.
71+
72+
In a few cases the Omnik inverter resets the `solar_power_today` to 0.0 after for example 21:00. By setting the `cache_power_today` config attribute to `true` (default) this component will cache the the value and only resets to 0.0 after midnight. If you do not experience this, then disable the cache by setting the config variable to `false`.
6273

6374
## References
6475

custom_components/omnik_inverter/sensor.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
sensor:
55
- platform: omnik_inverter
66
host: 192.168.100.100
7+
cache_power_today: true
78
"""
89
import logging
910
from datetime import timedelta
@@ -22,7 +23,9 @@
2223
import re
2324
import pickle
2425

25-
VERSION = '1.0.1'
26+
VERSION = '1.1.0'
27+
28+
CONF_CACHE_POWER_TODAY = 'cache_power_today'
2629

2730
BASE_URL = 'http://{0}/js/status.js'
2831
BASE_CACHE_NAME = '.{0}.pickle'
@@ -37,12 +40,16 @@
3740
'powertotal': ['Solar Power Total', ENERGY_KILO_WATT_HOUR, 'mdi:chart-line'],
3841
}
3942

40-
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({vol.Required(CONF_HOST): cv.string})
43+
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
44+
vol.Required(CONF_HOST): cv.string,
45+
vol.Optional(CONF_CACHE_POWER_TODAY, default=True): cv.boolean
46+
})
4147

4248

4349
def setup_platform(hass, config, add_devices, discovery_info=None):
4450
"""Setup the Solar Portal sensors."""
4551
host = config.get(CONF_HOST)
52+
cache = config.get(CONF_CACHE_POWER_TODAY)
4653

4754
try:
4855
data = OmnikInverterWeb(host)
@@ -53,7 +60,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
5360
entities = []
5461

5562
for sensor_type in SENSOR_TYPES:
56-
entities.append(OmnikInverterSensor(data, sensor_type))
63+
entities.append(OmnikInverterSensor(data, sensor_type, cache))
5764

5865
add_devices(entities)
5966

@@ -97,10 +104,11 @@ def update(self):
97104
class OmnikInverterSensor(Entity):
98105
"""Representation of a OmnikInverter sensor from the web data."""
99106

100-
def __init__(self, data, sensor_type):
107+
def __init__(self, data, sensor_type, cache):
101108
"""Initialize the sensor."""
102109
self.data = data
103110
self.type = sensor_type
111+
self.cache = cache
104112
self._name = SENSOR_TYPES[self.type][0]
105113
self._unit_of_measurement = SENSOR_TYPES[self.type][1]
106114
self._icon = SENSOR_TYPES[self.type][2]
@@ -151,6 +159,12 @@ def update(self):
151159
nextValue = int(result[6])
152160
nextTime = int(datetime.now().strftime('%H%M'))
153161

162+
# Check if caching is disabled
163+
if (self.cache == False):
164+
# Update the sensor state, divide by 100 to make it kWh
165+
self._state = (nextValue / 100)
166+
return
167+
154168
# Fetch data from the cache
155169
try:
156170
cache = pickle.load(open(cacheName, 'rb'))

0 commit comments

Comments
 (0)