From 636071a22ace2da9598cc7892e024dae4bb490f8 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Mon, 22 Jun 2015 17:59:02 +0200 Subject: [PATCH] add arduino sensor platform --- homeassistant/components/sensor/arduino.py | 87 ++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 homeassistant/components/sensor/arduino.py diff --git a/homeassistant/components/sensor/arduino.py b/homeassistant/components/sensor/arduino.py new file mode 100644 index 00000000000..4210a064d13 --- /dev/null +++ b/homeassistant/components/sensor/arduino.py @@ -0,0 +1,87 @@ +""" +homeassistant.components.sensor.arduino +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Support for getting information from Arduino pins. Only analog pins are +supported. + +Configuration: + +sensor: + platform: arduino + pins: + 7: + name: Door switch + type: analog + 0: + name: Brightness + type: analog + +Variables: + +pins +*Required +An array specifying the digital pins to use on the Arduino board. + +These are the variables for the pins array: + +name +*Required +The name for the pin that will be used in the frontend. + +type +*Required +The type of the pin: 'analog'. +""" +import logging + +import homeassistant.components.arduino as arduino +from homeassistant.helpers.entity import Entity +from homeassistant.const import DEVICE_DEFAULT_NAME + +DEPENDENCIES = ['arduino'] + +_LOGGER = logging.getLogger(__name__) + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """ Sets up the Arduino platform. """ + + # Verify that Arduino board is present + if arduino.BOARD is None: + _LOGGER.error('A connection has not been made to the Arduino board.') + return False + + sensors = [] + pins = config.get('pins') + for pinnum, pin in pins.items(): + if pin.get('name'): + sensors.append(ArduinoSensor(pin.get('name'), + pinnum, + 'analog')) + add_devices(sensors) + + +class ArduinoSensor(Entity): + """ Represents an Arduino Sensor. """ + def __init__(self, name, pin, pin_type): + self._pin = pin + self._name = name or DEVICE_DEFAULT_NAME + self.pin_type = pin_type + self.direction = 'in' + self._value = None + + arduino.BOARD.set_mode(self._pin, self.direction, self.pin_type) + + @property + def state(self): + """ Returns the state of the sensor. """ + return self._value + + @property + def name(self): + """ Get the name of the sensor. """ + return self._name + + def update(self): + """ Get the latest value from the pin. """ + self._value = arduino.BOARD.get_analog_inputs()[self._pin][1]