core/homeassistant/components/arduino/sensor.py

59 lines
1.6 KiB
Python
Raw Normal View History

"""Support for getting information from Arduino pins."""
2016-10-11 07:56:57 +00:00
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
2016-10-11 07:56:57 +00:00
from homeassistant.const import CONF_NAME
import homeassistant.helpers.config_validation as cv
from . import DOMAIN
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."""
board = hass.data[DOMAIN]
pins = config[CONF_PINS]
2016-10-11 07:56:57 +00:00
2015-06-22 15:59:02 +00:00
sensors = []
for pinnum, pin in pins.items():
sensors.append(ArduinoSensor(pin.get(CONF_NAME), pinnum, CONF_TYPE, board))
add_entities(sensors)
2015-06-22 15:59:02 +00:00
class ArduinoSensor(SensorEntity):
2016-03-08 15:46:34 +00:00
"""Representation of an Arduino Sensor."""
def __init__(self, name, pin, pin_type, board):
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
board.set_mode(self._pin, self.direction, self.pin_type)
self._board = board
2015-06-22 15:59:02 +00:00
@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."""
self._value = self._board.get_analog_inputs()[self._pin][1]