Embed all platforms into components (#20677)
* Consolidate all components with platforms * Organize tests * Fix more tests * Fix Verisure tests * one final test fix * Add change * Fix coverage
This commit is contained in:
parent
a24da611c5
commit
e2d3c27e85
490 changed files with 255 additions and 517 deletions
120
homeassistant/components/arduino/__init__.py
Normal file
120
homeassistant/components/arduino/__init__.py
Normal file
|
@ -0,0 +1,120 @@
|
|||
"""
|
||||
Support for Arduino boards running with the Firmata firmware.
|
||||
|
||||
For more details about this component, please refer to the documentation at
|
||||
https://home-assistant.io/components/arduino/
|
||||
"""
|
||||
import logging
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.const import (
|
||||
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
|
||||
from homeassistant.const import CONF_PORT
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
REQUIREMENTS = ['PyMata==2.14']
|
||||
|
||||
_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)
|
||||
|
||||
|
||||
def setup(hass, config):
|
||||
"""Set up the Arduino component."""
|
||||
import serial
|
||||
|
||||
port = config[DOMAIN][CONF_PORT]
|
||||
|
||||
global BOARD
|
||||
try:
|
||||
BOARD = ArduinoBoard(port)
|
||||
except (serial.serialutil.SerialException, FileNotFoundError):
|
||||
_LOGGER.error("Your port %s is not accessible", port)
|
||||
return False
|
||||
|
||||
try:
|
||||
if BOARD.get_firmata()[1] <= 2:
|
||||
_LOGGER.error("The StandardFirmata sketch should be 2.2 or newer")
|
||||
return False
|
||||
except IndexError:
|
||||
_LOGGER.warning("The version of the StandardFirmata sketch was not"
|
||||
"detected. This may lead to side effects")
|
||||
|
||||
def stop_arduino(event):
|
||||
"""Stop the Arduino service."""
|
||||
BOARD.disconnect()
|
||||
|
||||
def start_arduino(event):
|
||||
"""Start the Arduino service."""
|
||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_arduino)
|
||||
|
||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_arduino)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
class ArduinoBoard:
|
||||
"""Representation of an Arduino board."""
|
||||
|
||||
def __init__(self, port):
|
||||
"""Initialize the board."""
|
||||
from PyMata.pymata import PyMata
|
||||
self._port = port
|
||||
self._board = PyMata(self._port, verbose=False)
|
||||
|
||||
def set_mode(self, pin, direction, mode):
|
||||
"""Set the mode and the direction of a given pin."""
|
||||
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, 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):
|
||||
"""Get the values from the pins."""
|
||||
self._board.capability_query()
|
||||
return self._board.get_analog_response_table()
|
||||
|
||||
def set_digital_out_high(self, pin):
|
||||
"""Set a given digital pin to high."""
|
||||
self._board.digital_write(pin, 1)
|
||||
|
||||
def set_digital_out_low(self, pin):
|
||||
"""Set a given digital pin to low."""
|
||||
self._board.digital_write(pin, 0)
|
||||
|
||||
def get_digital_in(self, pin):
|
||||
"""Get the value from a given digital pin."""
|
||||
self._board.digital_read(pin)
|
||||
|
||||
def get_analog_in(self, pin):
|
||||
"""Get the value from a given analog pin."""
|
||||
self._board.analog_read(pin)
|
||||
|
||||
def get_firmata(self):
|
||||
"""Return the version of the Firmata firmware."""
|
||||
return self._board.get_firmata_version()
|
||||
|
||||
def disconnect(self):
|
||||
"""Disconnect the board and close the serial connection."""
|
||||
self._board.reset()
|
||||
self._board.close()
|
75
homeassistant/components/arduino/sensor.py
Normal file
75
homeassistant/components/arduino/sensor.py
Normal file
|
@ -0,0 +1,75 @@
|
|||
"""
|
||||
Support for getting information from Arduino pins.
|
||||
|
||||
Only analog pins are supported.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/sensor.arduino/
|
||||
"""
|
||||
import logging
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||
from homeassistant.components import arduino
|
||||
from homeassistant.const import CONF_NAME
|
||||
from homeassistant.helpers.entity import Entity
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONF_PINS = 'pins'
|
||||
CONF_TYPE = 'analog'
|
||||
|
||||
DEPENDENCIES = ['arduino']
|
||||
|
||||
PIN_SCHEMA = vol.Schema({
|
||||
vol.Required(CONF_NAME): cv.string,
|
||||
})
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_PINS):
|
||||
vol.Schema({cv.positive_int: PIN_SCHEMA}),
|
||||
})
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Set up the Arduino platform."""
|
||||
if arduino.BOARD is None:
|
||||
_LOGGER.error("A connection has not been made to the Arduino board")
|
||||
return False
|
||||
|
||||
pins = config.get(CONF_PINS)
|
||||
|
||||
sensors = []
|
||||
for pinnum, pin in pins.items():
|
||||
sensors.append(ArduinoSensor(pin.get(CONF_NAME), pinnum, CONF_TYPE))
|
||||
add_entities(sensors)
|
||||
|
||||
|
||||
class ArduinoSensor(Entity):
|
||||
"""Representation of an Arduino Sensor."""
|
||||
|
||||
def __init__(self, name, pin, pin_type):
|
||||
"""Initialize the sensor."""
|
||||
self._pin = pin
|
||||
self._name = 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):
|
||||
"""Return 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]
|
94
homeassistant/components/arduino/switch.py
Normal file
94
homeassistant/components/arduino/switch.py
Normal file
|
@ -0,0 +1,94 @@
|
|||
"""
|
||||
Support for switching Arduino pins on and off.
|
||||
|
||||
So far only digital pins are supported.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/switch.arduino/
|
||||
"""
|
||||
import logging
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components import arduino
|
||||
from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
|
||||
from homeassistant.const import CONF_NAME
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
DEPENDENCIES = ['arduino']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONF_PINS = 'pins'
|
||||
CONF_TYPE = 'digital'
|
||||
CONF_NEGATE = 'negate'
|
||||
CONF_INITIAL = 'initial'
|
||||
|
||||
PIN_SCHEMA = vol.Schema({
|
||||
vol.Required(CONF_NAME): cv.string,
|
||||
vol.Optional(CONF_INITIAL, default=False): cv.boolean,
|
||||
vol.Optional(CONF_NEGATE, default=False): cv.boolean,
|
||||
})
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_PINS, default={}):
|
||||
vol.Schema({cv.positive_int: PIN_SCHEMA}),
|
||||
})
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Set 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
|
||||
|
||||
pins = config.get(CONF_PINS)
|
||||
|
||||
switches = []
|
||||
for pinnum, pin in pins.items():
|
||||
switches.append(ArduinoSwitch(pinnum, pin))
|
||||
add_entities(switches)
|
||||
|
||||
|
||||
class ArduinoSwitch(SwitchDevice):
|
||||
"""Representation of an Arduino switch."""
|
||||
|
||||
def __init__(self, pin, options):
|
||||
"""Initialize the Pin."""
|
||||
self._pin = pin
|
||||
self._name = options.get(CONF_NAME)
|
||||
self.pin_type = CONF_TYPE
|
||||
self.direction = 'out'
|
||||
|
||||
self._state = options.get(CONF_INITIAL)
|
||||
|
||||
if options.get(CONF_NEGATE):
|
||||
self.turn_on_handler = arduino.BOARD.set_digital_out_low
|
||||
self.turn_off_handler = arduino.BOARD.set_digital_out_high
|
||||
else:
|
||||
self.turn_on_handler = arduino.BOARD.set_digital_out_high
|
||||
self.turn_off_handler = arduino.BOARD.set_digital_out_low
|
||||
|
||||
arduino.BOARD.set_mode(self._pin, self.direction, self.pin_type)
|
||||
(self.turn_on_handler if self._state else self.turn_off_handler)(pin)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Get the name of the pin."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if pin is high/on."""
|
||||
return self._state
|
||||
|
||||
def turn_on(self, **kwargs):
|
||||
"""Turn the pin to high/on."""
|
||||
self._state = True
|
||||
self.turn_on_handler(self._pin)
|
||||
|
||||
def turn_off(self, **kwargs):
|
||||
"""Turn the pin to low/off."""
|
||||
self._state = False
|
||||
self.turn_off_handler(self._pin)
|
Loading…
Add table
Add a link
Reference in a new issue