core/homeassistant/components/rpi_gpio/switch.py

81 lines
2.3 KiB
Python
Raw Normal View History

"""Allows to configure a switch using RPi GPIO."""
import voluptuous as vol
from homeassistant.components import rpi_gpio
from homeassistant.components.switch import PLATFORM_SCHEMA
2016-02-19 05:27:50 +00:00
from homeassistant.const import DEVICE_DEFAULT_NAME
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import ToggleEntity
from homeassistant.helpers.reload import setup_reload_service
from . import DOMAIN, PLATFORMS
2015-08-02 16:05:28 +00:00
2019-07-31 19:25:30 +00:00
CONF_PULL_MODE = "pull_mode"
CONF_PORTS = "ports"
CONF_INVERT_LOGIC = "invert_logic"
DEFAULT_INVERT_LOGIC = False
2019-07-31 19:25:30 +00:00
_SWITCHES_SCHEMA = vol.Schema({cv.positive_int: cv.string})
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_PORTS): _SWITCHES_SCHEMA,
vol.Optional(CONF_INVERT_LOGIC, default=DEFAULT_INVERT_LOGIC): cv.boolean,
}
)
2015-08-02 16:05:28 +00:00
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Raspberry PI GPIO devices."""
setup_reload_service(hass, DOMAIN, PLATFORMS)
invert_logic = config.get(CONF_INVERT_LOGIC)
switches = []
ports = config.get(CONF_PORTS)
for port, name in ports.items():
switches.append(RPiGPIOSwitch(name, port, invert_logic))
add_entities(switches)
2015-08-02 16:05:28 +00:00
class RPiGPIOSwitch(ToggleEntity):
2016-03-08 12:35:39 +00:00
"""Representation of a Raspberry Pi GPIO."""
def __init__(self, name, port, invert_logic):
2016-03-08 12:35:39 +00:00
"""Initialize the pin."""
2015-08-02 16:05:28 +00:00
self._name = name or DEVICE_DEFAULT_NAME
self._port = port
self._invert_logic = invert_logic
self._state = False
rpi_gpio.setup_output(self._port)
rpi_gpio.write_output(self._port, 1 if self._invert_logic else 0)
2015-08-02 16:05:28 +00:00
@property
def name(self):
2016-03-08 12:35:39 +00:00
"""Return the name of the switch."""
2015-08-02 16:05:28 +00:00
return self._name
@property
def should_poll(self):
2016-03-08 12:35:39 +00:00
"""No polling needed."""
2015-08-02 16:05:28 +00:00
return False
@property
def is_on(self):
2016-03-08 12:35:39 +00:00
"""Return true if device is on."""
2015-08-02 16:05:28 +00:00
return self._state
def turn_on(self, **kwargs):
2016-03-08 12:35:39 +00:00
"""Turn the device on."""
rpi_gpio.write_output(self._port, 0 if self._invert_logic else 1)
self._state = True
self.schedule_update_ha_state()
2015-08-02 16:05:28 +00:00
def turn_off(self, **kwargs):
2016-03-08 12:35:39 +00:00
"""Turn the device off."""
rpi_gpio.write_output(self._port, 1 if self._invert_logic else 0)
self._state = False
self.schedule_update_ha_state()