core/homeassistant/components/arduino/switch.py

81 lines
2.2 KiB
Python
Raw Normal View History

"""Support for switching Arduino pins on and off."""
2016-10-11 07:56:57 +00:00
import voluptuous as vol
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
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
def setup_platform(hass, config, add_entities, discovery_info=None):
2016-10-11 07:56:57 +00:00
"""Set up the Arduino platform."""
board = hass.data[DOMAIN]
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():
switches.append(ArduinoSwitch(pinnum, pin, board))
add_entities(switches)
2015-06-22 15:58:46 +00:00
class ArduinoSwitch(SwitchEntity):
2016-03-08 12:35:39 +00:00
"""Representation of an Arduino switch."""
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
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"
self._state = options[CONF_INITIAL]
if options[CONF_NEGATE]:
self.turn_on_handler = board.set_digital_out_low
self.turn_off_handler = board.set_digital_out_high
else:
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
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, **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
self.turn_on_handler(self._pin)
2015-06-22 15:58:46 +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
self.turn_off_handler(self._pin)