From afcf3eaac3796b685516c8aab738a73e69bb3d42 Mon Sep 17 00:00:00 2001 From: Malte Deiseroth Date: Sun, 29 Mar 2015 14:38:10 +0200 Subject: [PATCH 1/8] - add ds18S20 1-Wire sensor support - gitignore emacs backup files --- .gitignore | 5 +- homeassistant/components/sensor/onewire.py | 93 ++++++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 homeassistant/components/sensor/onewire.py diff --git a/.gitignore b/.gitignore index a82763e1b6d..c7e72545bf6 100644 --- a/.gitignore +++ b/.gitignore @@ -62,4 +62,7 @@ nosetests.xml # Mr Developer .mr.developer.cfg .project -.pydevproject \ No newline at end of file +.pydevproject + +# Hide emacs backups +*~ diff --git a/homeassistant/components/sensor/onewire.py b/homeassistant/components/sensor/onewire.py new file mode 100644 index 00000000000..384a792296e --- /dev/null +++ b/homeassistant/components/sensor/onewire.py @@ -0,0 +1,93 @@ +""" Support for DS18B20 One Wire Sensors""" +from homeassistant.helpers.entity import Entity +from homeassistant.const import TEMP_CELCIUS, TEMP_FAHRENHEIT +from glob import glob +import os +import time +import logging + + +BASE_DIR = '/sys/bus/w1/devices/' +DEVICE_FOLDERS = glob(os.path.join(BASE_DIR, '28*')) +SENSOR_IDS = [os.path.split(device_folder)[1] for device_folder in DEVICE_FOLDERS] +DEVICE_FILES = [os.path.join(device_folder, 'w1_slave') for device_folder in DEVICE_FOLDERS] + +_LOGGER = logging.getLogger(__name__) + +# pylint: disable=unused-argument +def setup_platform(hass, config, add_devices, discovery_info=None): + """ Sets up the one wire Sensors""" + # TODO check if kernel modules are loaded + + # TODO implment config fore the name, but also default solution + if DEVICE_FILES == []: + _LOGGER.error('No onewire sensor found') + return + + devs = [] + names = [] + try: + ## only one name given + if type(config['names']) == str: + names = [names] + + ## map names and sensors in given order + elif type(config['names']) == list: + names = config['names'] + + ## map names with ids + elif type(config['names']) == dict: + for sensor_id in SENSOR_IDS: + names.append(config['names'][sensor_id]) + + except KeyError: + ## use id as name + if not config['names']: + for sensor_id in SENSOR_IDS: + names.append(sensor_id) + + for device_file, name in zip(DEVICE_FILES, names): + devs.append(OneWire(name, device_file, TEMP_CELCIUS)) + add_devices(devs) + +class OneWire(Entity): + """ A Dallas 1 Wire Sensor""" + + def __init__(self, name, device_file, unit_of_measurement): + self._name = name + self._device_file = device_file + self._unit_of_measurement = unit_of_measurement + + def _read_temp_raw(self): + f = open(self._device_file, 'r') + lines = f.readlines() + f.close() + return lines + + @property + def should_poll(self): + return True + + @property + def name(self): + return self._name + + @property + def state(self): + """ return temperature in unit_of_measurement""" + lines = self._read_temp_raw() + while lines[0].strip()[-3:] != 'YES': + time.sleep(0.2) + lines = self._read_temp_raw() + equals_pos = lines[1].find('t=') + if equals_pos != -1: + temp_string = lines[1][equals_pos+2:] + temp = float(temp_string) / 1000.0 + if self._unit_of_measurement == TEMP_FAHRENHEIT: + temp = temp * 9.0 / 5.0 + 32.0 + return temp + + @property + def unit_of_measurement(self): + return self._unit_of_measurement + \ No newline at end of file From 0ed608abffdf349ebc9e74efd1ea7e0af395c8af Mon Sep 17 00:00:00 2001 From: Malte Deiseroth Date: Tue, 7 Apr 2015 21:10:16 +0200 Subject: [PATCH 2/8] little bug --- homeassistant/components/sensor/onewire.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/sensor/onewire.py b/homeassistant/components/sensor/onewire.py index 384a792296e..c3e0b5e3e9c 100644 --- a/homeassistant/components/sensor/onewire.py +++ b/homeassistant/components/sensor/onewire.py @@ -29,7 +29,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): try: ## only one name given if type(config['names']) == str: - names = [names] + names = config[names] ## map names and sensors in given order elif type(config['names']) == list: From b3683887147dd3031b8dc844d355d6e3e16b62c3 Mon Sep 17 00:00:00 2001 From: deisi Date: Wed, 16 Sep 2015 08:49:12 +0200 Subject: [PATCH 3/8] Update .gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index c7e72545bf6..65c584d143a 100644 --- a/.gitignore +++ b/.gitignore @@ -66,3 +66,5 @@ nosetests.xml # Hide emacs backups *~ +*# +*.orig From b6f954e082c171721ef177bd89d2218838274fdf Mon Sep 17 00:00:00 2001 From: deisi Date: Wed, 16 Sep 2015 10:18:11 +0200 Subject: [PATCH 4/8] Changed handling of config file I tried to implement your suggesteions for the default handlig of the device names. I think this way, everything you wanted is in. --- homeassistant/components/sensor/onewire.py | 39 +++++++++------------- 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/homeassistant/components/sensor/onewire.py b/homeassistant/components/sensor/onewire.py index c3e0b5e3e9c..bb2a0438a75 100644 --- a/homeassistant/components/sensor/onewire.py +++ b/homeassistant/components/sensor/onewire.py @@ -17,35 +17,26 @@ _LOGGER = logging.getLogger(__name__) # pylint: disable=unused-argument def setup_platform(hass, config, add_devices, discovery_info=None): """ Sets up the one wire Sensors""" - # TODO check if kernel modules are loaded - # TODO implment config fore the name, but also default solution if DEVICE_FILES == []: - _LOGGER.error('No onewire sensor found') + _LOGGER.error('No onewire sensor found. Check if dtoverlay=w1-gpio,gpiopin=4 is in your /boot/config.txt and the correct gpiopin number is set.') return devs = [] - names = [] - try: - ## only one name given - if type(config['names']) == str: - names = config[names] - - ## map names and sensors in given order - elif type(config['names']) == list: - names = config['names'] + names = SENSOR_IDS - ## map names with ids - elif type(config['names']) == dict: - for sensor_id in SENSOR_IDS: - names.append(config['names'][sensor_id]) - - except KeyError: - ## use id as name - if not config['names']: - for sensor_id in SENSOR_IDS: - names.append(sensor_id) - + for key in config.keys(): + if key=="names": + ## only one name given + if isinstance(config['names'], str): + names = [config['names']] + ## map names and sensors in given order + elif isinstance(config['names'], list): + names = config['names'] + ## map names to ids. + elif isinstance(config['names'], dict): + names = [config['names'].get(sensor_id, sensor_id) for sensor_id in SENSOR_IDS] + for device_file, name in zip(DEVICE_FILES, names): devs.append(OneWire(name, device_file, TEMP_CELCIUS)) add_devices(devs) @@ -90,4 +81,4 @@ class OneWire(Entity): @property def unit_of_measurement(self): return self._unit_of_measurement - \ No newline at end of file + From ce501ae627e0afa9b5629b4a22ea78a87de01b8d Mon Sep 17 00:00:00 2001 From: Malte Deiseroth Date: Wed, 16 Sep 2015 14:17:41 +0200 Subject: [PATCH 5/8] Improved onewire configuration --- homeassistant/components/sensor/onewire.py | 39 +++++++++------------- 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/homeassistant/components/sensor/onewire.py b/homeassistant/components/sensor/onewire.py index c3e0b5e3e9c..bb2a0438a75 100644 --- a/homeassistant/components/sensor/onewire.py +++ b/homeassistant/components/sensor/onewire.py @@ -17,35 +17,26 @@ _LOGGER = logging.getLogger(__name__) # pylint: disable=unused-argument def setup_platform(hass, config, add_devices, discovery_info=None): """ Sets up the one wire Sensors""" - # TODO check if kernel modules are loaded - # TODO implment config fore the name, but also default solution if DEVICE_FILES == []: - _LOGGER.error('No onewire sensor found') + _LOGGER.error('No onewire sensor found. Check if dtoverlay=w1-gpio,gpiopin=4 is in your /boot/config.txt and the correct gpiopin number is set.') return devs = [] - names = [] - try: - ## only one name given - if type(config['names']) == str: - names = config[names] - - ## map names and sensors in given order - elif type(config['names']) == list: - names = config['names'] + names = SENSOR_IDS - ## map names with ids - elif type(config['names']) == dict: - for sensor_id in SENSOR_IDS: - names.append(config['names'][sensor_id]) - - except KeyError: - ## use id as name - if not config['names']: - for sensor_id in SENSOR_IDS: - names.append(sensor_id) - + for key in config.keys(): + if key=="names": + ## only one name given + if isinstance(config['names'], str): + names = [config['names']] + ## map names and sensors in given order + elif isinstance(config['names'], list): + names = config['names'] + ## map names to ids. + elif isinstance(config['names'], dict): + names = [config['names'].get(sensor_id, sensor_id) for sensor_id in SENSOR_IDS] + for device_file, name in zip(DEVICE_FILES, names): devs.append(OneWire(name, device_file, TEMP_CELCIUS)) add_devices(devs) @@ -90,4 +81,4 @@ class OneWire(Entity): @property def unit_of_measurement(self): return self._unit_of_measurement - \ No newline at end of file + From 3027b4a5a85b4a0bd19eb9fc84fd62cbf759f5f8 Mon Sep 17 00:00:00 2001 From: Malte Deiseroth Date: Tue, 22 Sep 2015 12:32:45 +0200 Subject: [PATCH 6/8] respect pylint suggestions --- homeassistant/components/sensor/onewire.py | 35 +++++++++++----------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/sensor/onewire.py b/homeassistant/components/sensor/onewire.py index bb2a0438a75..60f30715073 100644 --- a/homeassistant/components/sensor/onewire.py +++ b/homeassistant/components/sensor/onewire.py @@ -19,24 +19,25 @@ def setup_platform(hass, config, add_devices, discovery_info=None): """ Sets up the one wire Sensors""" if DEVICE_FILES == []: - _LOGGER.error('No onewire sensor found. Check if dtoverlay=w1-gpio,gpiopin=4 is in your /boot/config.txt and the correct gpiopin number is set.') + _LOGGER.error('No onewire sensor found. Check if + dtoverlay=w1-gpio,gpiopin=4 is in your /boot/config.txt + and the correct gpiopin number is set.') return devs = [] names = SENSOR_IDS for key in config.keys(): - if key=="names": - ## only one name given - if isinstance(config['names'], str): - names = [config['names']] - ## map names and sensors in given order - elif isinstance(config['names'], list): - names = config['names'] - ## map names to ids. - elif isinstance(config['names'], dict): - names = [config['names'].get(sensor_id, sensor_id) for sensor_id in SENSOR_IDS] - + if key == "names": + ## only one name given + if isinstance(config['names'], str): + names = [config['names']] + ## map names and sensors in given order + elif isinstance(config['names'], list): + names = config['names'] + ## map names to ids. + elif isinstance(config['names'], dict): + names = [config['names'].get(sensor_id, sensor_id) for sensor_id in SENSOR_IDS] for device_file, name in zip(DEVICE_FILES, names): devs.append(OneWire(name, device_file, TEMP_CELCIUS)) add_devices(devs) @@ -50,9 +51,10 @@ class OneWire(Entity): self._unit_of_measurement = unit_of_measurement def _read_temp_raw(self): - f = open(self._device_file, 'r') - lines = f.readlines() - f.close() + """ read the temperature as it is returned by the sensor""" + ds_device_file = open(self._device_file, 'r') + lines = ds_device_file.readlines() + ds_device_file.close() return lines @property @@ -74,11 +76,10 @@ class OneWire(Entity): if equals_pos != -1: temp_string = lines[1][equals_pos+2:] temp = float(temp_string) / 1000.0 - if self._unit_of_measurement == TEMP_FAHRENHEIT: + if self._unit_of_measurement == TEMP_FAHRENHEIT: temp = temp * 9.0 / 5.0 + 32.0 return temp @property def unit_of_measurement(self): return self._unit_of_measurement - From d475e5362bb2e563ef7e77321ae1654bb63116df Mon Sep 17 00:00:00 2001 From: Malte Deiseroth Date: Tue, 22 Sep 2015 12:53:44 +0200 Subject: [PATCH 7/8] respect flake8 errors --- homeassistant/components/sensor/onewire.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/sensor/onewire.py b/homeassistant/components/sensor/onewire.py index 60f30715073..a85e25fc1ad 100644 --- a/homeassistant/components/sensor/onewire.py +++ b/homeassistant/components/sensor/onewire.py @@ -9,18 +9,22 @@ import logging BASE_DIR = '/sys/bus/w1/devices/' DEVICE_FOLDERS = glob(os.path.join(BASE_DIR, '28*')) -SENSOR_IDS = [os.path.split(device_folder)[1] for device_folder in DEVICE_FOLDERS] -DEVICE_FILES = [os.path.join(device_folder, 'w1_slave') for device_folder in DEVICE_FOLDERS] +SENSOR_IDS = [] +DEVICE_FILES = [] +for device_folder in DEVICE_FOLDERS: + SENSOR_IDS.append(os.path.split(device_folder)[1]) + DEVICE_FILES.append(os.path.join(device_folder, 'w1_slave')) _LOGGER = logging.getLogger(__name__) + # pylint: disable=unused-argument def setup_platform(hass, config, add_devices, discovery_info=None): """ Sets up the one wire Sensors""" if DEVICE_FILES == []: - _LOGGER.error('No onewire sensor found. Check if - dtoverlay=w1-gpio,gpiopin=4 is in your /boot/config.txt + _LOGGER.error('No onewire sensor found. Check if + dtoverlay=w1-gpio,gpiopin=4 is in your /boot/config.txt and the correct gpiopin number is set.') return @@ -29,19 +33,20 @@ def setup_platform(hass, config, add_devices, discovery_info=None): for key in config.keys(): if key == "names": - ## only one name given + # only one name given if isinstance(config['names'], str): names = [config['names']] - ## map names and sensors in given order + # map names and sensors in given order elif isinstance(config['names'], list): names = config['names'] - ## map names to ids. + # map names to ids. elif isinstance(config['names'], dict): names = [config['names'].get(sensor_id, sensor_id) for sensor_id in SENSOR_IDS] for device_file, name in zip(DEVICE_FILES, names): devs.append(OneWire(name, device_file, TEMP_CELCIUS)) add_devices(devs) + class OneWire(Entity): """ A Dallas 1 Wire Sensor""" From ef76047ba2224e5b6f3f2ca4334bc2558e65a15b Mon Sep 17 00:00:00 2001 From: Malte Deiseroth Date: Tue, 22 Sep 2015 13:27:08 +0200 Subject: [PATCH 8/8] new try to add everythin from the stylecheckers --- homeassistant/components/sensor/onewire.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/sensor/onewire.py b/homeassistant/components/sensor/onewire.py index a85e25fc1ad..06fdbcef141 100644 --- a/homeassistant/components/sensor/onewire.py +++ b/homeassistant/components/sensor/onewire.py @@ -23,9 +23,10 @@ def setup_platform(hass, config, add_devices, discovery_info=None): """ Sets up the one wire Sensors""" if DEVICE_FILES == []: - _LOGGER.error('No onewire sensor found. Check if - dtoverlay=w1-gpio,gpiopin=4 is in your /boot/config.txt - and the correct gpiopin number is set.') + _LOGGER.error('No onewire sensor found.') + _LOGGER.error('Check if dtoverlay=w1-gpio,gpiopin=4.') + _LOGGER.error('is in your /boot/config.txt and') + _LOGGER.error('the correct gpiopin number is set.') return devs = [] @@ -41,12 +42,14 @@ def setup_platform(hass, config, add_devices, discovery_info=None): names = config['names'] # map names to ids. elif isinstance(config['names'], dict): - names = [config['names'].get(sensor_id, sensor_id) for sensor_id in SENSOR_IDS] + names = [] + for sensor_id in SENSOR_IDS: + names.append(config['names'].get(sensor_id, sensor_id)) for device_file, name in zip(DEVICE_FILES, names): devs.append(OneWire(name, device_file, TEMP_CELCIUS)) add_devices(devs) - + class OneWire(Entity): """ A Dallas 1 Wire Sensor"""