From c1270cf0bb1f07b8dd7a73ecbe18a06ac80338f0 Mon Sep 17 00:00:00 2001 From: roqeer Date: Tue, 9 Feb 2016 18:58:04 +0100 Subject: [PATCH 1/3] Add name property to DHT sensor Add name to distinguish between multiple connected DHT sensors --- homeassistant/components/sensor/dht.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/sensor/dht.py b/homeassistant/components/sensor/dht.py index 983755aa7bd..5e1f7dc14df 100644 --- a/homeassistant/components/sensor/dht.py +++ b/homeassistant/components/sensor/dht.py @@ -23,6 +23,7 @@ SENSOR_TYPES = { 'temperature': ['Temperature', None], 'humidity': ['Humidity', '%'] } +DEFAULT_NAME = "DHT Sensor" # Return cached results if last scan was less then this time ago # DHT11 is able to deliver data once per second, DHT22 once every two MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30) @@ -53,12 +54,17 @@ def setup_platform(hass, config, add_devices, discovery_info=None): data = DHTClient(Adafruit_DHT, sensor, pin) dev = [] + if config['name']: + name = config['name'] + else: + name = DEFAULT_NAME + try: for variable in config['monitored_conditions']: if variable not in SENSOR_TYPES: _LOGGER.error('Sensor type: "%s" does not exist', variable) else: - dev.append(DHTSensor(data, variable, unit)) + dev.append(DHTSensor(data, variable, unit, name)) except KeyError: pass @@ -69,8 +75,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None): class DHTSensor(Entity): """ Implements an DHT sensor. """ - def __init__(self, dht_client, sensor_type, temp_unit): - self.client_name = 'DHT sensor' + def __init__(self, dht_client, sensor_type, temp_unit, name): + self.client_name = name self._name = SENSOR_TYPES[sensor_type][0] self.dht_client = dht_client self.temp_unit = temp_unit From 3e26af5ff13408a898fa08ae31c5639bf6234852 Mon Sep 17 00:00:00 2001 From: roqeer Date: Tue, 9 Feb 2016 19:09:48 +0100 Subject: [PATCH 2/3] Correct bad-indentation --- homeassistant/components/sensor/dht.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/sensor/dht.py b/homeassistant/components/sensor/dht.py index 5e1f7dc14df..d6f21924280 100644 --- a/homeassistant/components/sensor/dht.py +++ b/homeassistant/components/sensor/dht.py @@ -55,9 +55,9 @@ def setup_platform(hass, config, add_devices, discovery_info=None): data = DHTClient(Adafruit_DHT, sensor, pin) dev = [] if config['name']: - name = config['name'] + name = config['name'] else: - name = DEFAULT_NAME + name = DEFAULT_NAME try: for variable in config['monitored_conditions']: From 4bf4d9434440109de97a859e8b82eea11c806156 Mon Sep 17 00:00:00 2001 From: roqeer Date: Tue, 9 Feb 2016 22:09:44 +0100 Subject: [PATCH 3/3] Changed to dict lookup with default value Corrected on request --- homeassistant/components/sensor/dht.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/homeassistant/components/sensor/dht.py b/homeassistant/components/sensor/dht.py index d6f21924280..14bb7f6bf99 100644 --- a/homeassistant/components/sensor/dht.py +++ b/homeassistant/components/sensor/dht.py @@ -54,10 +54,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): data = DHTClient(Adafruit_DHT, sensor, pin) dev = [] - if config['name']: - name = config['name'] - else: - name = DEFAULT_NAME + name = config.get('name', DEFAULT_NAME) try: for variable in config['monitored_conditions']: