core/homeassistant/components/arduino/sensor.py

64 lines
1.7 KiB
Python
Raw Normal View History

"""Support for getting information from Arduino pins."""
2015-06-22 15:59:02 +00:00
import logging
2016-10-11 07:56:57 +00:00
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.components import arduino
2016-10-11 07:56:57 +00:00
from homeassistant.const import CONF_NAME
2016-02-19 05:27:50 +00:00
from homeassistant.helpers.entity import Entity
2016-10-11 07:56:57 +00:00
import homeassistant.helpers.config_validation as cv
2015-06-22 15:59:02 +00:00
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
CONF_PINS = "pins"
CONF_TYPE = "analog"
2016-10-11 07:56:57 +00:00
2019-07-31 19:25:30 +00:00
PIN_SCHEMA = vol.Schema({vol.Required(CONF_NAME): cv.string})
2016-10-11 07:56:57 +00:00
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{vol.Required(CONF_PINS): vol.Schema({cv.positive_int: PIN_SCHEMA})}
)
2016-10-11 07:56:57 +00:00
2015-06-22 15:59:02 +00:00
def setup_platform(hass, config, add_entities, discovery_info=None):
2016-10-11 07:56:57 +00:00
"""Set up the Arduino platform."""
2015-06-22 15:59:02 +00:00
if arduino.BOARD is None:
2016-10-11 07:56:57 +00:00
_LOGGER.error("A connection has not been made to the Arduino board")
2015-06-22 15:59:02 +00:00
return False
2016-10-11 07:56:57 +00:00
pins = config.get(CONF_PINS)
2015-06-22 15:59:02 +00:00
sensors = []
for pinnum, pin in pins.items():
2016-10-11 07:56:57 +00:00
sensors.append(ArduinoSensor(pin.get(CONF_NAME), pinnum, CONF_TYPE))
add_entities(sensors)
2015-06-22 15:59:02 +00:00
class ArduinoSensor(Entity):
2016-03-08 15:46:34 +00:00
"""Representation of an Arduino Sensor."""
2015-06-22 15:59:02 +00:00
def __init__(self, name, pin, pin_type):
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
2015-06-22 15:59:02 +00:00
self._pin = pin
2016-10-11 07:56:57 +00:00
self._name = name
2015-06-22 15:59:02 +00:00
self.pin_type = pin_type
2019-07-31 19:25:30 +00:00
self.direction = "in"
2015-06-22 15:59:02 +00:00
self._value = None
arduino.BOARD.set_mode(self._pin, self.direction, self.pin_type)
@property
def state(self):
2016-03-08 15:46:34 +00:00
"""Return the state of the sensor."""
2015-06-22 15:59:02 +00:00
return self._value
@property
def name(self):
2016-02-23 05:21:49 +00:00
"""Get the name of the sensor."""
2015-06-22 15:59:02 +00:00
return self._name
def update(self):
2016-02-23 05:21:49 +00:00
"""Get the latest value from the pin."""
2015-06-22 15:59:02 +00:00
self._value = arduino.BOARD.get_analog_inputs()[self._pin][1]