-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
Adds Orange Pi GPIO platform #22541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Adds Orange Pi GPIO platform #22541
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5b8862a
Adds Orange Pi GPIO platform
pascallj f5bc43f
Add manifest.json
pascallj 04e4448
Remove cover platform
pascallj c3593a1
Apply requested changes
pascallj f219e74
Remove switch platform
pascallj 632cd9d
Update CODEOWNERS
pascallj 9925917
Remove obsolete dependecies/requirements
pascallj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| """Support for controlling GPIO pins of a Orange Pi.""" | ||
| import logging | ||
|
|
||
| from homeassistant.const import ( | ||
| EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP) | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| CONF_PINMODE = 'pinmode' | ||
| DOMAIN = 'orangepi_gpio' | ||
| PINMODES = ['pc', 'zeroplus', 'zeroplus2', 'deo', 'neocore2'] | ||
|
|
||
|
|
||
| def setup(hass, config): | ||
| """Set up the Orange Pi GPIO component.""" | ||
| from OPi import GPIO | ||
|
|
||
| def cleanup_gpio(event): | ||
| """Stuff to do before stopping.""" | ||
| GPIO.cleanup() | ||
|
|
||
| def prepare_gpio(event): | ||
| """Stuff to do when home assistant starts.""" | ||
| hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, cleanup_gpio) | ||
|
|
||
| hass.bus.listen_once(EVENT_HOMEASSISTANT_START, prepare_gpio) | ||
| return True | ||
|
|
||
|
|
||
| def setup_mode(mode): | ||
| """Set GPIO pin mode.""" | ||
| from OPi import GPIO | ||
|
|
||
| if mode == 'pc': | ||
| import orangepi.pc | ||
| GPIO.setmode(orangepi.pc.BOARD) | ||
| elif mode == 'zeroplus': | ||
| import orangepi.zeroplus | ||
| GPIO.setmode(orangepi.zeroplus.BOARD) | ||
| elif mode == 'zeroplus2': | ||
| import orangepi.zeroplus | ||
| GPIO.setmode(orangepi.zeroplus2.BOARD) | ||
| elif mode == 'duo': | ||
| import nanopi.duo | ||
| GPIO.setmode(nanopi.duo.BOARD) | ||
| elif mode == 'neocore2': | ||
| import nanopi.neocore2 | ||
| GPIO.setmode(nanopi.neocore2.BOARD) | ||
|
|
||
|
|
||
| def setup_output(port): | ||
| """Set up a GPIO as output.""" | ||
| from OPi import GPIO | ||
| GPIO.setup(port, GPIO.OUT) | ||
|
|
||
|
|
||
| def setup_input(port): | ||
| """Set up a GPIO as input.""" | ||
| from OPi import GPIO | ||
| GPIO.setup(port, GPIO.IN) | ||
|
|
||
|
|
||
| def write_output(port, value): | ||
| """Write a value to a GPIO.""" | ||
| from OPi import GPIO | ||
| GPIO.output(port, value) | ||
|
|
||
|
|
||
| def read_input(port): | ||
| """Read a value from a GPIO.""" | ||
| from OPi import GPIO | ||
| return GPIO.input(port) | ||
|
|
||
|
|
||
| def edge_detect(port, event_callback): | ||
| """Add detection for RISING and FALLING events.""" | ||
| from OPi import GPIO | ||
| GPIO.add_event_detect( | ||
| port, | ||
| GPIO.BOTH, | ||
| callback=event_callback) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| """Support for binary sensor using Orange Pi GPIO.""" | ||
| import logging | ||
|
|
||
| from homeassistant.components import orangepi_gpio | ||
|
pascallj marked this conversation as resolved.
|
||
| from homeassistant.components.binary_sensor import ( | ||
| BinarySensorDevice, PLATFORM_SCHEMA) | ||
| from homeassistant.const import DEVICE_DEFAULT_NAME | ||
|
|
||
| from . import CONF_PINMODE | ||
| from .const import CONF_INVERT_LOGIC, CONF_PORTS, PORT_SCHEMA | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(PORT_SCHEMA) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we know that more platforms are likely incoming, it would be better to have all config under the integrating component section, and set up platforms via discovery helper. See the netgear_lte component for example. |
||
|
|
||
|
|
||
| def setup_platform(hass, config, add_entities, discovery_info=None): | ||
| """Set up the Orange Pi GPIO devices.""" | ||
| pinmode = config[CONF_PINMODE] | ||
| orangepi_gpio.setup_mode(pinmode) | ||
|
|
||
| invert_logic = config[CONF_INVERT_LOGIC] | ||
|
|
||
| binary_sensors = [] | ||
| ports = config[CONF_PORTS] | ||
| for port_num, port_name in ports.items(): | ||
| binary_sensors.append(OPiGPIOBinarySensor( | ||
| port_name, port_num, invert_logic)) | ||
| add_entities(binary_sensors, True) | ||
|
|
||
|
|
||
| class OPiGPIOBinarySensor(BinarySensorDevice): | ||
| """Represent a binary sensor that uses Orange Pi GPIO.""" | ||
|
|
||
| def __init__(self, name, port, invert_logic): | ||
| """Initialize the Orange Pi binary sensor.""" | ||
| self._name = name or DEVICE_DEFAULT_NAME | ||
| self._port = port | ||
| self._invert_logic = invert_logic | ||
| self._state = None | ||
|
|
||
| orangepi_gpio.setup_input(self._port) | ||
|
pascallj marked this conversation as resolved.
|
||
|
|
||
| def read_gpio(port): | ||
| """Read state from GPIO.""" | ||
| self._state = orangepi_gpio.read_input(self._port) | ||
| self.schedule_update_ha_state() | ||
|
|
||
| orangepi_gpio.edge_detect(self._port, read_gpio) | ||
|
pascallj marked this conversation as resolved.
|
||
|
|
||
| @property | ||
| def should_poll(self): | ||
| """No polling needed.""" | ||
| return False | ||
|
|
||
| @property | ||
| def name(self): | ||
| """Return the name of the sensor.""" | ||
| return self._name | ||
|
|
||
| @property | ||
| def is_on(self): | ||
| """Return the state of the entity.""" | ||
| return self._state != self._invert_logic | ||
|
|
||
| def update(self): | ||
| """Update the GPIO state.""" | ||
| self._state = orangepi_gpio.read_input(self._port) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| """Constants for Orange Pi GPIO.""" | ||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.helpers import config_validation as cv | ||
|
|
||
| from . import CONF_PINMODE, PINMODES | ||
|
|
||
| CONF_INVERT_LOGIC = 'invert_logic' | ||
| CONF_PORTS = 'ports' | ||
|
|
||
| DEFAULT_INVERT_LOGIC = False | ||
|
|
||
| _SENSORS_SCHEMA = vol.Schema({ | ||
| cv.positive_int: cv.string, | ||
| }) | ||
|
|
||
| PORT_SCHEMA = { | ||
| vol.Required(CONF_PORTS): _SENSORS_SCHEMA, | ||
| vol.Required(CONF_PINMODE): vol.In(PINMODES), | ||
| vol.Optional(CONF_INVERT_LOGIC, default=DEFAULT_INVERT_LOGIC): cv.boolean, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "domain": "orangepi_gpio", | ||
| "name": "Orangepi GPIO", | ||
| "documentation": "https://www.home-assistant.io/components/orangepi_gpio", | ||
| "requirements": [ | ||
| "OPi.GPIO==0.3.6" | ||
| ], | ||
| "dependencies": [], | ||
| "codeowners": [ | ||
| "@pascallj" | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.