core/homeassistant/components/arduino.py

119 lines
3.7 KiB
Python
Raw Normal View History

2015-06-22 15:58:27 +00:00
"""
2016-03-07 17:49:31 +00:00
Support for Arduino boards running with the Firmata firmware.
2015-06-22 15:58:27 +00:00
2015-10-23 20:34:23 +00:00
For more details about this component, please refer to the documentation at
2015-11-09 12:12:18 +00:00
https://home-assistant.io/components/arduino/
2015-06-22 15:58:27 +00:00
"""
import logging
import voluptuous as vol
2016-02-19 05:27:50 +00:00
from homeassistant.const import (
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
from homeassistant.const import CONF_PORT
import homeassistant.helpers.config_validation as cv
2015-06-22 15:58:27 +00:00
2016-09-14 05:52:11 +00:00
REQUIREMENTS = ['PyMata==2.13']
2015-06-22 15:58:27 +00:00
_LOGGER = logging.getLogger(__name__)
BOARD = None
DOMAIN = 'arduino'
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_PORT): cv.string,
}),
}, extra=vol.ALLOW_EXTRA)
2015-06-22 15:58:27 +00:00
def setup(hass, config):
2016-03-07 17:49:31 +00:00
"""Setup the Arduino component."""
2016-01-29 05:37:08 +00:00
import serial
2015-06-22 15:58:27 +00:00
global BOARD
try:
BOARD = ArduinoBoard(config[DOMAIN][CONF_PORT])
2015-06-22 15:58:27 +00:00
except (serial.serialutil.SerialException, FileNotFoundError):
_LOGGER.exception("Your port is not accessible.")
return False
if BOARD.get_firmata()[1] <= 2:
_LOGGER.error("The StandardFirmata sketch should be 2.2 or newer.")
return False
def stop_arduino(event):
2016-03-07 17:49:31 +00:00
"""Stop the Arduino service."""
2015-06-22 15:58:27 +00:00
BOARD.disconnect()
def start_arduino(event):
2016-03-07 17:49:31 +00:00
"""Start the Arduino service."""
2015-06-22 15:58:27 +00:00
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_arduino)
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_arduino)
return True
class ArduinoBoard(object):
2016-03-08 16:55:57 +00:00
"""Representation of an Arduino board."""
2015-06-22 15:58:27 +00:00
def __init__(self, port):
2016-03-08 16:55:57 +00:00
"""Initialize the board."""
2016-01-29 05:37:08 +00:00
from PyMata.pymata import PyMata
2015-06-22 15:58:27 +00:00
self._port = port
self._board = PyMata(self._port, verbose=False)
def set_mode(self, pin, direction, mode):
2016-03-07 17:49:31 +00:00
"""Set the mode and the direction of a given pin."""
2015-06-22 15:58:27 +00:00
if mode == 'analog' and direction == 'in':
self._board.set_pin_mode(pin,
self._board.INPUT,
self._board.ANALOG)
elif mode == 'analog' and direction == 'out':
self._board.set_pin_mode(pin,
self._board.OUTPUT,
self._board.ANALOG)
elif mode == 'digital' and direction == 'in':
self._board.set_pin_mode(pin,
self._board.INPUT,
2015-06-22 15:58:27 +00:00
self._board.DIGITAL)
elif mode == 'digital' and direction == 'out':
self._board.set_pin_mode(pin,
self._board.OUTPUT,
self._board.DIGITAL)
elif mode == 'pwm':
self._board.set_pin_mode(pin,
self._board.OUTPUT,
self._board.PWM)
def get_analog_inputs(self):
2016-03-07 17:49:31 +00:00
"""Get the values from the pins."""
2015-06-22 15:58:27 +00:00
self._board.capability_query()
return self._board.get_analog_response_table()
def set_digital_out_high(self, pin):
2016-03-07 17:49:31 +00:00
"""Set a given digital pin to high."""
2015-06-22 15:58:27 +00:00
self._board.digital_write(pin, 1)
def set_digital_out_low(self, pin):
2016-03-07 17:49:31 +00:00
"""Set a given digital pin to low."""
2015-06-22 15:58:27 +00:00
self._board.digital_write(pin, 0)
def get_digital_in(self, pin):
2016-03-07 17:49:31 +00:00
"""Get the value from a given digital pin."""
2015-06-22 15:58:27 +00:00
self._board.digital_read(pin)
def get_analog_in(self, pin):
2016-03-07 17:49:31 +00:00
"""Get the value from a given analog pin."""
2015-06-22 15:58:27 +00:00
self._board.analog_read(pin)
def get_firmata(self):
2016-03-07 17:49:31 +00:00
"""Return the version of the Firmata firmware."""
2015-06-22 15:58:27 +00:00
return self._board.get_firmata_version()
def disconnect(self):
2016-03-08 16:55:57 +00:00
"""Disconnect the board and close the serial connection."""
2015-06-22 15:58:27 +00:00
self._board.reset()
self._board.close()