core/homeassistant/components/switch/arduino.py

76 lines
2.2 KiB
Python
Raw Normal View History

2015-06-22 15:58:46 +00:00
"""
2016-03-08 12:35:39 +00:00
Support for switching Arduino pins on and off.
So far only digital pins are supported.
2015-06-22 15:58:46 +00:00
2015-10-21 20:53:09 +00:00
For more details about this platform, please refer to the documentation at
2015-11-09 12:12:18 +00:00
https://home-assistant.io/components/switch.arduino/
2015-06-22 15:58:46 +00:00
"""
import logging
import homeassistant.components.arduino as arduino
from homeassistant.components.switch import SwitchDevice
from homeassistant.const import DEVICE_DEFAULT_NAME
DEPENDENCIES = ['arduino']
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_devices, discovery_info=None):
2016-03-08 12:35:39 +00:00
"""Setup the Arduino platform."""
2015-06-22 15:58:46 +00:00
# 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
switches = []
pins = config.get('pins')
for pinnum, pin in pins.items():
if pin.get('name'):
switches.append(ArduinoSwitch(pinnum, pin))
2015-06-22 15:58:46 +00:00
add_devices(switches)
class ArduinoSwitch(SwitchDevice):
2016-03-08 12:35:39 +00:00
"""Representation of an Arduino switch."""
def __init__(self, pin, options):
2016-03-08 12:35:39 +00:00
"""Initialize the Pin."""
2015-06-22 15:58:46 +00:00
self._pin = pin
self._name = options.get('name') or DEVICE_DEFAULT_NAME
self.pin_type = options.get('type')
2015-06-22 15:58:46 +00:00
self.direction = 'out'
self._state = options.get('initial', False)
if options.get('negate', False):
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
2015-06-22 15:58:46 +00:00
arduino.BOARD.set_mode(self._pin, self.direction, self.pin_type)
(self.turn_on_handler if self._state else self.turn_off_handler)(pin)
2015-06-22 15:58:46 +00:00
@property
def name(self):
2016-03-08 12:35:39 +00:00
"""Get the name of the pin."""
2015-06-22 15:58:46 +00:00
return self._name
@property
def is_on(self):
2016-03-08 12:35:39 +00:00
"""Return true if pin is high/on."""
2015-06-22 15:58:46 +00:00
return self._state
def turn_on(self):
2016-03-08 12:35:39 +00:00
"""Turn the pin to high/on."""
2015-06-22 15:58:46 +00:00
self._state = True
self.turn_on_handler(self._pin)
2015-06-22 15:58:46 +00:00
def turn_off(self):
2016-03-08 12:35:39 +00:00
"""Turn the pin to low/off."""
2015-06-22 15:58:46 +00:00
self._state = False
self.turn_off_handler(self._pin)