2019-02-13 20:21:14 +00:00
|
|
|
"""Support for switching Arduino pins on and off."""
|
2016-10-11 07:56:57 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity
|
2016-10-11 07:56:57 +00:00
|
|
|
from homeassistant.const import CONF_NAME
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-06-22 15:58:46 +00:00
|
|
|
|
2020-04-05 21:27:58 +00:00
|
|
|
from . import DOMAIN
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_PINS = "pins"
|
|
|
|
CONF_TYPE = "digital"
|
|
|
|
CONF_NEGATE = "negate"
|
|
|
|
CONF_INITIAL = "initial"
|
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,
|
|
|
|
vol.Optional(CONF_INITIAL, default=False): cv.boolean,
|
|
|
|
vol.Optional(CONF_NEGATE, default=False): cv.boolean,
|
|
|
|
}
|
|
|
|
)
|
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, default={}): vol.Schema({cv.positive_int: PIN_SCHEMA})}
|
|
|
|
)
|
2016-10-11 07:56:57 +00:00
|
|
|
|
2015-06-22 15:58:46 +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:58:46 +00:00
|
|
|
switches = []
|
|
|
|
for pinnum, pin in pins.items():
|
2020-04-05 21:27:58 +00:00
|
|
|
switches.append(ArduinoSwitch(pinnum, pin, board))
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(switches)
|
2015-06-22 15:58:46 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class ArduinoSwitch(SwitchEntity):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Representation of an Arduino switch."""
|
|
|
|
|
2020-04-05 21:27:58 +00:00
|
|
|
def __init__(self, pin, options, board):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Initialize the Pin."""
|
2015-06-22 15:58:46 +00:00
|
|
|
self._pin = pin
|
2020-04-07 20:45:56 +00:00
|
|
|
self._name = options[CONF_NAME]
|
2016-10-11 07:56:57 +00:00
|
|
|
self.pin_type = CONF_TYPE
|
2019-07-31 19:25:30 +00:00
|
|
|
self.direction = "out"
|
2016-03-09 17:38:45 +00:00
|
|
|
|
2020-04-07 20:45:56 +00:00
|
|
|
self._state = options[CONF_INITIAL]
|
2016-03-09 17:38:45 +00:00
|
|
|
|
2020-04-07 20:45:56 +00:00
|
|
|
if options[CONF_NEGATE]:
|
2020-04-05 21:27:58 +00:00
|
|
|
self.turn_on_handler = board.set_digital_out_low
|
|
|
|
self.turn_off_handler = board.set_digital_out_high
|
2016-03-09 17:38:45 +00:00
|
|
|
else:
|
2020-04-05 21:27:58 +00:00
|
|
|
self.turn_on_handler = board.set_digital_out_high
|
|
|
|
self.turn_off_handler = board.set_digital_out_low
|
2015-06-22 15:58:46 +00:00
|
|
|
|
2020-04-05 21:27:58 +00:00
|
|
|
board.set_mode(self._pin, self.direction, self.pin_type)
|
2016-03-09 17:38:45 +00:00
|
|
|
(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
|
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def turn_on(self, **kwargs):
|
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
|
2016-03-09 17:38:45 +00:00
|
|
|
self.turn_on_handler(self._pin)
|
2015-06-22 15:58:46 +00:00
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def turn_off(self, **kwargs):
|
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
|
2016-03-09 17:38:45 +00:00
|
|
|
self.turn_off_handler(self._pin)
|