Skip to content

Commit

Permalink
Add custom exception to light_sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
JeetShetty committed Jun 28, 2016
1 parent a9f5355 commit 6860296
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 12 additions & 1 deletion greenpithumb/light_sensor.py
Expand Up @@ -4,6 +4,14 @@
_LIGHT_SENSOR_MAX_VALUE = adc.PIN_MAX_VALUE


class Error(Exception):
pass


class LightSensorLowError(Error):
pass


class LightSensor(object):
"""Wrapper for light sensor."""

Expand All @@ -21,7 +29,10 @@ def get_light_level(self):
light_level = self._adc.read_pin(adc.PIN_LIGHT_SENSOR)

if light_level < _LIGHT_SENSOR_MIN_VALUE:
raise ValueError('Light sensor reading out of range')
raise LightSensorLowError(
('Light sensor reading of %.1f is less than the minimum '
'expected value of %.1f.') % (light_level,
_LIGHT_SENSOR_MIN_VALUE))

light_level_as_pct = 100 * (
(light_level - _LIGHT_SENSOR_MIN_VALUE) /
Expand Down
2 changes: 1 addition & 1 deletion tests/test_light_sensor.py
Expand Up @@ -23,7 +23,7 @@ def test_light_50_pct(self):

def test_light_level_too_low(self):
"""Light sensor value less than min should raise a ValueError."""
with self.assertRaises(ValueError):
with self.assertRaises(light_sensor.LightSensorLowError):
self.mock_adc.read_pin.return_value = (
light_sensor._LIGHT_SENSOR_MIN_VALUE - 1)
self.light_sensor.get_light_level()

0 comments on commit 6860296

Please sign in to comment.