Skip to content

Commit

Permalink
Adding dht11 package as submodule
Browse files Browse the repository at this point in the history
Updating humidity sensor to use dht11's defined constants instead of magic
numbers.
  • Loading branch information
mtlynch committed May 13, 2016
1 parent cbc897c commit b6612c0
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
@@ -0,0 +1,3 @@
[submodule "greenpithumb/dht11"]
path = greenpithumb/dht11
url = https://github.com/szazo/DHT11_Python.git
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -3,6 +3,7 @@ language: python
python:
- "2.7"
install:
- pip install -r requirements.txt
- pip install -r test_requirements.txt
- pip install coveralls
script:
Expand Down
5 changes: 4 additions & 1 deletion build
@@ -1 +1,4 @@
coverage run --source greenpithumb -m unittest discover
coverage run \
--source greenpithumb \
--omit "greenpithumb/dht11/*" \
-m unittest discover
1 change: 1 addition & 0 deletions greenpithumb/dht11
Submodule dht11 added at 61f9ca
8 changes: 5 additions & 3 deletions greenpithumb/humidity_sensor.py
@@ -1,5 +1,7 @@
import dht11_exceptions

from dht11 import dht11


class HumiditySensor(object):
"""Wrapper for humidity sensor. Wraps DHT11 sensor."""
Expand All @@ -19,11 +21,11 @@ def get_humidity_level(self):

# TODO(JeetShetty): Replace error codes with constants from dht11
# module
if error_code != 0:
if error_code == 1:
if error_code != dht11.DHT11Result.ERR_NO_ERROR:
if error_code == dht11.DHT11Result.ERR_MISSING_DATA:
raise dht11_exceptions.MissingDataError(
"DHT11 sensor reported missing data")
elif error_code == 2:
elif error_code == dht11.DHT11Result.ERR_CRC:
raise dht11_exceptions.IncorrectCRCError(
"DHT11 sensor reported incorrect CRC")
else:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
@@ -0,0 +1 @@
RPi.GPIO
12 changes: 7 additions & 5 deletions tests/test_humidity_sensor.py
Expand Up @@ -5,6 +5,7 @@

from greenpithumb import humidity_sensor
from greenpithumb import dht11_exceptions
from greenpithumb.dht11 import dht11


class HumiditySensorTest(unittest.TestCase):
Expand All @@ -17,21 +18,22 @@ def setUp(self):

def test_humidity_50(self):
"""Value given should be value returned."""
self.mock_dht11_result.error_code = 0
self.mock_dht11_result.error_code = dht11.DHT11Result.ERR_NO_ERROR
self.mock_dht11_result.humidity = 50.0
humidity_level = self.humidity_sensor.get_humidity_level()
self.assertEqual(humidity_level, 50.0)

def test_missing_data_error(self):
"""Error code of 1 should raise an exception."""
"""Error code of ERR_MISSING_DATA should raise an exception."""
with self.assertRaises(dht11_exceptions.MissingDataError):
self.mock_dht11_result.error_code = 1
self.mock_dht11_result.error_code = (
dht11.DHT11Result.ERR_MISSING_DATA)
self.humidity_sensor.get_humidity_level()

def test_incorrect_crc_error(self):
"""Error code of 2 should raise an exception."""
"""Error code of ERR_CRC should raise an exception."""
with self.assertRaises(dht11_exceptions.IncorrectCRCError):
self.mock_dht11_result.error_code = 2
self.mock_dht11_result.error_code = dht11.DHT11Result.ERR_CRC
self.humidity_sensor.get_humidity_level()

def test_error_code_out_of_range(self):
Expand Down

0 comments on commit b6612c0

Please sign in to comment.