forked from sightsdev/sights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor_wrapper.py
38 lines (32 loc) · 1.3 KB
/
sensor_wrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import logging
class SensorWrapper:
def __init__(self, config):
# Setup logger
self.logger = logging.getLogger(__name__)
# Last time get_data() was called
self.last_run = 0
# If any of these required values do not exist in the config, display a warning
if {'enabled', 'type', 'period', 'name'} > set(config):
self.logger.error(f"Sensor config entry: {config} is missing a required option! Check your config.")
# Load essential configuration options
self.enabled = config.get('enabled', False)
self.period = config.get('period', -1)
self.name = config.get('name', self.type_)
def get_data(self):
return None
def get_initial(self):
return None
def is_ready(self, now):
# Time since last checked
elapsed_time = now - self.last_run
# Only get data if it has been long enough since last run
# Or if this is the first time checking since we need some data on the graph
if elapsed_time > self.period or self.last_run == 0:
# Store current time as last_time
self.last_run = now
# Only return True if sensor is actually enabled
return self.enabled
else:
return False
def close(self):
pass