summaryrefslogtreecommitdiff
path: root/envmon/onewire-ow.py
blob: 6e3420bcfc024203291919f4e47a52232d0d30ad (plain)
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
39
40
41
42
43
44
45
46
"""
Onewire interface using owserver
"""

import os
import ow

class OnewireException(Exception):
	pass

ow.init('127.0.0.1:4304')

_sensors = {}
with open(os.path.join(os.path.realpath(os.path.dirname(__file__)), 'onewire-sensors.txt'), 'r') as sensors_file:
	for line in sensors_file:
		line = line.strip()
		address, name = line.split(' ', 1)
		_sensors[name] = address

def sensors():
	return list(_sensors.keys())

class SensorFacade(object):
	def __init__(self, sensor, name):
		self._sensor = sensor
		self._name = name

	def __getattr__(self, name):
		return getattr(self._sensor, name)

	@property
	def temperature(self):
		for i in range(1,3):
			rv = float(self._sensor.temperature)
			if rv > 65:
				continue
			if rv < -25:
				continue
			return rv
		raise OnewireException("Bus error for %s" % self._name)

def sensor(name):
	if name not in _sensors:
		raise OnewireException('Don\'t know about sensor %s' % name)

	return SensorFacade(ow.Sensor('/%s' % _sensors[name]), name)