2019-02-13 20:21:14 +00:00
|
|
|
"""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
|
|
|
|
|
2019-12-09 10:26:03 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
2016-10-11 07:56:57 +00:00
|
|
|
from homeassistant.const import CONF_NAME
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-12-09 10:26:03 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2016-10-11 07:56:57 +00:00
|
|
|
|
2020-04-05 21:27:58 +00:00
|
|
|
from . import DOMAIN
|
|
|
|
|
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
|
|
|
|
2018-08-24 14:37:30 +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."""
|
2020-04-05 21:27:58 +00:00
|
|
|
board = hass.data[DOMAIN]
|
|
|
|
|
2020-04-07 20:45:56 +00:00
|
|
|
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():
|
2020-04-05 21:27:58 +00:00
|
|
|
sensors.append(ArduinoSensor(pin.get(CONF_NAME), pinnum, CONF_TYPE, board))
|
2018-08-24 14:37:30 +00:00
|
|
|
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."""
|
|
|
|
|
2020-04-05 21:27:58 +00:00
|
|
|
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
|
|
|
|
|
2020-04-05 21:27:58 +00:00
|
|
|
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."""
|
2020-04-05 21:27:58 +00:00
|
|
|
self._value = self._board.get_analog_inputs()[self._pin][1]
|